输入/搜索内容
欢迎使用发明者量化交易平台
编程语言
JavaScript
TypeScript
Python
Rust
C++
My语言(麦语言)
PINE语言
Blockly可视化
Workflow工作流
密钥安全性
实盘
策略库
托管者
部署托管者
一键租用托管者
手动部署托管者
托管者操作注意事项
全局指定IP地址
命令行版本托管者程序的参数
实盘数据迁移
托管者监控
交易所
策略编辑器
回测系统
策略入口函数
策略框架与API函数
模板类库
策略参数
交互控件
期权交易
Rust策略编写说明
C++策略编写说明
JavaScript策略编写说明
Web3
内置库
扩展API接口
MCP 服务
交易终端
数据探索
Alpha因子分析工具
通用协议
调试工具
远程编辑
完整策略的导入与导出
多语言支持
实盘、策略分组
实盘展示
策略分享与出租
实盘消息推送
实盘报错、异常退出的常见原因
交易所特殊说明

对于JavaScriptPythonRustC++语言的策略,发明者量化交易平台已经定义了以下入口函数。

函数名说明
main()入口函数,即策略的主函数。
onexit()正常退出时执行的收尾函数,最长执行时间为5分钟,可以不声明;如果执行超时,将报出interrupt错误。在实盘中,若已先触发onerror()函数,则不会再触发onexit()函数。
onerror()异常退出时触发执行的函数,最长执行时间为5分钟,可以不声明。Python语言、C++语言编写的策略不支持该函数,回测系统也不支持该函数。
init()初始化函数,策略程序在开始运行时会首先自动调用该函数,可以不声明。

注意事项:

  • main()函数执行结束时,所有已创建的子线程都会被自动终止。

  • Rust语言策略中,直接定义fn main()fn init()fn onexit()即可(由引导层自动调用);也可以在策略代码中调用OnExit()注册额外的退出钩子,详见「Rust策略编写说明」。

onexit()函数用于处理策略的扫尾工作,最长执行时间为5分钟,需由用户自行实现。

示例

  • 测试onexit()函数:

    javascript
    function 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) } }
    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)
    rust
    fn main() { Log!("Starting, will stop after 5 seconds and execute cleanup function!"); Sleep(1000 * 5); } // 扫尾函数的实现 fn onexit() { let beginTime = Unix() * 1000; loop { let nowTime = Unix() * 1000; Log!("Program stop countdown..cleanup started, elapsed time:", (nowTime - 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()函数的执行。

    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")
    rust
    fn onTick() { loop { match exchange.GetTicker(None) { Ok(ticker) => LogStatus!(_D(None), ticker.Last), Err(e) => { // 回测结束时,API 调用返回 Err,退出循环使 main 返回,从而触发 onexit() 扫尾函数 Log!("error:", e); break; } } Sleep(500); } } fn main() { if exchange.GetName().starts_with("Futures_") { Log!("Exchange is futures"); let _ = exchange.SetContractType("swap"); } else { Log!("Exchange is spot"); } onTick(); } fn 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() 函数,以完成策略中设计的初始化任务。

示例

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!")
rust
fn main() { Log!("First line of code executed!", "#FF0000"); Log!("Exiting!"); } // 初始化函数 fn init() { Log!("Initializing!"); }
c++
void main() { Log("First line of code executed!", "#FF0000"); Log("Exiting!"); } void init() { Log("Initializing!"); }

onerror(),当发生异常时会触发onerror()函数执行,该函数不支持PythonC++语言的策略。onerror()函数可以接收一个msg参数,该msg参数为异常触发时的错误信息。

示例

javascript
function main() { var arr = [] Log(arr[6].Close) // 这里故意引发一个程序异常 } function onerror(msg) { Log("Error:", msg) }
python
# python不支持
c++
// C++不支持