DateDiff( CurrentDate,FilledRecordDate(2) ) + 1, 以上語法疑似造成DAQ異常

  •   43 
  • 最後發表   蓬蘇王杜  2025 九月 16
蓬蘇王杜 發文於   2025/09/11

我想實現一個進場且加碼後就開始追蹤高點的邏輯,由於程式會重啟,所以高點都需要重新抓,我附上出問題的片段程式碼,然後在往下貼完整程式碼,

想實現的邏輯是 我進場加碼了 也有獲利了 ,我想追蹤這個高點 , 如果我進場加碼完第二天 ,我就讓變數從昨天高開始記錄

如果是第三天,就往前找出兩天前最高點,

使用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) ;


排序方式: 標準 | 最新
虎科大許教授 發文於   2025/09/11

建議策略部位選擇『延續前次執行』,若有部位,且有加碼(部位大於初始部位),且獲利達到目標,則用fiiledRecordDate判斷加碼那天的日期,然後用getBarOffset賦予該日期以抓取相對K棒(假設為value1),再用h[value1]抓該K棒最高價。

蓬蘇王杜 發文於   2025/09/11

謝謝教授提供的另一種寫法,可以請問教授原本我的寫法哪裡有問題嗎,我都是使用與庫存同步的策略部位,如果我這帳號只跑這策略也有影響嗎

虎科大許教授 發文於   2025/09/11

策略部位使用與庫存同步時,無法用filledRecord相關函數抓庫存各筆歷史成交記錄。若帳號只跑此策略,啟動策略時可抓到庫存數量及日期(預設為當日),你可以在啟動前設定庫存日期,用filledEntryDate抓該日期判斷。

蓬蘇王杜 發文於   2025/09/12

原來如此 謝謝教授

XS小編 發文於   2025/09/16

Hello 蓬蘇王杜,

 

小編補充,建議可以使用 FilledRecordCount 來確認目前記錄的交易筆數。

如果在交易筆數不足2筆的狀況下執行到 FilledRecordDate(2) 的話會因為超出範圍而錯誤。

發表回覆
Close