Strategy Structure
Strategies in JavaScript, Python and Rust consist of a few functions with agreed names, which the docker calls at fixed points. MyLanguage, PINE and Workflow strategies do not need to define them.
Lifecycle functions
| Function | Required | When it is called |
|---|---|---|
main() | Yes | The entry function and body of the strategy. When main() returns, the strategy has finished. |
init() | No | Called once before main(), for initialization. |
onexit() | No | Called when the strategy exits, for cleanup (cancel orders, close positions, save state, etc.). |
onerror(msg) | No | JavaScript only: called when main() ends with an uncaught exception (msg is the error); then onexit() is not called. |
destroy() | No | JavaScript template libraries only: called when the strategy exits, after onexit() or onerror(); see Template Library. |
Which function runs on exit:
| Exit reason | JavaScript | Python | Rust |
|---|---|---|---|
main() returns normally | onexit() | onexit() | onexit() |
| Live trading stopped | onexit() | onexit() | onexit() |
Uncaught exception or panic | onerror(msg) | neither | onexit() |
Notes
onexit()andonerror()may run for at most 5 minutes (the limit is sent by the server with each task; the default is 5 minutes) and are terminated when they exceed it.- In backtesting a strategy is usually an endless polling loop, so
main()does not return normally when the backtest ends andJavaScriptandPythonstrategies do not runonexit(); seeonexit()for how to handle this.Ruststrategies runonexit()automatically when the backtest ends. - When
main()of aJavaScriptstrategy returns, threads created withthreadingare terminated;setTimeoutcallbacks that have not fired yet run first, thenonexit()is called. JavaScriptandPythontemplate libraries can define their owninit(), which runs when the template is loaded, before the strategy'sinit().
Main loop and event-driven strategies
Most strategies run a loop in main(): each round fetches data, computes, places orders, then calls Sleep() to wait for the next round (see Main Loop). A strategy can also wait for market data, order updates and other events and handle them as they arrive (see Event-driven handling). For a categorized list of all API functions see API Quick Reference.
In this sectionRead the whole section
| Name | Description |
| init() | Optional initialization function, called automatically once when the strategy starts, before main(), to do initialization work. |
| onexit() | Optional clean-up function, called when the strategy exits to cancel orders, close positions, save state and so on. |
| onerror() | Optional exception handler, JavaScript only: called when main() ends with an uncaught exception; the argument msg is the error message. |
| Main Loop | A strategy usually runs a loop in main(): each round fetches market data, computes signals, places orders, then calls Sleep to wait for the next round. |
| Event-Driven | In addition to polling at fixed intervals, strategies can wait for events before processing data, reducing unnecessary requests and responding more quickly to m… |