Improve per on price update
This commit is contained in:
@@ -24,5 +24,5 @@ public interface ICandleRepository
|
|||||||
Enums.TradingExchanges exchange,
|
Enums.TradingExchanges exchange,
|
||||||
Enums.Timeframe timeframe,
|
Enums.Timeframe timeframe,
|
||||||
DateTime start);
|
DateTime start);
|
||||||
void InsertCandle(Candle candle);
|
Task InsertCandle(Candle candle);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,39 +39,35 @@ public class PricesService : IPricesService
|
|||||||
throw new Exception($"Enable to found account for exchange {exchange}");
|
throw new Exception($"Enable to found account for exchange {exchange}");
|
||||||
|
|
||||||
var lastCandles =
|
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 lastCandle = lastCandles.LastOrDefault();
|
||||||
var startDate = lastCandle != null ? lastCandle.Date : new DateTime(2017, 1, 1);
|
var startDate = lastCandle != null ? lastCandle.Date : new DateTime(2017, 1, 1);
|
||||||
|
|
||||||
List<Candle> newCandles;
|
List<Candle> newCandles =
|
||||||
if (!lastCandles.Any())
|
await _exchangeService.GetCandles(account, ticker, startDate, timeframe, true)
|
||||||
{
|
.ConfigureAwait(false);
|
||||||
newCandles = await _exchangeService.GetCandles(account, ticker, startDate, timeframe, true);
|
|
||||||
}
|
var candlesToInsert = lastCandle == null
|
||||||
else
|
? newCandles
|
||||||
{
|
: newCandles.Where(c => c.Date > lastCandle.Date);
|
||||||
newCandles = await _exchangeService.GetCandles(account, ticker, startDate, timeframe, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
var candles = !lastCandles.Any() ? newCandles : newCandles.Where(c => c.Date > lastCandle?.Date);
|
|
||||||
var candlesInserted = 0;
|
var candlesInserted = 0;
|
||||||
|
|
||||||
foreach (var newCandle in candles)
|
foreach (var newCandle in candlesToInsert)
|
||||||
{
|
{
|
||||||
if (lastCandle == null || newCandle.Date > lastCandle.Date)
|
await _candleRepository.InsertCandle(newCandle).ConfigureAwait(false);
|
||||||
{
|
|
||||||
_candleRepository.InsertCandle(newCandle);
|
|
||||||
candlesInserted++;
|
candlesInserted++;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (candlesInserted > 0)
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
SentrySdk.CaptureException(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,4 +8,5 @@ public interface IInfluxDbRepository
|
|||||||
|
|
||||||
Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action);
|
Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action);
|
||||||
void Write(Action<WriteApi> action);
|
void Write(Action<WriteApi> action);
|
||||||
|
Task WriteAsync(Func<WriteApi, Task> action);
|
||||||
}
|
}
|
||||||
@@ -111,9 +111,9 @@ public class CandleRepository : ICandleRepository
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InsertCandle(Candle candle)
|
public async Task InsertCandle(Candle candle)
|
||||||
{
|
{
|
||||||
_influxDbRepository.Write(write =>
|
await _influxDbRepository.WriteAsync(write =>
|
||||||
{
|
{
|
||||||
PriceDto price = PriceHelpers.Map(candle);
|
PriceDto price = PriceHelpers.Map(candle);
|
||||||
write.WriteMeasurement(
|
write.WriteMeasurement(
|
||||||
@@ -121,6 +121,7 @@ public class CandleRepository : ICandleRepository
|
|||||||
WritePrecision.Ns,
|
WritePrecision.Ns,
|
||||||
_priceBucket,
|
_priceBucket,
|
||||||
_influxDbRepository.Organization);
|
_influxDbRepository.Organization);
|
||||||
|
return Task.CompletedTask;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ public class InfluxDbRepository : IInfluxDbRepository
|
|||||||
action(write);
|
action(write);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task WriteAsync(Func<WriteApi, Task> action)
|
||||||
|
{
|
||||||
|
// Get write API asynchronously
|
||||||
|
using var client = new InfluxDBClient(_url, _token);
|
||||||
|
using var write = client.GetWriteApi();
|
||||||
|
return action(write);
|
||||||
|
}
|
||||||
public async Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action)
|
public async Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action)
|
||||||
{
|
{
|
||||||
using var client = InfluxDBClientFactory.Create(_url, _token);
|
using var client = InfluxDBClientFactory.Create(_url, _token);
|
||||||
|
|||||||
Reference in New Issue
Block a user