What wakes a module
Ticks, closed bars, schedules, signals, orders, fills and news: the decorators that call your code.
@on.tick("EQ:US:AAPL") # every tick, uncoalesced
@on.bar("EQ:US:AAPL", "5m") # each closed bar of that interval (hours="eth": the full session)
@on.bar(aapl) # the same, for a Bars line the class declares (see "Bars")
@on.schedule(every="10m") # a cadence — no market data needed
@on.signal("rebalance") # another module's while25.emit()
@on.signal("dip_ok", sender=DipWatch) # that signal from that one module only
@on.order(tag="mine") # order lifecycle events tagged "mine" (account-wide — see below)
@on.fill(tag="mine") # the same stream, narrowed to executions
@on.news("EQ:US:AAPL") # each live news article on that symbol (needs a news vendor)A symbol known only once the module runs — one picked in its settings, or an
option while25.option() named — cannot go in a decorator: the decorators are
read from the file when it is saved, before any setting has a value. Turn its
prices on while running and name the method its ticks call:
def on_start(self, ctx):
self.coin = while25.params["coin"] # a Symbol setting
while25.subscribe(self.coin, on_tick=self.on_coin)
def on_coin(self, ctx, tick): # every tick of that coin
...Each tick of that symbol then calls on_coin(ctx, tick), exactly as an
@on.tick line for it would. See "Live market data" below for the rules. Bars
have no such call: @on.bar takes a symbol written in the file. Where the
choice is a short list known in advance (SPY or QQQ), write a line for
each and read the setting inside the handler.
What each handler receives. @on.tick: (ctx, tick), the tick dict
above. @on.bar: (ctx, bar) with symbol, interval, hours ("" for
the regular session, "eth" for the full one), time_ms (the bar's start),
open, high, low, close and volume — closed bars only, never the one
still forming. @on.schedule: (ctx). @on.signal: (ctx, payload).
@on.order / @on.fill: (ctx, ev), the event dict under Orders.
@on.news: (ctx, article), a row as news() returns them.
@on.bar(symbol, interval, hours=None) takes the chart's intervals and the
session in an order's word: left out, the venue's regular session (futures
included); "eth", the full session. "rth" is refused. A regular and an
"eth" line on one symbol are two series, each delivered to its own
handler. An interval or session While25 cannot serve for that
instrument is refused BAD_INTERVAL / BAD_HOURS as a warning on the
module, which runs on with the rest.
@on.schedule(every=...) takes "250ms", "5s", "10m", "2h". The check
runs once a second, so nothing fires finer than that; a handler held up past
a period (a long history_span()) runs once and resumes its cadence, not
once per missed period. cron= and tz= are not implemented: a schedule
with no every= is refused at registration, like @on.position.
@on.order and @on.fill take tag= and symbol=, each an exact match;
left out, every event on the account. Write the parentheses even with no
filter: @on.order() and @on.fill(). A bare @on.order raises TypeError
when the file loads. @on.signal(name, sender=None) wakes
on while25.emit(name, payload) from another running module. sender names
the one module the handler listens to, by its class or its id
(sender=DipWatch, sender="dip_watch"): that signal from any other module
does not reach it. Which signals reach a module that trades at all is under
while25.emit in "Start and stop".
Not deliverable: @on.position. Nothing publishes position changes to
this runtime, so a module declaring one is refused at registration with a
message saying so. That is deliberate: it used to register, report running,
and never fire, which is indistinguishable from a strategy whose logic is
wrong. (Bar, order and fill delivery went the same way once: declarable before
they were deliverable. They are deliverable now; the refusal is the rule that
keeps a registered module one that will actually wake.)
A module may declare several, or none. A module with a run() method has it
called once, after on_start, on the module's one executor thread — the
escape hatch for anything the declarative surface does not cover. Nothing
else of the module runs while run() runs (no handler, no schedule), so a
run() that loops checks while25.alive() each pass and returns when it is
False: that is the moment the platform asked the module to stop, and a loop
that goes on is ended with the process two seconds later. A module with
neither a subscription nor run() is refused at registration: nothing could
ever wake it.
A class built on another. A module may subclass one of your own, and a method the child writes again replaces the parent's:
class Fast(MeanReversion):
id = "mean_reversion_fast"
@on.tick("EQ:US:AAPL") # tagged again: this tag is the one that counts
def on_tick(self, ctx, tick):
...- Tagged again, the child's tag replaces the parent's for that method, so it is called once per event, on what the child's tag says.
- Written again with no tag, it keeps the parent's tag and is the method that runs.
- A method the child does not write is inherited as it is. So are
inputs,monitorsandBarslines; a child that declares aBarsline again under the same name moves the handlers on that line with it.