public void Start()
 {
     try
     {
         _collectorContext = new CollectorContext();
         _collectorContext.Database.Migrate();
         started = true;
     }
     catch (Exception e)
     {
         started = false;
         Debug.WriteLine("Startup failed, trying again in 5 seconds..");
         Thread.Sleep(5000);
         if (!started)
         {
             Start();
         }
     }
     CreateServices();
     try
     {
         InitializeSubs();
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
     StartTimer();
 }
        private async void FetchTradeBins(Object source, System.Timers.ElapsedEventArgs e)
        {
            Console.WriteLine("Fetching bins.");
            Debug.WriteLine("Fetching bins.");
            using (var context = new CollectorContext())
            {
                try
                {
                    var latestBin = context.TradeBins.OrderByDescending(x => x.Timestamp).FirstOrDefault();
                    var @params   = new TradeBucketedGETRequestParams
                    {
                        BinSize = "1m",
                        Symbol  = "XBTUSD",
                        Count   = 200,
                        Reverse = true
                    };

                    // act
                    var result = new List <TradeBucketedDto>();
                    try
                    {
                        result = _bitmexApiService.Execute(BitmexApiUrls.Trade.GetTradeBucketed, @params).Result;
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception);
                    }

                    foreach (var bin in result)
                    {
                        if (latestBin == null || bin.Timestamp.LocalDateTime > latestBin.Timestamp)
                        {
                            await context.TradeBins.AddAsync(new TradeBin()
                            {
                                Symbol          = bin.Symbol,
                                Timestamp       = bin.Timestamp.UtcDateTime,
                                Open            = bin.Open.Value,
                                High            = bin.High.Value,
                                Low             = bin.Low.Value,
                                Close           = bin.Close.Value,
                                Trades          = bin.Trades,
                                Volume          = bin.Volume.Value,
                                ForeignNotional = bin.ForeignNotional,
                                HomeNotional    = bin.HomeNotional
                            });
                        }
                    }

                    await context.SaveChangesAsync();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }
        }
        private async Task HandleLiquidation(LiquidationDto dto)
        {
            try
            {
                using (var context = new CollectorContext())
                {
                    _lastupdate = DateTime.Now;
                    if (dto.Price != null)
                    {
                        if (dto.LeavesQty != null)
                        {
                            if (await _collectorContext.Liquidations.FirstOrDefaultAsync(a => a.LiquidationId == dto.OrderId) == null)
                            {
                                Console.WriteLine(DateTime.Now + ": " + dto.Symbol + " " + (dto.Side == "Sell" ? "long" : "short") +
                                                  " liquidation at " + dto.Price.Value + " with quantity " + dto.LeavesQty.Value);
                                Debug.WriteLine(DateTime.Now + ": " + dto.Symbol + " " + (dto.Side == "Sell" ? "long" : "short") +
                                                " liquidation at " + dto.Price.Value + " with quantity " + dto.LeavesQty.Value);
                                await context.Liquidations.AddAsync(new Liquidation()
                                {
                                    LiquidationId = dto.OrderId,
                                    Symbol        = dto.Symbol,
                                    Timestamp     = DateTime.Now.ToUniversalTime(),
                                    Direction     = dto.Side == "Sell" ? "Long" : "Short",
                                    Price         = dto.Price.Value,
                                    Quantity      = dto.LeavesQty.Value
                                });

                                await context.SaveChangesAsync();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }