Skip to content

Session DCA ​

Scheduled buys sized to the pool's capacity.

Kind strategy · Resolution 1h

Buys in small pieces during chosen UTC hours, sized to what the pool can absorb, while buyers dominate.

Buying. On each closed bar inside the session (From hour to To hour, UTC) it buys the smallest of:

  • TAO per buy;
  • Max share of capacity % of pool.capacity(Max impact %);
  • the remaining cash;
  • the room left under Max position TAO.

It only buys on bars where on-balance volume rose (ta.rising(ta.obv(), 1)).

Selling. A trailing stop, placed with trade.trailingStop in onPositionOpened, sells the position after it falls Trailing stop % from its high.

Output. Each buy raises an alert with its size and price; onStop logs the realized and unrealized profit.

Try it ​

On the chart, press /, open Strategies and choose Session DCA. Or copy the source below into the AlphaScript editor and click Add to chart.

Inputs ​

InputTypeDefault
From hour (UTC)int6
To hour (UTC)int14
TAO per buyfloat4
Max impact %float0.15
Max share of capacity %float25
Trailing stop %float6
Max position TAOfloat60

Source ​

From packages/alphascript/examples/session_dca_capacity.as.js.

js
strategy({ name: "Session DCA", version: "0.1", market: "ALPHA:61", resolution: "1h",
           capital: { tao: 100 }, actOn: "confirmed", risk: { maxPriceImpactPct: 0.3, minPoolTao: 300 } });

const fromHour = input.int("From hour (UTC)", 6, { min: 0, max: 23 });
const toHour   = input.int("To hour (UTC)", 14, { min: 0, max: 23 });
const chunk    = input.float("TAO per buy", 4, { min: 0.1, max: 100, step: 0.5 });
const maxImp   = input.float("Max impact %", 0.15, { min: 0.01, max: 2, step: 0.01 });
const share    = input.float("Max share of capacity %", 25, { min: 1, max: 100 });
const trailPct = input.float("Trailing stop %", 6, { min: 0.5, max: 30, step: 0.5 });
const budget   = input.float("Max position TAO", 60, { min: 1, max: 1000 });

const obv = ta.obv();
const flowUp = ta.rising(obv, 1);
plot(block.taoIn, { title: "TAO in pool", color: "#2bb889", style: "area", pane: 1 });

const hourOf = (ms = 0) => math.floor((ms / 3600000) % 24);
const inSession = (h = 0) => (fromHour <= toHour ? h >= fromHour && h < toHour : h >= fromHour || h < toHour);

event.onCandle((bar) => {
  const pos = account.position();
  if (!inSession(hourOf(bar.time))) return;
  if (pos.costTao >= budget) return;
  if (!flowUp.value) { log("skip: sellers dominated the bar"); return; }
  const cap = pool.capacity(maxImp / 100) * share / 100;
  const tao = math.min(chunk, cap, account.cash(), budget - pos.costTao);
  if (tao < 0.1) { log("skip: capacity", cap.toFixed(2), "τ"); return; }
  trade.buy({ tao, maxPrice: pool.price() * (1 + maxImp / 100), ttlBlocks: 5, partialOk: false, reason: "dca" });
});

event.onPositionOpened(() => trade.trailingStop({ pct: trailPct, reason: "trail" }));
event.onFill((f) => { if (f.side === "buy") alert("dca-fill", `${f.tao.toFixed(2)} τ at ${f.price.toFixed(7)}`); });
event.onStop(() => { const p = account.pnl(); log("realized", p.realizedTao.toFixed(4), "unrealized", p.unrealizedTao.toFixed(4)); });

AlphaScript language v0.1 · runtime 0.2.0