2014年4月21日 星期一

●指標組合 RSI + ADX [程式碼]

EasyTrader ArtNo 140
部落格舊文中曾介紹了 RSI 與 ADX 指標的交易策略, 本篇介紹在網路上讀到的這篇指標組合文章( 作者網頁內有很多的文章可學習喔 ) ,作者說明當商品走勢在趨勢中時 ,如何應用ADX 與RSI 作進場與出場的交易

Long Entries:
     The 20-bar moving average must be rising.  If the ADX is rising (ADX today is 0.20 or more higher than yesterday) then buy if the 14 bar RSI is less than 85.
     If the ADX is not rising (ADX today is not 0.20 higher than yesterday) then buy if the 14 bar RSI is less than 50. Here is where you can influence the frequency of trading. For more trades use a higher threshold like 60. For fewer trades use a lower threshold like 40.

Long Exits
     If the ADX is not rising (ADX today is not 0.20 higher than yesterday) then sell (long exit) if the 9-bar RSI is greater than 75.  If the ADX is rising (ADX today is 0.20 or more higher than yesterday) and the open profit is greater than (pick some amount - maybe 4 ATRs or some unit of price) then sell if the 9-bar RSI is greater than 75.
     You need some additional exit rule for the losing trades. Use your favorite loss-limiting exit or you might want to exit when the price goes below the 20-dat moving average or when the 20-day moving average turns down. (See entry rule 1.)
接下來是將文字敘述轉化成程式碼
多單進場與出場
if Average(Close ,20) > Average(Close,20)[1] then Begin
if ( ADX(14)-ADX(14)[1] ) > 0.2 and RSI(Close,14) < 85 then Buy next bar at market ;
if ( ADX(14)-ADX(14)[1] ) > 0 and ( ADX(14)-ADX(14)[1] ) < 0.2
and RSI(Close,14) < 50 then Buy next bar at market ;
end;

if RSI(close,9) > 75 and ( ADX(14)-ADX(14)[1] ) > 0 and ( ADX(14)-ADX(14)[1] ) < 0.2
then ExitLong next bar at market ;
if RSI(close,9) > 75 and ( ADX(14)-ADX(14)[1] ) > 0.2
then ExitLong next bar at EntryPrice + TrueRange * 4 stop ;
if Average(Close ,20) < Average(Close,20)[1] then ExitLong next bar at Average(Close ,20) stop ;

將數字部份轉化為參數作最佳化
台指期 日K 2004/3/31 ~2014/3/31 交易成本 1200 回測



反向邏輯的空單進場與出場
if Average(Close ,20) < Average(Close,20)[1] then Begin
if ( ADX(14)-ADX(14)[1] ) > 0.2 and RSI(Close,14) > 15 then Buy next bar at market ;
if ( ADX(14)-ADX(14)[1] ) > 0 and ( ADX(14)-ADX(14)[1] ) < 0.2
and RSI(Close,14) > 50 then Buy next bar at market ;
end;

if RSI(close,9) < 25 and ( ADX(14)-ADX(14)[1] ) > 0 and ( ADX(14)-ADX(14)[1] ) < 0.2
then ExitLong next bar at market ;
if RSI(close,9) < 25 and ( ADX(14)-ADX(14)[1] ) > 0.2
then ExitLong next bar at EntryPrice - TrueRange * 4 stop ;
if Average(Close ,20) > Average(Close,20)[1] then ExitLong next bar at Average(Close ,20) stop ;



多空組合的回測

在學習的過程中,如何將文字或圖型的描述轉化成策略程式碼是一件很重要的事,將回測成果的概念內化成未來策略開發的元素更是進步的基礎

0 留言:

張貼留言

如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!

----------------------------------------------------------------------------------------------------
網站聲明(Disclaimer)
本教學網站內所提供之程式碼(包括函數、指標、訊號)屬開放程式碼,用意在於讓使用者學習程式語法之撰寫,使用者可以任意修改語法內容並調整參數。本網站所有之內容(包括文章、影片、歷史紀錄、程式碼、教材)限用於個人學習使用,請勿轉寄、濫用,嚴禁私自串接帳戶交易。
-------------------------------------------------------------------------------------------------