可參考此帖
http://www.coco-in.net/thread-27601-1-3.html
// by joshsmi
OpenTime = 093000;
CloseTime = 160000;
tn = TimeNum();
mth = Month() == 7; // July only
MA1 = MA ( C, 10 );
MA2 = MA ( C, 50 );
CrossBuy = Cross ( MA1, MA2 );
CrossShort = Cross ( MA2, MA1 );
ExitTime = tn >= CloseTime; // compulsory Close position after 16:00
EntryTime = tn >= OpenTime AND tn < CloseTime; // Entry only between 09:00 AND 16:00
Buy = CrossBuy AND mth AND EntryTime;
Sell = CrossShort AND mth OR ExitTime;
Short = CrossShort AND mth AND EntryTime;
Cover = CrossBuy AND mth OR ExitTime;
==============================================
偶爾發現上code如果遇上提早收市的日子, eg. 12:00:00收市便無法平倉, 因為從沒有觸發ExitTime條件, 我加了小小變化, 但那支thread上鎖了不能回應, 我就在這裡分享一下:
在1min chart裡, 首先用上dayofweek() , 把dayofweek array 各左移15支Bar (ie. 15min), 再相互比較便能在每一日收市前15min弄幾枝flag出來
DayNumArrayGlobal = DayOfWeek();
minBeforeEndofDay = NOT( ( DayNumArrayGlobal ) == Ref( DayNumArrayGlobal, 15 ) )
然後出場條件改一改
ExitTime = (tn >= CloseTime) OR MinBeforeEndofDay;
EntryTime = tn >= OpenTime AND tn < CloseTime;
然後無意中又發現31Dec的時候沒有flag(因為我是array向左移), 而偏偏那一天又通常提早收市(除夕嘛...), 所以無奈地人手加:
for (m=BarCount-1;m>(BarCount-16);m--)
ExitTime[m]=1;
搞定~ |