文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
快速开始
指引
快速新建策略
下载掘金 3 终端
打开终端后,登陆掘金账号点击研究策略,新建策略 或者点击右上角新建策略
新建一个典型默认账户交易策略 新建 C#的默认账户交易策略
编译策略
- 打开新建策略文件目录 策略文件目录内容可以拷贝到本地其他盘符也可以进行编译生成
- 打开工程文件 sln 文件 需要用 visual studio 打开工程文件 (
注意:visual studio 2013 及以下版本需安装.net framework 4.5.2
) - 编写策略 打开 Program.cs 文件,可进行策略编辑 编译并运行策略
- 查看运行结果 掘金客户端中关闭新建策略窗口并打开回测结果列表 查看回测结果 回测相关数据指标
获取 SDK
通过网页下载 sdk安装包: C# SDK (opens new window)
或者通过 NuGet 方式安装 SDK
- 方式一:visual studio
- 项目->管理 NuGet 程序包->浏览
- 搜索 gmsdk-net,
32位
选择gmsdk-net-x86,64位
选择gmsdk-net-x64 - 选择最新版,安装
方式二:程序包管理器控制台
32 位
PM> Install-Package gmsdk-net-x86
64位
PM> Install-Package gmsdk-net-x64
注意: 根据策略选择32位或64位版本
切勿混装
建立我们第一个策略
打开Visual Studio新建空白工程并新建源码文件
工程中引用 gmsdk-net-xxx.dll
引入命名空间:GMSDK
using GMSDK;
策略应该是这样的
- 继承策略基类
- 重改关注事件
- 在 OnInit 里订阅行情,初始化
- 在 main 方法中实例化一个派生类对像
- 设置 token,策略 id,和 mode
- 开始运行
继承策略基类
public class MyStrategy: Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) {}
}
重改关注事件
public class MyStrategy: Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) {}
//重写OnInit事件,进行策略开发
public override void OnInit()
{
Console.WriteLine("OnInit");
return;
}
}
在 OnInit 里订阅行情,初始化
class MyStrategy :public Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) {}
//重写OnInit事件,进行策略开发
public override void OnInit()
{
Console.WriteLine("OnInit");
Subscribe("SHSE.600000", "tick");
return;
}
}
在 main 里实例化一个派生类对像
- 获取 token:打开客户端->点击右上角用户头像 -> 系统设置 -> 复制 token
- 获取策略 id:打开客户端->策略研究->右上角新建策略->新建 C#策略->复制策略 ID
- 策略模式:
- MODE_LIVE
- MODE_BACKTEST
MyStrategy s("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_LIVE);
开始运行
s.Run();
订阅行情策略示例
源文件
using GMSDK;
namespace example
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
//订阅行情数据
Subscribe("SHSE.600000", "tick");
return;
}
public override void OnTick(Tick tick)
{
System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2017-07-25 8:20:00", "2018-07-25 17:30:00");
s.Run();
System.Console.WriteLine("回测完成!");
System.Console.Read();
}
}
}
典型场景
空策略
//////////////////////////////////////////////////////////////////////////
//空策略
using GMSDK;
namespace example
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
//订阅行情数据
Subscribe("SHSE.600000", "tick");
return;
}
public override void OnTick(Tick tick)
{
System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2017-07-25 8:20:00", "2018-07-25 17:30:00");
s.Run();
System.Console.WriteLine("回测完成!");
System.Console.Read();
}
}
}
定时任务
//////////////////////////////////////////////////////////////////////////
//定时任务
//策略描述:
典型如选股交易。比如,策略每日收盘前10分钟执行:选股->决策逻辑->交易->退出。可能无需订阅实时数据。
using GMSDK;
namespace example_schedule
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
//设置定时任务
Schedule("1d", "13:24:00");
return;
}
//定时任务触发事件
public override void OnSchedule(string data_rule, string timeRule)
{
//购买200股浦发银行股票
GMData<Order> o = OrderValue("SHSE.600000", 200, OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0);
if (o.status == 0) //该判断仅表示函数调用无异常
{
//
}
}
//回测完成后收到绩效报告
public override void OnBacktestFinished(Indicator indicator)
{
System.Console.WriteLine("OnBacktestFinished:");
System.Console.WriteLine("{0,-50}{1}", "账号ID:", indicator.accountId);
System.Console.WriteLine("{0,-50}{1}", "累计收益率:", indicator.pnlRatio);
System.Console.WriteLine("{0,-50}{1}", "年化收益率:", indicator.pnlRatioAnnual);
System.Console.WriteLine("{0,-50}{1}", "夏普比率:", indicator.sharpRatio);
System.Console.WriteLine("{0,-50}{1}", "最大回撤:", indicator.maxDrawdown);
System.Console.WriteLine("{0,-50}{1}", "风险比率:", indicator.riskRatio);
System.Console.WriteLine("{0,-50}{1}", "开仓次数:", indicator.openCount);
System.Console.WriteLine("{0,-50}{1}", "平仓次数:", indicator.closeCount);
System.Console.WriteLine("{0,-50}{1}", "盈利次数:", indicator.winCount);
System.Console.WriteLine("{0,-50}{1}", "亏损次数:", indicator.loseCount);
System.Console.WriteLine("{0,-50}{1}", "胜率:", indicator.winRatio);
System.Console.WriteLine("{0,-50}{1}", "指标创建时间:", indicator.createdAt);
System.Console.WriteLine("{0,-50}{1}", "指标变更时间:", indicator.updatedAt);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
s.Run();
System.Console.WriteLine("回测完成!");
System.Console.Read();
}
}
}
数据事件驱动
//////////////////////////////////////////////////////////////////////////
//数据事件驱动
//策略描述:
//典型如选股交易策略。比如,策略每日收盘前10分钟执行:选股->决策逻辑->交易->退出。可能无需订阅实时数据
using GMSDK;
namespace example_dataevent
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件, 进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
//订阅浦发银行,bar频率为一天
Subscribe("SHSE.600000", "1d");
return;
}
//重写OnBar事件
public override void OnBar(Bar bar)
{
System.Console.WriteLine("OnBar:");
System.Console.WriteLine("{0, -50}{1}", "代码:", bar.symbol);
System.Console.WriteLine("{0, -50}{1}", "bar的开始时间:", bar.bob);
System.Console.WriteLine("{0, -50}{1}", "bar的结束时间:", bar.eob);
System.Console.WriteLine("{0, -50}{1}", "开盘价:", bar.open);
System.Console.WriteLine("{0, -50}{1}", "收盘价:", bar.close);
System.Console.WriteLine("{0, -50}{1}", "最高价:", bar.high);
System.Console.WriteLine("{0, -50}{1}", "最低价:", bar.low);
System.Console.WriteLine("{0, -50}{1}", "成交量:", bar.volume);
System.Console.WriteLine("{0, -50}{1}", "成交金额:", bar.amount);
System.Console.WriteLine("{0, -50}{1}", "前收盘价:", bar.preClose);
System.Console.WriteLine("{0, -50}{1}", "持仓量:", bar.position);
System.Console.WriteLine("{0, -50}{1}", "bar频度:", bar.frequency);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
s.Run();
System.Console.WriteLine("回测完成!");
System.Console.Read();
}
}
}
默认交易账号
//////////////////////////////////////////////////////////////////////////
//默认账号交易
//策略描述:
//默认账号进行交易,下单时不指定account
using GMSDK;
namespace example_defaccount
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
Subscribe("SHSE.600000,SZSE.000001", "1d");
return;
}
//重写OnBar事件
public override void OnBar(Bar bar)
{
//不指定account 使用默认账户下单
OrderVolume(bar.symbol, 200, OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0);
}
//回测完成后收到绩效报告
public override void OnBacktestFinished(Indicator indicator)
{
System.Console.WriteLine("OnBacktestFinished:");
System.Console.WriteLine("{0,-50}{1}", "账号ID:", indicator.accountId);
System.Console.WriteLine("{0,-50}{1}", "累计收益率:", indicator.pnlRatio);
System.Console.WriteLine("{0,-50}{1}", "年化收益率:", indicator.pnlRatioAnnual);
System.Console.WriteLine("{0,-50}{1}", "夏普比率:", indicator.sharpRatio);
System.Console.WriteLine("{0,-50}{1}", "最大回撤:", indicator.maxDrawdown);
System.Console.WriteLine("{0,-50}{1}", "风险比率:", indicator.riskRatio);
System.Console.WriteLine("{0,-50}{1}", "开仓次数:", indicator.openCount);
System.Console.WriteLine("{0,-50}{1}", "平仓次数:", indicator.closeCount);
System.Console.WriteLine("{0,-50}{1}", "盈利次数:", indicator.winCount);
System.Console.WriteLine("{0,-50}{1}", "亏损次数:", indicator.loseCount);
System.Console.WriteLine("{0,-50}{1}", "胜率:", indicator.winRatio);
System.Console.WriteLine("{0,-50}{1}", "指标创建时间:", indicator.createdAt);
System.Console.WriteLine("{0,-50}{1}", "指标变更时间:", indicator.updatedAt);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
s.Run();
System.Console.WriteLine("回测完成!");
System.Console.Read();
}
}
}
显示指定交易账号
//////////////////////////////////////////////////////////////////////////
//显示指定交易账号
//策略描述:
//下单时指定交易账号,account参数传账号id或者账号标题
using GMSDK;
namespace example_defaccount
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
Subscribe("SHSE.600000,SZSE.000001", "1d");
return;
}
//重写OnBar事件
public override void OnBar(Bar bar)
{
//不指定account 使用默认账户下单
OrderVolume(bar.symbol, 200, OrderSide.OrderSide_Buy, OrderType.OrderType_Market, PositionEffect.PositionEffect_Open, 0, ba8785aa-8641-11e8-98cb-7085c223669d);
}
//回测完成后收到绩效报告
public override void OnBacktestFinished(Indicator indicator)
{
System.Console.WriteLine("OnIndicator:");
System.Console.WriteLine("{0,-50}{1}", "账号ID:", indicator.accountId);
System.Console.WriteLine("{0,-50}{1}", "累计收益率:", indicator.pnlRatio);
System.Console.WriteLine("{0,-50}{1}", "年化收益率:", indicator.pnlRatioAnnual);
System.Console.WriteLine("{0,-50}{1}", "夏普比率:", indicator.sharpRatio);
System.Console.WriteLine("{0,-50}{1}", "最大回撤:", indicator.maxDrawdown);
System.Console.WriteLine("{0,-50}{1}", "风险比率:", indicator.riskRatio);
System.Console.WriteLine("{0,-50}{1}", "开仓次数:", indicator.openCount);
System.Console.WriteLine("{0,-50}{1}", "平仓次数:", indicator.closeCount);
System.Console.WriteLine("{0,-50}{1}", "盈利次数:", indicator.winCount);
System.Console.WriteLine("{0,-50}{1}", "亏损次数:", indicator.loseCount);
System.Console.WriteLine("{0,-50}{1}", "胜率:", indicator.winRatio);
System.Console.WriteLine("{0,-50}{1}", "指标创建时间:", indicator.createdAt);
System.Console.WriteLine("{0,-50}{1}", "指标变更时间:", indicator.updatedAt);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097 - 8b27 - 11e8 - b6af - 94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2016-07-11 17:20:00", "2017-07-11 17:30:00");
s.Run();
System.Console.WriteLine("回测完成!");
System.Console.Read();
}
}
}
模式选择
//////////////////////////////////////////////////////////////////////////
//模式选择
//策略描述:
//策略支持两种运行模式,实时模式和回测模式,用户需要在运行策略时选择模式,实例化策略参数mode=MODE_BACKTEST 表示回测模式,mode=MODE_LIVE表示实时模式
using GMSDK;
namespace example
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
//订阅行情数据
Subscribe("SHSE.600000", "tick");
return;
}
public override void OnTick(Tick tick)
{
System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_LIVE);
s.Run();
System.Console.Read();
}
}
}
数据研究
//////////////////////////////////////////////////////////////////////////
//数据研究
//策略描述:
//无需实时数据驱动策略,无需交易下单,只是取数据的场景
using GMSDK;
namespace example_datares
{
public class MyStrategy : Strategy
{
public MyStrategy(string token, string strategyId, StrategyMode mode) : base(token, strategyId, mode) { }
//重写OnInit事件,进行策略开发
public override void OnInit()
{
System.Console.WriteLine("OnInit");
GMDataList<Tick> ht = GMApi.HistoryTicks("SZSE.000002", "2017-07-11 10:20:00", "2017-07-11 10:30:00");
if (ht.status == 0)
{
foreach (var tick in ht.data)
{
System.Console.WriteLine("{0,-50}{1}", "代码", tick.symbol);
System.Console.WriteLine("{0,-50}{1}", "时间", tick.createdAt);
System.Console.WriteLine("{0,-50}{1}", "最新价", tick.price);
System.Console.WriteLine("{0,-50}{1}", "开盘价", tick.open);
System.Console.WriteLine("{0,-50}{1}", "最高价", tick.high);
System.Console.WriteLine("{0,-50}{1}", "最低价", tick.low);
System.Console.WriteLine("{0,-50}{1}", "成交总量", tick.cumVolume);
System.Console.WriteLine("{0,-50}{1}", "成交总额/最新成交额,累计值", tick.cumAmount);
System.Console.WriteLine("{0,-50}{1}", "合约持仓量(期), 累计值", tick.cumPosition);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交额", tick.lastAmount);
System.Console.WriteLine("{0,-50}{1}", "瞬时成交量", tick.lastVolume);
System.Console.WriteLine("{0,-50}{1}", "(保留)交易类型, 对应多开, 多平等类型", tick.tradeType);
System.Console.WriteLine("{0,-50}{1}", "一档委买价", tick.quotes[0].bidPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委买量", tick.quotes[0].bidVolume);
System.Console.WriteLine("{0,-50}{1}", "一档委卖价", tick.quotes[0].askPrice);
System.Console.WriteLine("{0,-50}{1}", "一档委卖量", tick.quotes[0].askVolume);
}
}
return;
}
}
class Program
{
static void Main(string[] args)
{
MyStrategy s = new MyStrategy("27cbdfd8cd9b86dea554a5612baa4a8eee51af79", "536f1097-8b27-11e8-b6af-94c69161828a", StrategyMode.MODE_BACKTEST);
s.SetBacktestConfig("2017-07-25 8:20:00", "2018-07-25 17:30:00");
s.Run();
System.Console.Read();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论