
This strategy uses the crossover of two moving averages to determine changes in market trends and makes buy/sell decisions based on the trend direction. It goes long when the short-term MA crosses above the long-term MA, and goes short when the short-term MA crosses below the long-term MA, aiming to follow the trend.
The core of this strategy is two moving averages: a fast MA (default period 32) and a slow MA (also default period 32, adjustable via parameters). When the closing price crosses above/below the channel formed by these two MAs, it indicates a trend reversal, and the strategy generates buy/sell signals accordingly:
Through this MA crossover method, the strategy can follow the trend, holding long positions in uptrends and short positions in downtrends, until a reversal signal appears.
To address these risks, consideration can be given to adding appropriate filters, such as ATR or average true range filters, to reduce overtrading in ranging markets; setting reasonable stop-losses to control single-trade losses; and continuously optimizing parameters to adapt to the market. However, the inherent limitations of the strategy are difficult to completely avoid.
The above optimizations can enhance the strategy’s ability to handle complex markets, but care must be taken to avoid over-optimization which may lead to curve fitting and poor future performance.
The double MA trend following strategy captures trends through MA crossovers. It is simple, easy to use, and widely applicable. However, it performs poorly in ranging markets, responds inadequately to extreme movements, and faces difficulties in parameter optimization.
The strategy can be optimized by introducing more filtering indicators, dynamic stop-losses, position sizing, multi-timeframe analysis, and adaptive parameters. But the inherent limitations of MA strategies are hard to completely avoid, and caution is still needed in live trading with flexible adjustments based on market characteristics.
Overall, this strategy can serve as a basic trend-following strategy, but it is difficult to stand alone and is more suitable as part of a portfolio of strategies.
/*backtest
start: 2023-03-16 00:00:00
end: 2024-03-21 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
//study(title="Demo - SSL Basic", shorttitle="Demo - SSL Basic", overlay=true)
strategy(title='Demo - SSL Basic', shorttitle='Demo - SSL Basic', overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=100, commission_value=0.15)
// Backtest Date Range
start_date_long = input(title='Backtest Long Start Date', defval=timestamp('01 Jan 2018 00:00 +0530'))
end_date_long = input(title='Backtest Long End Date', defval=timestamp('25 Jan 2030 00:00 +0530'))
backtest_range = true
// Inputs
maType = input.string(title='SSL MA Type', options=['SMA', 'EMA', 'WMA'], defval='SMA')
sslLen = input(title='SSL Length', defval=32)
showCross = input(title='Show Crossover?', defval=true)
showEntry = input(title='Show Entry?', defval=true)
showTrend = input(title='Show Trend Colors?', defval=true)
// Calc MA for SSL Channel
calc_ma(close, len, type) =>
float result = 0
if type == 'SMA' // Simple
result := ta.sma(close, len)
result
if type == 'EMA' // Exponential
result := ta.ema(close, len)
result
if type == 'WMA' // Weighted
result := ta.wma(close, len)
result
result
// Add SSL Channel
maHigh = calc_ma(high, sslLen, maType)
maLow = calc_ma(low, sslLen, maType)
Hlv = int(na)
Hlv := close > maHigh ? 1 : close < maLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? maHigh : maLow
sslUp = Hlv < 0 ? maLow : maHigh
ss1 = plot(sslDown, title='Down SSL', linewidth=2, color=showTrend ? na : color.red)
ss2 = plot(sslUp, title='Up SSL', linewidth=2, color=showTrend ? na : color.lime)
// Conditions
longCondition = ta.crossover(sslUp, sslDown)
shortCondition = ta.crossover(sslDown, sslUp)
// Strategy
if shortCondition
strategy.close('Long', comment='Long Exit', alert_message='JSON')
if longCondition
strategy.close('Short', comment='Short Exit', alert_message='JSON')
if backtest_range and longCondition
strategy.entry('Long', strategy.long, comment='Long Entry', alert_message='JSON')
if backtest_range and shortCondition
strategy.entry('Short', strategy.short, comment= 'Short Entry', alert_message='JSON')
// Plots
fill(ss1, ss2, color=showTrend ? sslDown < sslUp ? color.new(color.lime, transp=75) : color.new(color.red, transp=75) : na, title='Trend Colors')