Appearance
Block scalper
Block-level scalper for the 1B (bar per block) mode.
Kind strategy · Resolution 1B
A block-by-block dip buyer with a fixed take-profit and stop.
Needs the 1B fill model
All of its logic is in event.onBlock, which does not run in a quick backtest. Set Fill model to 1B in the strategy's Properties (Precise works too).
Entry. On a block whose price is Dip from high % below the highest price of the last Lookback blocks blocks, it buys up to half its equity, capped at pool.capacity(Max impact %). It waits Blocks between entries blocks before the next entry and never stacks entries.
Exit. Each buy fill places a take-profit Take profit % above and a stop Stop % below its fill price, in onFill.
Reorgs. lastEntryBlock is the script's own state, so onReorg resets it when the chain replaces the block it was set in: the pattern from Events and execution order.
Try it
On the chart, press /, open Strategies and choose Block scalper. Or copy the source below into the AlphaScript editor and click Add to chart.
Inputs
| Input | Type | Default |
|---|---|---|
| Lookback blocks | int | 25 |
| Dip from high % | float | 0.6 |
| Take profit % | float | 0.8 |
| Stop % | float | 1.5 |
| Max impact % | float | 0.25 |
| Blocks between entries | int | 10 |
Source
From packages/alphascript/examples/block_scalper_1b.as.js.
js
strategy({ name: "Block scalper", version: "0.1", market: "ALPHA:61", resolution: "1B",
capital: { tao: 100 }, fill: { mode: "precise", latencyBlocks: 2 },
risk: { maxPriceImpactPct: 0.3, maxIntentsPerHour: 20 } });
const look = input.int("Lookback blocks", 25, { min: 5, max: 300 });
const dipPct = input.float("Dip from high %", 0.6, { min: 0.1, max: 5, step: 0.1 });
const tpPct = input.float("Take profit %", 0.8, { min: 0.1, max: 10, step: 0.1 });
const slPct = input.float("Stop %", 1.5, { min: 0.1, max: 10, step: 0.1 });
const maxImp = input.float("Max impact %", 0.25, { min: 0.01, max: 2, step: 0.01 });
const cooldown = input.int("Blocks between entries", 10, { min: 0, max: 1000 });
const hi = ta.highest(block.price, look);
const lo = ta.lowest(block.price, look);
plot(hi, { title: "block high", color: "#5c6878" });
plot(lo, { title: "block low", color: "#5c6878" });
let lastEntryBlock = -1;
event.onBlock((b) => {
const pos = account.position();
if (pos.alpha > 0 || account.openIntents().length > 0) return;
if (b.number - lastEntryBlock < cooldown) return;
if (!(b.price < hi.value * (1 - dipPct / 100))) return;
const tao = math.min(account.equity() * 0.5, pool.capacity(maxImp / 100));
if (tao < 0.1) return;
trade.buy({ tao, maxPrice: b.price * (1 + maxImp / 100), ttlBlocks: 4, reason: "dip" });
lastEntryBlock = b.number;
});
event.onFill((f) => {
if (f.side !== "buy") return;
trade.takeProfit({ price: f.price * (1 + tpPct / 100), reason: "tp" });
trade.stop({ price: f.price * (1 - slPct / 100), reason: "sl" });
});
event.onReorg((r) => {
if (lastEntryBlock > r.toBlock) lastEntryBlock = -1;
log("reorg", r.fromBlock, "→", r.toBlock);
});