예제 #1
0
        public void SetSymbol(Symbol symbol)
        {
            lock (_symbolLock)
            {
                _symbol = symbol;
            }

            using (var db = new CryptowatchDbContext())
            {
                db.DataRequests.Where(d => d.Status == RequestStatus.Enqueued && d.Type == RequestType.Chart)
                .ToList()
                .ForEach(d =>
                {
                    d.Status = RequestStatus.Cancelled;
                    db.DataRequests.Attach(d);
                    db.Entry(d).State = System.Data.Entity.EntityState.Modified;
                });

                db.DataRequests.Add(CreateChartRequest(symbol.Name, ChartPeriod._5Min));
                db.DataRequests.Add(CreateChartRequest(symbol.Name, ChartPeriod._15Min));
                db.DataRequests.Add(CreateChartRequest(symbol.Name, ChartPeriod._30Min));
                db.DataRequests.Add(CreateChartRequest(symbol.Name, ChartPeriod._2Hr));
                db.DataRequests.Add(CreateChartRequest(symbol.Name, ChartPeriod._D));

                db.SaveChanges();
            }
        }
예제 #2
0
        public CryptowatchEngine()
        {
            InitializeSettings();

            using (var db = new CryptowatchDbContext())
            {
                db.DataRequests.Where(d => d.Status == RequestStatus.Pending)
                .ToList()
                .ForEach(d =>
                {
                    d.Status = RequestStatus.Cancelled;
                    db.DataRequests.Attach(d);
                    db.Entry(d).State = System.Data.Entity.EntityState.Modified;
                });

                db.SaveChanges();
            }

            Task.Run(new Action(() => Execute()));
        }
예제 #3
0
        public void Execute()
        {
            while (true)
            {
                using (var db = new CryptowatchDbContext())
                {
                    db.DataRequests
                    .Where(d => d.Status == Models.RequestStatus.Enqueued && d.DateExecuteAt <= DateTime.Now)
                    .OrderByDescending(d => d.Priority)
                    .ThenByDescending(d => d.DateCreated)
                    .Take(2)
                    .ToList()
                    .ForEach(r =>
                    {
                        r.Status = RequestStatus.Pending;
                        db.DataRequests.Attach(r);
                        db.Entry(r).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();

                        try
                        {
                            var request = RequestFactory.CreateRequest(r);
                            request.Execute(r);
                            r.Status = Models.RequestStatus.OK;
                        }
                        catch (Exception ex)
                        {
                            r.ExceptionMessage = ex.Message;
                            if (ex.InnerException != null)
                            {
                                r.ExceptionMessage += $" ({ex.InnerException.Message})";
                            }
                            r.Status = Models.RequestStatus.Error;
                        }

                        r.DateExecuted = DateTime.Now;
                        db.DataRequests.Attach(r);
                        db.Entry(r).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    });

                    #region Refresh ticker
                    if (db.DataRequests.Count(d => d.Status == Models.RequestStatus.Enqueued && d.Type == Models.RequestType.Ticker) == 0)
                    {
                        var tickerRequest = new DataRequest
                        {
                            DateExecuted     = DateTime.Now,
                            DateExecuteAt    = DateTime.Now.AddMilliseconds(Globals.Settings.ScannerRefreshIntervalMs),
                            Status           = RequestStatus.Enqueued,
                            Type             = RequestType.Ticker,
                            ExceptionMessage = String.Empty,
                            JsonResult       = String.Empty,
                            Priority         = RequestPriority.High
                        };

                        db.DataRequests.Add(tickerRequest);
                        db.SaveChanges();
                    }
                    #endregion

                    #region Refresh charts
                    string symbol = _symbol?.Name;
                    if (!string.IsNullOrEmpty(symbol) &&
                        db.DataRequests.Count(d => d.Status == RequestStatus.Enqueued &&
                                              d.Type == RequestType.Chart &&
                                              d.Parameter2 == ((int)ChartPeriod._5Min).ToString() &&
                                              d.Parameter1 == symbol) == 0)
                    {
                        var chartRequest = CreateChartRequest(symbol, ChartPeriod._5Min);
                        chartRequest.DateExecuteAt += TimeSpan.FromMilliseconds(Globals.Settings.ChartRefreshIntervalMs);
                        db.DataRequests.Add(chartRequest);
                        db.SaveChanges();
                    }

                    if (!string.IsNullOrEmpty(symbol) &&
                        db.DataRequests.Count(d => d.Status == RequestStatus.Enqueued &&
                                              d.Type == RequestType.Chart &&
                                              d.Parameter2 == ((int)ChartPeriod._15Min).ToString() &&
                                              d.Parameter1 == symbol) == 0)
                    {
                        var chartRequest = CreateChartRequest(symbol, ChartPeriod._15Min);
                        chartRequest.DateExecuteAt += TimeSpan.FromMilliseconds(Globals.Settings.ChartRefreshIntervalMs);
                        db.DataRequests.Add(chartRequest);
                        db.SaveChanges();
                    }

                    if (!string.IsNullOrEmpty(symbol) &&
                        db.DataRequests.Count(d => d.Status == RequestStatus.Enqueued &&
                                              d.Type == RequestType.Chart &&
                                              d.Parameter2 == ((int)ChartPeriod._30Min).ToString() &&
                                              d.Parameter1 == symbol) == 0)
                    {
                        var chartRequest = CreateChartRequest(symbol, ChartPeriod._30Min);
                        chartRequest.DateExecuteAt += TimeSpan.FromMilliseconds(Globals.Settings.ChartRefreshIntervalMs);
                        db.DataRequests.Add(chartRequest);
                        db.SaveChanges();
                    }

                    if (!string.IsNullOrEmpty(symbol) &&
                        db.DataRequests.Count(d => d.Status == RequestStatus.Enqueued &&
                                              d.Type == RequestType.Chart &&
                                              d.Parameter2 == ((int)ChartPeriod._2Hr).ToString() &&
                                              d.Parameter1 == symbol) == 0)
                    {
                        var chartRequest = CreateChartRequest(symbol, ChartPeriod._2Hr);
                        chartRequest.DateExecuteAt += TimeSpan.FromMilliseconds(Globals.Settings.ChartRefreshIntervalMs);
                        db.DataRequests.Add(chartRequest);
                        db.SaveChanges();
                    }

                    if (!string.IsNullOrEmpty(symbol) &&
                        db.DataRequests.Count(d => d.Status == RequestStatus.Enqueued &&
                                              d.Type == RequestType.Chart &&
                                              d.Parameter2 == ((int)ChartPeriod._D).ToString() &&
                                              d.Parameter1 == symbol) == 0)
                    {
                        var chartRequest = CreateChartRequest(symbol, ChartPeriod._D);
                        chartRequest.DateExecuteAt += TimeSpan.FromMilliseconds(Globals.Settings.ChartRefreshIntervalMs);
                        db.DataRequests.Add(chartRequest);
                        db.SaveChanges();
                    }
                    #endregion
                }

                Thread.Sleep(500);
            }
        }
예제 #4
0
        public void Process(DataRequest request)
        {
            DateTime DateTimeFromUnixDate(long unixDate)
            {
                var epoch         = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                var timeSpan      = TimeSpan.FromSeconds(unixDate);
                var localDateTime = epoch.Add(timeSpan).ToLocalTime();

                return(localDateTime);
            }

            dynamic data         = JsonConvert.DeserializeObject(request.JsonResult);
            var     candlesticks = new List <Candlestick>();

            foreach (var candle in data)
            {
                candlesticks.Add(new Candlestick
                {
                    Timestamp       = DateTime.Now,
                    Date            = DateTimeFromUnixDate((long)candle.date.Value),
                    High            = (decimal)candle.high.Value,
                    Low             = (decimal)candle.low.Value,
                    Open            = (decimal)candle.open.Value,
                    Close           = (decimal)candle.close.Value,
                    Volume          = (long)candle.volume.Value,
                    QuoteVolume     = (long)candle.quoteVolume.Value,
                    WeightedAverage = (decimal)candle.weightedAverage.Value,
                    Period          = (ChartPeriod)int.Parse(request.Parameter2),
                    Symbol          = request.Parameter1
                });
            }

            for (int i = 10; i < candlesticks.Count; i++)
            {
                var sma10 = candlesticks.Skip(i - 10).Take(10).Sum(s => s.Close) / 10;
                candlesticks[i].SMA10 = sma10;
            }

            for (int i = 20; i < candlesticks.Count; i++)
            {
                var sma20 = candlesticks.Skip(i - 20).Take(20).Sum(s => s.Close) / 20;
                candlesticks[i].SMA20 = sma20;
            }

            using (var db = new CryptowatchDbContext())
            {
                db.Candlesticks
                .ToList()
                .Where(c => c.Symbol == candlesticks.FirstOrDefault().Symbol &&
                       c.Period == candlesticks.FirstOrDefault().Period)
                .ToList()
                .ForEach(c => db.Entry(c).State = System.Data.Entity.EntityState.Deleted);

                foreach (var candlestick in candlesticks
                         .Skip(candlesticks.Count - Globals.Settings.ChartPeriodsToShow)
                         .Take(Globals.Settings.ChartPeriodsToShow).ToList())
                {
                    db.Candlesticks.Add(candlestick);
                }

                db.SaveChanges();
            }
        }
        public void Process(DataRequest request)
        {
            dynamic data    = JsonConvert.DeserializeObject(request.JsonResult);
            var     tickers = new List <Ticker>();

            foreach (var market in data)
            {
                if (Globals.Settings.ScannerOnlyBtcMarkets && !Regex.IsMatch(market.Name, "BTC"))
                {
                    continue;
                }

                tickers.Add(new Ticker
                {
                    Name          = market.Name,
                    Exchange      = "PLNX",
                    Timestamp     = DateTime.Now,
                    Last          = market.Value.last,
                    LowestAsk     = market.Value.lowestAsk,
                    HighestBid    = market.Value.highestBid,
                    PercentChange = market.Value.percentChange,
                    BaseVolume    = market.Value.baseVolume,
                    QuoteVolume   = market.Value.quoteVolume,
                    IsFrozen      = market.Value.isFrozen == "1" ? true : false,
                    High24Hour    = market.Value.high24hr,
                    Low24Hour     = market.Value.low24hr
                });
            }
            using (var db = new CryptowatchDbContext())
            {
                db.Tickers.ToList().ForEach(t => db.Entry(t).State = System.Data.Entity.EntityState.Deleted);
                foreach (var t in tickers)
                {
                    db.Tickers.Add(t);

                    var lastOf5MinChart = db.Candlesticks.Where(c => c.Symbol == t.Name && c.Period == ChartPeriod._5Min).OrderByDescending(c => c.Date).FirstOrDefault();
                    if (lastOf5MinChart != null && lastOf5MinChart.Timestamp < DateTime.Now && (lastOf5MinChart.Timestamp + TimeSpan.FromSeconds((int)ChartPeriod._5Min)) > DateTime.Now)
                    {
                        lastOf5MinChart.Close = t.Last;
                        db.Candlesticks.Attach(lastOf5MinChart);
                        db.Entry(lastOf5MinChart).State = System.Data.Entity.EntityState.Modified;
                    }

                    var lastOf15MinChart = db.Candlesticks.Where(c => c.Symbol == t.Name && c.Period == ChartPeriod._15Min).OrderByDescending(c => c.Date).FirstOrDefault();
                    if (lastOf15MinChart != null && lastOf15MinChart.Timestamp < DateTime.Now && (lastOf15MinChart.Timestamp + TimeSpan.FromSeconds((int)ChartPeriod._15Min)) > DateTime.Now)
                    {
                        lastOf15MinChart.Close = t.Last;
                        db.Candlesticks.Attach(lastOf15MinChart);
                        db.Entry(lastOf15MinChart).State = System.Data.Entity.EntityState.Modified;
                    }

                    var lastOf30MinChart = db.Candlesticks.Where(c => c.Symbol == t.Name && c.Period == ChartPeriod._30Min).OrderByDescending(c => c.Date).FirstOrDefault();
                    if (lastOf30MinChart != null && lastOf30MinChart.Timestamp < DateTime.Now && (lastOf30MinChart.Timestamp + TimeSpan.FromSeconds((int)ChartPeriod._30Min)) > DateTime.Now)
                    {
                        lastOf30MinChart.Close = t.Last;
                        db.Candlesticks.Attach(lastOf30MinChart);
                        db.Entry(lastOf30MinChart).State = System.Data.Entity.EntityState.Modified;
                    }

                    var lastOf2HourChart = db.Candlesticks.Where(c => c.Symbol == t.Name && c.Period == ChartPeriod._2Hr).OrderByDescending(c => c.Date).FirstOrDefault();
                    if (lastOf2HourChart != null && lastOf2HourChart.Timestamp < DateTime.Now && (lastOf2HourChart.Timestamp + TimeSpan.FromSeconds((int)ChartPeriod._2Hr)) > DateTime.Now)
                    {
                        lastOf2HourChart.Close = t.Last;
                        db.Candlesticks.Attach(lastOf2HourChart);
                        db.Entry(lastOf2HourChart).State = System.Data.Entity.EntityState.Modified;
                    }

                    var lastOfDailyChart = db.Candlesticks.Where(c => c.Symbol == t.Name && c.Period == ChartPeriod._D).OrderByDescending(c => c.Date).FirstOrDefault();
                    if (lastOfDailyChart != null && lastOfDailyChart.Timestamp < DateTime.Now && (lastOfDailyChart.Timestamp + TimeSpan.FromSeconds((int)ChartPeriod._D)) > DateTime.Now)
                    {
                        lastOfDailyChart.Close = t.Last;
                        db.Candlesticks.Attach(lastOfDailyChart);
                        db.Entry(lastOfDailyChart).State = System.Data.Entity.EntityState.Modified;
                    }
                }
                db.SaveChanges();
            }
        }