本策略基于布林带指标设计,当价格突破布林带上轨时做空,突破下轨时做多,实现智能追踪交易。
该策略使用布林带中的中线、上轨、下轨为基础指标。中线为n天收盘价的移动平均线,上轨是中线上偏移两个标准差,下轨是中线下偏移两个标准差。当价格从下轨顺势上穿时,做多;当价格从上轨顺势下穿时,做空。这样可以根据市场波动性智能追踪价格。
具体来说,策略主要判断两个指标:
ta.crossover(source, lower):收盘价上穿下轨,做多
ta.crossunder(source, upper):收盘价下穿上轨,做空
当触发平仓条件时,使用strategy.cancel()函数平掉当前持仓。
该策略主要有以下优势:
该策略也存在一些风险:
对应解决方法:
该策略还可进一步优化:
本策略基于布林带指标设计,使用价格突破上下轨的方式实现自动追踪。策略简单易懂,对市场波动性敏感,可通过参数优化和止损方式进一步优化效果。总体来说,该策略适用于波动性较大的股指或商品市场。交易者可根据自己的交易偏好,选择合适的品种和参数进行回测优化,从中获得astika的交易策略。
/*backtest
start: 2023-12-17 00:00:00
end: 2024-01-16 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Bollinger Bands Strategy with alerts (incl. pending orders) via TradingConnector to Forex", overlay=true)
source = close
length = input.int(20, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev
buyEntry = ta.crossover(source, lower)
sellEntry = ta.crossunder(source, upper)
if (ta.crossover(source, lower))
strategy.entry("BBandLE", strategy.long, stop=lower, oca_name="BollingerBands", comment="BBandLE")
alert(message='long price='+str.tostring(lower), freq=alert.freq_once_per_bar_close)
else
strategy.cancel(id="BBandLE")
alert(message='cancel long', freq=alert.freq_once_per_bar_close)
if (ta.crossunder(source, upper))
strategy.entry("BBandSE", strategy.short, stop=upper, oca_name="BollingerBands", comment="BBandSE")
alert(message='short price='+str.tostring(upper), freq=alert.freq_once_per_bar_close)
else
strategy.cancel(id="BBandSE")
alert(message='cancel short', freq=alert.freq_once_per_bar_close)
//plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
//Lines of code added to the original built-in script: 14, 17, 20 and 23 only.
//They trigger alerts ready to be executed on real markets through TradingConnector
//available for Forex, indices, crypto, stocks - anything your broker offers for trading via MetaTrader4/5