本策略基于布林带指标进行交易信号判断,使用止损止盈方式进行仓位管理。策略会监控布林带上轨和下轨的突破情况,在价格突破布林带上轨时做多,突破下轨时做空,并在反向突破时使用止损单平仓。
该策略使用布林带指标中的中轨、上轨和下轨线。中轨线是计算一定周期内的价格中值,上轨线是中轨线加上标准差的两倍,下轨线是中轨线减去标准差的两倍。
代码首先计算布林带的中轨、上轨和下轨。然后,判断价格是否突破上轨或下轨,如果突破上轨则做多,如果突破下轨则做空。同时,如果价格反向突破上轨或下轨,则使用止损单平仓。
具体来说,策略逻辑如下:
通过这种方式,可以在股价产生较大波动时捕捉趋势,同时也可以通过止损来限制亏损。
可以通过Combine指标组合,适当调整止损单位等方式来优化。
本策略基于布林带指标设计了一个较为简单的趋势跟踪策略。它可以在价格产生突破时快速形成仓位,同时利用止损来控制风险。但仅考虑价格因素可能导致误判,而过于灵敏的止损又可能增加交易频率。我们可以通过参数优化、指标组合、止损调整等方式进一步完善该策略。总体来说,本策略为我们提供了一个相对简单可靠的量化交易思路。
/*backtest start: 2023-09-26 00:00:00 end: 2023-10-26 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/ // © ROBO_Trading //@version=5 strategy(title = "Bollinger Stop Strategy", shorttitle = "BBStop", overlay = true, default_qty_type = strategy.percent_of_equity, initial_capital = 10000, default_qty_value = 100, commission_value = 0.1) //Settings long = input(true) short = input(true) length = input.int(20, minval=1) mult = input.float(2.0, minval=0.001, maxval=50) source = input(close) showbb = input(true, title = "Show Bollinger Bands") showof = input(true, title = "Show Offset") startTime = input(defval = timestamp("01 Jan 2000 00:00 +0000"), title = "Start Time", inline = "time1") finalTime = input(defval = timestamp("31 Dec 2099 23:59 +0000"), title = "Final Time", inline = "time1") //Bollinger Bands basis = ta.sma(source, length) dev = mult * ta.stdev(source, length) upper = basis + dev lower = basis - dev //Show indicator offset = showof ? 1 : 0 colorBasis = showbb ? color.gray : na colorUpper = showbb ? color.blue : na colorLower = showbb ? color.blue : na colorBands = showbb ? color.blue : na p0 = plot(basis, "Basis", color = colorBasis, offset = offset) p1 = plot(upper, "Upper", color = colorUpper, offset = offset) p2 = plot(lower, "Lower", color = colorLower, offset = offset) fill(p1, p2, title = "Background", color = colorBands, transp = 90) //Trading truetime = true if basis > 0 and truetime if long strategy.entry("Long", strategy.long, stop = upper, when = truetime) if short strategy.entry("Short", strategy.short, stop = lower, when = truetime) if long == false strategy.exit("Exit", "Short", stop = upper) if short == false strategy.exit("Exit", "Long", stop = lower) if time > finalTime strategy.close_all()