Skip to Content
While25SDK docs
Writing modules

Start and stop

on_start, run() and on_stop: what happens when a module starts and when it is asked to stop.

on_start(ctx) runs first, on the module's own thread, before any handler: the module reads starting until it returns, so a long fetch there (a warmup(), see "Warm-up") delays nothing but the module's own first decision. A raise in it puts the module in error with the message, and nothing of it runs.

on_stop(ctx) runs once when the module is stopped: by its Stop, a stop on its panel, a restart of its code, or the app closing. A crash or a kill of the process runs none of it. From the moment the Stop arrives, while25.alive() reads False, handlers still waiting are skipped (the log says how many), and every order call — buy, sell, place, bracket, combo, cancel, modify, square_off — raises While25Error with code MODULE_STOPPING and is not to be retried: the trader's Stop decides what happens to the module's orders. on_stop may log, save its own files and read what the module holds (while25.portfolio.my_positions(), while25.orders.mine()); it has two seconds before the process is ended, finished or not, and a raise in it is logged, never a halt.

When the computer sleeps, nothing runs. No price or bar reaches a module, no order goes out, and While25's held stops cannot act: a stop whose level was crossed meanwhile fires late, on the first price after waking (a stop the broker holds goes on working). On waking the module goes on with the prices that come now; it is not stopped. Its log says how long the computer was asleep ("The computer was asleep from 10:02 to 11:15 (73 min): no prices or bars reached this strategy, ..."), the app shows the same notice, and the bars it missed are not delivered after the fact.

while25.emit("name", payload) publishes a signal. Delivery is fire-and-forget to running subscribers only: nothing queues, nothing replays, and a signal with no listener is a no-op rather than an error. A publisher must not need to know who is listening.

What reaches a module that trades (a Strategy, or an App that imports one) depends on who sent it. A module that does not trade hears every signal.

Sent byReaches a module that trades
a Strategy or a trading App you deployed from the appyes
a Monitoronly when that module was deployed with the monitor box ticked
a strategy's own deskits own strategy only (its buttons)
any other App that does not trade, an Indicatornever
anything else: a script started any other way than from the app, one that is not runningnever

A monitor a strategy lists as its own (monitors = [DipWatch]) signals that strategy and no other module that trades (see "A monitor inside a strategy"). A signal that is held back is said once in the sender's log. A strategy that acts on an indicator works it out itself, from its own bars (see "An indicator's numbers in a strategy"). A handler with sender= hears only the module it names, and only when the table lets that module through.