static void Main(string[] args) { //可在Config类中修改用户名、密码、合约等信息 MarketTest(); TradeTest(); Thread.Sleep(2000); Console.WriteLine("Press any key to close."); Console.ReadKey(); //关闭连接 if (market != null) { market.Close(); ITradeApi.XFinApi_ReleaseMarketApi(market);//必须释放资源 } if (trade != null) { trade.Close(); ITradeApi.XFinApi_ReleaseTradeApi(trade);//必须释放资源 } Console.WriteLine("Closed."); Console.ReadKey(); }
static bool CreateAndOpenTrade15() { //创建 ITrade // char* path 指 xxx.exe 同级子目录中的 xxx.dll 文件 int err = -1; trade = ITradeApi.XFinApi_CreateTradeApi("XTA_W64/Api/CTP_v6.3.15_20190220/XFinApi.CTPTradeApiSE.dll", out err); if (err > 0 || trade == null) { Console.WriteLine(string.Format("* Trade XFinApiCreateError={0};", StrCreateErrors[err])); return(false); } //注册事件 tradeEvent = new TradeEvent(); trade.SetListener(tradeEvent); //连接服务器 OpenParams openParams = new OpenParams(); openParams.HostAddress = CfgSE.TradeAddress; openParams.BrokerID = CfgSE.BrokerID; openParams.UserID = CfgSE.UserName; openParams.Password = CfgSE.Password; openParams.Configs.Add("AppID", CfgSE.AppID); openParams.Configs.Add("AuthCode", CfgSE.AuthCode); openParams.IsUTF8 = false; trade.Open(openParams); return(true); }
static bool CreateAndOpenMarket15() { //创建 IMarket // char* path 指 xxx.exe 同级子目录中的 xxx.dll 文件 int err = -1; market = ITradeApi.XFinApi_CreateMarketApi("XTA_W64/Api/CTP_v6.3.15_20190220/XFinApi.CTPTradeApiSE.dll", out err); if (err > 0 || market == null) { Console.WriteLine(string.Format("* Market XFinApiCreateError={0};", StrCreateErrors[err])); return(false); } //注册事件 marketEvent = new MarketEvent(); market.SetListener(marketEvent); //连接服务器 OpenParams openParams = new OpenParams(); openParams.HostAddress = CfgSE.MarketAddress; openParams.BrokerID = CfgSE.BrokerID; openParams.UserID = CfgSE.UserName; openParams.Password = CfgSE.Password; openParams.IsUTF8 = true; market.Open(openParams); return(true); }
static void PrintPositionInfo(Position pos) { long buyposition = ITradeApi.IsDefaultValue(pos.BuyPosition) ? -1 : pos.BuyPosition; long sellposition = ITradeApi.IsDefaultValue(pos.SellPosition) ? -1 : pos.SellPosition; Console.WriteLine(string.Format(" InstID={0}, BuyPosition={1:d}, SellPosition={2:d}", pos.InstrumentID, buyposition, sellposition)); }
static void PrintPositionInfo(Position pos) { long positionToday = ITradeApi.IsDefaultValue(pos.PositionToday) ? -1 : pos.PositionToday; long positionYesterday = ITradeApi.IsDefaultValue(pos.PositionYesterday) ? -1 : pos.PositionYesterday; Console.WriteLine(string.Format(" InstID={0}, Direction={1:d}, PosToday={2:d}, PosYesterday={3:d}", pos.InstrumentID, pos.Direction, positionToday, positionYesterday)); }
public void ToggleLiveApi() { if (IsLiveApi) { if (tradingApi == null) { this.tradingApi = TradingApiFactory.GetTradingApi(""); } GetAccontInfo(); } }
public void Dispose() { if (ITradeApi != null) { ITradeApi.Dispose(); } if (IMarketApi != null) { IMarketApi.Dispose(); } }
static void PrintPositionInfo(Position pos) { long buyposition = ITradeApi.IsDefaultValue(pos.BuyPosition) ? -1 : pos.BuyPosition; long sellposition = ITradeApi.IsDefaultValue(pos.SellPosition) ? -1 : pos.SellPosition; long positionNet = ITradeApi.IsDefaultValue(pos.NetPosition) ? -1 : pos.NetPosition; long positionYesterday = ITradeApi.IsDefaultValue(pos.PositionYesterday) ? -1 : pos.PositionYesterday; Console.WriteLine(string.Format(" InstID={0}, PositionYesDirection={1:d}, PositionYesterday={2:d}, BuyPosition={3:d}, SellPosition={4:d}, NetPosition={5:d}", pos.InstrumentID, pos.PositionYesDirection, positionYesterday, buyposition, sellposition, positionNet)); }
static void PrintPositionInfo(Position pos) { long positionTotal = ITradeApi.IsDefaultValue(pos.PositionTotal) ? 0 : pos.PositionTotal; long positionSellable = ITradeApi.IsDefaultValue(pos.PositionSellable) ? 0 : pos.PositionSellable; long positionYesterday = ITradeApi.IsDefaultValue(pos.PositionYesterday) ? 0 : pos.PositionYesterday; double avgPrice = ITradeApi.IsDefaultValue(pos.AvgPrice) ? 0 : pos.AvgPrice; Console.WriteLine(string.Format(" InstID={0} {1}, Direction={2:d}, PositionTotal={3:d}, PositionSellable={4:d}, PositionYesterday={5:d}, AvgPrice={6:g}", pos.ExchangeID, pos.InstrumentID, pos.Direction, positionTotal, positionSellable, positionYesterday, avgPrice)); }
static void PrintPositionInfo(Position pos) { long buyposition = ITradeApi.IsDefaultValue(pos.BuyPosition) ? -1 : pos.BuyPosition; long sellposition = ITradeApi.IsDefaultValue(pos.SellPosition) ? -1 : pos.SellPosition; long netposition = ITradeApi.IsDefaultValue(pos.NetPosition) ? -1 : pos.NetPosition; Console.WriteLine(string.Format(" InstID={0} {1} {2}, BuyPosition={3:d}, SellPosition={4:d}, NetPosition={5:d}", pos.ExchangeID, pos.ProductID, pos.InstrumentID, buyposition, sellposition, netposition )); }
////////////////////////////////////////////////////////////////////////////////// //行情测试 static void MarketTest() { //创建 IMarket // char* path 指 xxx.exe 同级子目录中的 xxx.dll 文件 int err = -1; market = ITradeApi.XFinApi_CreateMarketApi("XTA_W32/Api/XTP_v1.1.18.13_20180516/XFinApi.XTPTradeApi.dll", out err); if (err > 0 || market == null) { Console.WriteLine(string.Format("* Market XFinApiCreateError={0};", StrCreateErrors[err])); return; } //注册事件 marketEvent = new MarketEvent(); market.SetListener(marketEvent); //连接服务器 OpenParams openParams = new OpenParams(); openParams.HostAddress = Cfg.MarketAddress; openParams.UserID = Cfg.UserName; openParams.Password = Cfg.Password; openParams.Configs.Add("AuthCode", Cfg.AuthCode); openParams.Configs.Add("ClientID", Cfg.ClientID);//可选 openParams.IsUTF8 = true; market.Open(openParams); /* * 连接成功后才能执行订阅行情等操作,检测方法有两种: * 1、IMarket.IsOpened()=true * 2、MarketListener.OnNotify中 * (int)XFinApi.TradeApi.ActionKind.Open == notifyParams.ActionType && * (int)ResultKind.Success == notifyParams.ResultType */ /* 行情相关方法 * while (!market.IsOpened()) * Thread.Sleep(1000); * * //订阅行情,已在MarketEvent.OnNotify中订阅 * XFinApi.QueryParams param = new XFinApi.QueryParams(); * param.ExchangeID = Cfg.ExchangeID; * param.InstrumentID = Cfg.InstrumentID; * market.Subscribe(param); * * //取消订阅行情 * market.Unsubscribe(param); */ }
static void PrintPositionInfo(Position pos) { long buyposition = ITradeApi.IsDefaultValue(pos.BuyPosition) ? -1 : pos.BuyPosition; long sellposition = ITradeApi.IsDefaultValue(pos.SellPosition) ? -1 : pos.SellPosition; long positionToday = ITradeApi.IsDefaultValue(pos.PositionToday) ? -1 : pos.PositionToday; long positionYesterday = ITradeApi.IsDefaultValue(pos.PositionYesterday) ? -1 : pos.PositionYesterday; Console.WriteLine(string.Format(" InstID={0}, Direction={1:d}, Price={2:g}, PosToday={3:d}, PosYesterday={4:d}, BuyPosition={5:d}, SellPosition={6:d}", pos.InstrumentID, pos.Direction, pos.PreSettlementPrice, positionToday, positionYesterday, buyposition, sellposition)); }
public void TurnAlgoOn() { try { this.tradingStrategy = TradingStrategyFactory.GetTradingStrategy("RideTheMacd"); if (this.tradingApi == null) { this.tradingApi = TradingApiFactory.GetTradingApi(""); } this.simulationStrategy = TradingStrategyFactory.GetTradingStrategy("Simulation"); this.simulationApi = TradingApiFactory.GetTradingApi(""); this.IsAlgoOn = true; } catch (Exception ex) { AddError(ex, "TurnAlgoOn"); } }
public AccountStatus StopLoss(FlipperDataModel fdm, TradeDecision td, ITradeApi tradeApi) { Ticker te = fdm.ticker.FirstOrDefault(); AccountStatus stat = new AccountStatus { CurrentBalance = (fdm.stats.Volume * te.last), LastPurchasePrice = fdm.stats.LastPurchasePrice, LastSalePrice = te.last, StartingBalance = fdm.stats.StartingBalance, StatDate = DateTime.Now, Volume = 0, IsSimulator = true, TransactionType = TransactionType.Sell, Source = TransactionSource.Simulator, LastOrderDate = DateTime.Now }; TickerDAL.InsertTransaction(stat); return(stat); }
private void OnUserLogin(object sender, RspEventArgs e) { switch (sender.ToString()) { case "ProgramTradeApi.XTradeSpi": if (e.ErrorID == 0) { IsTradeLogined = true; ITradeApi.RequestUserPosition(); ITradeApi.RequestQueryOrders(); } else { IsTradeLogined = false; } eventUserLogined?.Invoke(sender, e); break; case "ProgramTradeApi.XMduserSpi": if (e.ErrorID == 0) { IsMarketLogined = true; //HashSet<string> instruments = new HashSet<string>() { "a1607", "a1609", "a1611" }; HashSet <string> instruments = new HashSet <string>() { "*" }; IMarketApi.SubMarketData(instruments); } else { IsMarketLogined = false; } eventUserLogined?.Invoke(sender, e); break; } }
////////////////////////////////////////////////////////////////////////////////// //交易测试 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() { //创建 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); }