Params
Numeric RetracePct(0.5);
Vars
Series<Numeric> SwingPrice;
Series<Numeric> Swingindex1;//第二近高低点的索引
Series<Numeric> Swingindex2;//第三近高低点的索引
Series<Numeric> Swingindex0;//第一近高低点的索引
Series<Numeric> highpoint;
Series<Numeric> highpointbar;
Series<Numeric> lowpoint;
Series<Numeric> lowpointbar;
Series<Numeric> cb;
Numeric SwingHighPrice;
Numeric SwingLowPrice;
Series<Numeric> PreBar(0);
Series<Numeric> UpDn(0);
Bool SaveSwing(False);
Bool NewTL(False);
Bool UpdateTL(False);
array<Numeric> price_array;
array<Integer> pos_array;
array<numeric> vol_array;
plot pen;
Events
OnInit()
{
PrintClear;
pen.figure(1);
}
OnBar(ArrayRef<Integer> indexs)
{
If(CurrentBar == 0)
{
swingindex1 = 0;
swingindex2 = 0;
swingindex0 = 0;
SwingPrice = Close;
}
Commentary("cb"+text(currentbar));
cb = CurrentBar;
SwingHighPrice = SwingHigh( 1, Close, 1,2);
SwingLowPrice = SwingLow( 1, Close, 1, 2 );
If (SwingHighPrice <> -1)//转折高点有效
{
If(UpDn <=0 && SwingHighPrice >= SwingPrice * (1+RetracePct*0.01))//之前是低点,然后转折高点突破阈值
{
UpDn = 1;//状态设置为多头
NewTL = True;//出现转折点,处理新转折点业务
SaveSwing = True;//保存转折点
swingindex0 = swingindex1;
swingindex1 = swingindex2;
swingindex2 = cb[1];
}Else If(UpDn == 1 && SwingHighPrice >= SwingPrice)//之前是高点,现在刷新新高
{
UpdateTL = True;//处理更新转折点业务
SaveSwing = True;//保存转折点
swingindex2 = cb[1];
}
If(SaveSwing)//保存转折点数据
{
SwingPrice = SwingHighPrice;
PreBar = CurrentBar;
}
}Else If(SwingLowPrice <> -1)
{
If(UpDn >=0 && SwingLowPrice <= SwingPrice * (1-RetracePct*0.01))
{
UpDn = -1;
NewTL = True;
SaveSwing = True;
swingindex0 = swingindex1;
swingindex1 = swingindex2;
swingindex2 = cb[1];
}Else If(UpDn == -1 && SwingLowPrice <= SwingPrice)
{
UpdateTL = True;
SaveSwing = True;
swingindex2 = cb[1];
}
If(SaveSwing )
{
SwingPrice = SwingLowPrice;
PreBar = CurrentBar;
}
}
Commentary("updn:"+text(updn));
If( NewTL)
{
PlotAuto("ZigZag",SwingPrice,0,Color2(),-1,-1,Enum_3Pix,1);
PlotBool("mark",true);//close+5*MinMove*pricescale);
//Commentary("updn:"+text(updn));
//If()
If(updn == 1)
{
lowpoint = SwingPrice[1];
lowpointbar = prebar;
}
If(updn == -1)
{
highpoint = SwingPrice[1];
highpointbar = prebar;
}
}Else If(UpdateTL)
{
PlotBool("mark",false,close-5*MinMove*pricescale);
If (UpDn == UpDn[1])
{
Unplot("ZigZag",PreBar - PreBar[1]+1);
PlotAuto("ZigZag",SwingPrice,0,Color2(),-1,-1,Enum_3Pix,1);
}Else
{
PlotAuto("ZigZag",SwingPrice,0,Color2(),-1,-1,Enum_3Pix,1);
}
}
Commentary("highpoint:"+text(highpoint));
Commentary("highpointbar:"+text(highpointbar));
Commentary("lowpoint:"+text(lowpoint));
Commentary("swingindex0:"+text(swingindex0));
Commentary("swingindex1:"+text(swingindex1));
Commentary("swingindex2:"+text(swingindex2));
//swingindex2,swingindex1
If(BarStatus==2)
{
print("开始数据统计");
numeric i;
for i = currentbar - swingindex0 downto currentbar - Swingindex1
{
//循环处理每个tick数据的vol和Close
//初始化
If(i == currentbar - swingindex0)
{
price_array[0] = close[i];
vol_array[0] = vol[i];
print("初始化数组");
print(textArray(price_array));
print(textArray(vol_array));
print("-----");
}
Else
{
print("---check---");
print(textarray(price_array));
print(text(close[i]));
If(ArrayFind(price_array, close[i], pos_array)) print("找到数据");
If(ArrayFind(price_array, close[i], pos_array)) //价格数组里找到了这个价格
{
//只需要更新该价格的成交量数据即可
Numeric k = pos_array[0];
print("--刷新数据--");
print(text(i));
print(text(k));
vol_array[k] = vol_array[k] + vol[i];
print(TextArray(price_array));
print(TextArray(vol_array));
}
else //如果是没找到
{
print("----新增数据----");
print(text(i));
Numeric j;
for j = 0 to GetArraySize(price_array) - 1
{
If(close[i] > price_array[j])
{
If(j == GetArraySize(price_array) - 1)
{
print("---尾部插入---");
ArrayPushBack(price_array, close[i]);
ArrayPushBack(vol_array, vol[i]);
}
Continue;
}
Else
{
print("---中间插入----");
print(text(j));
ArrayInsert(price_array, j, close[i]);
ArrayInsert(vol_array, j, vol[i]);
print(TextArray(price_array));
print(TextArray(vol_array));
break;
}
}
}
}
}
print("最终结果");
print(TextArray(price_array));
print(TextArray(vol_array));
pen.line("mark",price_array,vol_array);
}
}
1
1
1