本策略名为“动态移动均线策略”,主要思想是利用移动均线的方向和价格的关系来判断趋势,在趋势方向进入场内,在无趋势时平仓。
该策略使用 length 个周期的源价格来计算出移动均线,源价格可以选择 OHLC4,HLC3,收盘价等。计算出的移动均线被定义为 sma。然后根据移动均线值的比例绘制出长线和短线,通过长线和短线的位置关系来判断目前处于上涨趋势还是下跌趋势。
具体来说,短线的计算公式是:shortline = sma * ((100 + shortlevel) / 100),其中 shortlevel 是可以由使用者设定的一个正数,代表短线距离移动均线的比例。长线类似,计算公式是:longline = sma * ((100 + longlevel) / 100),longlevel 是可以由使用者设定的一个负数,代表长线距离移动均线的比例。
这样,短线值永远大于移动均线,而长线值永远小于移动均线。当价格上穿短线时,代表进入上涨趋势,这个时候若 needlong 允许做多,则会在长线价格水平下单做多;当价格下穿长线时,代表进入下跌趋势,这个时候若 needshort 允许做空,则会在短线价格水平下单做空。
无论做多或做空,当价格重新回到移动均线时,代表趋势结束,这个时候会平掉之前的所有仓位。
这样通过长短线与移动均线的动态关系来判断趋势方向并据此入场和出场。
这种策略最大的优势在于通过长短线动态设定买卖点,能比较灵活地把握主要的趋势方向。相比简单地在固定水平触发买卖点的策略,这种策略更为高级和智能。
其次,移动均线自身也有一定滤波作用,一定程度避免被高频震荡套住。同时依据移动均线水平来判断趋势是否结束时及时出场,这也非常关键。
该策略最大的风险在于移动均线在不同时期的表现力度不同。正常情况下移动均线足以代表趋势方向,但是在某些极端行情中短期内移动均线可能被打穿,造成错误的入场,或者顶背离这样的情况。这时需要使用更长周期的移动均线来确保趋势判断的准确性。
风险的另一方面在于移动均线本身缓慢性较强。对于一些短小剧烈的价格波动,移动均线难以及时作出跟踪,这时可能错过入场点或出场点。需要降低周期来加快移动均线的反应速度。
该策略可以在以下几个方面继续优化: 1. 增加止损逻辑。移动均线判断趋势时有滞后性,无法完全避免被套,因此适当加上移动止损可以进一步降低风险。 2. 优化长短均线的参数。目前长短均线距离移动均线的比例是固定值,可以测试不同数据集,找出最优参数。 3. 增加趋势强度判断。除了长短均线位置,也可以通过一定算法判断趋势的强度,避免弱趋势下的错误信号。 4. 可以尝试将移动均线应用于其他交易品种,进行跨品种验证。
本策略通过动态设定买卖点的方式进行趋势判断和对应的多空交易。这种基于移动均线动态设定交易信号的方法,相比静态触发点能更加灵活和智能地捕捉价格趋势。同时也解决了移动均线本身缺乏及时性的问题。通过系统的回测和参数优化,相信该策略可以获得良好收益。
/*backtest
start: 2022-11-16 00:00:00
end: 2023-11-22 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//Noro
//2018
//@version=3
strategy(title = "Noro's ShiftMA Strategy v1.1", shorttitle = "ShiftMA str 1.1", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, pyramiding = 100)
//Settings
needlong = input(true, defval = true, title = "Long")
needshort = input(false, defval = false, title = "Short")
capital = input(100, defval = 100, minval = 1, maxval = 10000, title = "Lot, %")
per = input(3, title = "Length")
src = input(ohlc4, title = "Source")
shortlevel = input(10.0, title = "Short line (red)")
longlevel = input(-5.0, title = "Long line (lime)")
fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day")
//SMAs
sma = sma(src, per)
//sma = lowest(low, per)
shortline = sma * ((100 + shortlevel) / 100)
longline = sma * ((100 + longlevel) / 100)
plot(shortline, linewidth = 2, color = red, title = "Short line")
plot(sma, linewidth = 2, color = blue, title = "SMA line")
plot(longline, linewidth = 2, color = lime, title = "Long line")
//plot(round(buy * 100000000), linewidth = 2, color = lime)
//plot(round(sell * 100000000), linewidth = 2, color = red)
//Trading
size = strategy.position_size
lot = 0.0
lot := size == 0 ? strategy.equity / close * capital / 100 : lot[1]
if (not na(close[per])) and size == 0 and needlong
strategy.entry("L", strategy.long, lot, limit = longline, when = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
if (not na(close[per])) and size == 0 and needshort
strategy.entry("S", strategy.short, lot, limit = shortline, when = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
if (not na(close[per])) and size > 0
strategy.entry("Close", strategy.short, 0, limit = sma, when = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
if (not na(close[per])) and size < 0
strategy.entry("Close", strategy.long, 0, limit = sma, when = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59)))
if time > timestamp(toyear, tomonth, today, 23, 59)
strategy.close_all()