典型场景
空策略
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
subscribe("SHSE.600000", "tick");
return;
}
void on_tick(Tick *tick)
{
cout << "代码 " << tick->symbol << endl
<< "utc时间,精确到毫秒 " << tick->created_at << endl
<< "最新价 " << tick->price << endl
<< "开盘价 " << tick->open << endl
<< "最高价 " << tick->high << endl
<< "最低价 " << tick->low << endl
<< "成交总量 " << tick->cum_volume << endl
<< "成交总金额 / 最新成交额, 累计值 " << tick->cum_amount << endl
<< "合约持仓量(期), 累计值 " << tick->cum_position << endl
<< "瞬时成交额 " << tick->last_amount << endl
<< "瞬时成交量 " << tick->last_volume << endl
<< "保留)交易类型, 对应多开, 多平等类型 " << tick->trade_type << endl
<< "报价 " << tick->quotes << endl;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("07ea5d21-59ab-11e8-83bf-94c69161828a");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2017-07-11 14:20:00", "2017-07-11 15:30:00",1000000, 1, 0, 0, 0, 1);
s.run();
cout << "回测完成!" << endl;
getchar();
return 0;
}
定时任务
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
schedule("1d","13:24:00");
return;
}
void on_schedule(const char *data_rule, const char *time_rule)
{
Order o = order_volume("SHSE.600000", 200, 1, 2, 1, 0);
}
void on_backtest_finished()
{
cout << "on_backtest_finished" << endl;
}
void on_indicator(Indicator *indicator)
{
cout << "on_indicator" << endl
<< "账号ID: " << indicator->account_id << endl
<< "累计收益率: " << indicator->pnl_ratio << endl
<< "年化收益率: " << indicator->pnl_ratio_annual << endl
<< "夏普比率: " << indicator->sharp_ratio << endl
<< "最大回撤: " << indicator->max_drawdown << endl
<< "风险比率: " << indicator->risk_ratio << endl
<< "开仓次数: " << indicator->open_count << endl
<< "平仓次数: " << indicator->close_count << endl
<< "盈利次数: " << indicator->win_count << endl
<< "亏损次数: " << indicator->lose_count << endl
<< "胜率: " << indicator->win_ratio << endl
<< "指标创建时间: " << indicator->created_at << endl
<< "指标变更时间: " << indicator->updated_at << endl;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("4727c864-84da-11e8-81b2-7085c223669d");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
s.run();
return 0;
}
数据事件驱动
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
subscribe("SHSE.600000", "1d");
return;
}
void on_bar(Bar *bar)
{
cout << "代码: " << bar->symbol << endl
<< "bar的开始时间: " << bar->bob << endl
<< "bar的结束时间: " << bar->eob << endl
<< "开盘价: " << bar->open << endl
<< "收盘价: " << bar->close << endl
<< "最高价: " << bar->high << endl
<< "最低价: " << bar->low << endl
<< "成交量: " << bar->volume << endl
<< "成交金额: " << bar->amount << endl
<< "前收盘价: " << bar->pre_close << endl
<< "持仓量: " << bar->position << endl
<< "bar频度: " << bar->frequency << endl;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("07ea5d21-59ab-11e8-83bf-94c69161828a");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
s.run();
return 0;
}
默认交易账号
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
subscribe("SHSE.600000,SZSE.000001", "1d");
return;
}
void on_bar(Bar *bar)
{
order_volume(bar->symbol, 200, 1, 2, 1, 0);
}
void on_backtest_finished()
{
cout << "on_backtest_finished" << endl;
}
void on_indicator(Indicator *indicator)
{
cout << "on_indicator" << endl
<< "账号ID: " << indicator->account_id << endl
<< "累计收益率: " << indicator->pnl_ratio << endl
<< "年化收益率: " << indicator->pnl_ratio_annual << endl
<< "夏普比率: " << indicator->sharp_ratio << endl
<< "最大回撤: " << indicator->max_drawdown << endl
<< "风险比率: " << indicator->risk_ratio << endl
<< "开仓次数: " << indicator->open_count << endl
<< "平仓次数: " << indicator->close_count << endl
<< "盈利次数: " << indicator->win_count << endl
<< "亏损次数: " << indicator->lose_count << endl
<< "胜率: " << indicator->win_ratio << endl
<< "指标创建时间: " << indicator->created_at << endl
<< "指标变更时间: " << indicator->updated_at << endl;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
s.run();
return 0;
}
显示指定交易账号
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
subscribe("SHSE.600000,SZSE.000001", "1d");
return;
}
void on_bar(Bar *bar)
{
order_volume(bar->symbol, 200, 1, 2, 1, 0, "ba8785aa-8641-11e8-98cb-7085c223669d");
}
void on_backtest_finished()
{
cout << "on_backtest_finished" << endl;
}
void on_indicator(Indicator *indicator)
{
cout << "on_indicator" << endl
<< "账号ID: " << indicator->account_id << endl
<< "累计收益率: " << indicator->pnl_ratio << endl
<< "年化收益率: " << indicator->pnl_ratio_annual << endl
<< "夏普比率: " << indicator->sharp_ratio << endl
<< "最大回撤: " << indicator->max_drawdown << endl
<< "风险比率: " << indicator->risk_ratio << endl
<< "开仓次数: " << indicator->open_count << endl
<< "平仓次数: " << indicator->close_count << endl
<< "盈利次数: " << indicator->win_count << endl
<< "亏损次数: " << indicator->lose_count << endl
<< "胜率: " << indicator->win_ratio << endl
<< "指标创建时间: " << indicator->created_at << endl
<< "指标变更时间: " << indicator->updated_at << endl;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
s.run();
return 0;
}
模式选择
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
subscribe("SHSE.600000", "tick");
return;
}
void on_tick(Tick *tick)
{
cout << "代码: " << tick->symbol << endl
<< "utc时间,精确到毫秒: " << tick->created_at << endl
<< "最新价: " << tick->price << endl
<< "开盘价: " << tick->open << endl
<< "最高价: " << tick->high << endl
<< "最低价: " << tick->low << endl
<< "成交总量 " << tick->cum_volume << endl
<< "成交总金额/最新成交额,累计值: " << tick->cum_amount << endl
<< "合约持仓量(期),累计值: " << tick->cum_position << endl
<< "瞬时成交额: " << tick->last_amount << endl
<< "瞬时成交量: " << tick->last_volume << endl
<< "交易类型,对应多开,多平等类型: " << tick->trade_type << endl;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_LIVE);
s.run();
return 0;
}
数据研究
#include <iostream>
#include "strategy.h"
using namespace std;
class MyStrategy :public Strategy
{
public:
MyStrategy() {}
~MyStrategy(){}
void on_init()
{
cout << "on_init" << endl;
DataArray<Tick>* ht = history_ticks("SZSE.000002", "2017-07-11 10:20:00", "2017-07-11 10:30:00");
if (ht->status() == 0)
{
for (int i = 0; i < ht->count(); i++)
{
cout << "代码: " << ht->at(i).symbol << endl
<< "utc时间,精确到毫秒: " << ht->at(i).created_at << endl
<< "最新价: " << ht->at(i).price << endl
<< "开盘价: " << ht->at(i).open << endl
<< "最高价: " << ht->at(i).high << endl
<< "最低价: " << ht->at(i).low << endl
<< "成交总量: " << ht->at(i).cum_volume << endl
<< "成交总金额 / 最新成交额, 累计值: " << ht->at(i).cum_amount << endl
<< "合约持仓量(期), 累计值: " << ht->at(i).cum_position << endl
<< "瞬时成交额: " << ht->at(i).last_amount << endl
<< "瞬时成交量: " << ht->at(i).last_volume << endl
<< "保留)交易类型, 对应多开, 多平等类型: " << ht->at(i).trade_type << endl
<< "报价: " << ht->at(i).quotes << endl;
}
}
return;
}
private:
};
int main(int argc, char *argv[])
{
MyStrategy s;
s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
s.set_mode(MODE_BACKTEST);
s.set_backtest_config("2017-07-11 10:20:00", "2017-07-11 10:30:00",1000000, 1, 0, 0, 0, 1);
s.run();
return 0;
}