想利用日K的平均真實區間來計算停利和移動停損,
但是一直無法抓出正確的ATR數值,
不知道是哪裡有問題?
附上我的自動停損停利腳本和跨頻率計算日層級ATR的函數腳本
煩請解惑,謝謝
自動停損利腳本----------------------------
input: Period(14, "日ATR計算週期"); var: intrabarpersist stoploss_point(0); value1 = xf_dATR(Period); //計算日層級的平均真實區間 value2 = intPortion(value1*1.5); //計算停利點數 value3 = intPortion(value1*0.5); //計算移動停損點數 {多單停損停利} if Position > 0 and Filled > 0 then begin if stoploss_point = 0 then stoploss_point = FilledAvgPrice - value3; if Close > FilledAvgPrice then if Close - value3 > stoploss_point then stoploss_point = Close - value3; if Close >= FilledAvgPrice + value2 then begin SetPosition(0, market, label:="多單停利"); stoploss_point = 0; end else if Close <= stoploss_point then begin SetPosition(0, market, label:="多單移停"); stoploss_point = 0; end; end; {空單停損停利} if Position < 0 and Filled < 0 then begin if stoploss_point = 0 then stoploss_point = FilledAvgPrice + value3; if Close < FilledAvgPrice then if Close + value3 < stoploss_point then stoploss_point = Close + value3; if Close <= FilledAvgPrice - value2 then begin SetPosition(0, market, label:="空單停利"); stoploss_point = 0; end else if Close >= stoploss_point then begin SetPosition(0, market, label:="空單移停"); stoploss_point = 0; end; end; print(date, time, Filled, filledAvgPrice, close, stoploss_point, value1);
跨頻率計算日層級ATR的函數腳本(不含當日的區間)
SetBarMode(1); {傳回平均真實區間 Length: 計算期數} input: Length(numeric); vars:J(0); if getfield("Close", "D")[2] > getfield("High", "D")[1] then value1 = getfield("Close", "D")[2] else value1 = getfield("High", "D")[1]; if getfield("Close", "D")[2] < getfield("Low", "D")[1] then value2 = getfield("Close", "D")[2] else value2 = getfield("Low", "D")[1]; value3 = value1 - value2; value4 = value3; for value5 = 1 to Length-1 begin value4 += xf_GetValue("D", value3, value5); end; xf_dATR = (value4/Length);
3 評論