Skip to main content
Skip to main content
Version: Next 🚧

How to Use Moving Averages

Problem​

You want to use moving averages in your trading algorithm to identify trends, generate entry/exit signals, or filter trades based on price position relative to moving averages.

Prerequisites​

Moving Average Types​

Simple Moving Average (SMA)​

The SMA calculates the arithmetic mean of prices over a specified period.

Formula: SMA = (P₁ + Pā‚‚ + ... + Pā‚™) / n

Characteristics:

  • Equal weight to all prices in the period
  • Slower to respond to price changes
  • Smoother line, less noise
  • Best for identifying long-term trends

Exponential Moving Average (EMA)​

The EMA gives more weight to recent prices, making it more responsive to new information.

Formula: EMA = (Price Ɨ Multiplier) + (Previous EMA Ɨ (1 - Multiplier))

  • Multiplier = 2 / (Period + 1)

Characteristics:

  • More weight to recent prices
  • Faster response to price changes
  • More sensitive to short-term movements
  • Best for short-term trading and quick trend changes

Weighted Moving Average (WMA)​

The WMA assigns linearly increasing weights to more recent prices.

Formula: WMA = (P₁ Ɨ 1 + Pā‚‚ Ɨ 2 + ... + Pā‚™ Ɨ n) / (1 + 2 + ... + n)

Characteristics:

  • Linear weighting scheme
  • Response speed between SMA and EMA
  • Less common in practice
  • Best for specific weighting requirements

Period Selection​

Common Periods​

PeriodTimeframeUse Case
9-20Short-termDay trading, scalping, quick signals
50Medium-termSwing trading, trend confirmation
100Medium-termTrend identification, support/resistance
200Long-termMajor trend direction, key support/resistance

Choosing the Right Period​

Short periods (9-20):

  • More signals, more false signals
  • Better for volatile markets
  • Suitable for active trading

Long periods (50-200):

  • Fewer signals, more reliable
  • Better for trending markets
  • Suitable for position trading

Solution​

Example 1: Price Above Moving Average (Trend Filter)​

Only take long trades when price is above the 200 EMA (uptrend).

{
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "price_indicator",
"priceType": "close",
"indicator": "EMA",
"period": 200,
"comparison": "above"
},
{
"type": "indicator_value",
"indicator": "RSI",
"period": 14,
"comparison": "below",
"value": 30
}
]
}
}

Explanation: This strategy only enters long positions when:

  1. Price is above the 200 EMA (confirming uptrend)
  2. RSI is below 30 (oversold condition)

Example 2: Moving Average Crossover (Golden Cross)​

Enter long when fast EMA crosses above slow EMA.

{
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "indicator_indicator",
"indicator1": "EMA",
"period1": 50,
"indicator2": "EMA",
"period2": 200,
"comparison": "crosses_above"
}
]
}
}

Explanation: The "Golden Cross" occurs when the 50 EMA crosses above the 200 EMA, signaling a potential long-term uptrend.

Example 3: Death Cross (Short Entry)​

Enter short when fast EMA crosses below slow EMA.

{
"entryConditions": {
"positionType": "short",
"logicalOperator": "AND",
"conditions": [
{
"type": "indicator_indicator",
"indicator1": "EMA",
"period1": 50,
"indicator2": "EMA",
"period2": 200,
"comparison": "crosses_below"
}
]
}
}

Explanation: The "Death Cross" occurs when the 50 EMA crosses below the 200 EMA, signaling a potential long-term downtrend.

Example 4: Triple EMA Strategy​

Use three EMAs for trend confirmation and entry timing.

{
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "price_indicator",
"priceType": "close",
"indicator": "EMA",
"period": 9,
"comparison": "above"
},
{
"type": "indicator_indicator",
"indicator1": "EMA",
"period1": 9,
"indicator2": "EMA",
"period2": 21,
"comparison": "above"
},
{
"type": "indicator_indicator",
"indicator1": "EMA",
"period1": 21,
"indicator2": "EMA",
"period2": 55,
"comparison": "above"
}
]
}
}

Explanation: This strategy requires:

  1. Price above 9 EMA (immediate trend)
  2. 9 EMA above 21 EMA (short-term trend)
  3. 21 EMA above 55 EMA (medium-term trend)

All three conditions confirm a strong uptrend.

Example 5: SMA Support/Resistance Bounce​

Enter long when price bounces off the 50 SMA support.

{
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "price_indicator",
"priceType": "low",
"indicator": "SMA",
"period": 50,
"comparison": "below"
},
{
"type": "price_indicator",
"priceType": "close",
"indicator": "SMA",
"period": 50,
"comparison": "above"
}
],
"confirmationCandles": 1
}
}

Explanation: This strategy looks for:

  1. Candle low touches below 50 SMA (tests support)
  2. Candle close is above 50 SMA (bounces back)
  3. Wait 1 confirmation candle to verify bounce

Example 6: Moving Average as Dynamic Stop Loss​

Use a moving average as a trailing stop loss.

{
"exitConditions": {
"stopLoss": {
"type": "indicator",
"indicator": "EMA",
"period": 20
}
}
}

Explanation: The stop loss follows the 20 EMA. For long positions, exit when price closes below the 20 EMA. For short positions, exit when price closes above the 20 EMA.

Example 7: Price Crosses Moving Average​

Enter when price crosses above the moving average.

{
"entryConditions": {
"positionType": "long",
"logicalOperator": "AND",
"conditions": [
{
"type": "price_indicator",
"priceType": "close",
"indicator": "EMA",
"period": 20,
"comparison": "crosses_above"
}
]
}
}

Explanation: Enter long when the closing price crosses above the 20 EMA, indicating a potential trend reversal or continuation.

Verification​

After configuring your moving average conditions:

  1. Backtest the strategy to see how it performs historically
  2. Check signal frequency - Too many or too few signals?
  3. Verify crossover detection - Are crosses detected correctly?
  4. Test in paper mode before going live

Troubleshooting​

Problem: Too Many False Signals​

Solution:

  • Use longer periods (e.g., 50 instead of 20)
  • Add confirmation candles
  • Combine with other indicators (RSI, MACD)
  • Use multiple moving averages for confirmation

Problem: Signals Too Slow​

Solution:

  • Use shorter periods (e.g., 9 instead of 50)
  • Switch from SMA to EMA for faster response
  • Reduce confirmation candles

Problem: Crossovers Not Detected​

Solution:

  • Ensure you're using crosses_above or crosses_below comparison
  • Check that you have enough historical data
  • Verify the periods are different for indicator_indicator comparisons

Problem: Moving Average Not Acting as Support/Resistance​

Solution:

  • Try different periods (50, 100, 200 are common)
  • Use SMA instead of EMA for clearer support/resistance
  • Combine with volume confirmation
  • Consider market conditions (works better in trending markets)

Best Practices​

  1. Combine multiple timeframes - Use 200 EMA on daily chart with 20 EMA on 1-hour chart
  2. Use EMA for short-term, SMA for long-term - EMA responds faster, SMA is more stable
  3. Popular combinations:
    • 9/21 EMA (short-term)
    • 20/50 SMA (medium-term)
    • 50/200 EMA (long-term - Golden/Death Cross)
  4. Add volume confirmation - Crossovers with high volume are more reliable
  5. Respect the trend - Don't fight the 200 EMA direction
  6. Use as dynamic support/resistance - Price often bounces off major MAs