Getting Started
Platform Basics
Writing Strategies
Development Tools
Backtesting System
Advanced Topics
Data and Research
Integrations
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. In backtesting Sleep() advances backtest time and controls the replay speed; in live trading it controls the polling interval and therefore how often the exchange API is called. A loop without Sleep() calls the exchange API as fast as it can and easily hits the exchange's rate limits. To limit the API call rate on the docker, see Advanced Topics → API Rate Limit Control.
Examples
-
Basic framework:
javascriptfunction onTick(){ // strategy logic goes here and is called repeatedly, e.g. print market data Log(exchange.GetTicker()) } function main(){ while(true){ onTick() // Sleep controls the polling frequency so the exchange API is not called too often Sleep(60000) } }pythondef onTick(): Log(exchange.GetTicker()) def main(): while True: onTick() Sleep(60000)rustfn onTick() { // strategy logic goes here and is called repeatedly, e.g. print market data Log!(exchange.GetTicker(None)); } fn main() { loop { onTick(); // Sleep controls the polling frequency so the exchange API is not called too often Sleep(60000); } } -
The simplest example: place a buy order at price 100 for amount 1 every second:
javascriptfunction onTick(){ // only an example: it quickly spends all funds on orders, do not run it live exchange.Buy(100, 1) } function main(){ while(true){ onTick() // the pause is in milliseconds; 1 second = 1000 milliseconds Sleep(1000) } }pythondef onTick(): exchange.Buy(100, 1) def main(): while True: onTick() Sleep(1000)rustfn onTick() { // only an example: it quickly spends all funds on orders, do not run it live let _ = exchange.Buy(100, 1); } fn main() { loop { onTick(); // the pause is in milliseconds; 1 second = 1000 milliseconds Sleep(1000); } } -
A strategy that acts on K-line updates (On Bar):
onTick()runs only when the time of the latest K-line changes:javascriptfunction onTick() { Log("K-line updated, new BAR generated") } function main() { var exName = exchange.GetName() if (exName.includes("Futures_")) { exchange.SetContractType("swap") } var lastTs = 0 while (true) { var r = _C(exchange.GetRecords) if (r.length > 0 && r[r.length - 1].Time != lastTs) { onTick() lastTs = r[r.length - 1].Time } Sleep(1000) } }pythondef onTick(): Log("K-line updated, new BAR generated") def main(): exName = exchange.GetName() if "Futures_" in exName: exchange.SetContractType("swap") lastTs = 0 while True: r = _C(exchange.GetRecords) if len(r) > 0 and r[-1]["Time"] != lastTs: onTick() lastTs = r[-1]["Time"] Sleep(1000)rustfn onTick() { Log!("K-line updated, new BAR generated"); } fn main() { let exName = exchange.GetName(); if exName.contains("Futures_") { let _ = exchange.SetContractType("swap"); } let mut lastTs = 0; loop { let r = _C!(exchange.GetRecords(None, None, None)); if r.len() > 0 && r[r.len() - 1].Time != lastTs { onTick(); lastTs = r[r.len() - 1].Time; } Sleep(1000); } }