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ā
- Basic understanding of entry conditions
- Familiarity with indicator-based comparisons
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ā
| Period | Timeframe | Use Case |
|---|---|---|
| 9-20 | Short-term | Day trading, scalping, quick signals |
| 50 | Medium-term | Swing trading, trend confirmation |
| 100 | Medium-term | Trend identification, support/resistance |
| 200 | Long-term | Major 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:
- Price is above the 200 EMA (confirming uptrend)
- 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:
- Price above 9 EMA (immediate trend)
- 9 EMA above 21 EMA (short-term trend)
- 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:
- Candle low touches below 50 SMA (tests support)
- Candle close is above 50 SMA (bounces back)
- 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:
- Backtest the strategy to see how it performs historically
- Check signal frequency - Too many or too few signals?
- Verify crossover detection - Are crosses detected correctly?
- 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_aboveorcrosses_belowcomparison - 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ā
- Combine multiple timeframes - Use 200 EMA on daily chart with 20 EMA on 1-hour chart
- Use EMA for short-term, SMA for long-term - EMA responds faster, SMA is more stable
- Popular combinations:
- 9/21 EMA (short-term)
- 20/50 SMA (medium-term)
- 50/200 EMA (long-term - Golden/Death Cross)
- Add volume confirmation - Crossovers with high volume are more reliable
- Respect the trend - Don't fight the 200 EMA direction
- Use as dynamic support/resistance - Price often bounces off major MAs