Esempio n. 1
0
        public async Task MarketDataController_Should_ReturnMarketDataType()
        {
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.EnsureConnectedAsync();

            MarketDataTypeEventArgs marketDataTypeEventArgs = null;
            TickPriceEventArgs      tickPriceEventArgs      = null;

            twsObjectFactory.TwsCallbackHandler.MarketDataTypeEvent +=
                (sender, args) => { marketDataTypeEventArgs = args; };
            twsObjectFactory.TwsCallbackHandler.TickPriceEvent +=
                (sender, args) => { tickPriceEventArgs = args; };

            // Only real-time data provided for this contract
            var contract = new Contract()
            {
                Symbol   = TwsCurrency.Eur,
                Exchange = TwsExchange.Idealpro,
                SecType  = TwsContractSecType.Cash,
                Currency = TwsCurrency.Usd
            };

            var marketDataResult = await twsObjectFactory.TwsControllerBase.RequestMarketDataAsync(contract, "233", false, false, null);

            marketDataResult.Should().NotBeNull();
            tickPriceEventArgs.Should().NotBeNull();
            tickPriceEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId);

            marketDataTypeEventArgs.Should().NotBeNull();
            marketDataTypeEventArgs.MarketDataType.Should().Be(1);
        }
Esempio n. 2
0
        public async Task MarketDataTypeController_Should_ReturnMarketDataType()
        {
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.EnsureConnectedAsync();

            MarketDataTypeEventArgs marketDataTypeEventArgs = null;
            TickPriceEventArgs      tickPriceEventArgs      = null;

            twsObjectFactory.TwsCallbackHandler.MarketDataTypeEvent +=
                (sender, args) => { marketDataTypeEventArgs = args; };
            twsObjectFactory.TwsCallbackHandler.TickPriceEvent +=
                (sender, args) => { tickPriceEventArgs = args; };

            // This contract provide delayed data when requested
            Contract contract = new Contract
            {
                SecType     = TwsContractSecType.Stock,
                Symbol      = "MSFT",
                Exchange    = TwsExchange.Smart,
                PrimaryExch = TwsExchange.Island,
            };

            // Request delayed data feed
            twsObjectFactory.TwsControllerBase.RequestMarketDataType(3);
            var marketDataResult = await twsObjectFactory.TwsControllerBase.RequestMarketDataAsync(contract, "233", false, false, null);

            marketDataResult.Should().NotBeNull();
            tickPriceEventArgs.Should().NotBeNull();
            tickPriceEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId);

            marketDataTypeEventArgs.Should().NotBeNull();
            marketDataTypeEventArgs.MarketDataType.Should().Be(3);
        }
Esempio n. 3
0
        private void EventDispatcherOnTickPrice(object o, TickPriceEventArgs tickPriceEventArgs)
        {
            if (!_lastsByRequestId.TryGetValue(tickPriceEventArgs.RequestId, out var last))
            {
                last = new Level1MarketDataEventArgs();
                _lastsByRequestId.Add(tickPriceEventArgs.RequestId, last);
            }

            last.UpdateValues(tickPriceEventArgs.Field, tickPriceEventArgs.Price);
            MarketData.RaiseEvent(this, last.ShallowCopy());
        }
Esempio n. 4
0
        void tws_TickPrice(object sender, TickPriceEventArgs e)
        {
            // get tick object
            if (!tick_list.ContainsKey(e.TickerId))
            {
                return;
            }
            TickData t = tick_list[e.TickerId];

            switch (e.TickType)
            {
            case TickType.BidPrice:       // bid
                t.price.bid = (double)e.Price;
                break;

            case TickType.AskPrice:       // ask
                t.price.ask = (double)e.Price;
                break;

            case TickType.LastPrice:      // last
                t.price.last = (double)e.Price;
                break;

            case TickType.HighPrice:      // high
                t.price.high = (double)e.Price;
                break;

            case TickType.LowPrice:       // low
                t.price.low = (double)e.Price;
                break;

            case TickType.ClosePrice:     // close
                t.price.close = (double)e.Price;
                break;

            case TickType.OpenPrice:      // open
                t.price.open = (double)e.Price;
                break;

            default:
                break;
            }

            // remove from pending requests
            t.pending_requests.Remove(e.TickType);
            if (t.pending_requests.Count == 0)
            {
                t.ready_event.Set();
            }
        }
Esempio n. 5
0
        //////////////////////////////////////////////////////////////////////
        ////////////////////  TWS message event handlers /////////////////////
        //////////////////////////////////////////////////////////////////////

        void client_TickPrice(object sender, TickPriceEventArgs e)
        {
            //Console.WriteLine("Price: " + e.Price + " Tick Type: " + EnumDescConverter.GetEnumDescription(e.TickType));
            DateTime now         = DateTime.Now;
            int      millisecond = (int)(now.Ticks % TimeSpan.TicksPerSecond / (TimeSpan.TicksPerSecond / 256));

            lock (capturingWriterLock)
            {
                capturingWriter.Write((byte)millisecond); // record sub-second time (1/256th resolution)
                capturingWriter.Write((byte)e.TickType);  // kind of data (like bid, ask, last...)
                capturingWriter.Write((byte)e.TickerId);  // The Symbol ID (like AMD, INTC, INDU..)
                capturingWriter.Write((float)e.Price);    // The data - in this case price.
            }

            logger.Trace("{0} : {1} : {2} : {3}", e.TickType, symbols[e.TickerId].Symbol, e.TickerId, e.Price);
            totalCaptureEventsForDisplay++;
            lastRecievedUpdate = now;
        }
Esempio n. 6
0
        public async Task MarketOptionDataController_Should_ReturnMarketDataType()
        {
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", 7497, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.EnsureConnectedAsync();

            MarketDataTypeEventArgs        marketDataTypeEventArgs        = null;
            TickPriceEventArgs             tickPriceEventArgs             = null;
            TickOptionComputationEventArgs tickOptionComputationEventArgs = null;

            twsObjectFactory.TwsCallbackHandler.MarketDataTypeEvent +=
                (sender, args) => { marketDataTypeEventArgs = args; };
            twsObjectFactory.TwsCallbackHandler.TickPriceEvent +=
                (sender, args) => { tickPriceEventArgs = args; };
            twsObjectFactory.TwsCallbackHandler.TickOptionComputationEvent +=
                (sender, args) => { tickOptionComputationEventArgs = args; };


            Contract contract = new Contract
            {
                SecType    = TwsContractSecType.Option,
                Symbol     = "MSFT",
                Exchange   = TwsExchange.Smart,
                Multiplier = "100",
                Currency   = "USD",
                Right      = "C",
                Strike     = 200,
                LastTradeDateOrContractMonth = "20201113"
            };

            twsObjectFactory.TwsControllerBase.RequestMarketDataType(1);
            var marketDataResult = await twsObjectFactory.TwsControllerBase.RequestMarketDataAsync(contract, null, false, false, null);

            marketDataResult.Should().NotBeNull();
            tickPriceEventArgs.Should().NotBeNull();
            tickPriceEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId);
            tickOptionComputationEventArgs.Should().NotBeNull();
            tickOptionComputationEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId);
        }
Esempio n. 7
0
        private void Client_TickPrice(Object sender, TickPriceEventArgs e)
        {
            switch (e.TickType)
            {
            case Krs.Ats.IBNet.TickType.BidPrice:
                m_Bid = Convert.ToDouble(e.Price);
                break;

            case Krs.Ats.IBNet.TickType.AskPrice:
                m_Ask = Convert.ToDouble(e.Price);
                break;

            case Krs.Ats.IBNet.TickType.LastPrice:
                m_Last = Convert.ToDouble(e.Price);
                break;

            default:
                break;
            }
            //Tick m_Tick = new Tick(DateTime.Now, m_Last, m_LastQty);
            //BidAskUpdate(m_Tick);
        }
Esempio n. 8
0
        private void client_TickPrice(object sender, TickPriceEventArgs e)
        {
            SymbolHandler buffer = symbolHandlers[(ulong)e.TickerId];

            if (e.TickType == TickType.AskPrice)
            {
                buffer.Ask = (double)e.Price;
                buffer.SendQuote();
            }
            else if (e.TickType == TickType.BidPrice)
            {
                buffer.Bid = (double)e.Price;
                buffer.SendQuote();
            }
            else if (e.TickType == TickType.LastPrice)
            {
                buffer.Last = (double)e.Price;
                if (buffer.LastSize > 0)
                {
                    buffer.SendTimeAndSales();
                }
            }
        }
        /// <summary>
        /// Event raised when new Trade Price is received
        /// </summary>
        private void OnTickPrice(object sender, TickPriceEventArgs eventArgs)
        {
            try
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("New Trade Price receieved: " + eventArgs.Price, _type.FullName, "OnTickSize");
                }

                lock (_lock)
                {
                    TickType type = eventArgs.TickType;
                    if (type.Equals(TickType.LastPrice))
                    {
                        Tick tick = _tickList[eventArgs.TickerId];
                        tick.LastPrice = eventArgs.Price;
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Error(exception, _type.FullName, "OnTickSize");
            }
        }
        /// <inheritdoc/>
        public void tickPrice(int tickerId, int field, double price, TickAttrib attribs)
        {
            var eventArgs = new TickPriceEventArgs(tickerId, field, price, attribs);

            this.TickPriceEvent?.Invoke(this, eventArgs);
        }
Esempio n. 11
0
 static void client_TickPrice(object sender, TickPriceEventArgs e)
 {
     Console.WriteLine("Price: " + e.Price + " Tick Type: " + EnumDescConverter.GetEnumDescription(e.TickType));
 }
        protected void HandleTickPriceEvent(TickPriceEventArgs args)
        {
            //Log.Debug(args.Dump("Tick event fired"));

            _tickPriceEventArgs = args;
            if (!_activeRequests.ContainsKey(args.TickerId))
            {
                // Request latest price (not streaming)
                return;
            }

            var(contract, isStreaming) = _activeRequests[args.TickerId];
            var symbol = contract.Symbol;

            if (isStreaming)
            {
                if (!_prices.ContainsKey(symbol))
                {
                    _prices.Add(symbol, new Domain.Bar
                    {
                        Date = DateTime.UtcNow
                    });
                }
                var bar = _prices[symbol];

                switch (args.Field)
                {
                case TickType.OPEN:
                    bar.Open = args.Price;
                    break;

                case TickType.HIGH:
                    bar.High = args.Price;
                    break;

                case TickType.LOW:
                    bar.Low = args.Price;
                    break;

                case TickType.CLOSE:
                    bar.Close = args.Price;
                    break;

                case TickType.LAST:
                    // Send new messages out whenever a trade was made - i.e. type = Last
                    bar.Close = args.Price;
                    Messenger.Default.Send(new BarPriceMessage(symbol, bar));

                    // Flag for creation of a new bar on next tick
                    _prices.Remove(symbol);
                    break;
                }
            }
            else
            {
                if (args.Field == TickType.LAST)
                {
                    if (_oneOffPrices.ContainsKey(symbol))
                    {
                        _oneOffPrices.Remove(symbol);
                    }

                    _oneOffPrices.Add(symbol, args.Price);
                }
            }
        }
Esempio n. 13
0
 private void OnTickPriceEvent(object sender, TickPriceEventArgs args)
 {
     _tickPriceEventArgs = args;
 }
Esempio n. 14
0
 private void I_OnPriceDataUpdate(Object sender, TickPriceEventArgs e)
 {
     this.BeginInvoke(OnPriceUpdateDelegate, sender, e);
 }
Esempio n. 15
0
///////////////////////////////////////////////////////////////////////////////////
//////////////////////// Real time tick data //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////  Switch event update to main thread ///////////////////////////////////////

        private void S_OnPriceDataUpdate(Object sender, TickPriceEventArgs e)
        {
            Instrument p = contracts[e.TickerId];

            p.I_OnPriceDataUpdate(sender, e);
        }