Moving Average
A moving average smooths price data by calculating the average price over a specific period, creating a single flowing line that helps filter out short-term fluctuations and highlight longer-term trends. Different types of moving averages weight historical prices differently, with exponential and smoothed variants giving more weight to recent prices.
Moving averages were among the first technical indicators developed and remain one of the most widely used tools in technical analysis. They form the foundation for many other indicators and trading strategies.
What It Measures
Moving averages measure the average price of an asset over a specified time period, smoothing out price fluctuations to reveal the underlying trend direction and momentum.
When to Use
- Identify Trend Direction: Price above the MA suggests an uptrend; price below suggests a downtrend
- Dynamic Support/Resistance: Moving averages often act as support in uptrends and resistance in downtrends
- Smoothing Noisy Data: Filter out short-term volatility to focus on longer-term movements
- Component for Other Indicators: Moving averages are building blocks for Bollinger Bands, MACD, and many other technical indicators
- Crossover Strategies: Multiple MAs can be used together for trading signals (e.g., golden cross, death cross)
Period Selection Guidelines
The period parameter significantly affects the moving average's behavior:
| Period Range | Timeframe | Characteristics | Use Case |
|---|---|---|---|
| 5-10 | Very Short | High responsiveness, more noise | Scalping, intraday trading |
| 10-20 | Short | Balanced sensitivity | Day trading, swing entries |
| 20-50 | Medium | Moderate smoothing | Swing trading, trend identification |
| 50-100 | Long | Smooth, established trends | Position trading, long-term trends |
| 100-200+ | Very Long | Very smooth, major trends | Long-term investing, macro trends |
Common Period Combinations
Traders often use multiple moving averages together:
- Fast/Slow Pairs: 10/20, 12/26, 20/50 for crossover signals
- Classic: 50/200 for "golden cross" / "death cross" signals
- Ribbon: Multiple MAs (5, 10, 15, 20, 25, 30) to visualize trend strength
Default Usage
use rust_ti::moving_average::bulk::moving_average;
use rust_ti::MovingAverageType;
pub fn main() {
// fetch the data in your preferred way
// let prices = vec![...]; // price data
// Simple Moving Average
let ma = moving_average(&prices, MovingAverageType::Simple, 20);
println!("MA: {:?}", ma);
// Exponential Moving Average
let ema = moving_average(&prices, MovingAverageType::Exponential, 20);
println!("EMA: {:?}", ema);
// Smoothed Moving Average
let sma = moving_average(&prices, MovingAverageType::Smoothed, 20);
println!("SMA: {:?}", sma);
}
import pytechnicalindicators as pti
# Simple Moving Average
ma = pti.moving_average.bulk.moving_average(prices, "simple", 20)
print("MA:", ma)
# Exponential Moving Average
ema = pti.moving_average.bulk.moving_average(prices, "exponential", 20)
print("EMA:", ema)
# Smoothed Moving Average
sma = pti.moving_average.bulk.moving_average(prices, "smoothed", 20)
print("SMA:", sma)
import init, {
movingAverage_bulk_movingAverage,
MovingAverageType
} from 'https://cdn.jsdelivr.net/npm/ti-engine@latest/dist/web/ti_engine.js';
await init();
// fetch the data in your preferred way
// const prices = [...]; // price data
// Simple Moving Average
const ma = movingAverage_bulk_movingAverage(prices, MovingAverageType.Simple, 20);
console.log("MA:", ma);
// Exponential Moving Average
const ema = movingAverage_bulk_movingAverage(prices, MovingAverageType.Exponential, 20);
console.log("EMA:", ema);
// Smoothed Moving Average
const sma = movingAverage_bulk_movingAverage(prices, MovingAverageType.Smoothed, 20);
console.log("SMA:", sma);
Interactive Chart
Use the interactive playground below to explore how different parameters affect the indicator's behavior.