onexit()
Optional clean-up function, called when the strategy exits to cancel orders, close positions, save state and so on. It may run for at most 5 minutes and is terminated when it exceeds that. For when each language calls onexit(), see Strategy Structure.
In a backtest the strategy is usually an endless polling loop, so main() has not returned normally when the backtest data ends, and JavaScript and Python strategies therefore do not run onexit(). In a backtest (IsVirtual() is true) you can catch the exception (EOF) thrown when the backtest ends so that main() returns and onexit() runs; see the second example. Rust strategies need nothing special: when the backtest ends the SDK runs onexit() automatically and then stops the strategy.
Examples
-
main()returns after 5 seconds;onexit()logs the elapsed time every second until it is terminated after 5 minutes.javascriptfunction main(){ Log("starting; stops after 5 seconds and runs the clean-up function") Sleep(1000 * 5) } // clean-up function function onexit(){ var beginTime = new Date().getTime() while(true){ var nowTime = new Date().getTime() Log("cleaning up, elapsed", (nowTime - beginTime) / 1000, "seconds") Sleep(1000) } }pythonimport time def main(): Log("starting; stops after 5 seconds and runs the clean-up function") Sleep(1000 * 5) # clean-up function def onexit(): beginTime = time.time() * 1000 while True: ts = time.time() * 1000 Log("cleaning up, elapsed", (ts - beginTime) / 1000, "seconds") Sleep(1000)rustfn main() { Log!("starting; stops after 5 seconds and runs the clean-up function"); Sleep(1000 * 5); } // clean-up function fn onexit() { let beginTime = Unix() * 1000; loop { let nowTime = Unix() * 1000; Log!("cleaning up, elapsed", (nowTime - beginTime) / 1000, "seconds"); Sleep(1000); } } -
Catch the exception at the end of a backtest so that
main()returns andonexit()runs.javascriptfunction main() { if (exchange.GetName().startsWith("Futures_")) { Log("exchange is futures") exchange.SetContractType("swap") } else { Log("exchange is spot") } if (IsVirtual()) { try { onTick() } catch (e) { Log("error:", e) } } else { onTick() } } function onTick() { while (true) { var ticker = exchange.GetTicker() LogStatus(_D(), ticker ? ticker.Last : "--") Sleep(500) } } function onexit() { Log("running the clean-up function") }pythondef main(): if exchange.GetName().startswith("Futures_"): Log("exchange is futures") exchange.SetContractType("swap") else: Log("exchange is spot") if IsVirtual(): try: onTick() except Exception as e: Log(e) else: onTick() def onTick(): while True: ticker = exchange.GetTicker() LogStatus(_D(), ticker["Last"] if ticker else "--") Sleep(500) def onexit(): Log("running the clean-up function")rustfn onTick() { loop { // at the end of a backtest the SDK runs onexit() first; nothing to handle here match exchange.GetTicker(None) { Ok(ticker) => LogStatus!(_D(None), ticker.Last), Err(e) => Log!("error:", e), } Sleep(500); } } fn main() { if exchange.GetName().starts_with("Futures_") { Log!("exchange is futures"); let _ = exchange.SetContractType("swap"); } else { Log!("exchange is spot"); } onTick(); } fn onexit() { Log!("running the clean-up function"); }