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

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 API Rate Limiting Control.

Examples

  • Basic framework: run onTick() once per round, then wait 60 seconds with Sleep().

    javascript
    function 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) } }
    python
    def onTick(): Log(exchange.GetTicker()) def main(): while True: onTick() Sleep(60000)
    rust
    fn 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); } }
  • Place a buy order at price 100 for amount 1 every second (a demo only; it quickly spends all funds on orders).

    javascript
    function 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) } }
    python
    def onTick(): exchange.Buy(100, 1) def main(): while True: onTick() Sleep(1000)
    rust
    fn 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); } }
  • Acting on K-line updates (On Bar): onTick() runs only when the time of the latest K-line changes.

    javascript
    function 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) } }
    python
    def 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)
    rust
    fn 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); } }