この戦略は,平均真波幅指数ATRに基づいてトレンドの方向を判断し,トレンドが上昇するときに多めにし,トレンドが低下するときに空白にし,トレンドを追跡するタイプの戦略である.
この戦略は,まず,価格の単純移動平均smaと指数移動平均emaを計算する.そして,ATR指標,つまり過去N日の平均波動範囲を計算する.
策略は,ema平均線,上線 ((ema + ATR *係数) と下線 ((ema - ATR *係数) を用いてトレンド方向を判断する.価格が上線に突入する時は,多めに;価格が下線に突入する時は,空っぽにする.
コードの主な論理:
ATRでポジションを動的に調整し,トレンドを効果的に追跡する Directions。
解決策は
このATRトレンド追跡戦略は,全体的な考えが明確で,ATR指標によってトレンドの方向性を判断し,典型的なトレンド追跡戦略の1つである.戦略の優点は,操作が簡単で,トレンドを効果的に追跡できることです.しかし,一定のリスクも存在し,異なる市場環境に対して最適化調整を行う必要がある.戦略の最大限の効果を発揮するためには.全体的に,この戦略は,量化取引ツールとして,広範囲の展開空間と使用価値があります.
/*backtest
start: 2023-08-28 00:00:00
end: 2023-09-27 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Investoz
//@version=4
strategy("ATR Strategy FOREX", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
len = input(26, type=input.integer, minval=1, title="Length")
mul = input(2.618, type=input.float, minval=0, title="Length")
mullow = input(2.386, type=input.float, minval=0, title="Length")
price = sma(close, 1)
average = ema(close, len)
diff = atr(len) * mul
difflow = atr(len) * mullow
bull_level = average + diff
bear_level = average - difflow
bull_cross = crossunder(price, bear_level)
bear_cross = crossunder(bull_level, price)
FromMonth = input(defval = 8, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 18, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2008, title = "From Year", minval = 2008)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 2020, title = "To Year", minval = 2019)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
startTimeOk() => true
if (startTimeOk()) and ema(close,1) > ema(close,528)
strategy.entry("KOP", strategy.long, when=bull_cross)
strategy.close("KOP", when=bear_cross)
if (startTimeOk()) and ema(close,1) < ema(close,528)
strategy.entry("SALJ", strategy.short, when=bear_cross)
strategy.close("SALJ", when=bull_cross)
plot(price, title="price", color=color.black, transp=50, linewidth=2)
a0 = plot(average, title="average", color=color.red, transp=50, linewidth=1)
a1 = plot(bull_level, title="bull", color=color.green, transp=50, linewidth=1)
a2 = plot(bear_level, title="bear", color=color.red, transp=50, linewidth=1)
fill(a0, a1, color=color.green, transp=97)
fill(a0, a2, color=color.red, transp=97)