策略入口函数
对于JavaScript、Python、C++语言的策略,发明者量化交易平台已定义以下入口函数。
| 函数名 | 说明 |
|---|---|
main() | 入口函数,策略的主函数。 |
onexit() | 正常退出时的清理函数,最长执行时间为5分钟,可以不声明。如果超时会报interrupt错误。在实盘中,如果先触发了onerror()函数,则不会再触发onexit()函数。 |
onerror() | 异常退出时触发执行的函数,最长执行时间为5分钟,可以不声明。Python语言、C++语言编写的策略不支持该函数,回测系统也不支持该函数。 |
init() | 初始化函数,策略程序在开始运行时会首先自动调用,可以不声明。 |
注意事项:
- 当
main()函数执行结束时,所有创建的子线程会被自动终止。
onexit()
onexit()函数用于处理策略退出时的清理工作,最长执行时间为5分钟,需由用户自行实现。
示例
-
测试
onexit()函数:javascriptfunction main(){ Log("Starting, will stop after 5 seconds and execute cleanup function!") Sleep(1000 * 5) } // 扫尾函数实现 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); } } -
由于回测系统中策略通常设计为无限循环持续轮询执行,导致回测系统无法触发策略中实现的
onexit()函数。可以通过检测回测系统结束标记(EOF异常)来触发onexit()函数的执行。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(),用户实现初始化函数init(),策略开始运行时会首先自动执行init()函数,用于完成策略中设计的初始化任务。
示例
javascript
function main(){
Log("First line of code executed!", "#FF0000")
Log("Exiting!")
}
// 初始化函数
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!");
}