Appearance
EMA cross on alpha
Buys a fast/slow EMA cross up while the pool is deep enough, exits on the cross down.
Kind strategy · Resolution 15m
A trend-following starting point: buy when a fast EMA crosses above a slow one, sell when it crosses back.
Entry. On a closed bar where the fast EMA crosses above the slow EMA, the strategy buys Size % of equity, but only if pool.impact(size) says the trade stays under 0.25 % impact (half the 0.5 % risk limit, so the exit fits too), and with a maxPrice 1 % above the current price.
Exit. On the cross back down it closes the position. Independently, onBlock watches every block and closes the position if the price falls Stop loss % below the entry. That stop only runs where blocks are delivered: in a precise or 1B backtest and on the live stream. In a quick backtest only the EMA exit applies.
What to try. Lengthen both EMAs on short intervals to cut whipsaw trades, and compare the quick and precise results to see how much the block-level stop changes.
Try it
On the chart, press /, open Strategies and choose EMA cross on alpha. Or copy the source below into the AlphaScript editor and click Add to chart.
Inputs
| Input | Type | Default |
|---|---|---|
| Fast EMA | int | 9 |
| Slow EMA | int | 21 |
| Stop loss % | float | 5 |
| Size % of equity | float | 25 |
Source
From packages/alphascript/examples/ema_cross.as.js.
js
strategy({
name: "EMA cross on alpha",
version: "0.1",
market: "ALPHA:61",
resolution: "15m",
capital: { tao: 100 },
actOn: "confirmed",
fill: { mode: "quick", latencyBlocks: 2 },
risk: { maxPriceImpactPct: 0.5, maxPositionTao: 50, maxIntentsPerHour: 20 },
});
const fastLen = input.int("Fast EMA", 9, { min: 2, max: 200 });
const slowLen = input.int("Slow EMA", 21, { min: 2, max: 400 });
const stopPct = input.float("Stop loss %", 5, { min: 0.5, max: 50, step: 0.5 });
const sizePct = input.float("Size % of equity", 25, { min: 1, max: 100 });
const fast = ta.ema(candle.close, fastLen);
const slow = ta.ema(candle.close, slowLen);
const goLong = ta.crossover(fast, slow);
const goFlat = ta.crossunder(fast, slow);
plot(fast, { title: "EMA fast", color: "#2bb889" });
plot(slow, { title: "EMA slow", color: "#f2b544" });
plotShape(goLong, { shape: "triangleUp", location: "belowBar", color: "#2bb889", text: "L" });
event.onCandle((bar) => {
const size = account.equity() * (sizePct / 100);
if (goLong.value && pool.impact(size) * 100 < 0.25) {
trade.buy({ tao: size, maxPrice: pool.price() * 1.01, ttlBlocks: 3, reason: "ema-cross" });
log("long signal", bar.close, "impact", pool.impact(size));
}
if (goFlat.value && account.position().alpha > 0) trade.closePosition({ reason: "ema-cross-down" });
});
event.onBlock((b) => {
const pos = account.position();
if (pos.alpha > 0 && b.price < pos.avgPrice * (1 - stopPct / 100)) trade.closePosition({ reason: "stop" });
});
event.onFill((f) => log(`filled ${f.side} ${f.alpha.toFixed(3)} α at ${f.price} (${f.impactBps} bps)`));
event.onReject((r) => log("rejected", r.intent.id, r.reason));