AlgoHive
Examples

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

Loading strategy viewer...

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

SettingValue
Aliasbtc
TypeExchange
SymbolBTC-USD
Timeframe4h

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 slow
  • cross_down — True only on the bar where fast crosses below slow

Entry Rule

SettingValue
DirectionLong
Marketbtc
Whensignals.cross_up

Risk Management

Stop Loss:

SettingValue
TypeStop Loss
Level5% (percentage)

Take Profit:

SettingValue
TypeTake Profit
Level3R (risk multiple)

This means we risk 5% to make 15% (3:1 reward-to-risk).

Risk Settings

SettingValue
Position Sizing1% risk per trade
Slippage0.05%
Commission0.05%

Building This Strategy

Follow these steps to recreate this strategy in the Studio:

  1. Add a Data Source — Click "Add Data Source", select Exchange, and configure BTC-USD on 4h timeframe with alias btc
  2. Create Analysis Block — Add an Advanced block named signals with the EMA calculations
  3. Add Entry Rule — Create a Long entry that triggers on signals.cross_up
  4. 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 < 70

Use 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_200

Short Entries

Add the reverse for short positions:

Entry when: signals.cross_down with direction: short

Backtest Results

Typical characteristics of this strategy:

MetricTypical Range
Win Rate35-45%
Profit Factor1.2-1.8
Max Drawdown15-25%

Note: Results vary significantly based on market conditions and parameters.

Tips

  1. Optimize parameters — Test different EMA periods (e.g., 12/26, 20/50)
  2. Filter false signals — Add volume or trend filters
  3. Consider the timeframe — Higher timeframes = fewer but cleaner signals
  4. Manage expectations — Trend-following strategies have lower win rates but larger wins

On this page