【如何用Trading View寫每天只交易一次的策略】
創富坊

程式交易 www.quants.hk (導師: 財經書藉作家: 麥振威)・2024-06-17
最記得以前有學員曾說過,他過去試過很多的交易策略,最後在實戰時的成績都不太好,然後「嬲嬲地」就每天只看到MACD的第一個訊號便入市,開市後見MACD的快線升穿慢線便買入,相反,若MACD的快線跌穿慢線便造淡,然後見MACD的快線繼續上升便平好倉,造淡時則見MACD的快線繼續下跌就平淡倉,就是這樣簡單 但效果反而比很多複雜的策略更好。 這個只是他的意見,最後成績如何他沒有告訴我,但筆者自己研究過很多的Daytrade策略也都是每天只交易一次的,因為交易次數太多,交易成本就會增加,而且長時間交易會覺得更亂,特別是遇上連續虧損的時候,而每天只交易一次就是讓自己有足夠時間冷靜下來。 不過,若要用pine script寫這類每天只交易一次的策略,又應怎樣寫 以下是一個很簡單運用Zero Lag MACD的交易策略,就是快線升穿慢線便買入,當買入後看到連續三支陰陽燭的時間內MACD的快線都在上升,那就平倉離場。 This Pine Scripttrade; code is subject to the terms of the Mozilla Public License 2.0 at httpsmozilla.orgMPL2.0 copy; markchunwaipaul @version=5 strategyquot;zero lag MACD交易例子quot;, margin_long=100, margin_short=100, initial_capital =1000,default_qty_type = strategy.percent_of_equity,default_qty_value = 100 SN=input12 LP=input26 M=input9 ema1=ta.emaclose,SN ema2=ta.emaema1,SN ema3=ta.emaclose,LP ema4=ta.emaema3,LP ZerolagMACDLine=2ema1ema22ema3ema4 ema5=ta.emaZerolagMACDLine,M ema6=ta.emaema5,M ZerolagSignalLine=2ema5ema6 Histogram=ZerolagMACDLineZerolagSignalLine var bool traded =false closeCond=ta.risingZerolagMACDLine,3 noposition=strategy.position_size==0 buyCond=ta.crossoverZerolagMACDLine,ZerolagSignalLine if buyCond and noposition strategy.entryquot;BUYquot;,strategy.long if closeCond and not noposition strategy.closequot;BUYquot; plotZerolagMACDLine,title=quot;MACDLinequot;,color=color.yellow ,linewidth=2 plotZerolagSignalLine,title=quot;SignalLinequot;,color=color.green,linewidth=2 plotHistogram, color=color.black, style=plot.style_histogram,linewidth=2 以上策略的Backtest report 可以看到這樣寫每天的交易次數肯定不只一次,交易了1023次,獲利交易只有514次,勝率約50.24%,一年的虧損約37.45%。 另以下是同一個策略但每日只交易一次的寫法 This Pine Scripttrade; code is subject to the terms of the Mozilla Public License 2.0 at httpsmozilla.orgMPL2.0 copy; markchunwaipaul @version=5 strategyquot;用zero lag MACD每日只交易一次例子quot;, margin_long=100, margin_short=100, initial_capital =1000,default_qty_type = strategy.percent_of_equity,default_qty_value = 100 SN=input12 LP=input26 M=input9 ema1=ta.emaclose,SN ema2=ta.emaema1,SN ema3=ta.emaclose,LP ema4=ta.emaema3,LP ZerolagMACDLine=2ema1ema22ema3ema4 ema5=ta.emaZerolagMACDLine,M ema6=ta.emaema5,M ZerolagSignalLine=2ema5ema6 Histogram=ZerolagMACDLineZerolagSignalLine var bool traded =false closeCond=ta.risingZerolagMACDLine,3 noposition=strategy.position_size==0 buyCond=ta.crossoverZerolagMACDLine,ZerolagSignalLine if buyCond and not traded and noposition strategy.entryquot;BUYquot;,strategy.long traded=true if closeCond and not noposition strategy.closequot;BUYquot; if ta.changetimequot;Dquot;=0 traded=false plotZerolagMACDLine,title=quot;MACDLinequot;,color=color.yellow ,linewidth=2 plotZerolagSignalLine,title=quot;SignalLinequot;,color=color.green,linewidth=2 plotHistogram, color=color.black, style=plot.style_histogram,linewidth=2 留意克體的部份就是加上後令策略變成「每天只交易一次」。 先設定traded為false,然後當買入後便設定為true,由於入市條件加上了not traded,代表要traded 必需為false時才會入市,這樣交易一次後就不會再交易,最後加上ta.changetimequot;Dquot;=0,代表要轉為第二個交易日,traded才會再轉變為false,然後第二日當ZerolagMACD的快線升穿慢線時就會符合入市條件。 策略的backtest report 同一樣的交易策略,只是將其改變為「每天只交易一次」,可以看到結果也是虧損,不過,虧損幅度卻由37.45%大幅下降至10.75%。另外要留意,筆者寫這兩個策略是沒有計算「佣金」及「滑價」的,而第一個策略在一年裏交易了1023次,但加上「每天只交易一次」這個條件後,一年裏只交易了258次,交易成本會相差很遠,不過勝率就未見有大幅改善,獲利的次數只有132次,勝率只輕微由50.24%提高至51.16%。 交易策略當然不可能這樣簡單,但只要將以上兩個策略作比較便可看到,每天只交易一次的Daytrade策略確實能提高成效。 網頁 www.quants.hk Youtube httpswww.youtube.com@markchunwai Facebook專頁 httpswww.facebook.comquantshk Patreon httpswww.patreon.comquantshk