```javascript
function main(){
Log("开始运行, 5秒后停止,并执行扫尾函数!")
Sleep(1000 * 5)
}
// 扫尾函数实现
function onexit(){
var beginTime = new Date().getTime()
while(true){
var nowTime = new Date().getTime()
Log("程序停止倒计时..扫尾开始,已经过去:", (nowTime - beginTime) / 1000, "秒!")
Sleep(1000)
}
}
import time
def main():
Log("开始运行, 5秒后停止,并执行扫尾函数!")
Sleep(1000 * 5)
def onexit():
beginTime = time.time() * 1000
while True:
ts = time.time() * 1000
Log("程序停止倒计时..扫尾开始,已经过去:", (ts - beginTime) / 1000, "秒!")
Sleep(1000)
void main() {
Log("开始运行, 5秒后停止,并执行扫尾函数!");
Sleep(1000 * 5);
}
void onexit() {
auto beginTime = Unix() * 1000;
while(true) {
auto ts = Unix() * 1000;
Log("程序停止倒计时..扫尾开始,已经过去:", (ts - beginTime) / 1000, "秒!");
Sleep(1000);
}
}
测试onexit()
函数:
function main() {
if (exchange.GetName().startsWith("Futures_")) {
Log("交易所为期货")
exchange.SetContractType("swap")
} else {
Log("交易所为现货")
}
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("执行扫尾函数")
}
def main():
if exchange.GetName().startswith("Futures_"):
Log("交易所为期货")
else:
Log("交易所为现货")
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("执行扫尾函数")
#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.SetContractType("swap");
} else {
Log("交易所为现货");
}
if (IsVirtual()) {
try {
onTick();
} catch (...) {
std::cerr << "Caught unknown exception" << std::endl;
}
} else {
onTick();
}
}
void onexit() {
Log("执行扫尾函数");
}
由于回测系统中策略通常设计为一个死循环不停轮询执行,会导致在回测系统中无法触发执行策略中实现的onexit()
函数。可以通过检测回测系统结束标记(EOF异常)来触发onexit()
函数执行。