示例#1
0
 public static BlockingCollection <MarketData> UpdateMarketData(this BlockingCollection <MarketData> collection, Instruments instrument, int minuteInterval, int index)
 {
     using (MarketsEntities context = new MarketsEntities())
     {
         DateTime timeCutoff = DateTime.UtcNow.AddMinutes(-minuteInterval);
         var      query      = context.MarketData.Where(x => x.InstrumentID == instrument.InstrumentID && x.MarketDataID > index && x.Timestamp > timeCutoff).OrderBy(y => y.Timestamp).Select(p => p).ToList();
         query.ForEach(q => collection.Add(q));
     };
     return(collection);
 }
示例#2
0
        public static List <Instruments> GetInstruments(int exchangeID)
        {
            List <Instruments> instruments = new List <Instruments>();

            using (MarketsEntities context = new MarketsEntities())
            {
                instruments = context.Instruments.Where(x => x.ExchangeID == exchangeID && x.Active == true).Select(y => y).ToList();
            };
            return(instruments);
        }
示例#3
0
        public static List <CurrentAggData> GetRecentCurrentAggData(int instrumentID, int minuteInterval, int lastIndex)
        {
            List <CurrentAggData> data = new List <CurrentAggData>();

            using (MarketsEntities context = new MarketsEntities())
            {
                DateTime timeCutoff = DateTime.UtcNow.AddMinutes(-minuteInterval);
                var      query      = context.CurrentAggData.Where(x => x.CurrentAggID > lastIndex && x.InstrumentID == instrumentID && x.LastUpdate > timeCutoff).OrderBy(y => y.LastUpdate).Select(p => p).ToList();
            }
            return(data);
        }
示例#4
0
        /// <summary>
        /// Gets the aggreggated data already provided by the socket
        /// </summary>
        /// <param name="instrumentID"></param>
        /// <param name="minuteInterval"></param>
        /// <returns></returns>
        internal static BlockingCollection <CurrentAggData> GetCurrentAggData(int instrumentID, int minuteInterval)
        {
            BlockingCollection <CurrentAggData> collection = new BlockingCollection <CurrentAggData>();

            using (MarketsEntities context = new MarketsEntities())
            {
                DateTime timeCutoff = DateTime.UtcNow.AddMinutes(-minuteInterval);
                var      query      = context.CurrentAggData.Where(x => x.InstrumentID == instrumentID && x.LastUpdate > timeCutoff).OrderBy(y => y.LastUpdate).Select(p => p).ToList();
                query.ForEach(q => collection.Add(q)); //Add the list to the blockingcollection.
            }
            return(collection);
        }
 // distance is in meters
 public List<ResultViewModel> GetMarketsWithinRange(double inputx, double inputy, double distance)
 {
     using (var context = new MarketsEntities()) {
         DbGeography startLoc = DbGeography.PointFromText(string.Format("POINT({0} {1})", inputx, inputy), 4326);
         var marketQuery =   (from fm in context.FarmerMarkets
                              let dif = fm.GeoLocation.Distance(startLoc)
                              where dif < distance
                              orderby dif ascending
                              select fm);
         var results = new List<ResultViewModel>();
         foreach (var market in marketQuery) {
             var difference = (double)market.GeoLocation.Distance(startLoc);
             results.Add(ConvertToResult(market, difference));
         }
         return results;
     }
 }
示例#6
0
 public static Instruments AddInstrument(Instruments instrument)
 {
     using (MarketsEntities context = new MarketsEntities())
     {
         if (instrument.Exchanges != null)
         {
             context.Exchanges.Attach(instrument.Exchanges);
         }
         if (instrument.Markets != null)
         {
             context.Markets.Attach(instrument.Markets);
         }
         //  instruments = context.Instruments.Where(x => x.ExchangeID == exchangeID).Select(y => y).ToList();
         instrument = context.Instruments.Add(instrument);
         context.SaveChanges();
     };
     return(instrument);
 }
        public ResultViewModel GetClosestMarket(double inputx, double inputy)
        {
            using (var context = new MarketsEntities()) {

                //var query = (from fm in context.FarmerMarkets
                //             let dif = (Math.Abs((double)fm.x - inputx) + Math.Abs((double)fm.y - inputy))
                //             orderby dif ascending
                //             select fm).First();
                //double distance = Math.Abs((double)market.x - inputx) + Math.Abs((double)market.y - inputy);
                DbGeography startLoc = DbGeography.PointFromText(string.Format("POINT({0} {1})", inputx, inputy), 4326);
                var query = (from fm in context.FarmerMarkets
                             let dif = fm.GeoLocation.Distance(startLoc)
                             orderby dif ascending
                             select fm).First();
                FarmerMarket market = query;
                double distance = (double)market.GeoLocation.Distance(startLoc);

                return ConvertToResult(market, distance);
            }
        }
示例#8
0
        public static CurrentData AddCurrentData(CurrentData currentData)
        {
            using (MarketsEntities context = new MarketsEntities())
            {
                if (currentData.Instruments != null)
                {
                    context.Instruments.Attach(currentData.Instruments);
                }
                if (currentData.Markets != null)
                {
                    context.Markets.Attach(currentData.Markets);
                }
                if (currentData.Exchanges != null)
                {
                    context.Exchanges.Attach(currentData.Exchanges);
                }

                currentData = context.CurrentData.Add(currentData);
                context.SaveChanges();
            }
            return(currentData);
        }
示例#9
0
        private void HandleData(string queuedMessage)
        {
            string id    = Regex.Match(queuedMessage, @"^([0-9]{1,2})").Value;
            string value = queuedMessage.Substring(id.Length);

            if (!string.IsNullOrEmpty(value))
            {
                dynamic thing = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(value);
                if (id == "0" && isPinging == false)
                {
                    PingModel pingModel = JsonConvert.DeserializeObject <PingModel>(value);
                    ThreadPool.QueueUserWorkItem(_ => Ping(pingModel));
                    isPinging = true;
                }
                if (id == "42")
                {
                    CryptoCompareSet set = new CryptoCompareSet(thing[1].ToString());
                    if (set.Length < 6)
                    {
                        return;
                    }
                    if (set.Type == "5")
                    {
                        if (set.Ignore == true)
                        {
                            return;
                        }

                        int exchangeID   = 0;
                        int instrumentID = 0;
                        int interval     = 0;
                        // = Manager.FirstOrDefault(x => x.instrument.InstrumentID == instrument.InstrumentID);
                        using (MarketsEntities context = new MarketsEntities())
                        {
                            Instruments instrument = context.Instruments.FirstOrDefault(x => x.Name == set.LASTMARKET && x.BaseCurrency == set.FromCurrency && x.QuoteCurrency == set.ToCurrency && x.Active == true);
                            if (instrument == null)
                            {
                                instrument = DataCalls.AddInstrument(new Instruments()
                                {
                                    ExchangeID = 2, MarketID = 1, Name = set.LASTMARKET, BaseCurrency = set.FromCurrency, QuoteCurrency = set.ToCurrency, Active = true, Interval = 60
                                });
                            }
                            exchangeID   = instrument.ExchangeID;
                            instrumentID = instrument.InstrumentID;
                            interval     = instrument.Interval;
                        };
                        InstrumentManager current = Manager.FirstOrDefault(x => x.InstrumentID == instrumentID && x.Type == 5);

                        if (current != null)
                        {
                            if (DateTime.UtcNow <= current.Last.AddSeconds(interval))
                            {
                                return;
                            }
                            else
                            {
                                current.Last = DateTime.UtcNow;
                            }
                        }
                        else if (current == null)
                        {
                            Manager.Add(new InstrumentManager()
                            {
                                InstrumentID = instrumentID, Last = DateTime.UtcNow, Type = 5
                            });
                        }

                        CurrentAggData currentAggData = new CurrentAggData()
                        {
                            ExchangeID     = exchangeID,
                            InstrumentID   = instrumentID,
                            MarketID       = 1,
                            Flag           = set.Flag,
                            Price          = set.Price,
                            LastUpdate     = FromUnixTime(set.LastUpdate),
                            LastVolume     = set.LastVolume,
                            LastVolumeTo   = set.LastVolumeTo,
                            LastTradeId    = set.LastTradeId,
                            Volume24Hour   = set.VOLUME24HOUR,
                            Volume24HourTo = set.VOLUME24HOURTO,
                            Low24Hour      = set.LOW24HOUR,
                            High24Hour     = set.HIGH24HOUR,
                            Open24Hour     = set.OPEN24HOUR,
                            Volume24h      = set.Volume24h,
                            Volume24hTo    = set.Volume24hTo,
                            MaskInt        = set.MaskInt
                        };
                        DataCalls.AddCurrentAggData(currentAggData);
                    }
                    else
                    {
                        if ((set.Flag == 1 || set.Flag == 2 || set.Flag == 4)) //|| set.Flag == 5))
                        {
                            int exchangeID   = 0;
                            int instrumentID = 0;
                            int interval     = 0;
                            // = Manager.FirstOrDefault(x => x.instrument.InstrumentID == instrument.InstrumentID);
                            using (MarketsEntities context = new MarketsEntities())
                            {
                                Instruments instrument = context.Instruments.FirstOrDefault(x => x.Name == set.ExchangeName && x.BaseCurrency == set.FromCurrency && x.QuoteCurrency == set.ToCurrency && x.Active == true);
                                if (instrument == null)
                                {
                                    instrument = DataCalls.AddInstrument(new Instruments()
                                    {
                                        ExchangeID = 2, MarketID = 1, Name = set.ExchangeName, BaseCurrency = set.FromCurrency, QuoteCurrency = set.ToCurrency, Active = true, Interval = 60
                                    });
                                }
                                exchangeID   = instrument.ExchangeID;
                                instrumentID = instrument.InstrumentID;
                                interval     = instrument.Interval;
                            };
                            InstrumentManager current = Manager.FirstOrDefault(x => x.InstrumentID == instrumentID && x.Type == 4);

                            if (current != null)
                            {
                                if (DateTime.UtcNow <= current.Last.AddSeconds(interval))
                                {
                                    return;
                                }
                                else
                                {
                                    current.Last = DateTime.UtcNow;
                                }
                            }
                            else if (current == null)
                            {
                                Manager.Add(new InstrumentManager()
                                {
                                    InstrumentID = instrumentID, Last = DateTime.UtcNow, Type = 4
                                });
                            }

                            CurrentData currentData = new CurrentData()
                            {
                                //Instruments = instrument,
                                ExchangeID   = exchangeID,
                                InstrumentID = instrumentID,
                                MarketID     = 1,
                                Flag         = set.Flag,
                                Price        = set.Price,
                                LastUpdate   = FromUnixTime(set.LastUpdate),
                                LastVolume   = set.LastVolume,
                                LastVolumeTo = set.LastVolumeTo,
                                LastTradeId  = set.LastTradeId,
                                Volume24h    = set.Volume24h,
                                Volume24hTo  = set.Volume24hTo,
                                MaskInt      = set.MaskInt
                            };
                            DataCalls.AddCurrentData(currentData);
                        }
                    }
                }

                return;
            }
            else
            {
                return;
            }
        }