For strategies in JavaScript
, Python
, and C++
languages, the following entry functions have been defined for the FMZ Quant Trading Platform.
Function Name | Description |
---|---|
main() |
The entry function, it’s the main function of the strategy. |
onexit() |
It is a cleanup function when exiting normally, its maximum execution time is 5 minutes, which can be left undeclared; if the timeout occurs, an interrupt error will be reported. If the onerror() function is triggered first during live trading, the onexit() function will not be triggered again. |
onerror() |
It is an abnormal exit function, its maximum execution time is 5 minutes, which can be left undeclared. The strategies written in Python and C++ don’t support this function, and the function is not supported by the backtesting system. |
init() |
It is an initialization function, its strategy program will be called automatically when it starts running, which can be left undeclared. |
onexit()
, processing clean-up work, with a maximum execution time of 5 minutes, which is realized by the user.
function main(){
Log("Start running, stop after 5 seconds, and execute onexit function!")
Sleep(1000 * 5)
}
// onexit function implementation
function onexit(){
var beginTime = new Date().getTime()
while(true){
var nowTime = new Date().getTime()
Log("The program stops counting down..The cleaning starts and has passed:", (nowTime - beginTime) / 1000, "Seconds!")
Sleep(1000)
}
}
import time
def main():
Log("Start running, stop after 5 seconds, and execute onexit function!")
Sleep(1000 * 5)
def onexit():
beginTime = time.time() * 1000
while True:
ts = time.time() * 1000
Log("The program stops counting down.The cleaning starts and has passed:", (nowTime - beginTime) / 1000, "Seconds!")
Sleep(1000)
void main() {
Log("Start running, stop after 5 seconds, and execute onexit function!");
Sleep(1000 * 5);
}
void onexit() {
auto beginTime = Unix() * 1000;
while(true) {
auto ts = Unix() * 1000;
Log("The program stops counting down.The cleaning starts and has passed:", (nowTime - beginTime) / 1000, "Seconds!");
Sleep(1000);
}
}
Test the onexit()
function:
function main() {
if (exchange.GetName().startsWith("Futures_")) {
Log("The exchange is futures")
exchange.SetContractType("swap")
} else {
Log("The 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("Execute the sweep function")
}
def main():
if exchange.GetName().startswith("Futures_"):
Log("The exchange is futures")
else:
Log("The 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("Execute the sweep function")
#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("The exchange is futures");
exchange.SetContractType("swap");
} else {
Log("The exchange is spot");
}
if (IsVirtual()) {
try {
onTick();
} catch (...) {
std::cerr << "Caught unknown exception" << std::endl;
}
} else {
onTick();
}
}
void onexit() {
Log("Execute the sweep function");
}
Since the strategy in the backtesting system is usually designed as an endless loop, the onexit()
function implemented in the execution strategy cannot be triggered in the backtesting system. The onexit()
function can be triggered by detecting the end mark of the backtesting system (EOF exception).
The user implements the initialization function init()
, which will automatically execute the function init()
at the beginning of the strategy to complete the initialization task.
function main(){
Log("The first line of the code executed in the program!", "#FF0000")
Log("Exit!")
}
// Initialization the function
function init(){
Log("Initialization!")
}
def main():
Log("The first line of the code executed in the program!", "#FF0000")
Log("Exit!")
def init():
Log("Initialization!")
void main() {
Log("The first line of the code executed in the program!", "#FF0000");
Log("Exit!");
}
void init() {
Log("Initialization!");
}
The execution of function onerror()
will be triggered when an exception occurs. This function does not support strategies written inPython
and C++
. The onerror()
function can take a msg
parameter, which is the error message that is reported when the exception is triggered.
function main() {
var arr = []
Log(arr[6].Close) // A program exception is intentionally raised here.
}
function onerror(msg) {
Log("error:", msg)
}
# not supported by python
// not supported by C++
Backtest System
Strategy Framework and API Functions