private void ConnectToIB() { DisposeIBClient(); logger.Info("Rebuilding IBClient"); if (client != null) client.Dispose(); client = new IBClient(); Thread.Sleep(1000); logger.Info("IBClient Rebuilt"); //client.ThrowExceptions = true; int retryCt = 5; while (!client.Connected) { if ((retryCt--) <= 0) { logger.Info("Tried to reconnect 5 times going to try re-creating the client..."); return; } if (terminateRequested) ShutdownRecorder(); try { //int clientID = (new Random()).Next(0, 2000); logger.Info("Connecting to TWS Interactive brokers with host "+ settings.IBHostToConnectTo + " on port " + settings.IBPortToConnectTo + "..." +"(Try " + (4-retryCt) + " of 5)" ); client.Connect(settings.IBHostToConnectTo, settings.IBPortToConnectTo, 1); logger.Info("Connection initiated, requesting data"); Thread.Sleep(2000); client.RequestIds(1); Thread.Sleep(2000); } catch (Exception ex) { logger.Info("IB Connecting Exception: " + ex.Message); if (terminateRequested) // changed: 2004-1-28 ShutdownRecorder(); Thread.Sleep(3000); } } logger.Info("TWS Client Connected is now true."); Thread.Sleep(2000); logger.Info("Connected to TWS server version {0} at {1}.", client.ServerVersion, client.TwsConnectionTime); client.TickPrice += client_TickPrice; client.TickSize += client_TickSize; client.Error += client_Error; //client.UpdateMarketDepth += new EventHandler<UpdateMarketDepthEventArgs>(client_UpdateMarketDepth); //client.UpdateMarketDepthL2 += new EventHandler<UpdateMarketDepthL2EventArgs>(client_UpdateMarketDepthL2); //// client.TickString += new EventHandler<TickStringEventArgs>(client_TickString); // nothing of value that I see //client.TickGeneric += new EventHandler<TickGenericEventArgs>(client_TickGeneric); //client.TickEfp += new EventHandler<TickEfpEventArgs>(client_TickEfp); //client.FundamentalData += new EventHandler<FundamentalDetailsEventArgs>(client_FundamentalData); //client.ContractDetails += new EventHandler<ContractDetailsEventArgs>(client_ContractDetails); //client.RequestNewsBulletins(false); /////////// Register the list of symbols for TWS /////////// string listToEcho = ""; for (int s = 0; s < symbols.Count(); s++) { string name = symbols[s].Symbol.Trim(); Contract item = new Contract(name, symbols[s].Market, symbols[s].securityType, "USD"); client.RequestMarketData(symbols[s].SymbolID, item, null, false, false); // Examples.. //axTws1.reqMktData(curID++, symbols[s], "STK", "", 0, "", "", "SMART", "ISLAND", "USD", "", 0); //client.RequestMarketDepth(symbol.SymbolID, item, 10); //client.RequestContractDetails(symbol.SymbolID, item); //client.RequestFundamentalData(symbol.SymbolID, item, "Estimates"); //client.RequestFundamentalData(symbol.SymbolID, item, "Financial Statements"); //client.RequestFundamentalData(symbol.SymbolID, item, "Summary"); listToEcho += ", " + name; } logger.Debug("Symbols: {0}", listToEcho); //(NowString() + ": Symbols: " + listToEcho).Log(); }
/// <summary> /// The main entry point for the application. /// </summary> //[STAThread] static void Main(string[] args) { client = new IBClient(); client.ThrowExceptions = true; client.TickPrice += client_TickPrice; client.TickSize += client_TickSize; client.Error += client_Error; client.NextValidId += client_NextValidId; client.UpdateMarketDepth += client_UpdateMktDepth; client.RealTimeBar += client_RealTimeBar; client.OrderStatus += client_OrderStatus; client.ExecDetails += client_ExecDetails; Console.WriteLine("Connecting to IB."); client.Connect("127.0.0.1", 7496, 0); TF = new Contract("TF", "NYBOT", SecurityType.Future, "USD", "200909"); YmEcbot = new Contract("YM", "ECBOT", SecurityType.Future, "USD", "200909"); ES = new Contract("ES", "GLOBEX", SecurityType.Future, "USD", "200909"); SPY = new Contract("SPY", "GLOBEX", SecurityType.Future, "USD", "200909"); ZN = new Contract("ZN", "ECBOT", SecurityType.Future, "USD", "200909"); ZB = new Contract("ZB", "ECBOT", SecurityType.Future, "USD", "200909"); ZT = new Contract("ZT", "ECBOT", SecurityType.Future, "USD", "200909"); AAPL = new Contract("ZF", "ECBOT", SecurityType.Future, "USD", "200909"); //TickNasdaq = new Contract("TICK-NASD", "NASDAQ", SecurityType.Index, "USD"); //VolNasdaq = new Contract("VOL-NASD", "NASDAQ", SecurityType.Index, "USD"); //AdNasdaq = new Contract("AD-NASD", "NASDAQ", SecurityType.Index, "USD"); //TickNyse = new Contract("TICK-NYSE", "NYSE", SecurityType.Index, "USD"); //VolNyse = new Contract("VOL-NYSE", "NYSE", SecurityType.Index, "USD"); //AdNyse = new Contract("AD-NYSE", "NYSE", SecurityType.Index, "USD"); //New Contract Creation Features Equity Google = new Equity("GOOG"); client.RequestMarketData(14, Google, null, false, false); client.RequestMarketDepth(15, Google, 5); client.RequestRealTimeBars(16, Google, 5, RealTimeBarType.Trades, false); Order BuyContract = new Order(); BuyContract.Action = ActionSide.Sell; BuyContract.OutsideRth = false; BuyContract.LimitPrice = 560; BuyContract.OrderType = OrderType.Market; BuyContract.TotalQuantity = 1; BuyContract.AuxPrice = 560; client.PlaceOrder(NextOrderId, Google, BuyContract); client.RequestIds(1); client.RequestExecutions(34, new ExecutionFilter()); client.RequestAllOpenOrders(); //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); while (true) { Thread.Sleep(100); } }
private void ConnectToIB() { DisposeIBClient(); logger.Info("Rebuilding IBClient"); if (client != null) { client.Dispose(); } client = new IBClient(); Thread.Sleep(1000); logger.Info("IBClient Rebuilt"); //client.ThrowExceptions = true; int retryCt = 5; while (!client.Connected) { if ((retryCt--) <= 0) { logger.Info("Tried to reconnect 5 times going to try re-creating the client..."); return; } if (terminateRequested) { ShutdownRecorder(); } try { //int clientID = (new Random()).Next(0, 2000); logger.Info("Connecting to TWS Interactive brokers on port 7496..." + "(Try " + (4 - retryCt) + " of 5)"); client.Connect(settings.IBHostToConnectTo, settings.IBPortToConnectTo, 1); logger.Info("Connection initiated, requesting data"); Thread.Sleep(2000); client.RequestIds(1); Thread.Sleep(2000); } catch (Exception ex) { logger.Info("IB Connecting Exception: " + ex.Message); if (terminateRequested) // changed: 2004-1-28 { ShutdownRecorder(); } Thread.Sleep(3000); } } logger.Info("TWS Client Connected is now true."); Thread.Sleep(2000); logger.Info("Connected to TWS server version {0} at {1}.", client.ServerVersion, client.TwsConnectionTime); client.TickPrice += client_TickPrice; client.TickSize += client_TickSize; client.Error += client_Error; //client.UpdateMarketDepth += new EventHandler<UpdateMarketDepthEventArgs>(client_UpdateMarketDepth); //client.UpdateMarketDepthL2 += new EventHandler<UpdateMarketDepthL2EventArgs>(client_UpdateMarketDepthL2); //// client.TickString += new EventHandler<TickStringEventArgs>(client_TickString); // nothing of value that I see //client.TickGeneric += new EventHandler<TickGenericEventArgs>(client_TickGeneric); //client.TickEfp += new EventHandler<TickEfpEventArgs>(client_TickEfp); //client.FundamentalData += new EventHandler<FundamentalDetailsEventArgs>(client_FundamentalData); //client.ContractDetails += new EventHandler<ContractDetailsEventArgs>(client_ContractDetails); /////////// Register the list of symbols for TWS /////////// string listToEcho = ""; for (int s = 0; s < symbols.Count(); s++) { string name = symbols[s].Symbol.Trim(); Contract item = new Contract(name, symbols[s].Market, symbols[s].securityType, "USD"); client.RequestMarketData(symbols[s].SymbolID, item, null, false, false); // Examples.. //axTws1.reqMktData(curID++, symbols[s], "STK", "", 0, "", "", "SMART", "ISLAND", "USD", "", 0); //client.RequestMarketDepth(s, item, 10); //client.RequestContractDetails(s, item); //client.RequestFundamentalData(s, item, "Estimates"); //client.RequestFundamentalData(s, item, "Financial Statements"); //client.RequestFundamentalData(s, item, "Summary"); //client.RequestNewsBulletins(false); listToEcho += ", " + name; } logger.Debug("Symbols: {0}", listToEcho); //(NowString() + ": Symbols: " + listToEcho).Log(); }