本策略基于简单移动平均线(SMA)的金叉死叉原理设计。策略使用两个SMA,即快速SMA和慢速SMA,当快速SMA从下方向上突破慢速SMA时,产生买入信号;当快速SMA从上方向下跌破慢速SMA时,产生卖出信号。
该策略主要依赖两个SMA指标线。其中,快速SMA期间设置较短,能更快捕捉价格变动;慢速SMA期间设置较长,能过滤掉部分噪音。当快速SMA从下方向上交叉慢速SMA时,表示短期价格上涨速度较快,产生买入信号。当快速SMA从上方向下交叉慢速SMA时,表示短期价格下跌速度较快,产生卖出信号。
通过设置不同的SMA周期参数,可以在一定程度上调整策略的参数,适应不同的市场环境。同时,该策略还允许设置回测的时间范围,方便在历史数据上测试策略 parameter。
针对上述风险,可以采用以下措施: - 适当缩短SMA周期,提高敏感性 - 结合其他指标判断趋势力度 - 借助参数优化工具寻找最佳参数组合
本策略属于典型的趋势跟踪策略。运用简单的双均线交叉原理,在参数设置合适的前提下,可以获取较好的跟踪效果。但SMA本身存在一定的滞后性,无法判断趋势的力度。因此,实际应用中,需要引入其他辅助工具,形成指标组合,同时辅以自动化的参数优化和风险控制手段,才能使策略稳定盈利。
/*backtest
start: 2023-12-17 00:00:00
end: 2023-12-18 19:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
//strategy(title="MA Cross Entry & Exit w/Date Range", overlay=true, initial_capital=10000, currency='USD')
strategy(title="SMA Cross Entry & Exit Strategy", overlay=true)
// Credit goes to this developer for the "Date Range Code"
// https://www.tradingview.com/script/62hUcP6O-How-To-Set-Backtest-Date-Range/
// === GENERAL INPUTS ===
// short ma
maFastSource = input(defval = open, title = "Fast MA Source")
maFastLength = input(defval = 36, title = "Fast MA Period", minval = 1)
// long ma
maSlowSource = input(defval = open , title = "Slow MA Source")
maSlowLength = input(defval = 46, title = "Slow MA Period", minval = 1)
// === SERIES SETUP ===
// a couple of ma's..
maFast = sma(maFastSource, maFastLength)
maSlow = sma(maSlowSource, maSlowLength)
// === PLOTTING ===
fast = plot(maFast, title = "Fast MA", color = red, linewidth = 2, style = line, transp = 30)
slow = plot(maSlow, title = "Slow MA", color = green, linewidth = 2, style = line, transp = 30)
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2018, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2017)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
// === LOGIC ===
//enterLong = crossover(maFast, maSlow)
//exitLong = crossover(maSlow, maFast)
enterLong = crossover(maSlow, maFast)
exitLong = crossover(maFast, maSlow)
// Entry //
strategy.entry(id="Long Entry", long=true, when=window() and enterLong)
strategy.entry(id="Short Entry", long=false, when=window() and exitLong)
// === FILL ====
fill(fast, slow, color = maFast > maSlow ? green : red)