從現成的程式碼練習及思考,是我認為能很快速切入程式交易領域的不二法門,希望讀者仔細品嚐,所附之程式碼是個好東西!!更多優質的文章可以直接參考EasyTrader的blog唷! (Wen)
延續 Part 2 這次將變動 UpBand 與 DnBand 來看看有什麼不同?
EasyTrader ArtNo 068
整體而言,原來不好的約有30%以上的改善!
既然 UpBand , DnBand 有幫助,我們重新在這部份作思考,在原來的指標程式碼加入以下內容並作成新的HighBand/UpBand/DnBand/LowBand上下通道指標如下圖
if Type= 3 then Begin
Plot1(BullPower,"Bull",iff(BullPower >= 0,Magenta,Yellow),Black,1) ;
Plot2(BearPower,"Bear",iff(BearPower >= 0,White,Green),Black,1) ;
Plot3(UpBand,"UpBand") ;
Plot4(-DnBand,"DnBand") ;
{ for MultiChart
Plot5(HighBand,"HighBand") ;
Plot6(-LowBand,"LowBand") ; }
end;
現在看[K線圖與指標圖在圓圈圈裡的對應關係],是否有機會透過程式回測來尋找最佳轉折點呢 ?
新策略測試程式碼:
inputs:BaseLen(40),RefLen(50),TradeProfit(0.04),TradeStopLoss(0.04),HighBand(200),LowBand(200),UpBand(0),DnBand(0);
input:HLRange1(50),HLRange2(250),TscoreGap(6);
Vars:HighOsc(0),LowOsc(0),CloseAvg(0),BuySetup(false),SellSetup(false),MP(0),IsBalanceDay(False);
Vars:PFpoint(0),LossPoint(0),HL(0) ,TTXN_Score(0),Zscore1(0),Tscore1(0),TTXN(0);
PFPoint = AvgPrice*TradeProfit ;
LossPoint = AvgPrice*TradeStopLoss ;
if DataCompression > 1 then Begin
HL = High[1]-Low[1] ;
Condition1 = Close > Open and Open > Close[1] ;
Condition2 = Close < Open and Open < Close[1] ;
end else Begin
HL = HighD(1)-LowD(1) ;
Condition1 = Close > OpenD(0) and OpenD(0) > CloseD(1) ;
Condition2 = Close < OpenD(0) and OpenD(0) < CloseD(1) ;
end;
MP = MarketPosition ;
if DAYofMonth(Date) > 14 and DAYofMonth(Date) < 22 and DAYofWeek(Date)= 3 then isBalanceDay = True else isBalanceDay =False ;
HighOsc = High - Xaverage(High,BaseLen) ;
LowOsc = Low - Xaverage(Low,BaseLen) ;
CloseAvg = Xaverage(Close,RefLen) ;
Condition3 = HL > HLRange1 and HL < HLRange2 ;
Condition4 = Highest(High,RefLen)-Lowest(Low,RefLen) < HLRange2 ;
if date <> date[1] then Begin
Value92 = Close of Data3 ;
if StdDev(Value92,15) <> 0 then Zscore1 = (Value92- Average(Value92,15))/StdDev(Value92,15);
TScore1 = 10*Zscore1 + 50 ;
{ TTXN }
if StdDev(Close,15) <> 0 then TTXN_Score = (Close- Average(Close,15))/StdDev(Close,15);
TTXN = 10*TTXN_score + 50 ;
Condition5 = TTXN > Tscore1 and TTXN > HLRange1 ;
Condition6 = TTXN < TScore1 and TTXN < HLRange2 ;
Condition7 = Tscore1 > HLRange1 and AbsValue(TTXN-Tscore1) < TscoreGap;
Condition8 = Tscore1 < HLRange2 and AbsValue(TTXN-Tscore1) < TscoreGap;
Condition9 = TTXN > HLRange1 ;
Condition10 = TTXN < HLRange2 ;
end;
Value9 = Dayofweek(date) ;
Condition11 = ( Value9 = 1 ) ;
Condition12 = (Value9 = 5 or Value9 = 4) ;
Condition13 = Close > MaxList(HighD(1),HighD(2),HighD(3)) ;
Condition14 = Close < MinList(LowD(1),LowD(2)) ;
BuySetup = CloseAvg > CloseAvg[1] and Close > CloseAvg and LowOsc < -DnBand ;
SellSetup = CloseAvg < CloseAvg[1] and Close < CloseAvg and HighOsc > UpBand ;
{主程式碼 }
if HighOsc > UpBand and LowOsc > UpBand and HighOsc > LowOsc
and Condition7 then Buy ("HB") next bar at Market ;
if HighOsc < -DnBand and LowOsc < -DnBand and HighOsc < LowOsc
and Condition8 then Sell ("LS") next bar at Market ;
{if MP > 0 and HighOsc Cross under HighBand then ExitLong next bar at Market ;
if MP < 0 and LowOsc Cross over -LowBand then ExitShort next bar at Market ;}
setProfitTarget(PFPoint*BigPointValue) ;
setstoploss(LossPoint*BigPointValue) ;
if IsBalanceDay then SetExitonClose ;
經過組合測試後,新策略加上T指標勝出,回測結果如下圖
1 留言:
哇!改良後的曲線也太漂亮了吧!
張貼留言
如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!