PDF下载 下载

典型场景

阅读 16473

空策略

  1. //////////////////////////////////////////////////////////////////////////
  2. //空策略
  3. using GMSDK;
  4. namespace example
  5. {
  6. public class MyStrategy : Strategy
  7. {
  8. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  9. //重写OnInit事件,进行策略开发
  10. public override void OnInit()
  11. {
  12. System.Console.WriteLine("OnInit");
  13. //订阅行情数据
  14. Subscribe("SHSE.600000", "tick");
  15. return;
  16. }
  17. public override void OnTick(Tick tick)
  18. {
  19. System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
  20. System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
  21. System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
  22. System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
  23. System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
  24. System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
  25. System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
  26. System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
  27. System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
  28. System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
  29. System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
  30. System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
  31. System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
  32. System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
  33. System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
  34. System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
  35. }
  36. }
  37. class Program
  38. {
  39. static void Main(string[] args)
  40. {
  41. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_BACKTEST);
  42. s.SetBacktestConfig("2017-07-25 8:20:00", "2018-07-25 17:30:00");
  43. s.Run();
  44. System.Console.WriteLine("回测完成!");
  45. System.Console.Read();
  46. }
  47. }
  48. }

定时任务

  1. //////////////////////////////////////////////////////////////////////////
  2. //定时任务
  3. //策略描述:
  4. 典型如选股交易。比如,策略每日收盘前10分钟执行:选股->决策逻辑->交易->退出。可能无需订阅实时数据。
  5. using GMSDK;
  6. namespace example_schedule
  7. {
  8. public class MyStrategy : Strategy
  9. {
  10. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  11. //重写OnInit事件,进行策略开发
  12. public override void OnInit()
  13. {
  14. System.Console.WriteLine("OnInit");
  15. //设置定时任务
  16. Schedule("1d", "13:24:00");
  17. return;
  18. }
  19. //定时任务触发事件
  20. public override void OnSchedule(string data_rule, string timeRule)
  21. {
  22. //购买200股浦发银行股票
  23. GMData<Order> o = OrderValue("SHSE.600000", 200, OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0);
  24. if (o.status == 0) //该判断仅表示函数调用无异常
  25. {
  26. //
  27. }
  28. }
  29. //回测完成后收到绩效报告
  30. public override void OnBacktestFinished(Indicator indicator)
  31. {
  32. System.Console.WriteLine("OnBacktestFinished:");
  33. System.Console.WriteLine("{0,-50}{1}", "账号ID:", indicator.accountId);
  34. System.Console.WriteLine("{0,-50}{1}", "累计收益率:", indicator.pnlRatio);
  35. System.Console.WriteLine("{0,-50}{1}", "年化收益率:", indicator.pnlRatioAnnual);
  36. System.Console.WriteLine("{0,-50}{1}", "夏普比率:", indicator.sharpRatio);
  37. System.Console.WriteLine("{0,-50}{1}", "最大回撤:", indicator.maxDrawdown);
  38. System.Console.WriteLine("{0,-50}{1}", "风险比率:", indicator.riskRatio);
  39. System.Console.WriteLine("{0,-50}{1}", "开仓次数:", indicator.openCount);
  40. System.Console.WriteLine("{0,-50}{1}", "平仓次数:", indicator.closeCount);
  41. System.Console.WriteLine("{0,-50}{1}", "盈利次数:", indicator.winCount);
  42. System.Console.WriteLine("{0,-50}{1}", "亏损次数:", indicator.loseCount);
  43. System.Console.WriteLine("{0,-50}{1}", "胜率:", indicator.winRatio);
  44. System.Console.WriteLine("{0,-50}{1}", "指标创建时间:", indicator.createdAt);
  45. System.Console.WriteLine("{0,-50}{1}", "指标变更时间:", indicator.updatedAt);
  46. }
  47. }
  48. class Program
  49. {
  50. static void Main(string[] args)
  51. {
  52. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
  53. s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
  54. s.Run();
  55. System.Console.WriteLine("回测完成!");
  56. System.Console.Read();
  57. }
  58. }
  59. }

数据事件驱动

  1. //////////////////////////////////////////////////////////////////////////
  2. //数据事件驱动
  3. //策略描述:
  4. //典型如选股交易策略。比如,策略每日收盘前10分钟执行:选股->决策逻辑->交易->退出。可能无需订阅实时数据
  5. using GMSDK;
  6. namespace example_dataevent
  7. {
  8. public class MyStrategy : Strategy
  9. {
  10. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  11. //重写OnInit事件, 进行策略开发
  12. public override void OnInit()
  13. {
  14. System.Console.WriteLine("OnInit");
  15. //订阅浦发银行,bar频率为一天
  16. Subscribe("SHSE.600000", "1d");
  17. return;
  18. }
  19. //重写OnBar事件
  20. public override void OnBar(Bar bar)
  21. {
  22. System.Console.WriteLine("OnBar:");
  23. System.Console.WriteLine("{0, -50}{1}", "代码:", bar.symbol);
  24. System.Console.WriteLine("{0, -50}{1}", "bar的开始时间:", bar.bob);
  25. System.Console.WriteLine("{0, -50}{1}", "bar的结束时间:", bar.eob);
  26. System.Console.WriteLine("{0, -50}{1}", "开盘价:", bar.open);
  27. System.Console.WriteLine("{0, -50}{1}", "收盘价:", bar.close);
  28. System.Console.WriteLine("{0, -50}{1}", "最高价:", bar.high);
  29. System.Console.WriteLine("{0, -50}{1}", "最低价:", bar.low);
  30. System.Console.WriteLine("{0, -50}{1}", "成交量:", bar.volume);
  31. System.Console.WriteLine("{0, -50}{1}", "成交金额:", bar.amount);
  32. System.Console.WriteLine("{0, -50}{1}", "前收盘价:", bar.preClose);
  33. System.Console.WriteLine("{0, -50}{1}", "持仓量:", bar.position);
  34. System.Console.WriteLine("{0, -50}{1}", "bar频度:", bar.frequency);
  35. }
  36. }
  37. class Program
  38. {
  39. static void Main(string[] args)
  40. {
  41. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
  42. s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
  43. s.Run();
  44. System.Console.WriteLine("回测完成!");
  45. System.Console.Read();
  46. }
  47. }
  48. }

默认交易账号

  1. //////////////////////////////////////////////////////////////////////////
  2. //默认账号交易
  3. //策略描述:
  4. //默认账号进行交易,下单时不指定account
  5. using GMSDK;
  6. namespace example_defaccount
  7. {
  8. public class MyStrategy : Strategy
  9. {
  10. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  11. //重写OnInit事件,进行策略开发
  12. public override void OnInit()
  13. {
  14. System.Console.WriteLine("OnInit");
  15. Subscribe("SHSE.600000,SZSE.000001", "1d");
  16. return;
  17. }
  18. //重写OnBar事件
  19. public override void OnBar(Bar bar)
  20. {
  21. //不指定account 使用默认账户下单
  22. OrderVolume(bar.symbol, 200, OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0);
  23. }
  24. //回测完成后收到绩效报告
  25. public override void OnBacktestFinished(Indicator indicator)
  26. {
  27. System.Console.WriteLine("OnBacktestFinished:");
  28. System.Console.WriteLine("{0,-50}{1}", "账号ID:", indicator.accountId);
  29. System.Console.WriteLine("{0,-50}{1}", "累计收益率:", indicator.pnlRatio);
  30. System.Console.WriteLine("{0,-50}{1}", "年化收益率:", indicator.pnlRatioAnnual);
  31. System.Console.WriteLine("{0,-50}{1}", "夏普比率:", indicator.sharpRatio);
  32. System.Console.WriteLine("{0,-50}{1}", "最大回撤:", indicator.maxDrawdown);
  33. System.Console.WriteLine("{0,-50}{1}", "风险比率:", indicator.riskRatio);
  34. System.Console.WriteLine("{0,-50}{1}", "开仓次数:", indicator.openCount);
  35. System.Console.WriteLine("{0,-50}{1}", "平仓次数:", indicator.closeCount);
  36. System.Console.WriteLine("{0,-50}{1}", "盈利次数:", indicator.winCount);
  37. System.Console.WriteLine("{0,-50}{1}", "亏损次数:", indicator.loseCount);
  38. System.Console.WriteLine("{0,-50}{1}", "胜率:", indicator.winRatio);
  39. System.Console.WriteLine("{0,-50}{1}", "指标创建时间:", indicator.createdAt);
  40. System.Console.WriteLine("{0,-50}{1}", "指标变更时间:", indicator.updatedAt);
  41. }
  42. }
  43. class Program
  44. {
  45. static void Main(string[] args)
  46. {
  47. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
  48. s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
  49. s.Run();
  50. System.Console.WriteLine("回测完成!");
  51. System.Console.Read();
  52. }
  53. }
  54. }

显示指定交易账号

  1. //////////////////////////////////////////////////////////////////////////
  2. //显示指定交易账号
  3. //策略描述:
  4. //下单时指定交易账号,account参数传账号id或者账号标题
  5. using GMSDK;
  6. namespace example_defaccount
  7. {
  8. public class MyStrategy : Strategy
  9. {
  10. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  11. //重写OnInit事件,进行策略开发
  12. public override void OnInit()
  13. {
  14. System.Console.WriteLine("OnInit");
  15. Subscribe("SHSE.600000,SZSE.000001", "1d");
  16. return;
  17. }
  18. //重写OnBar事件
  19. public override void OnBar(Bar bar)
  20. {
  21. //不指定account 使用默认账户下单
  22. OrderVolume(bar.symbol, 200, OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0, ba8785aa-8641-11e8-98cb-7085c223669d);
  23. }
  24. //回测完成后收到绩效报告
  25. public override void OnBacktestFinished(Indicator indicator)
  26. {
  27. System.Console.WriteLine("OnIndicator:");
  28. System.Console.WriteLine("{0,-50}{1}", "账号ID:", indicator.accountId);
  29. System.Console.WriteLine("{0,-50}{1}", "累计收益率:", indicator.pnlRatio);
  30. System.Console.WriteLine("{0,-50}{1}", "年化收益率:", indicator.pnlRatioAnnual);
  31. System.Console.WriteLine("{0,-50}{1}", "夏普比率:", indicator.sharpRatio);
  32. System.Console.WriteLine("{0,-50}{1}", "最大回撤:", indicator.maxDrawdown);
  33. System.Console.WriteLine("{0,-50}{1}", "风险比率:", indicator.riskRatio);
  34. System.Console.WriteLine("{0,-50}{1}", "开仓次数:", indicator.openCount);
  35. System.Console.WriteLine("{0,-50}{1}", "平仓次数:", indicator.closeCount);
  36. System.Console.WriteLine("{0,-50}{1}", "盈利次数:", indicator.winCount);
  37. System.Console.WriteLine("{0,-50}{1}", "亏损次数:", indicator.loseCount);
  38. System.Console.WriteLine("{0,-50}{1}", "胜率:", indicator.winRatio);
  39. System.Console.WriteLine("{0,-50}{1}", "指标创建时间:", indicator.createdAt);
  40. System.Console.WriteLine("{0,-50}{1}", "指标变更时间:", indicator.updatedAt);
  41. }
  42. }
  43. class Program
  44. {
  45. static void Main(string[] args)
  46. {
  47. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
  48. s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
  49. s.Run();
  50. System.Console.WriteLine("回测完成!");
  51. System.Console.Read();
  52. }
  53. }
  54. }

模式选择

  1. //////////////////////////////////////////////////////////////////////////
  2. //模式选择
  3. //策略描述:
  4. //策略支持两种运行模式,实时模式和回测模式,用户需要在运行策略时选择模式,实例化策略参数mode=MODE_BACKTEST 表示回测模式,mode=MODE_LIVE表示实时模式
  5. using GMSDK;
  6. namespace example
  7. {
  8. public class MyStrategy : Strategy
  9. {
  10. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  11. //重写OnInit事件,进行策略开发
  12. public override void OnInit()
  13. {
  14. System.Console.WriteLine("OnInit");
  15. //订阅行情数据
  16. Subscribe("SHSE.600000", "tick");
  17. return;
  18. }
  19. public override void OnTick(Tick tick)
  20. {
  21. System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
  22. System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
  23. System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
  24. System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
  25. System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
  26. System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
  27. System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
  28. System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
  29. System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
  30. System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
  31. System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
  32. System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
  33. System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
  34. System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
  35. System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
  36. System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
  37. }
  38. }
  39. class Program
  40. {
  41. static void Main(string[] args)
  42. {
  43. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_LIVE);
  44. s.Run();
  45. System.Console.Read();
  46. }
  47. }
  48. }

数据研究

  1. //////////////////////////////////////////////////////////////////////////
  2. //数据研究
  3. //策略描述:
  4. //无需实时数据驱动策略,无需交易下单,只是取数据的场景
  5. using GMSDK;
  6. namespace example_datares
  7. {
  8. public class MyStrategy : Strategy
  9. {
  10. public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
  11. //重写OnInit事件,进行策略开发
  12. public override void OnInit()
  13. {
  14. System.Console.WriteLine("OnInit");
  15. GMDataList<Tick> ht = GMApi.HistoryTicks("SZSE.000002", "2017-07-11 10:20:00", "2017-07-11 10:30:00");
  16. if (ht.status == 0)
  17. {
  18. foreach (var tick in ht.data)
  19. {
  20. System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
  21. System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
  22. System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
  23. System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
  24. System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
  25. System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
  26. System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
  27. System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
  28. System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
  29. System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
  30. System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
  31. System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
  32. System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
  33. System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
  34. System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
  35. System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
  36. }
  37. }
  38. return;
  39. }
  40. }
  41. class Program
  42. {
  43. static void Main(string[] args)
  44. {
  45. MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_BACKTEST);
  46. s.SetBacktestConfig("2017-07-25 8:20:00", "2018-07-25 17:30:00");
  47. s.Run();
  48. System.Console.Read();
  49. }
  50. }
  51. }
0 篇笔记