PDF下载 下载

基类原型

阅读 30962

策略类简介

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

策略类定义

  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. public AccountStatus GetAccountStatus(string accountId);
  69. //查询所有账户状态
  70. public List<AccountStatus> GetAccountStatus()
  71. //====================================数据函数=============================================
  72. //订阅行情
  73. public int Subscribe(string symbols, string frequency, bool unsubscribePrevious = false);
  74. //退订行情
  75. public int Unsubscribe(string symbols, string frequency);
  76. //========================================事件函数==============================================
  77. //初始化完成
  78. public virtual void OnInit();
  79. //实盘账号状态变化
  80. public virtual void OnAccountStatus(AccountStatus accountStatus);
  81. //收到bar行情
  82. public virtual void OnBar(Bar bar);
  83. //cash发生变化
  84. public virtual void OnCashStatus(Cash order);
  85. //错误事件
  86. public virtual void OnError(int errorCode, string errorMsg);
  87. //执行回报
  88. public virtual void OnExecutionReport(ExecRpt rpt);
  89. //回测结束
  90. public virtual void OnBacktestFinished(Indicator indicator);
  91. //数据库已连接
  92. public virtual void OnMarketDataConnected();
  93. //数据库断开
  94. public virtual void OnMarketDataDisconnected();
  95. //委托发生变化
  96. public virtual void OnOrderStatus(Order order);
  97. //运行时参数发生变化
  98. public virtual void OnParameter(List<Parameter> param);
  99. //position发生变化
  100. public virtual void OnPosition(Position position);
  101. //定时任务触发
  102. public virtual void OnSchedule(string dataRule, string timeRule);
  103. //策略结束
  104. public virtual void OnStop();
  105. //收到tick行情
  106. public virtual void OnTick(Tick tick);
  107. //交易已连接
  108. public virtual void OnTradeDataConnected();
  109. //交易断开
  110. public virtual void OnTradeDataDisconnected();
  111. }
0 篇笔记