MA Crossover Strategy
A classic moving average crossover strategy for trend following.
The moving average crossover is one of the most popular trading strategies. It's simple, effective, and a great starting point for learning AlgoHive.
Visual Overview
EMA Crossover
Buy when 9 EMA crosses above 21 EMA
The Concept
Buy when a fast moving average crosses above a slow moving average (bullish signal).
Sell when price hits a stop loss or take profit, or when the averages cross back.
The idea: when the faster average crosses above the slower one, momentum is shifting upward.
Strategy Setup
Data Source
| Setting | Value |
|---|---|
| Alias | btc |
| Type | Exchange |
| Symbol | BTC-USD |
| Timeframe | 4h |
Analysis Block: signals
Create an Advanced Block with these outputs:
fast_ema: EMA(btc.close, 9)
slow_ema: EMA(btc.close, 21)
trend_up: fast_ema > slow_ema
cross_up: cross_above(fast_ema, slow_ema)
cross_down: cross_below(fast_ema, slow_ema)What each output does:
fast_ema— 9-period EMA (reacts quickly to price)slow_ema— 21-period EMA (smoother, slower)trend_up— True when fast is above slow (bullish trend)cross_up— True only on the bar where fast crosses above slowcross_down— True only on the bar where fast crosses below slow
Entry Rule
| Setting | Value |
|---|---|
| Direction | Long |
| Market | btc |
| When | signals.cross_up |
Risk Management
Stop Loss:
| Setting | Value |
|---|---|
| Type | Stop Loss |
| Level | 5% (percentage) |
Take Profit:
| Setting | Value |
|---|---|
| Type | Take Profit |
| Level | 3R (risk multiple) |
This means we risk 5% to make 15% (3:1 reward-to-risk).
Risk Settings
| Setting | Value |
|---|---|
| Position Sizing | 1% risk per trade |
| Slippage | 0.05% |
| Commission | 0.05% |
Building This Strategy
Follow these steps to recreate this strategy in the Studio:
- Add a Data Source — Click "Add Data Source", select Exchange, and configure BTC-USD on 4h timeframe with alias
btc - Create Analysis Block — Add an Advanced block named
signalswith the EMA calculations - Add Entry Rule — Create a Long entry that triggers on
signals.cross_up - Set Risk Management — Add Stop Loss at 5% and Take Profit at 3R
Variations
Add RSI Filter
Only enter when RSI confirms the trend isn't overbought:
rsi: RSI(btc.close, 14)
entry: signals.cross_up AND rsi < 70Use ATR-Based Stops
Dynamic stop based on volatility:
atr: ATR(btc.high, btc.low, btc.close, 14)Then set stop loss level to: atr * 2 (expression)
Add Trend Filter
Only trade when price is above a longer-term average:
above_200: btc.close > EMA(btc.close, 200)
entry: signals.cross_up AND above_200Short Entries
Add the reverse for short positions:
Entry when: signals.cross_down with direction: short
Backtest Results
Typical characteristics of this strategy:
| Metric | Typical Range |
|---|---|
| Win Rate | 35-45% |
| Profit Factor | 1.2-1.8 |
| Max Drawdown | 15-25% |
Note: Results vary significantly based on market conditions and parameters.
Tips
- Optimize parameters — Test different EMA periods (e.g., 12/26, 20/50)
- Filter false signals — Add volume or trend filters
- Consider the timeframe — Higher timeframes = fewer but cleaner signals
- Manage expectations — Trend-following strategies have lower win rates but larger wins