diff --git a/KLHZ.Trader.Core.Contracts/Messaging/Dtos/Interfaces/INewPrice.cs b/KLHZ.Trader.Core.Contracts/Messaging/Dtos/Interfaces/INewPrice.cs index 2b27275..9ad2df7 100644 --- a/KLHZ.Trader.Core.Contracts/Messaging/Dtos/Interfaces/INewPrice.cs +++ b/KLHZ.Trader.Core.Contracts/Messaging/Dtos/Interfaces/INewPrice.cs @@ -7,6 +7,7 @@ public string Figi { get; set; } public string Ticker { get; set; } public DateTime Time { get; set; } - public bool IsSellPrice { get; set; } + public long Count { get; set; } + public int Direction { get; set; } } } diff --git a/KLHZ.Trader.Core.Contracts/Messaging/Dtos/NewPriceMessage.cs b/KLHZ.Trader.Core.Contracts/Messaging/Dtos/NewPriceMessage.cs index 4a99c5f..4da2112 100644 --- a/KLHZ.Trader.Core.Contracts/Messaging/Dtos/NewPriceMessage.cs +++ b/KLHZ.Trader.Core.Contracts/Messaging/Dtos/NewPriceMessage.cs @@ -1,5 +1,4 @@ using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces; -using System.ComponentModel.DataAnnotations.Schema; namespace KLHZ.Trader.Core.Contracts.Messaging.Dtos { @@ -10,7 +9,7 @@ namespace KLHZ.Trader.Core.Contracts.Messaging.Dtos public required string Ticker { get; set; } public DateTime Time { get; set; } public bool IsHistoricalData { get; set; } - [NotMapped] - public bool IsSellPrice { get; set; } + public long Count { get; set; } + public int Direction { get; set; } } } diff --git a/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs b/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs index 9c1b788..83debc3 100644 --- a/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs +++ b/KLHZ.Trader.Core.Math/Declisions/Utils/MovingAverage.cs @@ -20,9 +20,9 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils return (startTime, sum / count); } - public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps, - decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart, - decimal downtrendStartingDetectionMeanfullStep = 3m, decimal uptrendEndingDetectionMeanfullStep = 3m) + public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps, + decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart, + decimal uptrendStartingDetectionMeanfullStep = 0m, decimal uptrendEndingDetectionMeanfullStep = 3m) { var res = TradingEvent.None; var bigWindowAv = 0m; @@ -112,7 +112,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils // если фильтрация окном 120 наползает на окно 15 сверху, потенциальное время открытия лонга и закрытия шорта if (twavss[size - 1] >= twavbs[size - 1] && twavss[size - 2] < twavbs[size - 2]) { - if (pricesForFinalComparison[crossings[0]] - pricesForFinalComparison[crossings[1]] <= downtrendStartingDetectionMeanfullStep + if (pricesForFinalComparison[crossings[0]] - pricesForFinalComparison[crossings[1]] <= uptrendStartingDetectionMeanfullStep && times[crossings[0]] - times[crossings[1]] >= timeForUptreandStart) { res |= TradingEvent.UptrendStart; diff --git a/KLHZ.Trader.Core/DataLayer/Entities/Prices/PriceChange.cs b/KLHZ.Trader.Core/DataLayer/Entities/Prices/PriceChange.cs index 3d5be3e..3ecade0 100644 --- a/KLHZ.Trader.Core/DataLayer/Entities/Prices/PriceChange.cs +++ b/KLHZ.Trader.Core/DataLayer/Entities/Prices/PriceChange.cs @@ -23,7 +23,10 @@ namespace KLHZ.Trader.Core.DataLayer.Entities.Prices [NotMapped] public bool IsHistoricalData { get; set; } - [NotMapped] - public bool IsSellPrice { get; set; } + [Column("count")] + public long Count { get; set; } + + [Column("direction")] + public int Direction { get; set; } } } diff --git a/KLHZ.Trader.Core/DataLayer/Entities/Prices/ProcessedPrice.cs b/KLHZ.Trader.Core/DataLayer/Entities/Prices/ProcessedPrice.cs index eea0bd1..0b28df1 100644 --- a/KLHZ.Trader.Core/DataLayer/Entities/Prices/ProcessedPrice.cs +++ b/KLHZ.Trader.Core/DataLayer/Entities/Prices/ProcessedPrice.cs @@ -27,6 +27,9 @@ namespace KLHZ.Trader.Core.DataLayer.Entities.Prices public required string Processor { get; set; } [NotMapped] - public bool IsSellPrice { get; set; } + public long Count { get; set; } + + [NotMapped] + public int Direction { get; set; } } } diff --git a/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs b/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs index 021f3d3..9ea8d21 100644 --- a/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs @@ -57,10 +57,11 @@ namespace KLHZ.Trader.Core.Exchange.Services { try { - //_ = SubscribeMyTrades(); if (_exchangeDataRecievingEnabled) { - await SubscribePrices(); + var t1 = SubscribePrices(); + var t2 = SubscribeMyTrades(); + await Task.WhenAll(t1, t2); } await Task.Delay(1000); } @@ -88,7 +89,6 @@ namespace KLHZ.Trader.Core.Exchange.Services { try { - await _semaphoreSlim.WaitAsync(TimeSpan.FromSeconds(5)); await _tradeDataProvider.SyncPortfolio(response.Portfolio.AccountId); } catch (Exception ex) @@ -155,22 +155,23 @@ namespace KLHZ.Trader.Core.Exchange.Services Time = response.Trade.Time.ToDateTime().ToUniversalTime(), Value = response.Trade.Price, IsHistoricalData = false, - IsSellPrice = response.Trade.Direction == TradeDirection.Sell + Direction = (int)TradeDirection.Sell, + Count = response.Trade.Quantity, }; await _eventBus.Broadcast(message); pricesBuffer.Add(message); - var trade = new KLHZ.Trader.Core.DataLayer.Entities.Trades.InstrumentTrade() - { - Figi = response.Trade.Figi, - BoughtAt = response.Trade.Time.ToDateTime().ToUniversalTime(), - Ticker = _tradeDataProvider.GetTickerByFigi(response.Trade.Figi), - Price = response.Trade.Price, - Count = response.Trade.Quantity, - Direction = response.Trade.Direction == Tinkoff.InvestApi.V1.TradeDirection.Sell ? DataLayer.Entities.Trades.Enums.TradeDirection.Sell : DataLayer.Entities.Trades.Enums.TradeDirection.Buy, - }; - tradesBuffer.Add(trade); + //var trade = new KLHZ.Trader.Core.DataLayer.Entities.Trades.InstrumentTrade() + //{ + // Figi = response.Trade.Figi, + // BoughtAt = response.Trade.Time.ToDateTime().ToUniversalTime(), + // Ticker = _tradeDataProvider.GetTickerByFigi(response.Trade.Figi), + // Price = response.Trade.Price, + // Count = response.Trade.Quantity, + // Direction = response.Trade.Direction == Tinkoff.InvestApi.V1.TradeDirection.Sell ? DataLayer.Entities.Trades.Enums.TradeDirection.Sell : DataLayer.Entities.Trades.Enums.TradeDirection.Buy, + //}; + //tradesBuffer.Add(trade); } if (response.Orderbook != null) { diff --git a/KLHZ.Trader.Core/Exchange/Services/Trader.cs b/KLHZ.Trader.Core/Exchange/Services/Trader.cs index 0404c51..d6d577a 100644 --- a/KLHZ.Trader.Core/Exchange/Services/Trader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/Trader.cs @@ -146,7 +146,7 @@ namespace KLHZ.Trader.Core.Exchange.Services { var res = TradingEvent.None; var resultMoveAvFull = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, - windowMaxSize, 30, 180, TimeSpan.FromSeconds(20), 1m, 1.5m); + windowMaxSize, 30, 180, TimeSpan.FromSeconds(20), -1m, 2m); res |= resultMoveAvFull.events; @@ -163,8 +163,8 @@ namespace KLHZ.Trader.Core.Exchange.Services && (data.timestamps[data.timestamps.Length - 1] - data.timestamps[data.timestamps.Length - 2] < TimeSpan.FromMinutes(1)) ) { - var fullData = await _tradeDataProvider.GetData(message.Figi, TimeSpan.FromMinutes(30)); - if (fullData.isFullIntervalExists) + //var fullData = await _tradeDataProvider.GetData(message.Figi, TimeSpan.FromMinutes(30)); + //if (fullData.isFullIntervalExists && fullData.prices.Last() - fullData.prices.First()>-8) { if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) { diff --git a/KLHZ.Trader.Service/Controllers/PlayController.cs b/KLHZ.Trader.Service/Controllers/PlayController.cs index ba480ab..9dc2761 100644 --- a/KLHZ.Trader.Service/Controllers/PlayController.cs +++ b/KLHZ.Trader.Service/Controllers/PlayController.cs @@ -24,7 +24,7 @@ namespace KLHZ.Trader.Service.Controllers { try { - var time = DateTime.UtcNow.AddMinutes(-120); + var time = DateTime.UtcNow.AddDays(-7); using var context1 = await _dbContextFactory.CreateDbContextAsync(); context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var data = await context1.PriceChanges @@ -51,26 +51,30 @@ namespace KLHZ.Trader.Service.Controllers } } - ////[HttpGet] - //public async Task LoadTradesToHistory(string figi) + //[HttpGet] + //public async Task LoadTradesToHistory() //{ // try // { // using var context1 = await _dbContextFactory.CreateDbContextAsync(); // context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; // var data = await context1.InstrumentTrades - // .Where(c => c.Figi == figi) - // .OrderBy(c => c.BoughtAt) + // //.Where(c => c.Figi == figi) + // //.OrderBy(c => c.BoughtAt) // .Select(c => new PriceChange() // { - // Figi = figi, + // Figi = c.Figi, // Ticker = c.Ticker, // Time = c.BoughtAt, // Value = c.Price, - // IsHistoricalData = true + // IsHistoricalData = true, + // Count = (long)c.Count, + // Direction = (int)c.Direction, + // }) // .ToArrayAsync(); - // await context1.PriceChanges.Where(p => p.Figi == figi).ExecuteDeleteAsync(); + // data = data.DistinctBy(d => new { d.Figi, d.Time }).ToArray(); + // //await context1.PriceChanges.Where(p => p.Figi == figi).ExecuteDeleteAsync(); // await context1.PriceChanges.AddRangeAsync(data); // await context1.SaveChangesAsync(); // }