程式交易最怕的莫過於網路斷線,今天居然就發生了!!!雖然沒有造成什麼損失,不過感覺就是不好。
==================================
主題 : 大週期均線交易
作者 :sam
內文介紹 :
Wen您好,有幸拜讀許多您的文章,雖然還處於入門階段,也希望能夠盡一分心力,也希望能夠收到您的教學電子檔,更加精進,下文為我採用的方式分享,不周的地方還請多多包涵.交易策略的部分,我的想法是週期要夠大才不會有失效的問題,希望能夠越單純越好。所以我預設的方式如下 :(如果有思考不周的地方,還請前輩指導,謝謝)
1.長週期均線 ->選用30min , 1800根K棒
2.平滑因子選用
3.通道突破策略
程式碼如下(適用MC) :
inputs:cc(1800),dd(1800);
Vars:var0(0),var1(0);
var0=WAverage(2*WAverage(c,cc/2)-WAverage(c,cc),SquareRoot(cc));
var1=XAverage(2*XAverage(c,dd/2)-XAverage(c,dd),SquareRoot(dd));
ifcurrentbar>=1then
ifMarketPosition<>1and Close>var0andClose>var1thenbuy("B1")1
sharenextatbarmarket;
ifMarketPosition<>-1andClose<var0andClose<var1then sellshort("S1")1sharenextatbarmarket;
1.我先選擇兩條不同加權方式的均線,在此我選擇WMA+EMA組合成一條通道,參數我任意挑一組夠大的週期,我選擇1800,平滑因子我採用HMA的方式:
MA(*MA(Close,Lengte/2) – MA(Close, Length), Sqrt(Length));
買賣策略相當單純突破通道買進,跌破通道賣出. 下面列出回測的報表(相同參數不同時間週期的回測,細部回測設定1min,來回成本1200元)
台指2001~2012 30分K (Length = 1800)

台指 60 分K (Length = 900)

2.加上加碼的回測圖表 , 加碼方式可因人喜好選用,在這邊用最簡單的固定100點數加碼(或是前輩有更好的建議供參考),程式碼如下 :
if MarketPosition =1 and CurrentContracts = 1 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) > 100 then buy ("B1_1") 1 contracts next bar at market;
if MarketPosition =-1 and CurrentContracts = 1 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) < -100 then sellshort ("S1_1") 1 contracts next bar at market ;
if MarketPosition =1 and CurrentContracts = 2 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) > 100 then buy ("B2_1") 1 contracts next bar at market;
if MarketPosition =-1 and CurrentContracts = 2 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) < -100 then sellshort ("S2_1") 1 contracts next bar at market ;
if MarketPosition =1 and CurrentContracts = 4 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) > 100 then buy ("B4_2") 1 contracts next bar at market;
if MarketPosition =-1 and CurrentContracts = 4 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) < -100 then sellshort ("S4_2") 1 contracts next bar at market ;
if MarketPosition =1 and CurrentContracts = 0.5 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) > 100 then buy ("B0.5_2") 1 contracts next bar at market;
if MarketPosition =-1 and CurrentContracts = 0.5 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) < -100 then sellshort ("S0.5_2") 1 contracts next bar at market ;
if MarketPosition =1 and CurrentContracts = 1.5 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) > 100 then buy ("B1.5_2") 1 contracts next bar at market;
if MarketPosition =-1 and CurrentContracts = 1.5 and Close - PosTradeEntryPrice( 0, CurrentEntries-1 ) < -100 then sellshort ("S1.5_2") 1 contracts next bar at market ;
台指2001~2012 30分K (Length = 1800)


3.觀察報表可以發現加碼相比直接放大口數更能有效控制MDD並能夠不減少太多獲利,如果我捫再加入加大部位,在此採用簡單的方式->保證金賺一倍就放大一倍的方式,程式碼如下 :
// StartM = 準備金
TotalEquity=StartM+NetProfit+OpenPositionProfit;
PosBuy=(IntPortion((StartM+NetProfit+OpenPositionProfit)/StartM));
PosSel=(IntPortion(( StartM+NetProfit+OpenPositionProfit)/StartM));
台指30minK

台指60min K

4.我們可以發現當口數放大時大賺大賠的情況就放大了,會變成稱不到下次大波段的來臨
,在此採用兩種方式去作平衡,先採用ATR大於HR(high risk) ,所下的部位縮減,Risk的部份可以依造個人喜好,也可是切很多段,我只會用最簡單的方式,還請前輩提點.近來網路上討論熱烈的波動率的循環,嘗試用ADX實作,效果並沒有很明顯,希望您能給我一些Hint.
程式碼如下 : 只單純切一段,也可以分更多段
TradeRisk=1;
Risk=AvgTrueRange(30);
ifRisk>HRthenTradeRisk=2;
PosBuy=IntPortion(PosBuy/TradeRisk);
PosSel=IntPortion(PosBuy/TradeRisk);
ifPosBuy<=1thenPosBuy=1;
ifPosSel<=1thenPosSel=1;
台指30minK


5.我們可以發現實際賺賠的部分差不多,但是改善了大賺大賠的情況(DD太大),更能夠忍耐到下一波大波段的來臨(因為口數下的很小),接著我捫再加入Risk小時口數放大
程式碼如下 : 只切一段
ifRisk<LRthenPosBuy=PosBuy*2;
ifRisk<LRthenPosSel=PosSel*2;
台指 30/60 min K ,參數的部分就大概隨意挑一組
30 min HR = 30 ; LR = 20
60 min HR = 40 ; LR = 30

6.總結:觀察圖表發現又有回到原先大賺大賠的情況,但好處是DD並沒有因為口數加倍而成倍數的成長,如果小弟的結論是正確的話,只要不斷重複Step.4 and 5 的方式去作平滑,在資金可容許的情況下分出越多段,那是不是我捫的獲利曲線會越來越平滑,更有機會等到下一次大波段的來臨,這樣我捫就可以把這個波段策略,分出來放著長期跑,再去開發其他策略,以上是小弟粗淺的研究,希望能聽聽前輩的建議,謝謝.
7.補充說明 :
再平衡DD的部分,前文有提到採用兩種方式,在此介紹第二種方式,但礙於程式上無法實作(功力不足),目前在實務上(實際的策略)是採取手動的方式 :
下圖是網路上抓到的Delta與Gamma關係圖
平衡策略如下 :
當手中有部位時 or 第n次加碼時採取買進反向選擇權Gamma避險策略
我希望能買在K值(價平)外 , 實務上我會買價外一至二檔作避險
當我手上部位大時,買進相同口數反向,只要有跳空產生即出場.
可以採取固定金額的方式購買 , or 考慮願意付出的時間價值.
也是會有平滑資金曲線的效果.
