Moving Average Cross Signal Strategy

Author: ChaoZhang, Date: 2024-01-08 15:54:32



This strategy calculates and plots different types of moving averages to implement moving average cross signals for generating buy and sell signals.

Strategy Principle

  1. The strategy allows the selection of different types of moving averages, including SMA, EMA, WMA, etc.
  2. The strategy calculates the main moving average and also allows the selection of a second moving average.
  3. Judge the market trend based on the cross situation between the main moving average and the second moving average.
  4. When the main moving average crosses above its own specified cycle moving average, a buy signal is generated; When the main moving average falls below its own specified cycle moving average, a sell signal is generated.
  5. Thus, by the cross situation of the moving averages, the market trend can be judged more clearly.

Advantages of the Strategy

  1. Customizable types of moving averages to meet different needs.
  2. Add a second moving average for clearer signals.
  3. Customizable cycles of moving averages, suitable for different time cycles.
  4. Smooth color rendering for clearer graphs.
  5. Uses a cross signal mechanism for accurate judgment of trends.

Risks & Optimization of the Strategy

  1. Moving averages have lagging properties, false signals may occur. Curve fitting moving averages can be used appropriately.
  2. Improper setting of moving average cycles may lead to missed trading opportunities. More combinations can be tested to find optimal parameters.
  3. It is recommended to use other indicators such as trading volume energy for verification to reduce risks.
  4. Consider changing the signal moving average to curl average to improve signal accuracy.
  5. Models like LSTM can be used to optimize the strategy.


The overall idea of ​​the strategy is clear, using the principle of moving average cross to judge market trend, customizable parameters to meet different needs. There are also some problems, but they can be improved by optimizing models and parameters. Overall, this strategy is a typical representative of trading strategies based on moving averages.

start: 2023-01-01 00:00:00
end: 2024-01-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy("Moving averages-Strategy", overlay=true)
//Created by user ChrisMoody 4-24-2014
//Plots The Majority of Moving Averages
//Defaults to Current Chart Time Frame --- But Can Be Changed to Higher Or Lower Time Frames
//2nd MA Capability with Show Crosses Feature

src = close
useCurrentRes = input(true, title="Use Current Chart Resolution?")
resCustom = input(title="Use Different Timeframe? Uncheck Box Above",defval="D")
len = input(20, title="Moving Average Length - LookBack Period")
atype = input(1,minval=1,maxval=7,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA")
cc = input(true,title="Change Color Based On Direction?")
smoothe = input(2, minval=1, maxval=10, title="Color Smoothing - 1 = No Smoothing")
doma2 = input(false, title="Optional 2nd Moving Average")
len2 = input(50, title="Moving Average Length - Optional 2nd MA")
atype2 = input(1,minval=1,maxval=7,title="1=SMA, 2=EMA, 3=WMA, 4=HullMA, 5=VWMA, 6=RMA, 7=TEMA")
cc2 = input(true,title="Change Color Based On Direction 2nd MA?")
warn = input(false, title="***You Can Turn On The Show Dots Parameter Below Without Plotting 2nd MA to See Crosses***")
warn2 = input(false, title="***If Using Cross Feature W/O Plotting 2ndMA - Make Sure 2ndMA Parameters are Set Correctly***")
sd = input(false, title="Show Dots on Cross of Both MA's")

res = useCurrentRes ? timeframe.period : resCustom
//hull ma definition
hullma = wma(2*wma(src, len/2)-wma(src, len), round(sqrt(len)))
//TEMA definition
ema1 = ema(src, len)
ema2 = ema(ema1, len)
ema3 = ema(ema2, len)
tema = 3 * (ema1 - ema2) + ema3

avg = atype == 1 ? sma(src,len) : atype == 2 ? ema(src,len) : atype == 3 ? wma(src,len) : atype == 4 ? hullma : atype == 5 ? vwma(src, len) : atype == 6 ? rma(src,len) : tema
//2nd Ma - hull ma definition
hullma2 = wma(2*wma(src, len2/2)-wma(src, len2), round(sqrt(len2)))
//2nd MA TEMA definition
sema1 = ema(src, len2)
sema2 = ema(sema1, len2)
sema3 = ema(sema2, len2)
stema = 3 * (sema1 - sema2) + sema3

avg2 = atype2 == 1 ? sma(src,len2) : atype2 == 2 ? ema(src,len2) : atype2 == 3 ? wma(src,len2) : atype2 == 4 ? hullma2 : atype2 == 5 ? vwma(src, len2) : atype2 == 6 ? rma(src,len2) : tema

out = avg 
out_two = avg2

out1 =, res, out)
out2 =, res, out_two)

ma_up = out1 >= out1[smoothe]
ma_down = out1 < out1[smoothe]

col = cc ? ma_up ? lime : ma_down ? red : aqua : aqua
col2 = cc2 ? ma_up ? lime : ma_down ? red : aqua : aqua

circleYPosition = out2

plot(out1, title="Multi-Timeframe Moving Avg", style=line, linewidth=4, color = col)
plot(doma2 and out2 ? out2 : na, title="2nd Multi-TimeFrame Moving Average", style=circles, linewidth=4, color=col2)
plot(sd and cross(out1, out2) ? circleYPosition : na,style=cross, linewidth=5, color=yellow)

longCondition = crossover(out1, out1[smoothe])
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(out1, out1[smoothe])
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)