/// <summary>
        /// Disconnects/Stops a client
        /// </summary>
        public bool Stop()
        {
            try
            {
                if (_ibClient != null)
                {
                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info("Sending disconnect call for IB.", _type.FullName, "Stop");
                    }
                    // Toggle Field Value
                    _logoutRequest = true;

                    foreach (var ids in _idsMap.Values)
                    {
                        // Send Unsubscription Map
                        _ibClient.CancelMarketData(ids);
                    }

                    _ibClient = null;

                    // Raised Logout Event
                    if (LogoutArrived != null)
                    {
                        LogoutArrived(_marketDataProviderName);
                    }
                }
                else
                {
                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info("Connection no longer exists.", _type.FullName, "Stop");
                    }
                }

                // Indicating calls were successfully sent
                return(true);
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "Stop");
                return(false);
            }
        }
        public void StopSymbol(Receiver receiver, SymbolInfo symbol)
        {
            if (debug)
            {
                log.Debug("StartSymbol");
            }
            client.CancelMarketData((int)symbol.BinaryIdentifier);
            SymbolHandler buffer = symbolHandlers[symbol.BinaryIdentifier];

            buffer.Stop();
            receiver.OnEvent(symbol, (int)EventType.EndRealTime, null);
        }
Exemple #3
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);
        }