private async void EnsureHistoricalTrades()
        {
            if (!Exchange.SupportsHistoricalLoad)
            {
                return;
            }

            Logger.LogInformation("Ensure historical trades are captured.");

            foreach (var symbolCode in ExchangeWorker.Configuration.Symbol)
            {
                var symbol          = SymbolFactory.Get(symbolCode);
                var lastTradeFilter = await HistorianRepository.GetTradeFilter(Exchange.Name, symbolCode);

                var priority = string.Equals(lastTradeFilter, Exchange.GetHttpClient().InitialTradeFilter) ? 2 : 1;

                var catchup = new HistorianTradeCatchup
                {
                    Exchange           = Exchange.Name,
                    SymbolCode         = symbolCode,
                    TradeFilter        = lastTradeFilter,
                    EpochTo            = Epoch.Now,
                    CurrentTradeFilter = lastTradeFilter,
                    Priority           = priority
                };

                await HistorianRepository.AddTradeCatchup(catchup);
            }
        }
Beispiel #2
0
        public async Task RemoveTradeCatchup(HistorianTradeCatchup catchup)
        {
            using (var context = ContextFactory.CreateDbContext(null))
            {
                var entity = await context.HistorianTradeCatchup.FindAsync((int)catchup.Exchange, (int)catchup.SymbolCode, catchup.TradeFilter);

                if (entity != null)
                {
                    context.HistorianTradeCatchup.Remove(entity);

                    await context.SaveChangesAsync();
                }
            }
        }
Beispiel #3
0
        public async Task AddTradeCatchup(HistorianTradeCatchup catchup)
        {
            using (var context = ContextFactory.CreateDbContext(null))
            {
                var entity = await context.HistorianTradeCatchup.FindAsync((int)catchup.Exchange, (int)catchup.SymbolCode, catchup.TradeFilter);

                if (entity == null)
                {
                    await context.HistorianTradeCatchup.AddAsync(new HistorianTradeCatchupEntity
                    {
                        ExchangeId         = (int)catchup.Exchange,
                        SymbolId           = (int)catchup.SymbolCode,
                        TradeFilter        = catchup.TradeFilter,
                        TimestampTo        = catchup.EpochTo.TimestampMilliseconds,
                        CurrentTradeFilter = catchup.CurrentTradeFilter,
                        Priority           = catchup.Priority
                    });

                    await context.SaveChangesAsync();
                }
            }
        }
        private async Task Ping(bool initial = false)
        {
            try
            {
                var ping = new Ping();

                var uri = new Uri(Exchange.GetHttpClient().ApiUrl);

                var reply = await ping.SendPingAsync(uri.Host);

                if (reply.Status == IPStatus.Success)
                {
                    // If the exchange is running web socket, and supports historical loads, ensure the historian does not produce gaps
                    if (!initial && !Online && Exchange.SupportsHistoricalLoad && Exchange.GetWebSocketClient() != null)
                    {
                        Logger.LogInformation($"Regained connectivity");

                        var symbols = await HistorianRepository.GetSymbols(Exchange.Name);

                        foreach (var historianSymbol in symbols)
                        {
                            var symbol = SymbolFactory.Get(historianSymbol.SymbolCode);

                            if (!symbol.Tradable)
                            {
                                continue;
                            }

                            using (Logger.BeginSymbolScope(symbol.Code))
                            {
                                var now = Epoch.Now;

                                var catchup = new HistorianTradeCatchup
                                {
                                    Exchange           = Exchange.Name,
                                    SymbolCode         = historianSymbol.SymbolCode,
                                    TradeFilter        = historianSymbol.TradeFilter,
                                    EpochTo            = now,
                                    CurrentTradeFilter = historianSymbol.TradeFilter,
                                    Priority           = 1
                                };

                                Logger.LogWarning($"Adding trade catch up job with filter '{historianSymbol.TradeFilter}'");

                                await HistorianRepository.AddTradeCatchup(catchup);
                            }
                        }
                    }

                    Online = true;
                }
                else
                {
                    Logger.LogWarning($"Lost connectivity: {reply.Status}");

                    Online = false;
                }
            }
            catch (Exception ex)
            {
                Logger.LogWarning(ex, $"Unexpected error occurred when pinging");

                Online = false;
            }
        }