什麼是平均真實區間 ATR(AvgtrueRange) 看 這一篇
在powerlanguage裡面的內建訊號就有 ATR Trailing,從裡面的程式碼解釋,就是從高點拉回或是低點轉折多少倍的ATR就平倉,算是保護停損停利的方法之一
運用ATR可衍生很多的變化,不同的計算公式 也可套用到策略當作出場機制。這是網路上其他高手分享的程式碼,有機會大家研究參考
範例1 從獲利高點折返N倍ATR出場
ATRVal = AvgTrueRange(ATRLen)*TrailingATR;
if d[0]<>d[1] then begin pHigh=0; pLow=9999; end;
if marketposition = 1 then begin
if barssinceentry =0 then pHigh = H;
if H>pHigh then pHigh = H;
if c-entryprice(0)>N then sell next bar at pHigh - ATRVal stop;
end;
if marketposition =-1 then begin
if barssinceentry =0 then pLow = L;
if L<pLow then pLow = L;
if entryprice(0)-c>N then buytocover next bar at pLow + ATRVal stop;
end;
if marketposition<>marketposition[1] then begin pHigh=0;pLow=9999; end;
範例2 用ATR停損
ATRval=AvgTrueRange(ATRLength)*ProtectiveATR;
if marketposition=1 then sell next bar at entryprice-ATRval stop;
if marketposition=-1 then buytocover next bar at entryprice+ATRval stop;
範例3 移動式ATR
ATRval=AvgTrueRange(ATRLength)*TrailingATR;
if marketposition=1 then begin
if barssinceentry=0 then PosHigh=High;
if High>PosHigh then PosHigh=high;
sell next bar at PosHigh-ATRval stop;
end;
if marketposition=-1 then begin
if barssinceentry=0 then PosLow= Low;
if Low<PosLow then PosLow=Low;
buytocover next bar at PosLow+ATRval stop;
end;
範例4 用N倍ATR停損
ATRVal=AvgTrueRange(ATRLength)*N;
if marketposition=1 then sell next bar at entryprice-ATRVal stop;
if marketposition=-1 then buytocover next bar at entryprice+ATRVal stop;
對的進場很重要,好的出場更重要,更重要的是對交易的堅持
留言列表