如何提高保歷加通道交易策略的勝算
創富坊

程式交易 www.quants.hk (導師: 財經書藉作家: 麥振威)・2024-07-10
我們已學習了一些pine script的基本語法,相信大家經練習後已能逐漸掌握。這篇則會講解一些比較深入的分析技巧。 有一種策略是很多新手都經常問我的,若要寫股價跌穿保歷加通道底部便買入,升穿保歷加通道頂部便造淡,應該怎樣用pine script 寫出來 以下的入市準則是,收市價低於保歷加通道底部便買入,然後待收市價跌穿保歷加通道中軸便平倉,相反,收市價高於保歷加通道頂部則造淡,然後待收市價升穿保歷加通道中軸平倉。 @version=5 strategyquot;升穿bollinger's band錯誤用法quot;, overlay=true, margin_long=100, margin_short=100 sma20=ta.smaclose,20 mult=ta.stdevclose,20 upper=sma202mult lower=sma202mult noposition=strategy.position_size==0 var bool traded =false buyCond=closelt;lower and closelt;sma20 shortCond=closegt;upper and closegt;sma20 buycloseCond=ta.crossoverclose,sma20 shortcloseCond=ta.crossunderclose,sma20 if buyCond and noposition and not traded strategy.entryquot;BUYquot;,strategy.long traded=true if buycloseCond and not noposition strategy.closequot;BUYquot; if shortCond and noposition and not traded strategy.entryquot;SHORTquot;,strategy.short traded=true if shortcloseCond and not noposition strategy.closequot;SHORTquot; if ta.changetimequot;Dquot;=0 traded=false 這是最簡單的寫法,但大家應會想到,若收市價低於保歷加通道底部便買入,那以下情況可能會出現「連續多次買入」,所以在策略中已設定了每天只交易一次,而且入市情況需要「沒有持倉」才會入市。 可以看到這類交易策略,交易一年後仍然要虧損,數據是用了TeslaUSTSLA的5分鐘數據,而這個策略在一年裏交易了259次,獲利的有151次,勝率大約是58.3%。 若要修改這類運用保歷加通道頂部及底部的策略,其實比較好的處理方法是,當股價升穿保歷加通道頂部後,等待股價再回落至保歷加通道之內才入市造淡,同樣地,若股價跌穿保歷加通道底部,也是等待股價回升至保歷加通道之內才入市造好。 寫這類策略的方法是運用ta.crossover 及ta.crossunder, 若寫成ta.crossoverclose,lower,就是代表了股價由保歷加通道底部以下,升穿保歷加通道底部之時便會入市買入。 可以想想,要出現這種情況必然是股價之前已經跌穿了保歷加通道底部才會發生,那便既符合跌穿保歷加通道底部的要求,同時又符合了股價再回升至保歷加通道內的要求。 而升穿保歷加通道頂部後再待股價回落至通道之內的寫法應大家現在也懂得怎樣寫,那便是ta.crossunderclose,upper。 以下是整個策略的完整寫法 @version=5 strategyquot;升穿bollinger's band及跌穿bollinger's band策略quot;, overlay=true, margin_long=100, margin_short=100 sma20=ta.smaclose,20 mult=ta.stdevclose,20 upper=sma202mult lower=sma202mult noposition=strategy.position_size==0 var bool traded =false buyCond=ta.crossoverclose,lower and closelt;sma20 shortCond=ta.crossunderclose,upper and closegt;sma20 buycloseCond=ta.crossoverclose,sma20 shortcloseCond=ta.crossunderclose,sma20 if buyCond and noposition and not traded strategy.entryquot;BUYquot;,strategy.long traded=true if buycloseCond and not noposition strategy.closequot;BUYquot; if shortCond and noposition and not traded strategy.entryquot;SHORTquot;,strategy.short traded=true if shortcloseCond and not noposition strategy.closequot;SHORTquot; if ta.changetimequot;Dquot;=0 traded=false 從backtest report可以看到勝率會較第一個的策略為高,一年裏交易了258次,獲利的有167次,勝率提高至64.73%,但最重要的是原本是虧損的策略已變成輕微獲利。 不過,獲利確實不多,那又有沒有方法可以改得更好 最常見的做法是觀察圖表上的入市訊號,特別是留意出現裂口高開或裂口低開的情況,因為不少人都會認為出現裂口高開或裂口低開會引發上日持倉過夜的炒家的平倉盤,但這並不代表當日即市的走勢,只會在開市初段產生短暫影響。 而我們在圖表上觀察這個策略的入市訊號時,又確實發現有些日子的造淡的訊號會因為當日出現裂口高開,因而升穿了保歷加通道頂部,其後股價重返保歷加通道之內便入市造淡,不過最後卻出現虧損。 那若我們剔除因裂口高開而出現的入市造淡訊號又會怎樣 但這種修改方法又很可能把原本能獲利的訊號也剔除的,結果是交易表現可能更差。 筆者就建議可以試試與平均線配合作修改,例如運用「Hull Moving Average,HMA」,這是一種了特別重視最近價格變動的加權移動平均線,有點像「Weighted Moving Average,WMA」,但HMA的滯後情況會較WMA少。 我們試試加上一些新的條件,買入時必需HMA比上一支陰陽燭的HMA為高,造淡時則必需HMA比上一支陰陽燭的HMA為低。 以下便是修改後的版本,在主圖上的紅線便是HMA。 @version=5 strategyquot;升穿bollinger's band 改良版quot;, overlay=true, margin_long=100, margin_short=100 sma20=ta.smaclose,20 mult=ta.stdevclose,20 upper=sma202mult lower=sma202mult noposition=strategy.position_size==0 var bool traded =false hmaValue=ta.hmaclose,10 buyCond=ta.crossoverclose,lower and closelt;sma20 shortCond=ta.crossunderclose,upper and closegt;sma20 buycloseCond=ta.crossoverclose,sma20 shortcloseCond=ta.crossunderclose,sma20 buyCond2=hmaValuegt;hmaValue1 shortCond2=hmaValuelt;hmaValue1 if buyCond and noposition and not traded and buyCond2 strategy.entryquot;BUYquot;,strategy.long traded=true if buycloseCond and not noposition strategy.closequot;BUYquot; if shortCond and noposition and not traded and shortCond2 strategy.entryquot;SHORTquot;,strategy.short traded=true if shortcloseCond and not noposition strategy.closequot;SHORTquot; if ta.changetimequot;Dquot;=0 traded=false plothmaValue,title=quot;HMAquot;,color=color.red,linewidth=1 結果可以看到訊號大幅減少,但勝率再提升至75%,但看最大獲利的最大虧損的比例達到31,這類策略即使遇上最壞的情況也是虧損有限。但問題就是交易次數真的真的太小的,一年只有8次入市機會,雖然獲利的次數有6次,但交易次數太小也會令最終的回報有限。 但大家可以想想,若你把「10個」本來只有六成中的交易策略,修改至七成中以上,而且Maximum Drawdown不大,盈虧比更大幅提升至3比1,那麼你獲利的機會根本便很大。 然後我們這10個策略同時執行,那你的交易次數就不會少了,而回報也會因而增加。若為了達到有足夠多交易次數的目的而勉強去運用一些勝率較低,盈虧比又較低的交易策略,那最終的回報反而不會太好。 網頁 www.quants.hk Youtube httpswww.youtube.com@markchunwai Facebook專頁 httpswww.facebook.comquantshk Patreon httpswww.patreon.comquantshk