如題 我用以下的寫法可以在實際的交易腳本抓到第一單庫存進場多久的天數
var : hold_days (0) ; if filled >0 THEN hold_days = DateDiff( CurrentDate,FilledRecordDate(1) ) + 1 ;
所以我以此為構想 ,想寫出一個持有至少五天線且最近五天的低點有跌破五日 ,意思是有一段小區間整理後再加碼的邏輯,程式碼如下
//第三次加碼 if filled >0 then value6 = DateDiff( CurrentDate,FilledRecordDate(1) ) + 1 ; //持有天數 condition4 = lowest(close,5) <= average(close,5) ; //最近五天有跌破五日線 condition5 = value6 >= 10 ; //母單進場超過五天 condition6 = close >= highest(close,5)[1] ; //突破小盤整區 if filled = 3 and condition4 and condition5 and condition6 and close > filledavgPrice then setposition(4 , label:="第2次加碼") ;
但我發現回測並不會實現我想要的邏輯 我把資料print出來看後 ,發現 持有天數會無止境的提升,這邊我附上檔案
這個腳本有2次加碼 ,第一次加碼條件為母單獲利5% ,第二次條件就是持有五天後再判斷的邏輯 ,但回測會在第一次加碼後馬上加碼第二次,大部分情況都是這樣 , 實在不知道如何處理 ,想上來請教一下 謝謝大家 ,以下附上全部程式碼
value1 = 12 ; //停損 value2 = 30 ; //停利點 value4 = lowest (low,value2)[1] ; //停利點 value5 = GetField("股本(億)"); condition1 = close >= close[1]*1.08 ; condition2 = close >= close[3]*1.08 ; condition3 = value5 >= 50 ; //第三次加碼 if filled >0 then value6 = DateDiff( CurrentDate,FilledRecordDate(1) ) + 1 ; //持有天數 condition4 = lowest(close,5) <= average(close,5) ; //最近五天有跌破五日線 condition5 = value6 >= 10 ; //母單進場超過五天 condition6 = close >= highest(close,5)[1] ; //突破小盤整區 if filled = 0 and condition3 then begin if condition1 or condition1 then setposition(1 , label:="進場") ; end; if filled = 1 and close < filledavgPrice*(1-value1*0.01) then setposition(0 , label:="停損") ; if filled = 1 and close > filledavgPrice*1.05 then setposition(3 , label:="加碼") ; if filled = 3 and close < filledavgPrice*0.96 then setposition(0 , label:="加碼停損") ; if filled = 3 and condition4 and condition5 and condition6 and close > filledavgPrice then setposition(4 , label:="第2次加碼") ; if filled = 4 and close < filledavgPrice*0.97 then setposition(0 , label:="加碼2停損") ; if filled = 4 and close > filledavgPrice and close < value4 then setposition(0 , label:="停利") ; if filled > 0 then print("持有天數",value6) ;
4 評論