Type/to search
Welcome to FMZ Quant Trading Platform
Programming Languages
JavaScript
TypeScript
Python
C++
MyLanguage
PINE Language
Blockly Visual Programming
Workflow
Key Security
Live Trading
Strategy Library
Docker
Deploy Docker
One-Click Docker Rental
Manual Deployment of Bot
Docker Operation Precautions
Global IP Address Specification
Command Line Parameters for Bot Program
Live Trading Data Migration
Docker Monitor
Exchange
Strategy Editor
Backtesting System
Strategy Entry Functions
Strategy Framework and API Functions
Template Library
Strategy Parameters
Interactive Controls
Options Trading
C++ Strategy Writing Guide
JavaScript Strategy Writing Guide
Web3
Built-in Libraries
Extended API Interface
MCP Service
Trading Terminal
Data Explorer
Alpha Factor Analysis Tool
General Protocol
Debugging Tool
Remote Editing
Import and Export of Complete Strategies
Multi-language Support
Live Trading and Strategy Grouping
Live Trading Display
Strategy Sharing and Renting
Live Trading Message Push
Common Causes of Live Trading Errors and Abnormal Exits
Exchange-Specific Notes

Strategy Entry Functions

For strategies written in JavaScript, Python, and C++ languages, the FMZ Quant Trading Platform has defined the following entry functions.

Function NameDescription
main()Entry function, the main function of the strategy.
onexit()Cleanup function executed upon normal exit, with a maximum execution time of 5 minutes. Declaration is optional. If timeout occurs, an interrupt error will be reported. In live trading, if the onerror() function is triggered first, the onexit() function will not be triggered.
onerror()Function triggered upon abnormal exit, with a maximum execution time of 5 minutes. Declaration is optional. Strategies written in Python and C++ languages do not support this function, and the backtesting system also does not support this function.
init()Initialization function, automatically called first when the strategy program starts running. Declaration is optional.

Notes:

  • When the main() function finishes execution, all created sub-threads will be automatically terminated.

onexit() function is used to handle cleanup operations when the strategy exits, with a maximum execution time of 5 minutes, and needs to be implemented by the user.

Examples

  • Test the onexit() function:

    javascript
    function main(){ Log("Starting, will stop after 5 seconds and execute cleanup function!") Sleep(1000 * 5) } // Cleanup function implementation function onexit(){ var beginTime = new Date().getTime() while(true){ var nowTime = new Date().getTime() Log("Program stop countdown..cleanup started, elapsed time:", (nowTime - beginTime) / 1000, "seconds!") Sleep(1000) } }
    python
    import time def main(): Log("Starting, will stop after 5 seconds and execute cleanup function!") Sleep(1000 * 5) def onexit(): beginTime = time.time() * 1000 while True: ts = time.time() * 1000 Log("Program stop countdown..cleanup started, elapsed time:", (ts - beginTime) / 1000, "seconds!") Sleep(1000)
    c++
    void main() { Log("Starting, will stop after 5 seconds and execute cleanup function!"); Sleep(1000 * 5); } void onexit() { auto beginTime = Unix() * 1000; while(true) { auto ts = Unix() * 1000; Log("Program stop countdown..cleanup started, elapsed time:", (ts - beginTime) / 1000, "seconds!"); Sleep(1000); } }
  • Since strategies in the backtesting system are typically designed to run in an infinite loop with continuous polling execution, the backtesting system cannot trigger the onexit() function implemented in the strategy. The execution of the onexit() function can be triggered by detecting the backtesting system's end marker (EOF exception).

    javascript
    function 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("Executing cleanup function") }
    python
    def main(): if exchange.GetName().startswith("Futures_"): Log("Exchange is futures") 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("Executing cleanup function")
    c++
    #include <iostream> #include <exception> #include <string> void onTick() { while (true) { auto ticker = exchange.GetTicker(); LogStatus(_D(), ticker); Sleep(500); } } void main() { std::string prefix = "Futures_"; bool startsWith = exchange.GetName().substr(0, prefix.length()) == prefix; if (startsWith) { Log("Exchange is futures"); exchange.SetContractType("swap"); } else { Log("Exchange is spot"); } if (IsVirtual()) { try { onTick(); } catch (...) { std::cerr << "Caught unknown exception" << std::endl; } } else { onTick(); } } void onexit() { Log("Executing cleanup function"); }

init(), user-implemented initialization function init(), which will be automatically executed first when the strategy starts running, used to complete initialization tasks designed in the strategy.

Examples

javascript
function main(){ Log("First line of code executed!", "#FF0000") Log("Exiting!") } // Initialization function function init(){ Log("Initializing!") }
python
def main(): Log("First line of code executed!", "#FF0000") Log("Exiting!") def init(): Log("Initializing!")
c++
void main() { Log("First line of code executed!", "#FF0000"); Log("Exiting!"); } void init() { Log("Initializing!"); }

onerror(), triggered when an exception occurs, the onerror() function will be executed. This function is not supported in Python and C++ language strategies. The onerror() function can accept a msg parameter, which contains the error message when the exception is triggered.

Examples

javascript
function main() { var arr = [] Log(arr[6].Close) // Intentionally trigger a program exception here } function onerror(msg) { Log("Error:", msg) }
python
# Python not supported
c++
// C++ not supported