Improve per on price update
This commit is contained in:
@@ -39,39 +39,35 @@ public class PricesService : IPricesService
|
||||
throw new Exception($"Enable to found account for exchange {exchange}");
|
||||
|
||||
var lastCandles =
|
||||
await _candleRepository.GetCandles(exchange, ticker, timeframe, DateTime.UtcNow.AddDays(-30), limit: 5);
|
||||
await _candleRepository.GetCandles(exchange, ticker, timeframe, DateTime.UtcNow.AddDays(-30), limit: 1)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var lastCandle = lastCandles.LastOrDefault();
|
||||
var startDate = lastCandle != null ? lastCandle.Date : new DateTime(2017, 1, 1);
|
||||
|
||||
List<Candle> newCandles;
|
||||
if (!lastCandles.Any())
|
||||
{
|
||||
newCandles = await _exchangeService.GetCandles(account, ticker, startDate, timeframe, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
newCandles = await _exchangeService.GetCandles(account, ticker, startDate, timeframe, false);
|
||||
}
|
||||
List<Candle> newCandles =
|
||||
await _exchangeService.GetCandles(account, ticker, startDate, timeframe, true)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var candlesToInsert = lastCandle == null
|
||||
? newCandles
|
||||
: newCandles.Where(c => c.Date > lastCandle.Date);
|
||||
|
||||
var candles = !lastCandles.Any() ? newCandles : newCandles.Where(c => c.Date > lastCandle?.Date);
|
||||
var candlesInserted = 0;
|
||||
|
||||
foreach (var newCandle in candles)
|
||||
foreach (var newCandle in candlesToInsert)
|
||||
{
|
||||
if (lastCandle == null || newCandle.Date > lastCandle.Date)
|
||||
{
|
||||
_candleRepository.InsertCandle(newCandle);
|
||||
candlesInserted++;
|
||||
}
|
||||
await _candleRepository.InsertCandle(newCandle).ConfigureAwait(false);
|
||||
candlesInserted++;
|
||||
}
|
||||
|
||||
if (candlesInserted > 0)
|
||||
_logger.LogInformation($"[{exchange}][{ticker}][{timeframe}] New candles inserted : {candlesInserted}");
|
||||
_logger.LogInformation("[{exchange}][{ticker}][{timeframe}] New candles inserted : {candlesInserted}", exchange, ticker, timeframe, candlesInserted);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SentrySdk.CaptureException(ex);
|
||||
_logger.LogError($"[{exchange}][{ticker}][{timeframe}] Error : {ex.Message} | {ex.StackTrace}");
|
||||
_logger.LogError("[{exchange}][{ticker}][{timeframe}] Error : {ex.Message} | {ex.StackTrace}", exchange, ticker, timeframe, ex.Message, ex.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user