← Terminal
Strategy Engine

EMA Cross
Strategy Guide

How the signal fires, how positions are sized, and how the backtest engine simulates it all.

Default Short EMA
9
Default Long EMA
21
Stop Loss
2%
Take Profit
5%
Max Leverage
10×

EMA Crossover Signal

Short EMA (9) vs Long EMA (21)
SELL BUY Short EMA (9) Long EMA (21)

BUY → LONG

Fires when the short EMA crosses above the long EMA at the close of a candle.

If a SHORT is open, it is closed first. Then the new LONG opens at the same price.

short[i-1] ≤ long[i-1]
short[i] > long[i]  → BUY

SELL → SHORT

Fires when the short EMA crosses below the long EMA at the close of a candle.

If a LONG is open, it is closed first. Then the new SHORT opens at the same price.

short[i-1] ≥ long[i-1]
short[i] < long[i]  → SELL

EMA Formula

Standard exponential · SMA seed

Standard EMA with an SMA seed for the first period candles. The smoothing factor k controls how fast it responds to new prices.

k = 2 / (period + 1)
seed = avg(closes[0..period])
EMA[i] = close[i] × k
       + EMA[i-1] × (1 − k)

Default periods and what they mean in practice:

9
Short EMA — fast-reacting, tracks recent price action closely
21
Long EMA — slow, smoothed trend line
Risk Management
How each trade is sized and when it exits.

Position Sizing

Each trade uses 95% of available balance as margin. The remaining 5% stays liquid.

margin = balance × 0.95
size = (margin × leverage) / entry

Leverage is baked into size, so PnL is simply Δprice × size — no second multiply.

Stop Loss & Take Profit

SL and TP are calculated as a percentage of entry at open time. Checked on every live WebSocket tick to catch intra-candle wicks.

SL LONG = entry × (1 − sl%)
TP LONG = entry × (1 + tp%)
SL SHORT = entry × (1 + sl%)
TP SHORT = entry × (1 − tp%)

PnL & Wallet Update

On every close
Leveraged PnL
LONG
pnl = (close − entry) × size

SHORT
pnl = (entry − close) × size
Wallet after close
balance += margin + pnl

// floor at zero — can't go negative
balance = max(0, balance)

Example: $1,000 balance, 3× leverage, entry $64,000 → margin $950, size ≈ 0.0445 BTC. Price +2%: PnL ≈ +$57 (+6% of margin).

Illustrative Equity Curve — 6M · EMA 9/21 · 3× Leverage

+34.2%
$1,000 $1,342

Backtest Candle Intervals

Auto-selected per date range to balance data resolution and Binance API limits.

PresetRange (days)IntervalCandles (approx)
1M, 3M, 6M≤ 90 days1h~720
1Y91 – 365 days4h~720
2Y, 5Y> 365 days1dup to 1826