示例#1
0
 void OnPriceFeed(InstrumentPrice info)
 {
     if (Program.ShowFeed)
     {
         Console.WriteLine($"UTC[{DateTime.UtcNow.ToDateTimeString()}] | BidDate[{info.Date.ToDateTimeString()}] | {info.Instrument} {info.Bid}/{info.Ask}");
     }
 }
示例#2
0
        public static List <InstrumentPrice> Prices(String S)
        {
            var instruments      = S.Split(';', '|');
            var n                = instruments.Count();
            var p                = 0;
            var Name             = "";
            var AskPrice         = "";
            var BidPrice         = "";
            var tradeInstruments = new List <InstrumentPrice>();

            foreach (var i in instruments)
            {
                n--;
                if (p == 0)
                {
                    Name = i;
                    p++;
                }
                else if (p == 1)
                {
                    AskPrice = i;
                    p++;
                }
                else
                {
                    BidPrice = i;
                    var tradeInstrument = new InstrumentPrice(Name, AskPrice, BidPrice);
                    tradeInstruments.Add(tradeInstrument);
                    p = 0;
                }
            }
            return(tradeInstruments);
        }
        private void OnMessageReceived(InstrumentPrice bestBidAsk)
        {
            IBoxSize assetCfg = null;

            // Lock Asset Configuration
            lock (AssetConfigurationLock)
            {
                assetCfg = _assetConfiguration.FirstOrDefault(a => a.AssetPair == bestBidAsk.Instrument);
            }
            if (assetCfg == null)
            {
                return;
            }

            // If asset configured to save history, add it to history
            if (assetCfg.SaveHistory)
            {
                _history?.AddToAssetHistory(new BestBidAsk()
                {
                    Asset       = bestBidAsk.Instrument,
                    BestAsk     = bestBidAsk.Ask,
                    BestBid     = bestBidAsk.Bid,
                    Source      = bestBidAsk.Source,
                    Timestamp   = bestBidAsk.Date,
                    ReceiveDate = DateTime.UtcNow
                });
            }

            // If asset allowed in game, raise event
            if (assetCfg.GameAllowed)
            {
                MessageReceived?.Invoke(this, (InstrumentPrice)bestBidAsk.ClonePrice());
            }
        }
        private Task ProcessBestBidAsk(BestBidAsk bestBidAsk)
        {
            // If Price is zero/null publish exception to support slack channel
            // and discard entry
            if (bestBidAsk.BestAsk == null || bestBidAsk.BestAsk <= 0 ||
                bestBidAsk.BestBid == null || bestBidAsk.BestBid <= 0)
            {
                //log
                //LogWarning("ProcessBestBidAsk", string.Format("Received BestBidAsk with price zero [0], BestBidAsk discarded. {0}", bestBidAsk));

                // Discard it
                return(Task.FromResult(0));
            }

            InstrumentPrice assetbid = new InstrumentPrice()
            {
                Instrument  = bestBidAsk.Asset,
                Source      = bestBidAsk.Source,
                Ask         = bestBidAsk.BestAsk.Value,
                Bid         = bestBidAsk.BestBid.Value,
                Date        = bestBidAsk.Timestamp,
                ReceiveDate = DateTime.UtcNow
            };

            OnMessageReceived(assetbid);

            return(Task.FromResult(0));
        }
示例#5
0
        private List <InstrumentPrice> ReadFromFile(string filePath)
        {
            List <InstrumentPrice> dataList = new List <InstrumentPrice>();
            var    fileStream = new StreamReader(filePath);
            string line;

            fileStream.ReadLine(); // Skip first line
            InstrumentPrice instrumentPrice;
            decimal         price;
            DateTime        date;

            while ((line = fileStream.ReadLine()) != null)
            {
                var properties = line.Split(',');
                if (Decimal.TryParse(properties[4], out price))
                {
                    if (DateTime.TryParseExact(properties[3], _format,
                                               CultureInfo.InvariantCulture,
                                               DateTimeStyles.None,
                                               out date))
                    {
                        try
                        {
                            instrumentPrice = new InstrumentPrice(properties[0], properties[1], properties[2], date, TimeSlot.DateTimeToTimeSlot(date), price);
                            dataList.Add(instrumentPrice);
                        }
                        catch (Exception e)
                        {
                            _logger.LogInformation(e, "");
                            continue;
                        }
                    }
                    else
                    {
                        _logger.LogInformation($"Error parsing Date - {properties[3]}");
                    }
                }
                else
                {
                    _logger.LogInformation($"Error parsing Price - {properties[4]}");
                }
            }
            return(dataList);
        }
        public void UpdateDataSource(AggregatedPriceChangeNotification value)
        {
            AggregatedPriceChangeNotification tmpValue = value;
            InstrumentPrice instrumentPrice            = InstrumentPrices.FirstOrDefault(x => x.InstrumentName == value.LatestUpdate.Instrument);

            if (instrumentPrice == null)
            {
                InstrumentPrices.Add(new InstrumentPrice
                {
                    AveragePrice       = value.AveragePrice,
                    InstrumentName     = value.LatestUpdate.Instrument,
                    Price              = value.LatestUpdate.Price,
                    RecentPriceChanges = new ObservableCollection <InstrumentPriceHistory>(value.RecentPriceChanges.Where(x => x != null).Select(x => new InstrumentPriceHistory
                    {
                        InstrumentName = x.Instrument, Price = x.Price
                    }
                                                                                                                                                 )),
                    PriceChangeStatus        = value.PriceChangeStatus,
                    AveragePriceChangeStatus = value.AveragePriceChangeStatus
                });
            }
            else
            {
                instrumentPrice.AveragePrice       = value.AveragePrice;
                instrumentPrice.InstrumentName     = value.LatestUpdate.Instrument;
                instrumentPrice.Price              = value.LatestUpdate.Price;
                instrumentPrice.RecentPriceChanges =
                    new ObservableCollection <InstrumentPriceHistory>(
                        value
                        .RecentPriceChanges
                        .Where(x => x != null)
                        .Select(x => new InstrumentPriceHistory {
                    InstrumentName = x.Instrument, Price = x.Price
                }
                                ));
                instrumentPrice.PriceChangeStatus        = value.PriceChangeStatus;
                instrumentPrice.AveragePriceChangeStatus = value.AveragePriceChangeStatus;
            }
        }
示例#7
0
        public static List <InstrumentPrice> Trades(string S)
        {
            var           SArray      = S.ToArray();
            var           Commas      = 0;
            var           LastComma   = 0;
            var           Instruments = new List <InstrumentPrice>();
            var           LastPipe    = 0;
            var           NameS       = "";
            var           PriceS      = "";
            var           SizeS       = "";
            StringBuilder Name        = new StringBuilder();
            StringBuilder Price       = new StringBuilder();
            StringBuilder Size        = new StringBuilder();


            for (var i = 0; i < SArray.Length; i++)
            {
                if (Commas == 0)
                {
                    if (SArray[i] == ',')
                    {
                        for (var n = i - 1; n >= LastComma; n--)
                        {
                            var p = 0;
                            Name.Insert(p, SArray[n]);
                            var namess = SArray[n];
                            p++;
                        }
                        NameS = Name.ToString();
                        NameS = ReverseString(NameS);
                        Commas++;
                        LastComma = i;
                    }
                }
                else if (Commas == 1)
                {
                    if (SArray[i] == ',')
                    {
                        for (var n = i - 1; n > LastComma; n--)
                        {
                            var p = 0;
                            Price.Insert(p, SArray[n]);
                            p++;
                        }
                        PriceS = Name.ToString();
                        PriceS = ReverseString(PriceS);
                        Commas++;
                        LastComma = i;
                    }
                    else
                    {
                    }
                }
                else if (Commas == 2)
                {
                    if (SArray[i] == '|')
                    {
                        for (var n = i - 1; n > LastComma; n--)
                        {
                            var p = 0;
                            Size.Insert(p, SArray[n]);
                            p++;
                        }
                        SizeS    = Name.ToString();
                        SizeS    = ReverseString(SizeS);
                        Commas   = 0;
                        LastPipe = i;
                        var Instrument = new InstrumentPrice(NameS, PriceS, SizeS);
                        Instruments.Add(Instrument);
                    }
                }
            }
            return(Instruments);
        }
        private Task ProcessAssetQuote(AssetQuote assetQuote)
        {
            // If Price is zero publish exception to support slack channel
            if (assetQuote.Price <= 0)
            {
                //LogWarning("ProcessAssetQuote", string.Format("Received AssetQuote with price zero [0], AssetQuote discarded. {0}", assetQuote));
                return(Task.FromResult(0));
            }


            // Get Asset from cache
            InstrumentPrice assetbid = (from a in _assetCache
                                        where a.Instrument == assetQuote.AssetPair
                                        select a).FirstOrDefault();

            if (assetbid == null)
            {
                // AssetPair is not in cache
                // Add AssetQuote to cache
                assetbid = new InstrumentPrice()
                {
                    Instrument  = assetQuote.AssetPair,
                    Source      = "AssetQuote",
                    Date        = assetQuote.Timestamp,
                    Ask         = assetQuote.IsBuy == Statics.ASK ? assetQuote.Price : 0,
                    Bid         = assetQuote.IsBuy == Statics.ASK ? 0 : assetQuote.Price,
                    ReceiveDate = DateTime.UtcNow
                };
                _assetCache.Add(assetbid);
            }
            else
            {
                // AssetPair is in cache
                // Update Bid Quote
                if (assetQuote.IsBuy == Statics.ASK)
                {
                    assetbid.Ask = assetQuote.Price;
                }
                else
                {
                    assetbid.Bid = assetQuote.Price;
                }
            }

            // Only publish if bid and ask prices have changed since last publish
            bool publish = false;

            if (!_lastPrices.ContainsKey(assetbid.Instrument))
            {
                // Asset not in history, add it and set publish = true
                _lastPrices.Add(assetQuote.AssetPair, (InstrumentPrice)assetbid.ClonePrice());
                publish = true;
            }
            else
            {
                if (_lastPrices[assetbid.Instrument].Ask > 0 &&
                    _lastPrices[assetbid.Instrument].Bid > 0 &&
                    (_lastPrices[assetbid.Instrument].Ask == assetbid.Ask || _lastPrices[assetbid.Instrument].Bid == assetbid.Bid))
                {
                    // One price (Ask or Bid) has not changed. do not publish it
                    // Must only be published when both Ask Bid prices have changed
                    publish = false;
                }
                else
                {
                    // Both prices have changed publish it and assign this new bid to history
                    publish = true;
                    _lastPrices[assetbid.Instrument] = (InstrumentPrice)assetbid.ClonePrice();
                }
            }

            if (assetbid.Ask <= 0 || assetbid.Bid <= 0)
            {
                publish = false;
            }

            if (publish)
            {
                OnMessageReceived(assetbid);
            }
            return(Task.FromResult(0));
        }