Type/to search
Getting Started
Platform Basics
Writing Strategies
Strategy Structure
init()
onexit()
onerror()
Main Loop
Event-Driven
Programming Languages
Strategy Parameters
Interactive Controls
Strategy Frontend
Template Library
Built-in Libraries
UI Text Localization
API Quick Reference
Development Tools
Backtesting System
Advanced Topics
Data and Research
Extended API Interface

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

FunctionRequiredWhen it is called
main()YesThe entry function and body of the strategy. When main() returns, the strategy has finished.
init()NoCalled once before main(), for initialization.
onexit()NoCalled when the strategy exits, for cleanup (cancel orders, close positions, save state, etc.).
onerror(msg)NoJavaScript only: called when main() ends with an uncaught exception (msg is the error); then onexit() is not called.
destroy()NoJavaScript template libraries only: called when the strategy exits, after onexit() or onerror(); see Template Library.

Which function runs on exit:

Exit reasonJavaScriptPythonRust
main() returns normallyonexit()onexit()onexit()
Live trading stoppedonexit()onexit()onexit()
Uncaught exception or paniconerror(msg)neitheronexit()

Notes

  • onexit() and onerror() 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 and JavaScript and Python strategies do not run onexit(); see onexit() for how to handle this. Rust strategies run onexit() automatically when the backtest ends.
  • When main() of a JavaScript strategy returns, threads created with threading are terminated; setTimeout callbacks that have not fired yet run first, then onexit() is called.
  • JavaScript and Python template libraries can define their own init(), which runs when the template is loaded, before the strategy's init().

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

NameDescription
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 LoopA 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-DrivenIn addition to polling at fixed intervals, strategies can wait for events before processing data, reducing unnecessary requests and responding more quickly to m…