我想實現一個進場且加碼後就開始追蹤高點的邏輯,由於程式會重啟,所以高點都需要重新抓,我附上出問題的片段程式碼,然後在往下貼完整程式碼,
想實現的邏輯是 我進場加碼了 也有獲利了 ,我想追蹤這個高點 , 如果我進場加碼完第二天 ,我就讓變數從昨天高開始記錄
如果是第三天,就往前找出兩天前最高點,
使用FilledRecordDate(2) 是因為我加碼後才要開始記錄,不然會有點失真 ,以下附上程式碼 ,我在寄log給xq客服, 先謝謝各位的幫忙,感謝
出問題的片段
inputs : in_(5,"漲幅進場條件"),
stopX(6,"停損%"),
all_in(3,"加碼%"),
TGT(6,"獲利目標"),
backX(6,"停利回測%"),
pos_1(7,"母單金額(萬)");
condition3 = filledavgPrice*filled*1000 > pos_1*10000; //判斷是否加碼了
//追蹤高點
if filled >0 and condition3 then begin
hold_days = DateDiff( CurrentDate,FilledRecordDate(2) ) + 1 ;
if hold_days = 2 and highD(1) > filledavgPrice then begin
if highx < highD(1) then highx = highD(1) ;
end;
if hold_days = 3 then begin
Array: arrA[2](0);
arrA[1] = highD(1); arrA[2] = highD(2);
value10 = HighestArray(arrA,2);
if value10 > filledavgPrice and highx < value10 then highx = value10 ;
end;
if hold_days = 4 then begin
Array: arrB[3](0);
arrB[1] = highD(1); arrB[2] = highD(2); arrB[3] = highD(3);
value11 = HighestArray(arrB,3);
if value11 > filledavgPrice and highx < value11 then highx = value11 ;
end;
if hold_days = 5 then begin
Array: arrC[4](0);
arrC[1] = highD(1); arrC[2] = highD(2); arrC[3] = highD(3); arrC[4] = highD(4);
value12 = HighestArray(arrC,4);
if value12 > filledavgPrice and highx < value12 then highx = value12 ;
end;
if hold_days >= 6 then begin
Array: arrD[5](0);
arrD[1] = highD(1); arrD[2] = highD(2); arrD[3] = highD(3); arrD[4] = highD(4); arrD[5] = highD(5);
value13 = HighestArray(arrD,5);
if value13 > filledavgPrice and highx < value13 then highx = value13 ;
end;
end;
完整版
inputs : in_(5,"漲幅進場條件"),
stopX(6,"停損%"),
all_in(3,"加碼%"),
TGT(6,"獲利目標"),
backX(6,"停利回測%"),
pos_1(7,"母單金額(萬)");
input:stoploss_en(1,"是否開單",inputkind:=dict(["Yes",1],["No",0]));
vars: intrabarpersist opposition (true), //是否開單
intrabarpersist highx(0),
order_pos(0),
order_pos2(0),
hold_days(0);
if stoploss_en=1 then opposition = true; //是否開單
if stoploss_en=0 then opposition = false; //是否開單
//進場資料
value3 = getfield("Close","D");
value4 = getfield("Close","D")[1];
value5 = getfield("Close","D")[2];
value6 = getfield("Close","D")[3];
value7 = getfield("Close","D")[4];
value8 = (value3+value4+value5+value6+value7)/5 ; //五日線
value9 = closeD(1)*1.05 ; //進場點
//進場邏輯
condition1 = close cross Over value9 ; //漲幅超過5%
condition2 = close <= value8*1.1 ; //不能乖離5MA超過10%
condition3 = filledavgPrice*filled*1000 > pos_1*10000; //判斷是否加碼了
//進場
if filled = 0 and condition1 and condition2 and opposition then begin
order_pos = IntPortion((pos_1 * 10000) / (close * 1000));
if order_pos > 0 then
setposition(order_pos, label:="進場");
highx = 0 ;
end;
//加碼
if filled >0 and not condition3 and close > filledavgPrice *(1+all_in*0.01) then begin
order_pos2 = IntPortion((pos_1 * 3 * 10000) / (close * 1000));
if order_pos2 > 0 then
setposition(order_pos2, label:="加碼'");
end;
//停損
if filled >0 and close < filledavgPrice then begin
if not condition3 and close < filledavgPrice*(1-stopX*0.01) then
setposition(0, label:="試單停損'")
else if condition3 and close < filledavgPrice*(1-(stopX*0.01/3)) then
setposition(0, label:="加碼停損'");
end;
//追蹤高點
if filled >0 and condition3 then begin
hold_days = DateDiff( CurrentDate,FilledRecordDate(2) ) + 1 ;
if hold_days = 2 and highD(1) > filledavgPrice then begin
if highx < highD(1) then highx = highD(1) ;
end;
if hold_days = 3 then begin
Array: arrA[2](0);
arrA[1] = highD(1); arrA[2] = highD(2);
value10 = HighestArray(arrA,2);
if value10 > filledavgPrice and highx < value10 then highx = value10 ;
end;
if hold_days = 4 then begin
Array: arrB[3](0);
arrB[1] = highD(1); arrB[2] = highD(2); arrB[3] = highD(3);
value11 = HighestArray(arrB,3);
if value11 > filledavgPrice and highx < value11 then highx = value11 ;
end;
if hold_days = 5 then begin
Array: arrC[4](0);
arrC[1] = highD(1); arrC[2] = highD(2); arrC[3] = highD(3); arrC[4] = highD(4);
value12 = HighestArray(arrC,4);
if value12 > filledavgPrice and highx < value12 then highx = value12 ;
end;
if hold_days >= 6 then begin
Array: arrD[5](0);
arrD[1] = highD(1); arrD[2] = highD(2); arrD[3] = highD(3); arrD[4] = highD(4); arrD[5] = highD(5);
value13 = HighestArray(arrD,5);
if value13 > filledavgPrice and highx < value13 then highx = value13 ;
end;
end;
//停利
if close > highx then highx = close ;
if highx > filledavgPrice*(1+TGT*0.01) and close < highx*(1-backX*0.01) then
setposition(0, label:="停利'");
value1 = highx*(1-backX*0.01);
value2 = filledavgPrice*(1+TGT*0.01);
print ("highx",highx,"hold_days",hold_days,"停利點",value1,"+6%",value2) ;
5 評論