Beispiel #1
0
        /// <summary>
        /// Event handler for streaming ticks
        /// </summary>
        /// <param name="json">The data object containing the received tick</param>
        private void OnPricingDataReceived(string json)
        {
            var obj  = (JObject)JsonConvert.DeserializeObject(json);
            var type = obj["type"].ToString();

            switch (type)
            {
            case "HEARTBEAT":
                PricingConnectionHandler.KeepAlive(DateTime.UtcNow);
                break;

            case "PRICE":
                var data = obj.ToObject <Price>();

                var securityType = SymbolMapper.GetBrokerageSecurityType(data.Instrument);
                var symbol       = SymbolMapper.GetLeanSymbol(data.Instrument, securityType, Market.Oanda);
                var time         = GetTickDateTimeFromString(data.Time);

                // live ticks timestamps must be in exchange time zone
                DateTimeZone exchangeTimeZone;
                if (!_symbolExchangeTimeZones.TryGetValue(symbol, out exchangeTimeZone))
                {
                    exchangeTimeZone = MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.Oanda, symbol, securityType).TimeZone;
                    _symbolExchangeTimeZones.Add(symbol, exchangeTimeZone);
                }
                time = time.ConvertFromUtc(exchangeTimeZone);

                var bidPrice = data.Bids.Last().Price.ConvertInvariant <decimal>();
                var askPrice = data.Asks.Last().Price.ConvertInvariant <decimal>();
                var tick     = new Tick(time, symbol, bidPrice, askPrice);

                EmitTick(tick);
                break;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Event handler for streaming ticks
        /// </summary>
        /// <param name="data">The data object containing the received tick</param>
        private void OnDataReceived(RateStreamResponse data)
        {
            if (data.IsHeartbeat())
            {
                PricingConnectionHandler.KeepAlive(DateTime.UtcNow);
                return;
            }

            if (data.tick == null)
            {
                return;
            }

            var securityType = SymbolMapper.GetBrokerageSecurityType(data.tick.instrument);
            var symbol       = SymbolMapper.GetLeanSymbol(data.tick.instrument, securityType, Market.Oanda);
            var time         = OandaBrokerage.GetDateTimeFromString(data.tick.time);

            // live ticks timestamps must be in exchange time zone
            DateTimeZone exchangeTimeZone;

            if (!_symbolExchangeTimeZones.TryGetValue(symbol, out exchangeTimeZone))
            {
                exchangeTimeZone = MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.Oanda, symbol, securityType).TimeZone;
                _symbolExchangeTimeZones.Add(symbol, exchangeTimeZone);
            }
            time = time.ConvertFromUtc(exchangeTimeZone);

            var bidPrice = Convert.ToDecimal(data.tick.bid);
            var askPrice = Convert.ToDecimal(data.tick.ask);
            var tick     = new Tick(time, symbol, bidPrice, askPrice);

            EmitTick(tick);
        }