PDF下载 下载

基类原型

阅读 32316

策略类简介

策略类集成了行情、交易和事件的接口,用户的策略都从此类继承实现自己的业务逻辑。每个进程只能实例化一个策略类对象。

策略类定义

  1. class GM_CLASS Strategy
  2. {
  3. public:
  4. Strategy(const char *token, const char *strategy_id, int mode);
  5. Strategy();
  6. virtual ~Strategy();
  7. public: //基础函数
  8. //运行策略
  9. int run();
  10. //停止策略
  11. void stop();
  12. //设置策略ID
  13. void set_strategy_id(const char *strategy_id);
  14. //设置用户token
  15. void set_token(const char *token);
  16. //设置策略运行模式
  17. void set_mode(int mode);
  18. //定时任务
  19. int schedule(const char *data_rule, const char *time_rule);
  20. //当前时间
  21. double now();
  22. //设置回测参数
  23. int set_backtest_config(
  24. const char *start_time,
  25. const char *end_time,
  26. double initial_cash = 1000000,
  27. double transaction_ratio = 1,
  28. double commission_ratio = 0,
  29. double slippage_ratio = 0,
  30. int adjust = 0,
  31. int check_cache = 1
  32. );
  33. public: //数据函数
  34. // 订阅行情
  35. int subscribe(const char *symbols, const char * frequency, bool unsubscribe_previous = false);
  36. // 退订行情
  37. int unsubscribe(const char *symbols, const char * frequency);
  38. public: //交易函数
  39. //查询交易账号
  40. DataArray<Account>* get_accounts();
  41. //查询指定交易账号状态
  42. int get_account_status(const char *account, AccountStatus &as);
  43. //查询所有交易账号状态
  44. DataArray<AccountStatus>* get_all_account_status();
  45. //按指定量委托
  46. Order order_volume(const char *symbol, int volume, int side, int order_type, int position_effect, double price = 0, const char *account = NULL);
  47. //按指定价值委托
  48. Order order_value(const char *symbol, double value, int side, int order_type, int position_effect, double price = 0, const char *account = NULL);
  49. //按总资产指定比例委托
  50. Order order_percent(const char *symbol, double percent, int side, int order_type, int position_effect, double price = 0, const char *account = NULL);
  51. //调仓到目标持仓量
  52. Order order_target_volume(const char *symbol, int volume, int position_side, int order_type, double price = 0, const char *account = NULL);
  53. //调仓到目标持仓额
  54. Order order_target_value(const char *symbol, double value, int position_side, int order_type, double price = 0, const char *account = NULL);
  55. //调仓到目标持仓比例(总资产的比例)
  56. Order order_target_percent(const char *symbol, double percent, int position_side, int order_type, double price = 0, const char *account = NULL);
  57. //平当前所有可平持仓
  58. DataArray<Order>* order_close_all();
  59. //委托撤单
  60. int order_cancel(const char *cl_ord_id, const char *account = NULL);
  61. //撤销所有委托
  62. int order_cancel_all();
  63. //委托下单
  64. Order place_order(const char *symbol, int volume, int side, int order_type, int position_effect, double price = 0, int order_duration = 0, int order_qualifier = 0, double stop_price = 0, int order_business = 0, const char *account = NULL);
  65. //盘后定价交易
  66. Order order_after_hour(const char *symbol, int volume, int side, double price, const char *account = NULL);
  67. //查询委托
  68. DataArray<Order>* get_orders(const char *account = NULL);
  69. //查询未结委托
  70. DataArray<Order>* get_unfinished_orders(const char *account = NULL);
  71. //查询成交
  72. DataArray<ExecRpt>* get_execution_reports(const char *account = NULL);
  73. //查询资金
  74. DataArray<Cash>* get_cash(const char *accounts = NULL);
  75. //查询持仓
  76. DataArray<Position>* get_position(const char *account = NULL);
  77. //委托算法单
  78. AlgoOrder order_algo(const char *symbol, int volume, int position_effect, int side, int order_type, double price, AlgoParam &algo_param, const char *account = NULL);
  79. //撤单算法委托
  80. int algo_order_cancel(const char *cl_ord_id, const char *account = NULL);
  81. //暂停/恢复算法单
  82. int algo_order_pause(const char *cl_ord_id, int status, const char *account = NULL);
  83. //查询算法委托
  84. DataArray<AlgoOrder>* get_algo_orders(const char *account = NULL);
  85. //查询算法子委托
  86. DataArray<Order>* get_algo_child_orders(const char *cl_ord_id, const char *account = NULL);
  87. //功能号调用
  88. int raw_func(const char *account, const char *func_id, const char *func_args, char*&rsp);
  89. /* 两融业务 */
  90. //融资买入
  91. Order credit_buying_on_margin(int position_src, const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  92. //融券卖出
  93. Order credit_short_selling(int position_src, const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  94. //买券还券
  95. Order credit_repay_share_by_buying_share(const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  96. //卖券还款
  97. Order credit_repay_cash_by_selling_share(const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  98. //担保品买入
  99. Order credit_buying_on_collateral(const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  100. //担保品卖出
  101. Order credit_selling_on_collateral(const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  102. //直接还券
  103. Order credit_repay_share_directly(const char *symbol, int volume, const char *account = NULL);
  104. //直接还款
  105. int credit_repay_cash_directly(double amount, const char *account = NULL, double *actual_repay_amount = NULL, char *error_msg_buf = NULL, int buf_len = 0);
  106. //担保品转入
  107. Order credit_collateral_in(const char *symbol, int volume, const char *account = NULL);
  108. //担保品转出
  109. Order credit_collateral_out(const char *symbol, int volume, const char *account = NULL);
  110. //查询担保证券
  111. DataArray<CollateralInstrument>* credit_get_collateral_instruments(const char *account = NULL);
  112. //查询标的证券,可做融券标的股票列表
  113. DataArray<BorrowableInstrument>* credit_get_borrowable_instruments(int position_src, const char *account = NULL);
  114. //查询券商融券账户头寸,可用融券的数量
  115. DataArray<BorrowableInstrumentPosition>* credit_get_borrowable_instruments_positions(int position_src, const char *account = NULL);
  116. //查询融资融券合约
  117. DataArray<CreditContract>* credit_get_contracts(int position_src, const char *account = NULL);
  118. //查询融资融券资金
  119. int credit_get_cash(CreditCash &cash, const char *account = NULL);
  120. /* 新股业务 */
  121. //新股申购
  122. Order ipo_buy(const char *symbol, int volume, double price, const char *account = NULL);
  123. //查询客户新股申购额度
  124. int ipo_get_quota(double &quota, const char *account = NULL);
  125. //查询当日新股清单
  126. DataArray<IPOInstruments>* ipo_get_instruments(const char *account = NULL);
  127. //配号查询
  128. DataArray<IPOMatchNumber>* ipo_get_match_number(const char *account = NULL);
  129. //中签查询
  130. DataArray<IPOLotInfo>* ipo_get_lot_info(const char *account = NULL);
  131. /* 基金业务 */
  132. //ETF申购
  133. Order fund_etf_buy(const char *symbol, int volume, double price, const char *account = NULL);
  134. //ETF赎回
  135. Order fund_etf_redemption(const char *symbol, int volume, double price, const char *account = NULL);
  136. //基金认购
  137. Order fund_subscribing(const char *symbol, int volume, const char *account = NULL);
  138. //基金申购
  139. Order fund_buy(const char *symbol, int volume, const char *account = NULL);
  140. //基金赎回
  141. Order fund_redemption(const char *symbol, int volume, const char *account = NULL);
  142. /* 债券业务 */
  143. //国债逆回购
  144. Order bond_reverse_repurchase_agreement(const char *symbol, int volume, double price, int order_type = OrderType_Limit, int order_duration = OrderDuration_Unknown, int order_qualifier = OrderQualifier_Unknown, const char *account = NULL);
  145. public: //策略参数类函数
  146. //添加参数
  147. int add_parameters(Parameter *params, int count);
  148. //删除参数
  149. int del_parameters(const char *keys);
  150. //设置参数
  151. int set_parameters(Parameter *params, int count);
  152. //获取参数
  153. DataArray<Parameter>* get_parameters();
  154. //设置标的
  155. int set_symbols(const char *symbols);
  156. //获取标的
  157. DataArray<Symbol>* get_symbols();
  158. public: //事件函数
  159. //初始化完成
  160. virtual void on_init();
  161. //收到Tick行情
  162. virtual void on_tick(Tick *tick);
  163. //收到bar行情
  164. virtual void on_bar(Bar *bar);
  165. //收到逐笔成交(L2行情时有效)
  166. virtual void on_l2transaction(L2Transaction *l2transaction);
  167. //收到逐笔委托(深交所L2行情时有效)
  168. virtual void on_l2order(L2Order *l2order);
  169. //收到委托队列(上交所L2行情时有效)
  170. virtual void on_l2order_queue(L2OrderQueue *l2queue);
  171. //委托变化
  172. virtual void on_order_status(Order *order);
  173. //执行回报
  174. virtual void on_execution_report(ExecRpt *rpt);
  175. //算法委托变化
  176. virtual void on_algo_order_status(AlgoOrder *order);
  177. //参数变化
  178. virtual void on_parameter(Parameter *param);
  179. //定时任务触发
  180. virtual void on_schedule(const char *data_rule, const char *time_rule);
  181. //回测完成后收到绩效报告
  182. virtual void on_backtest_finished(Indicator *indicator);
  183. //实盘账号状态变化
  184. virtual void on_account_status(AccountStatus *account_status);
  185. //错误产生
  186. virtual void on_error(int error_code, const char *error_msg);
  187. //收到策略停止信号
  188. virtual void on_stop();
  189. //数据已经连接上
  190. virtual void on_market_data_connected();
  191. //交易已经连接上
  192. virtual void on_trade_data_connected();
  193. //数据连接断开了
  194. virtual void on_market_data_disconnected();
  195. //交易连接断开了
  196. virtual void on_trade_data_disconnected();
  197. };
0 篇笔记