This strategy dynamically selects different types of moving averages across multiple timeframes to generate trading signals.
The strategy allows selecting from SMA, EMA, TEMA, WMA and HMA moving averages, with customizable period lengths. Different types of moving averages will be plotted dynamically based on selection. It goes long when the closing price breaks above the moving average, and goes short when the closing price breaks below.
Specifically, the strategy first defines the backtest period based on input parameters. It then calculates five types of moving averages:
The corresponding moving average is plotted based on selection. It goes long when the closing price is above the moving average, and goes short when below.
By combining different types of moving averages, the strategy can smooth price data and filter out market noise to generate more reliable trading signals. Customizable period lengths allow trading different trends across timeframes.
Risks can be reduced by:
The strategy can be improved in several aspects:
Add other filters for more stable signals
e.g. volume indicators to avoid false breakouts without volume confirmation.
Optimize entry and exit logic
Set price channels and stop losses to reduce unnecessary losses.
Dynamic moving average periods
Use longer periods in strong trends and shorter periods during consolidations.
Improve money management
Adjust position sizes based on drawdowns and profit taking.
The strategy combines various moving averages across timeframes to generate relatively stable trend following effects. With ample room for optimizations in entries, exits, parameters and money management, it can be improved for better real-world performance.
/*backtest start: 2022-10-20 00:00:00 end: 2023-10-26 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=3 strategy("MA_strategy ", shorttitle="MA_strategy", overlay=true, initial_capital=100000) qty = input(100000000, "Buy quantity") testStartYear = input(2017, "Backtest Start Year") testStartMonth = input(1, "Backtest Start Month") testStartDay = input(1, "Backtest Start Day") testStartHour = input(0, "Backtest Start Hour") testStartMin = input(0, "Backtest Start Minute") testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,testStartHour,testStartMin) testStopYear = input(2099, "Backtest Stop Year") testStopMonth = input(1, "Backtest Stop Month") testStopDay = input(30, "Backtest Stop Day") testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0) testPeriodBackground = input(title="Color Background?", type=bool, defval=true) testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na bgcolor(testPeriodBackgroundColor, transp=97) testPeriod() => time >= testPeriodStart and time <= testPeriodStop ? true : false ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"]) len1 = input(7, minval=1, title="Period") s=sma(close,len1) e=ema(close,len1) xEMA1 = ema(close, len1) xEMA2 = ema(xEMA1, len1) xEMA3 = ema(xEMA2, len1) t = 3 * xEMA1 - 3 * xEMA2 + xEMA3 f_hma(_src, _length)=> _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length))) h = f_hma(close, len1) w = wma(close, len1) ma = ma1 == "SMA"?s:ma1=="EMA"?e:ma1=="WMA"?w:ma1=="HMA"?h:ma1=="TEMA"?t:na buy= close>ma sell= close<ma alertcondition(buy, title='buy', message='buy') alertcondition(sell, title='sell', message='sell') ordersize=floor(strategy.equity/close) if testPeriod() strategy.entry("long",strategy.long,ordersize,when=buy) strategy.close("long", when = sell )