该策略主要利用双重移动平均线作为买入和卖出信号,在趋势反转时获利。当短期移动平均线上穿长期移动平均线时做多,当短期移动平均线下穿长期移动平均线时做空,属于常见的跟踪止损策略。
该策略首先设置两个移动平均线,一个较短期的20日均线,一个较长期的60日均线。然后判断短期均线和长期均线的交叉情况来决定入场。
具体来说,当短期均线上穿长期均线时,表示目前处于上升趋势,这时做多;当短期均线下穿长期均线时,表示目前处于下降趋势,这时做空。
做多做空后的止损方式是跟踪止损,根据最高价和最低价来trailing stop,可以锁定最大利润。
代码主要逻辑如下:
该策略具有以下优势:
该策略也存在一些风险:
针对风险,可以通过以下方式进行优化:
该策略可以从以下几个方面进行进一步优化:
增加其他指标过滤,形成多重条件入场机制,避免假突破。例如可以加入RSI指标判定。
优化移动均线的周期参数,找到最佳参数组合。可以通过步进遍历方式测试不同周期参数。
优化止损范围。可以通过回测数据计算出最佳的止损范围。也可以设置动态止损范围。
设置再入场机制。在止损退出后,可以设置合理的再入场逻辑,减少交易次数。
结合趋势判断指标,在趋势不明显时暂停交易,避免无效交易。
加入仓位管理机制,根据市场情况动态调整仓位和止损范围。
该双重移动平均线反转策略整体来说较为简单实用,通过双均线判断趋势转折点,属于一种常见而有效的方法。但存在一定风险,需要对参数设定及止损范围进行优化测试,并加入其他过滤指标进行配合使用,才能发挥策略最大效用。如果经过细致优化和严格的风险管理,该策略可以成为稳定盈利的波段交易策略。
/*backtest
start: 2023-09-23 00:00:00
end: 2023-10-15 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=2
strategy("Noro's Bands Scalper Strategy v1.4", shorttitle = "Scalper str 1.4", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value=100.0, pyramiding=0)
//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(true, defval = true, title = "Short")
takepercent = input(0, defval = 0, minval = 0, maxval = 1000, title = "take, %")
needbe = input(true, defval = true, title = "Bands Entry")
needct = input(false, defval = false, title = "Counter-trend entry")
needdb = input(true, defval = true, title = "Double Body")
len = input(20, defval = 20, minval = 2, maxval = 200, title = "Period")
needbb = input(true, defval = true, title = "Show Bands")
needbg = input(true, defval = true, title = "Show Background")
src = close
//PriceChannel 1
lasthigh = highest(src, len)
lastlow = lowest(src, len)
center = (lasthigh + lastlow) / 2
//Distance
dist = abs(src - center)
distsma = sma(dist, len)
hd = center + distsma
ld = center - distsma
hd2 = center + distsma * 2
ld2 = center - distsma * 2
//Trend
trend = close < ld and high < center ? -1 : close > hd and low > center ? 1 : trend[1]
//Lines
colo = needbb == false ? na : black
plot(hd2, color = colo, linewidth = 1, transp = 0, title = "High band 2")
plot(hd, color = colo, linewidth = 1, transp = 0, title = "High band 1")
plot(center, color = colo, linewidth = 1, transp = 0, title = "center")
plot(ld, color = colo, linewidth = 1, transp = 0, title = "Low band 1")
plot(ld2, color = colo, linewidth = 1, transp = 0, title = "Low band 2")
//Background
col = needbg == false ? na : trend == 1 ? lime : red
bgcolor(col, transp = 80)
//Body
body = abs(close - open)
smabody = needdb == false ? ema(body, 30) : ema(body, 30) * 2
candle = high - low
//Signals
bar = close > open ? 1 : close < open ? -1 : 0
up7 = trend == 1 and ((bar == -1 and bar[1] == -1) or (body > smabody and bar == -1)) ? 1 : 0
dn7 = trend == 1 and ((bar == 1 and bar[1] == 1) or (close > hd and needbe == true)) and close > strategy.position_avg_price * (100 + takepercent) / 100 ? 1 : 0
up8 = trend == -1 and ((bar == -1 and bar[1] == -1) or (close < ld2 and needbe == true)) and close < strategy.position_avg_price * (100 - takepercent) / 100 ? 1 : 0
dn8 = trend == -1 and ((bar == 1 and bar[1] == 1) or (body > smabody and bar == 1)) ? 1 : 0
if up7 == 1 or up8 == 1
strategy.entry("Long", strategy.long, needlong == false ? 0 : trend == -1 and needct == false ? 0 : na)
if dn7 == 1 or dn8 == 1
strategy.entry("Short", strategy.short, needshort == false ? 0 : trend == 1 and needct == false ? 0 : na)