K線包含關系的順序原則:先用第1、2根K線的包含關系確認新的K線,然后用新的K線去和第3根比,如果有包含關系,繼續用包含關系的法則結合成新的K線;如果沒有,就按正常K線去處理。
程序代碼:
runmode:1;//序列模式;
i:=BARPOS;
variable:up1=0;//定義一個數組用來存放向上包含的k線高點;
variable:up2=0;//定義一個數組用來存放向上包含的k線低點;
variable:dn1=0;//定義一個數組用來存放向下包含的k線高點;
variable:dn2=0;//定義一個數組用來存放向下包含的k線低點;
beforehigh:=high[2];
beforelow:=low[2];
thishigh:=high[3];
thislow:=low[3];
upordn:=BARPOS;
aa:=setlbound(high,2);//設置序列變量high的下界為2,起始有效變量從第2根k線開始;
bb:=setlbound(low,2);//設置序列變量low的下界為2,起始有效變量從第2根k線開始;
for i=3 to BARPOS do//循環開始
begin
upordn:=i;
if up1[i-1]>0 then
begin
beforehigh=up1[i-1];
beforelow=up2[i-1];
upordn=1;
end
if dn1[i-1]>0 then
begin
beforehigh=dn1[i-1];
beforelow=dn2[i-1];
upordn=-1;
end
else begin
beforehigh=high[i-1];
beforelow=low[i-1];
end //通過該循環對向上/向下數組循環賦值(將最近的up和dn數組值賦值給前一k線的高低點以便于和最新的k線高低點進行比較)
thishigh=high[i];
thislow=low[i];
if ((ThisHigh>=BeforeHigh) AND (ThisLow<=BeforeLow)) OR ((ThisHigh<=BeforeHigh) AND (ThisLow>=BeforeLow)) //循環開始,開始判斷包含關系;
then begin
if upordn=1 then //向上包含
Up1[i] = MAX(ThisHigh, BeforeHigh);
Up2[i] = MAX(ThisLow, BeforeLow);
up1[i-1]=up1[i];
up2[i-1]=up2[i];
if upordn=-1 then //向下包含
dn1[i]=MIN(thishigh,beforehigh);
dn2[i]=MIN(thislow,beforelow);
dn1[i-1]=dn1[i];
dn2[i-1]=dn2[i];
end;
else //沒有包含關系
if thishigh>beforehigh then begin//向上
up1[i]=thishigh;
up2[i]=thislow; end;
if thislow<beforelow then begin//向下
dn1[i]=thishigh;
dn2[i]=thislow; end;
cc:=stickline(up1[i]>up2[i],up1[i],up2[i],8,1,colorblue); //該語句參數需要調整確認
dd:=stickline(dn1[i]<dn2[i],dn1[i],dn2[i],8,1,colorred); //該語句參數需要調整確認
end;
系統在剛進入循環,if up1[i-1]>0 then 語句處,提醒說 數組越界操作; 盼請各位大拿開藥方診斷~!!!