////////////////////////////////////////////////////////////////////////////////// //交易测试 static void TradeTest() { //创建 ITrade // char* path 指 xxx.exe 同级子目录中的 xxx.dll 文件 int err = -1; trade = ITradeApi.XFinApi_CreateTradeApi("XTA_W32/Api/XTP_v1.1.18.13_20180516/XFinApi.XTPTradeApi.dll", out err); if (err > 0 || trade == null) { Console.WriteLine(string.Format("* Trade XFinApiCreateError={0};", StrCreateErrors[err])); return; } //注册事件 tradeEvent = new TradeEvent(); trade.SetListener(tradeEvent); //连接服务器 OpenParams openParams = new OpenParams(); openParams.HostAddress = Cfg.TradeAddress; openParams.UserID = Cfg.UserName; openParams.Password = Cfg.Password; openParams.Configs.Add("AuthCode", Cfg.AuthCode); openParams.Configs.Add("ClientID", Cfg.ClientID);//可选 openParams.IsUTF8 = true; trade.Open(openParams); /* * //连接成功后才能执行查询、委托等操作,检测方法有两种: * 1、ITrade.IsOpened()=true * 2、TradeListener.OnNotify中 * (int)XFinApi.TradeApi.ActionKind.Open == notifyParams.ActionType && * (int)ResultKind.Success == notifyParams.ResultType */ while (!trade.IsOpened()) { Thread.Sleep(1000); } QueryParams qryParam = new QueryParams(); //查询委托单 Thread.Sleep(1000);//有些接口查询有间隔限制,如:CTP查询间隔为1秒 Console.WriteLine("Press any key to QueryOrder."); Console.ReadKey(); trade.QueryOrder(qryParam); //查询成交单 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryTradeOrder."); Console.ReadKey(); trade.QueryTradeOrder(qryParam); //查询合约 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryInstrument."); Console.ReadKey(); trade.QueryInstrument(qryParam); //查询持仓 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryPosition."); Console.ReadKey(); trade.QueryPosition(qryParam); //查询账户 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryAccount."); Console.ReadKey(); trade.QueryAccount(qryParam); //委托下单 Thread.Sleep(1000); Console.WriteLine("Press any key to OrderAction."); Console.ReadKey(); Order order = new Order(); order.ExchangeID = Cfg.MarketID; order.InstrumentID = Cfg.InstrumentID; order.Price = Cfg.SellPrice1; order.Volume = 100; order.Direction = DirectionKind.Buy; //下单高级选项,可选择性设置 order.ActionType = OrderActionKind.Insert; //下单 order.OrderType = OrderKind.Order; //标准单 order.PriceCond = PriceConditionKind.LimitPrice; //限价 trade.OrderAction(order); }
////////////////////////////////////////////////////////////////////////////////// //交易测试 static void TradeTest() { //创建 ITrade // char* path 指 xxx.exe 同级子目录中的 xxx.dll 文件 int err = -1; trade = ITradeApi.XFinApi_CreateTradeApi("XTA_W64/Api/CTP_v6.3.6_20160606/XFinApi.CTPTradeApi.dll", out err); if (err > 0 || trade == null) { Console.WriteLine(string.Format("* Trade XFinApiCreateError={0};", StrCreateErrors[err])); return; } //注册事件 tradeEvent = new TradeEvent(); trade.SetListener(tradeEvent); //连接服务器 //Cfg.SetNonTradeTime();//非交易时段 OpenParams openParams = new OpenParams(); openParams.HostAddress = Cfg.TradeAddress; openParams.BrokerID = Cfg.BrokerID; openParams.UserID = Cfg.UserName; openParams.Password = Cfg.Password; openParams.IsUTF8 = true; trade.Open(openParams); /* * //连接成功后才能执行查询、委托等操作,检测方法有两种: * 1、ITrade.IsOpened()=true * 2、TradeListener.OnNotify中 * (int)XFinApi.TradeApi.ActionKind.Open == notifyParams.ActionType && * (int)ResultKind.Success == notifyParams.ResultType */ while (!trade.IsOpened()) { Thread.Sleep(1000); } QueryParams qryParam = new QueryParams(); qryParam.InstrumentID = Cfg.InstrumentID; //查询委托单 Thread.Sleep(1000);//有些接口查询有间隔限制,如:CTP查询间隔为1秒 Console.WriteLine("Press any key to QueryOrder."); Console.ReadKey(); trade.QueryOrder(qryParam); //查询成交单 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryTradeOrder."); Console.ReadKey(); trade.QueryTradeOrder(qryParam); //查询合约 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryInstrument."); Console.ReadKey(); trade.QueryInstrument(qryParam); //查询持仓 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryPosition."); Console.ReadKey(); trade.QueryPosition(qryParam); //查询账户 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryAccount."); Console.ReadKey(); trade.QueryAccount(qryParam); //委托下单 Thread.Sleep(1000); Console.WriteLine("Press any key to OrderAction."); Console.ReadKey(); Order order = new Order(); order.InstrumentID = Cfg.InstrumentID; order.Price = Cfg.SellPrice1; order.Volume = 1; order.Direction = DirectionKind.Buy; order.OpenCloseType = OpenCloseKind.Open; //下单高级选项,可选择性设置 order.ActionType = OrderActionKind.Insert; //下单 order.OrderType = OrderKind.Order; //标准单 order.PriceCond = PriceConditionKind.LimitPrice; //限价 order.VolumeCond = VolumeConditionKind.AnyVolume; //任意数量 order.TimeCond = TimeConditionKind.GFD; //当日有效 order.ContingentCond = ContingentCondKind.Immediately; //立即 order.HedgeType = HedgeKind.Speculation; //投机 order.ExecResult = ExecResultKind.NoExec; //没有执行 trade.OrderAction(order); }
static void TradeTest() { if (!CreateAndOpenTrade6()) //CTP_v6.3.6_20160606版本 { return; } //if (!CreateAndOpenTrade13()) //CTP_v6.3.13_20181119版本 // return; //if (!CreateAndOpenTrade15()) //CTP_v6.3.15_20190220版本 // return; /* * //连接成功后才能执行查询、委托等操作,检测方法有两种: * 1、ITrade.IsOpened()=true * 2、TradeListener.OnNotify中 * (int)XFinApi.TradeApi.ActionKind.Open == notifyParams.ActionType && * (int)ResultKind.Success == notifyParams.ResultType */ while (!trade.IsOpened()) { Thread.Sleep(1000); } QueryParams qryParam = new QueryParams(); qryParam.InstrumentID = Cfg.InstrumentID; //查询委托单 Thread.Sleep(1000);//有些接口查询有间隔限制,如:CTP查询间隔为1秒 Console.WriteLine("Press any key to QueryOrder."); Console.ReadKey(); trade.QueryOrder(qryParam); //查询成交单 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryTradeOrder."); Console.ReadKey(); trade.QueryTradeOrder(qryParam); //查询合约 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryInstrument."); Console.ReadKey(); trade.QueryInstrument(qryParam); //查询持仓 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryPosition."); Console.ReadKey(); trade.QueryPosition(qryParam); //查询账户 Thread.Sleep(3000); Console.WriteLine("Press any key to QueryAccount."); Console.ReadKey(); trade.QueryAccount(qryParam); //委托下单 Thread.Sleep(1000); Console.WriteLine("Press any key to OrderAction."); Console.ReadKey(); Order order = new Order(); order.InstrumentID = Cfg.InstrumentID; order.Price = Cfg.SellPrice1; order.Volume = 1; order.Direction = DirectionKind.Buy; order.OpenCloseType = OpenCloseKind.Open; //下单高级选项,可选择性设置 order.ActionType = OrderActionKind.Insert; //下单 order.OrderType = OrderKind.Order; //标准单 order.PriceCond = PriceConditionKind.LimitPrice; //限价 order.VolumeCond = VolumeConditionKind.AnyVolume; //任意数量 order.TimeCond = TimeConditionKind.GFD; //当日有效 order.ContingentCond = ContingentCondKind.Immediately; //立即 order.HedgeType = HedgeKind.Speculation; //投机 order.ExecResult = ExecResultKind.NoExec; //没有执行 trade.OrderAction(order); }