PDF下载 下载

典型场景

阅读 3370

空策略

  1. //////////////////////////////////////////////////////////////////////////
  2. //空策略
  3. #include <iostream>
  4. #include "strategy.h"
  5. using namespace std;
  6. class MyStrategy :public Strategy
  7. {
  8. public:
  9. MyStrategy() {}
  10. ~MyStrategy(){}
  11. //重写on_init事件,进行策略开发
  12. void on_init()
  13. {
  14. cout << "on_init" << endl;
  15. //订阅行情数据
  16. subscribe("SHSE.600000", "tick");
  17. return;
  18. }
  19. //接收tick行情事件
  20. void on_tick(Tick *tick)
  21. {
  22. cout << "代码 " << tick->symbol << endl
  23. << "utc时间,精确到毫秒 " << tick->created_at << endl
  24. << "最新价 " << tick->price << endl
  25. << "开盘价 " << tick->open << endl
  26. << "最高价 " << tick->high << endl
  27. << "最低价 " << tick->low << endl
  28. << "成交总量 " << tick->cum_volume << endl
  29. << "成交总金额 / 最新成交额, 累计值 " << tick->cum_amount << endl
  30. << "合约持仓量(期), 累计值 " << tick->cum_position << endl
  31. << "瞬时成交额 " << tick->last_amount << endl
  32. << "瞬时成交量 " << tick->last_volume << endl
  33. << "保留)交易类型, 对应多开, 多平等类型 " << tick->trade_type << endl
  34. << "报价 " << tick->quotes << endl;
  35. }
  36. private:
  37. };
  38. int main(int argc, char *argv[])
  39. {
  40. MyStrategy s;
  41. s.set_strategy_id("07ea5d21-59ab-11e8-83bf-94c69161828a");
  42. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  43. s.set_mode(MODE_BACKTEST);
  44. s.set_backtest_config("2017-07-11 14:20:00", "2017-07-11 15:30:00",1000000, 1, 0, 0, 0, 1);
  45. s.run();
  46. cout << "回测完成!" << endl;
  47. getchar();
  48. return 0;
  49. }

定时任务

  1. //////////////////////////////////////////////////////////////////////////
  2. //定时任务
  3. //策略描述:
  4. //典型如选股交易。比如,策略每日收盘前10分钟执行:选股->决策逻辑->交易->退出。可能无需订阅实时数据。
  5. #include <iostream>
  6. #include "strategy.h"
  7. using namespace std;
  8. class MyStrategy :public Strategy
  9. {
  10. public:
  11. MyStrategy() {}
  12. ~MyStrategy(){}
  13. //重写on_init事件,进行策略开发
  14. void on_init()
  15. {
  16. cout << "on_init" << endl;
  17. //设置定时任务
  18. schedule("1d","13:24:00");
  19. return;
  20. }
  21. //定时任务触发事件
  22. void on_schedule(const char *data_rule, const char *time_rule)
  23. {
  24. //购买200股浦发银行股票
  25. Order o = order_volume("SHSE.600000", 200, 1, 2, 1, 0);
  26. }
  27. //回测完成事件
  28. void on_backtest_finished()
  29. {
  30. cout << "on_backtest_finished" << endl;
  31. }
  32. //回测完成后收到绩效报告
  33. void on_indicator(Indicator *indicator)
  34. {
  35. cout << "on_indicator" << endl
  36. << "账号ID: " << indicator->account_id << endl
  37. << "累计收益率: " << indicator->pnl_ratio << endl
  38. << "年化收益率: " << indicator->pnl_ratio_annual << endl
  39. << "夏普比率: " << indicator->sharp_ratio << endl
  40. << "最大回撤: " << indicator->max_drawdown << endl
  41. << "风险比率: " << indicator->risk_ratio << endl
  42. << "开仓次数: " << indicator->open_count << endl
  43. << "平仓次数: " << indicator->close_count << endl
  44. << "盈利次数: " << indicator->win_count << endl
  45. << "亏损次数: " << indicator->lose_count << endl
  46. << "胜率: " << indicator->win_ratio << endl
  47. << "指标创建时间: " << indicator->created_at << endl
  48. << "指标变更时间: " << indicator->updated_at << endl;
  49. }
  50. private:
  51. };
  52. int main(int argc, char *argv[])
  53. {
  54. MyStrategy s;
  55. s.set_strategy_id("4727c864-84da-11e8-81b2-7085c223669d");
  56. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  57. s.set_mode(MODE_BACKTEST);
  58. s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
  59. s.run();
  60. return 0;
  61. }

数据事件驱动

  1. //////////////////////////////////////////////////////////////////////////
  2. //数据事件驱动
  3. //策略描述:
  4. //典型如选股交易策略。比如,策略每日收盘前10分钟执行:选股->决策逻辑->交易->退出。可能无需订阅实时数据
  5. #include <iostream>
  6. #include "strategy.h"
  7. using namespace std;
  8. class MyStrategy :public Strategy
  9. {
  10. public:
  11. MyStrategy() {}
  12. ~MyStrategy(){}
  13. //重写on_init事件,进行策略开发
  14. void on_init()
  15. {
  16. cout << "on_init" << endl;
  17. //订阅浦发银行, bar频率为一天
  18. subscribe("SHSE.600000", "1d");
  19. return;
  20. }
  21. void on_bar(Bar *bar)
  22. {
  23. cout << "代码: " << bar->symbol << endl
  24. << "bar的开始时间: " << bar->bob << endl
  25. << "bar的结束时间: " << bar->eob << endl
  26. << "开盘价: " << bar->open << endl
  27. << "收盘价: " << bar->close << endl
  28. << "最高价: " << bar->high << endl
  29. << "最低价: " << bar->low << endl
  30. << "成交量: " << bar->volume << endl
  31. << "成交金额: " << bar->amount << endl
  32. << "前收盘价: " << bar->pre_close << endl
  33. << "持仓量: " << bar->position << endl
  34. << "bar频度: " << bar->frequency << endl;
  35. }
  36. private:
  37. };
  38. int main(int argc, char *argv[])
  39. {
  40. MyStrategy s;
  41. s.set_strategy_id("07ea5d21-59ab-11e8-83bf-94c69161828a");
  42. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  43. s.set_mode(MODE_BACKTEST);
  44. s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
  45. s.run();
  46. return 0;
  47. }

默认交易账号

  1. //////////////////////////////////////////////////////////////////////////
  2. //默认账号交易
  3. //策略描述:
  4. //默认账号进行交易,下单时不指定account
  5. #include <iostream>
  6. #include "strategy.h"
  7. using namespace std;
  8. class MyStrategy :public Strategy
  9. {
  10. public:
  11. MyStrategy() {}
  12. ~MyStrategy(){}
  13. //重写on_init事件,进行策略开发
  14. void on_init()
  15. {
  16. cout << "on_init" << endl;
  17. subscribe("SHSE.600000,SZSE.000001", "1d");
  18. return;
  19. }
  20. void on_bar(Bar *bar)
  21. {
  22. //不指定account 使用默认账户下单
  23. order_volume(bar->symbol, 200, 1, 2, 1, 0);
  24. }
  25. //回测完成事件
  26. void on_backtest_finished()
  27. {
  28. cout << "on_backtest_finished" << endl;
  29. }
  30. //回测完成后收到绩效报告
  31. void on_indicator(Indicator *indicator)
  32. {
  33. cout << "on_indicator" << endl
  34. << "账号ID: " << indicator->account_id << endl
  35. << "累计收益率: " << indicator->pnl_ratio << endl
  36. << "年化收益率: " << indicator->pnl_ratio_annual << endl
  37. << "夏普比率: " << indicator->sharp_ratio << endl
  38. << "最大回撤: " << indicator->max_drawdown << endl
  39. << "风险比率: " << indicator->risk_ratio << endl
  40. << "开仓次数: " << indicator->open_count << endl
  41. << "平仓次数: " << indicator->close_count << endl
  42. << "盈利次数: " << indicator->win_count << endl
  43. << "亏损次数: " << indicator->lose_count << endl
  44. << "胜率: " << indicator->win_ratio << endl
  45. << "指标创建时间: " << indicator->created_at << endl
  46. << "指标变更时间: " << indicator->updated_at << endl;
  47. }
  48. private:
  49. };
  50. int main(int argc, char *argv[])
  51. {
  52. MyStrategy s;
  53. s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
  54. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  55. s.set_mode(MODE_BACKTEST);
  56. s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
  57. s.run();
  58. return 0;
  59. }

显示指定交易账号

  1. //////////////////////////////////////////////////////////////////////////
  2. //显示指定交易账号
  3. //策略描述:
  4. //下单时指定交易账号,account参数传账号id或者账号标题
  5. #include <iostream>
  6. #include "strategy.h"
  7. using namespace std;
  8. class MyStrategy :public Strategy
  9. {
  10. public:
  11. MyStrategy() {}
  12. ~MyStrategy(){}
  13. //重写on_init事件,进行策略开发
  14. void on_init()
  15. {
  16. cout << "on_init" << endl;
  17. subscribe("SHSE.600000,SZSE.000001", "1d");
  18. return;
  19. }
  20. void on_bar(Bar *bar)
  21. {
  22. //不指定account 使用默认账户下单
  23. order_volume(bar->symbol, 200, 1, 2, 1, 0, "ba8785aa-8641-11e8-98cb-7085c223669d");
  24. }
  25. //回测完成事件
  26. void on_backtest_finished()
  27. {
  28. cout << "on_backtest_finished" << endl;
  29. }
  30. //回测完成后收到绩效报告
  31. void on_indicator(Indicator *indicator)
  32. {
  33. cout << "on_indicator" << endl
  34. << "账号ID: " << indicator->account_id << endl
  35. << "累计收益率: " << indicator->pnl_ratio << endl
  36. << "年化收益率: " << indicator->pnl_ratio_annual << endl
  37. << "夏普比率: " << indicator->sharp_ratio << endl
  38. << "最大回撤: " << indicator->max_drawdown << endl
  39. << "风险比率: " << indicator->risk_ratio << endl
  40. << "开仓次数: " << indicator->open_count << endl
  41. << "平仓次数: " << indicator->close_count << endl
  42. << "盈利次数: " << indicator->win_count << endl
  43. << "亏损次数: " << indicator->lose_count << endl
  44. << "胜率: " << indicator->win_ratio << endl
  45. << "指标创建时间: " << indicator->created_at << endl
  46. << "指标变更时间: " << indicator->updated_at << endl;
  47. }
  48. private:
  49. };
  50. int main(int argc, char *argv[])
  51. {
  52. MyStrategy s;
  53. s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
  54. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  55. s.set_mode(MODE_BACKTEST);
  56. s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
  57. s.run();
  58. return 0;
  59. }

模式选择

  1. //////////////////////////////////////////////////////////////////////////
  2. //模式选择
  3. //策略描述:
  4. //策略支持两种运行模式,实时模式和回测模式,用户需要在运行策略时选择模式,执行run函数时mode=1 表示回测模式,mode=0表示实时模式
  5. #include <iostream>
  6. #include "strategy.h"
  7. using namespace std;
  8. class MyStrategy :public Strategy
  9. {
  10. public:
  11. MyStrategy() {}
  12. ~MyStrategy(){}
  13. //重写on_init事件,进行策略开发
  14. void on_init()
  15. {
  16. cout << "on_init" << endl;
  17. subscribe("SHSE.600000", "tick");
  18. return;
  19. }
  20. void on_tick(Tick *tick)
  21. {
  22. cout << "代码: " << tick->symbol << endl
  23. << "utc时间,精确到毫秒: " << tick->created_at << endl
  24. << "最新价: " << tick->price << endl
  25. << "开盘价: " << tick->open << endl
  26. << "最高价: " << tick->high << endl
  27. << "最低价: " << tick->low << endl
  28. << "成交总量 " << tick->cum_volume << endl
  29. << "成交总金额/最新成交额,累计值: " << tick->cum_amount << endl
  30. << "合约持仓量(期),累计值: " << tick->cum_position << endl
  31. << "瞬时成交额: " << tick->last_amount << endl
  32. << "瞬时成交量: " << tick->last_volume << endl
  33. << "交易类型,对应多开,多平等类型: " << tick->trade_type << endl;
  34. }
  35. private:
  36. };
  37. int main(int argc, char *argv[])
  38. {
  39. MyStrategy s;
  40. s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
  41. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  42. // mode = MODE_LIVE 实时模式
  43. // mode = MODE_BACKTEST 回测模式, 指定回测开始时间backtest_start_time和结束时间backtest_end_time
  44. //s.set_backtest_config("2016-07-11 17:20:00", "2017-07-11 17:30:00",1000000, 1, 0, 0, 0, 1);
  45. s.set_mode(MODE_LIVE);
  46. s.run();
  47. return 0;
  48. }

数据研究

  1. //////////////////////////////////////////////////////////////////////////
  2. //数据研究
  3. //策略描述:
  4. //无需实时数据驱动策略,无需交易下单,只是取数据的场景
  5. #include <iostream>
  6. #include "strategy.h"
  7. using namespace std;
  8. class MyStrategy :public Strategy
  9. {
  10. public:
  11. MyStrategy() {}
  12. ~MyStrategy(){}
  13. //重写on_init事件,进行策略开发
  14. void on_init()
  15. {
  16. cout << "on_init" << endl;
  17. DataArray<Tick>* ht = history_ticks("SZSE.000002", "2017-07-11 10:20:00", "2017-07-11 10:30:00");
  18. if (ht->status() == 0)
  19. {
  20. for (int i = 0; i < ht->count(); i++)
  21. {
  22. cout << "代码: " << ht->at(i).symbol << endl
  23. << "utc时间,精确到毫秒: " << ht->at(i).created_at << endl
  24. << "最新价: " << ht->at(i).price << endl
  25. << "开盘价: " << ht->at(i).open << endl
  26. << "最高价: " << ht->at(i).high << endl
  27. << "最低价: " << ht->at(i).low << endl
  28. << "成交总量: " << ht->at(i).cum_volume << endl
  29. << "成交总金额 / 最新成交额, 累计值: " << ht->at(i).cum_amount << endl
  30. << "合约持仓量(期), 累计值: " << ht->at(i).cum_position << endl
  31. << "瞬时成交额: " << ht->at(i).last_amount << endl
  32. << "瞬时成交量: " << ht->at(i).last_volume << endl
  33. << "保留)交易类型, 对应多开, 多平等类型: " << ht->at(i).trade_type << endl
  34. << "报价: " << ht->at(i).quotes << endl;
  35. }
  36. }
  37. return;
  38. }
  39. private:
  40. };
  41. int main(int argc, char *argv[])
  42. {
  43. MyStrategy s;
  44. s.set_strategy_id("ba8785aa-8641-11e8-98cb-7085c223669d");
  45. s.set_token("39624b0f1916ae0b2a4cb1f2d13704368badf576");
  46. s.set_mode(MODE_BACKTEST);
  47. s.set_backtest_config("2017-07-11 10:20:00", "2017-07-11 10:30:00",1000000, 1, 0, 0, 0, 1);
  48. s.run();
  49. return 0;
  50. }
0 篇笔记