PDF下载 下载

策略类简介

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

策略类定义

  1. public class Strategy
  2. {
  3. //策略基类构造函数
  4. //token:
  5. //strategy_id:策略ID
  6. //mode:运行模式
  7. public Strategy(string token, string strategyId, int mode);
  8. //=====================================策略参数类函数=====================================
  9. //添加参数
  10. public int AddParameters(Parameter param);
  11. //删除参数
  12. public int DelParameters(string keys);
  13. //获取标的
  14. public GMDataList<string> GetSymbols();
  15. //设置标的
  16. public int SetSymbols(string symbols);
  17. //设置回测参数
  18. public int SetBacktestConfig(string startTime, string endTime, double initialCash = 1000000, double transactionRatio = 1, double commissionRatio = 0, double slippageRatio = 0, int adjust = 0, int checkCache = 1);
  19. //设置参数
  20. public int SetParameters(List<Parameter> parameters);
  21. //=======================================交易函数================================================
  22. //查询交易账号
  23. public GMDataList<Account> GetAccounts();
  24. //查询资金
  25. public GMDataList<Cash> GetCash(string account = null);
  26. //查询成交
  27. public GMDataList<ExecRpt> GetExecutionReports(string account = null);
  28. //查询委托
  29. public GMDataList<Order> GetOrders(string account = null);
  30. //查询持仓
  31. public GMDataList<Position> GetPosition(string account = null);
  32. //查询未结委托
  33. public GMDataList<Order> GetUnfinishedOrders(string account = null);
  34. //委托撤单,
  35. public int OrderCancel(string clOrdIds);
  36. //撤销所有委托
  37. public void OrderCancelAll();
  38. //平当前所有可平持仓
  39. public GMDataList<Order> OrderCloseAll();
  40. //按总资产指定比例委托
  41. public GMData<Order> OrderPercent(string symbol, double percent, int side, int orderType, int positionEffect, double price = 0, string account = null);
  42. //调仓到目标持仓比例(总资产的比例)
  43. public GMData<Order> OrderTargetPercent(string symbol, double percent, int positionSide, int orderType, double price = 0, string account = null);
  44. //调仓到目标持仓额
  45. public GMData<Order> OrderTargetValue(string symbol, double value, int positionSide, int orderType, double price = 0, string account = null);
  46. //调仓到目标持仓量
  47. public GMData<Order> OrderTargetVolume(string symbol, int volume, int positionSide, int orderType, double price = 0, string account = null);
  48. //按指定价值委托
  49. public GMData<Order> OrderValue(string symbol, double value, int side, int orderType, int positionEffect, double price = 0, string account = null);
  50. //按指定量委托
  51. public GMData<Order> OrderVolume(string symbol, int volume, OrderSide side, OrderType orderType, PositionEffect positionEffect, double price = 0, string account = null);
  52. //=====================================基础函数=================================
  53. //运行策略
  54. public int Run();
  55. //定时任务
  56. public int Schedule(string dataRule, string timeRule);
  57. //停止策略
  58. public int Stop();
  59. //当前事件
  60. public long Now();
  61. //设置token
  62. public int SetToken(string token)
  63. //设置运行模式
  64. public int SetMode(StrategyMode mode)
  65. //设置策略ID
  66. public int SetStrategyId(string strategyId)
  67. //====================================数据函数=============================================
  68. //订阅行情
  69. public int Subscribe(string symbols, string frequency, bool unsubscribePrevious = false);
  70. //退订行情
  71. public int Unsubscribe(string symbols, string frequency);
  72. //========================================事件函数==============================================
  73. //初始化完成
  74. public virtual void OnInit();
  75. //实盘账号状态变化
  76. public virtual void OnAccountStatus(AccountStatus accountStatus);
  77. //收到bar行情
  78. public virtual void OnBar(Bar bar);
  79. //cash发生变化
  80. public virtual void OnCashStatus(Cash order);
  81. //错误事件
  82. public virtual void OnError(int errorCode, string errorMsg);
  83. //执行回报
  84. public virtual void OnExecutionReport(ExecRpt rpt);
  85. //回测结束
  86. public virtual void OnBacktestFinished(Indicator indicator);
  87. //数据库已连接
  88. public virtual void OnMarketDataConnected();
  89. //数据库断开
  90. public virtual void OnMarketDataDisconnected();
  91. //委托发生变化
  92. public virtual void OnOrderStatus(Order order);
  93. //运行时参数发生变化
  94. public virtual void OnParameter(List<Parameter> param);
  95. //position发生变化
  96. public virtual void OnPosition(Position position);
  97. //定时任务触发
  98. public virtual void OnSchedule(string dataRule, string timeRule);
  99. //策略结束
  100. public virtual void OnStop();
  101. //收到tick行情
  102. public virtual void OnTick(Tick tick);
  103. //交易已连接
  104. public virtual void OnTradeDataConnected();
  105. //交易断开
  106. public virtual void OnTradeDataDisconnected();
  107. }
0 篇笔记