
动态止损追涨策略通过计算股票的平均真实波动范围ATR作为基准,结合用户设定的ATR系数动态设定止损线和追涨线,实现止损追涨的目的。当股票价格突破追涨线时,采用传统趋势追踪策略建立多头头寸;当股票价格跌破止损线时,采用反转策略建立空头头寸,运用双向交易获利。
该策略主要运用了ATR技术指标计算股价的平均真实波动范围,并结合用户输入的ATR系数作为股票突破买入和止损卖出的依据。具体来说,策略首先计算股票过去120天的ATR值,然后乘以用户设定的卖出ATR系数得到止损卖出参考价,即止损线;乘以买入ATR系数得到买入参考价,即追涨线。当今日最高价突破追涨线时,采取趋势追踪策略建立多头头寸;当今日最低价跌破止损线且持有多头头寸时,采取反转策略建立空头头寸。
该策略同时绘制了止损线和追涨线,这两条线的位置会根据股价的波动而变化,具有一定的动态追踪功能。ATR指标能较好地反映出股票的平均真实波动程度,运用ATR指标设定止损追涨线,可以一定程度上規避股票巨大波动带来的亏损。
本策略总体来说是一个典型的止损追涨策略,核心思路是基于ATR指标设定止损线和追涨线,进行趋势跟踪。该策略优点是可以双向交易,持仓灵活;运用ATR指标控制风险,适合于高波动的股票。但由于买卖规则较简单,存在一定的盲目追踪风险;参数设定不当也会影响策略效果。未来可从完善买卖时机判断、控制仓位规模、减少过度交易等方面进行优化,使策略效果更加稳定。
/*backtest
start: 2022-11-14 00:00:00
end: 2023-11-20 00:00:00
period: 1d
basePeriod: 1h
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/
// © phobo3s
//@version=4
strategy("ATR Stop Buy Strategy",shorttitle="ATR-ST",initial_capital=1000, overlay = true, default_qty_type = strategy.percent_of_equity, pyramiding = 5, default_qty_value = 20, commission_type = strategy.commission.cash_per_order, commission_value = 1, calc_on_every_tick = true)
daysBack = input(defval=120, title="Days Back", type=input.integer)
sellCoeff = input(defval=1.5, title="Selling Coefficent For ATR", type=input.float, minval= 0.01, step=0.1)
buyCoeff = input(defval=1.2, title = "Buying Coefficent For ATR", type=input.float, minval= 0.01, step=0.1)
fromDate = timenow - (daysBack*24*60*60*1000)
toDate = timenow
ATR = atr(14)
stopLossPoint = ATR * sellCoeff
buyPoint = ATR * buyCoeff
StoplossLine = close[1] - stopLossPoint[1]
BuyLine = close[1] + buyPoint[1]
if (high > BuyLine and time >= fromDate and time <= toDate )
strategy.entry("GG", strategy.long, comment="Gir")
if (low < StoplossLine and strategy.position_avg_price < close and time >= fromDate and time <= toDate )
strategy.entry("GG", strategy.short, comment="Çık")
//longFlags = close < StoplossLine
//shortFlags = close > BuyLine
//plotshape(shortFlags, style=shape.triangledown, location=location.abovebar, color=color.red)
//plotshape(longFlags, style=shape.triangleup, location=location.belowbar, color=color.blue)
plot(StoplossLine)
plot(BuyLine)