Strategy Entry Functions
For strategies written in JavaScript, Python, and C++ languages, the FMZ Quant Trading Platform has defined the following entry functions.
| Function Name | Description |
|---|---|
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()
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:javascriptfunction 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) } }pythonimport 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 theonexit()function can be triggered by detecting the backtesting system's end marker (EOF exception).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("Executing cleanup function") }pythondef 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()
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()
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