/// <summary>
        /// Market data request message
        /// </summary>
        public bool SubscribeTickData(Subscribe request)
        {
            try
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info("Sending market data request for: " + request.Security.Symbol, _type.FullName,
                                "SubscribeTickData");
                }

                _nextValidId++;

                // Update IDs Map
                _idsMap.Add(request.Id, _nextValidId);

                // Add Value to the Internal Ticks Map
                _tickList.Add(_nextValidId, new Tick {
                    Security = request.Security, MarketDataProvider = _marketDataProviderName
                });

                var list = new Collection <GenericTickType> {
                    GenericTickType.MarkPrice
                };

                _ibClient.RequestMarketData(Convert.ToInt32(_nextValidId), new Equity(request.Security.Symbol), list, snapshot: false, marketDataOff: false);
                _ibClient.RequestMarketDepth(Convert.ToInt32(_nextValidId), new Equity(request.Security.Symbol), 1);

                return(true);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "SubscribeTickData");
                return(false);
            }
        }
        public void StartSymbol(Receiver receiver, SymbolInfo symbol, StartSymbolDetail detail)
        {
            if (debug)
            {
                log.Debug("StartSymbol " + symbol + ", " + detail.LastTime);
            }
//            Equity equity = new Equity(symbol.Symbol);
//			Contract contract = new Contract(symbol.Symbol,"SMART",SecurityType.Stock,"USD");
//            Contract contract = new Contract(symbol.Symbol,"GLOBEX",SecurityType.Future,"USD","201006");
            Contract      contract = SymbolToContract(symbol);
            SymbolHandler handler  = GetSymbolHandler(symbol, receiver);

//            tickerid.Add((int)symbol.BinaryIdentifier);
//            client.RequestMarketData((int)symbol.BinaryIdentifier, contract, null, false, false);
            client.RequestMarketData((int)symbol.BinaryIdentifier, contract, null, false, false);
            receiver.OnEvent(symbol, (int)EventType.StartRealTime, null);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            client = new IBClient {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;
            client.CommissionReport += client_CommissionReport;

            Console.WriteLine("Connecting to IB.");
            client.Connect("127.0.0.1", 7496, 10);

            //TF = new Future("TF", "NYBOT", "USD", "200909");
            //YmEcbot = new Future("YM", "ECBOT", "USD", "200909");
            //ES = new Future("ES", "GLOBEX", "USD", "200909");
            //SPY = new Future("SPY", "GLOBEX", "USD", "200909");
            //ZN = new Future("ZN", "ECBOT", "USD", "200909");
            //ZB = new Future("ZB", "ECBOT", "USD", "200909");
            //ZT = new Future("ZT", "ECBOT", "USD", "200909");
            //ZF = new Future("ZF", "ECBOT", "USD", "200909");

            //TickNasdaq = new Index("TICK-NASD", "NASDAQ");
            //VolNasdaq = new Index("VOL-NASD", "NASDAQ");
            //AdNasdaq = new Index("AD-NASD", "NASDAQ");
            //TickNyse = new Index("TICK-NYSE", "NYSE");
            //VolNyse = new Index("VOL-NYSE", "NYSE");
            //AdNyse = new Index("AD-NYSE", "NYSE");

            //New Contract Creation Features
            //var Google = new Equity("GOOG");

            //Forex Test
            var EUR = new Forex("EUR", "USD");

            //Order BuyContract = KrsOrderFactory.CreateOrder(ActionSide.Buy, OrderType.Limit, 560, 0, 1, false, 0);
            //BuyContract.OutsideRth = false;
            //BuyContract.LimitPrice = 560;
            //BuyContract.OrderType = OrderType.Limit;
            //BuyContract.TotalQuantity = 1;
            //client.PlaceOrder(503, TF, BuyContract);

            client.RequestExecutions(NextOrderId++, new KrsExecutionFilter());
            client.RequestAllOpenOrders();

            client.RequestMarketData(NextOrderId++, EUR, null, false, false);
            client.RequestMarketDepth(NextOrderId++, EUR, 5);
            client.RequestRealTimeBars(NextOrderId++, EUR, 5, RealTimeBarType.Midpoint, false);
            //client.RequestMarketData(NextOrderId++, EUR, null, false, false);

            while(true)
            {
                Thread.Sleep(100);
            }
        }
Exemple #4
0
        /// <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);
            }
        }
Exemple #5
0
        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");
            ZF      = 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");

            //Forex Test
            Forex EUR = new Forex("EUR", "USD");

            client.RequestMarketData(14, Google, null, false, false);
            client.RequestMarketDepth(15, Google, 5);
            client.RequestRealTimeBars(16, Google, 5, RealTimeBarType.Trades, false);
            client.RequestMarketData(17, EUR, null, false, false);

            Order BuyContract = new Order();

            BuyContract.Action        = ActionSide.Buy;
            BuyContract.OutsideRth    = false;
            BuyContract.LimitPrice    = 560;
            BuyContract.OrderType     = OrderType.Limit;
            BuyContract.TotalQuantity = 1;
            //client.PlaceOrder(503, TF, BuyContract);

            client.RequestExecutions(34, new ExecutionFilter());

            client.RequestAllOpenOrders();

            while (true)
            {
                Thread.Sleep(100);
            }
        }
Exemple #6
0
        static void Main(string[] args)
        {
            client = new IBClient {
                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;
            client.CommissionReport  += client_CommissionReport;

            Console.WriteLine("Connecting to IB.");
            client.Connect("127.0.0.1", 7496, 10);

            //TF = new Future("TF", "NYBOT", "USD", "200909");
            //YmEcbot = new Future("YM", "ECBOT", "USD", "200909");
            //ES = new Future("ES", "GLOBEX", "USD", "200909");
            //SPY = new Future("SPY", "GLOBEX", "USD", "200909");
            //ZN = new Future("ZN", "ECBOT", "USD", "200909");
            //ZB = new Future("ZB", "ECBOT", "USD", "200909");
            //ZT = new Future("ZT", "ECBOT", "USD", "200909");
            //ZF = new Future("ZF", "ECBOT", "USD", "200909");

            //TickNasdaq = new Index("TICK-NASD", "NASDAQ");
            //VolNasdaq = new Index("VOL-NASD", "NASDAQ");
            //AdNasdaq = new Index("AD-NASD", "NASDAQ");
            //TickNyse = new Index("TICK-NYSE", "NYSE");
            //VolNyse = new Index("VOL-NYSE", "NYSE");
            //AdNyse = new Index("AD-NYSE", "NYSE");

            //New Contract Creation Features
            //var Google = new Equity("GOOG");

            //Forex Test
            var EUR = new Forex("EUR", "USD");

            //Order BuyContract = KrsOrderFactory.CreateOrder(ActionSide.Buy, OrderType.Limit, 560, 0, 1, false, 0);
            //BuyContract.OutsideRth = false;
            //BuyContract.LimitPrice = 560;
            //BuyContract.OrderType = OrderType.Limit;
            //BuyContract.TotalQuantity = 1;
            //client.PlaceOrder(503, TF, BuyContract);

            client.RequestExecutions(NextOrderId++, new KrsExecutionFilter());
            client.RequestAllOpenOrders();

            client.RequestMarketData(NextOrderId++, EUR, null, false, false);
            client.RequestMarketDepth(NextOrderId++, EUR, 5);
            client.RequestRealTimeBars(NextOrderId++, EUR, 5, RealTimeBarType.Midpoint, false);
            //client.RequestMarketData(NextOrderId++, EUR, null, false, false);

            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 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();
        }
        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();
        }
Exemple #9
0
        public List <TickData> RequestQuote(string symbol, string exchange, string primary_exchange, SecurityType type, string currency, List <ContractDetails> details)
        {
            // make sure we are connected
            if (!Connect)
            {
                Connect = true;
                if (!Connect)
                {
                    return(null);
                }
            }

            // create returned list
            List <TickData> list = new List <TickData>();

            // get quotes count
            int count = 1;

            if (details != null && details.Count > 1)
            {
                count = details.Count;
            }

            for (int i = 0; i < count; i++)
            {
                // get id from id queue
                if (id_queue.Count == 0)
                {
                    break;
                }
                int id = id_queue.Dequeue();

                // get tick object
                if (!tick_list.ContainsKey(id))
                {
                    tick_list[id] = new TickData(id);
                }
                TickData t = tick_list[id];

                t.name     = symbol;
                t.symbol   = symbol;
                t.exchange = exchange;
                t.currency = currency;

                // keep request rate
                if (i % 24 == 0 && i != 0)
                {
                    Thread.Sleep(500 + tws_backoff_time / 2);
                }

                t.pending_requests.AddRange(new TickType[]
                {
                    TickType.BidPrice,
                    TickType.AskPrice,
                    TickType.BidSize,
                    TickType.AskSize,
                    TickType.Volume
                });

                if (type == SecurityType.Index)
                {
                    t.pending_requests.Add(TickType.ClosePrice);
                    t.pending_requests.Add(TickType.OpenPrice);
                    t.pending_requests.Add(TickType.HighPrice);
                    t.pending_requests.Add(TickType.LowPrice);
                    t.pending_requests.Add(TickType.LastPrice);
                    t.pending_requests.Add(TickType.LastSize);
                    t.pending_requests.Add(TickType.LastTimestamp);

                    Krs.Ats.IBNet.Contracts.Index contract = new Krs.Ats.IBNet.Contracts.Index(symbol, exchange);
                    contract.PrimaryExchange = primary_exchange;
                    contract.Currency        = currency;
                    tws.RequestMarketData(id, contract, null, false);
                }
                else if (type == SecurityType.Stock)
                {
                    t.pending_requests.Add(TickType.ClosePrice);
                    t.pending_requests.Add(TickType.OpenPrice);
                    t.pending_requests.Add(TickType.HighPrice);
                    t.pending_requests.Add(TickType.LowPrice);
                    t.pending_requests.Add(TickType.LastPrice);
                    t.pending_requests.Add(TickType.LastSize);
                    t.pending_requests.Add(TickType.LastTimestamp);

                    Krs.Ats.IBNet.Contracts.Equity contract = new Krs.Ats.IBNet.Contracts.Equity(symbol, exchange);
                    contract.PrimaryExchange = primary_exchange;
                    contract.Currency        = currency;
                    tws.RequestMarketData(id, contract, null, false);
                }
                else if (type == SecurityType.Future)
                {
                    t.pending_requests.Add(TickType.ClosePrice);
                    t.pending_requests.Add(TickType.OpenPrice);
                    t.pending_requests.Add(TickType.HighPrice);
                    t.pending_requests.Add(TickType.LowPrice);
                    t.pending_requests.Add(TickType.LastPrice);
                    t.pending_requests.Add(TickType.LastSize);
                    t.pending_requests.Add(TickType.LastTimestamp);

                    Krs.Ats.IBNet.Contracts.Future contract = new Krs.Ats.IBNet.Contracts.Future(symbol, exchange, details[i].Summary.Expiry);
                    contract.PrimaryExchange = primary_exchange;
                    contract.Currency        = currency;
                    tws.RequestMarketData(id, contract, null, false);
                }
                else if (type == SecurityType.Option || type == SecurityType.FutureOption)
                {
                    Collection <GenericTickType> g = null;

                    if (tws_download_openint)
                    {
                        t.pending_requests.Add(TickType.OpenInterest);

                        g = new Collection <GenericTickType>();
                        g.Add(GenericTickType.OptionOpenInterest);
                        g.Add(GenericTickType.OptionVolume);
                    }

                    // Krs.Ats.IBNet.Contracts.Option contract = new Krs.Ats.IBNet.Contracts.Option(details[i].Summary.Symbol, details[i].Summary.LocalSymbol, details[i].Summary.Expiry, details[i].Summary.Right, (decimal)details[i].Summary.Strike);
                    // contract.PrimaryExchange = primary_exchange;
                    // contract.Currency = details[i].Summary.Currency;
                    // tws.RequestMarketData(id, contract, g, false);

                    tws.RequestMarketData(id, details[i].Summary, g, false);
                }

                // add tick to return list
                list.Add(t);
            }

            // setup timeout
            DateTime to = DateTime.Now.AddSeconds(tws_quote_timeout);

            foreach (TickData t in list)
            {
                if (to > DateTime.Now)
                {
                    // wait for result
                    t.ready_event.WaitOne((TimeSpan)(to - DateTime.Now), false);
                }

                // remove tick from list
                tick_list.Remove(t.id);

                // cancel market data return id to id queue
                tws.CancelMarketData(t.id);
                id_queue.Enqueue(t.id);
            }

            return(list);
        }
Exemple #10
0
        public Instrument(String m_Symbol, String m_Expiry, InstrumentType m_Type)
        {
            this.CreateHandle();
            this.Visible = false;

            if (m_Client == null)
            {
                m_Client = new IBClient();
                m_Client.ThrowExceptions = true;
                m_Client.TickPrice      += new EventHandler <TickPriceEventArgs>(S_OnPriceDataUpdate);
                m_Client.TickSize       += new EventHandler <TickSizeEventArgs>(S_OnSizeDataUpdate);
                m_Client.Error          += new EventHandler <ErrorEventArgs>(S_OnError);
                m_Client.NextValidId    += new EventHandler <NextValidIdEventArgs>(S_OnNextValidId);
                //m_Client.OrderStatus += new EventHandler< OrderStatusEventArgs >( OnOrderStatus);
                m_Client.ExecDetails += new EventHandler <ExecDetailsEventArgs>(S_OnFill);

                m_Client.HistoricalData += new EventHandler <HistoricalDataEventArgs>(OnHistoricalDataUpdate);

                m_Client.Connect("127.0.0.1", 7496, 0);

                contracts = new Dictionary <int, Instrument>();
            }

            m_ID = id;
            id++;

            Symbol = m_Symbol;

            switch (m_Type)
            {
            case InstrumentType.EQUITY:
                m_Contract = new Equity(m_Symbol);
                break;

            case InstrumentType.FOREX:
                //m_Contract = new Forex();
                break;

            case InstrumentType.FUTURE:
                m_Contract = new Future(m_Symbol, "GLOBEX", m_Expiry);
                break;

            //case InstrumentType.FUTURE:
            //    m_Contract = new Future( "CCK2", "NYBOT", "JUN12" );
            //    break;

            case InstrumentType.INDEX:
                m_Contract = new Index(m_Symbol, "CBOE");
                break;

            case InstrumentType.OPTION:
                m_Contract = new Option("IBM", "IBM   120518C00170000", 2012, 5, Krs.Ats.IBNet.RightType.Call, 170);
                break;
            }
            ;

            m_Client.RequestMarketData(m_ID, m_Contract, null, false, false);
            contracts.Add(m_ID, this);

            //client.RequestExecutions(34, new ExecutionFilter());

            ///////////////////////////////////////////////////////////////////
            ////////////  These delegates perform cross-thread operation ///////
            ////////////////////////////////////////////////////////////////////
            OnPriceUpdateDelegate = new PriceUpdateEventHandler(Client_TickPrice);
            OnSizeUpdateDelegate  = new SizeUpdateEventHandler(Client_TickSize);
            OnFillUpdateDelegate  = new FillUpdateEventHandler(Client_Fill);
            ////////////////////////////////////////////////////////////////////
        }