Фикс бага с потоком данных
test / deploy_trader_prod (push) Successful in 33s Details

dev
vlad zverzhkhovskiy 2025-09-09 17:13:42 +03:00
parent 90d0abe1e6
commit f801620c8a
8 changed files with 47 additions and 36 deletions

View File

@ -7,6 +7,7 @@
public string Figi { get; set; } public string Figi { get; set; }
public string Ticker { get; set; } public string Ticker { get; set; }
public DateTime Time { get; set; } public DateTime Time { get; set; }
public bool IsSellPrice { get; set; } public long Count { get; set; }
public int Direction { get; set; }
} }
} }

View File

@ -1,5 +1,4 @@
using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces; using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces;
using System.ComponentModel.DataAnnotations.Schema;
namespace KLHZ.Trader.Core.Contracts.Messaging.Dtos 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 required string Ticker { get; set; }
public DateTime Time { get; set; } public DateTime Time { get; set; }
public bool IsHistoricalData { get; set; } public bool IsHistoricalData { get; set; }
[NotMapped] public long Count { get; set; }
public bool IsSellPrice { get; set; } public int Direction { get; set; }
} }
} }

View File

@ -22,7 +22,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils
public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps, public static (TradingEvent events, decimal bigWindowAv, decimal smallWindowAv) CheckByWindowAverageMean(DateTime[] timestamps,
decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart, decimal[] prices, int size, int smallWindow, int bigWindow, TimeSpan timeForUptreandStart,
decimal downtrendStartingDetectionMeanfullStep = 3m, decimal uptrendEndingDetectionMeanfullStep = 3m) decimal uptrendStartingDetectionMeanfullStep = 0m, decimal uptrendEndingDetectionMeanfullStep = 3m)
{ {
var res = TradingEvent.None; var res = TradingEvent.None;
var bigWindowAv = 0m; var bigWindowAv = 0m;
@ -112,7 +112,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Utils
// если фильтрация окном 120 наползает на окно 15 сверху, потенциальное время открытия лонга и закрытия шорта // если фильтрация окном 120 наползает на окно 15 сверху, потенциальное время открытия лонга и закрытия шорта
if (twavss[size - 1] >= twavbs[size - 1] && twavss[size - 2] < twavbs[size - 2]) 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) && times[crossings[0]] - times[crossings[1]] >= timeForUptreandStart)
{ {
res |= TradingEvent.UptrendStart; res |= TradingEvent.UptrendStart;

View File

@ -23,7 +23,10 @@ namespace KLHZ.Trader.Core.DataLayer.Entities.Prices
[NotMapped] [NotMapped]
public bool IsHistoricalData { get; set; } public bool IsHistoricalData { get; set; }
[NotMapped] [Column("count")]
public bool IsSellPrice { get; set; } public long Count { get; set; }
[Column("direction")]
public int Direction { get; set; }
} }
} }

View File

@ -27,6 +27,9 @@ namespace KLHZ.Trader.Core.DataLayer.Entities.Prices
public required string Processor { get; set; } public required string Processor { get; set; }
[NotMapped] [NotMapped]
public bool IsSellPrice { get; set; } public long Count { get; set; }
[NotMapped]
public int Direction { get; set; }
} }
} }

View File

@ -57,10 +57,11 @@ namespace KLHZ.Trader.Core.Exchange.Services
{ {
try try
{ {
//_ = SubscribeMyTrades();
if (_exchangeDataRecievingEnabled) if (_exchangeDataRecievingEnabled)
{ {
await SubscribePrices(); var t1 = SubscribePrices();
var t2 = SubscribeMyTrades();
await Task.WhenAll(t1, t2);
} }
await Task.Delay(1000); await Task.Delay(1000);
} }
@ -88,7 +89,6 @@ namespace KLHZ.Trader.Core.Exchange.Services
{ {
try try
{ {
await _semaphoreSlim.WaitAsync(TimeSpan.FromSeconds(5));
await _tradeDataProvider.SyncPortfolio(response.Portfolio.AccountId); await _tradeDataProvider.SyncPortfolio(response.Portfolio.AccountId);
} }
catch (Exception ex) catch (Exception ex)
@ -155,22 +155,23 @@ namespace KLHZ.Trader.Core.Exchange.Services
Time = response.Trade.Time.ToDateTime().ToUniversalTime(), Time = response.Trade.Time.ToDateTime().ToUniversalTime(),
Value = response.Trade.Price, Value = response.Trade.Price,
IsHistoricalData = false, IsHistoricalData = false,
IsSellPrice = response.Trade.Direction == TradeDirection.Sell Direction = (int)TradeDirection.Sell,
Count = response.Trade.Quantity,
}; };
await _eventBus.Broadcast(message); await _eventBus.Broadcast(message);
pricesBuffer.Add(message); pricesBuffer.Add(message);
var trade = new KLHZ.Trader.Core.DataLayer.Entities.Trades.InstrumentTrade() //var trade = new KLHZ.Trader.Core.DataLayer.Entities.Trades.InstrumentTrade()
{ //{
Figi = response.Trade.Figi, // Figi = response.Trade.Figi,
BoughtAt = response.Trade.Time.ToDateTime().ToUniversalTime(), // BoughtAt = response.Trade.Time.ToDateTime().ToUniversalTime(),
Ticker = _tradeDataProvider.GetTickerByFigi(response.Trade.Figi), // Ticker = _tradeDataProvider.GetTickerByFigi(response.Trade.Figi),
Price = response.Trade.Price, // Price = response.Trade.Price,
Count = response.Trade.Quantity, // 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, // Direction = response.Trade.Direction == Tinkoff.InvestApi.V1.TradeDirection.Sell ? DataLayer.Entities.Trades.Enums.TradeDirection.Sell : DataLayer.Entities.Trades.Enums.TradeDirection.Buy,
}; //};
tradesBuffer.Add(trade); //tradesBuffer.Add(trade);
} }
if (response.Orderbook != null) if (response.Orderbook != null)
{ {

View File

@ -146,7 +146,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
{ {
var res = TradingEvent.None; var res = TradingEvent.None;
var resultMoveAvFull = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, 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; 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)) && (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)); //var fullData = await _tradeDataProvider.GetData(message.Figi, TimeSpan.FromMinutes(30));
if (fullData.isFullIntervalExists) //if (fullData.isFullIntervalExists && fullData.prices.Last() - fullData.prices.First()>-8)
{ {
if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase())
{ {

View File

@ -24,7 +24,7 @@ namespace KLHZ.Trader.Service.Controllers
{ {
try try
{ {
var time = DateTime.UtcNow.AddMinutes(-120); var time = DateTime.UtcNow.AddDays(-7);
using var context1 = await _dbContextFactory.CreateDbContextAsync(); using var context1 = await _dbContextFactory.CreateDbContextAsync();
context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var data = await context1.PriceChanges var data = await context1.PriceChanges
@ -51,26 +51,30 @@ namespace KLHZ.Trader.Service.Controllers
} }
} }
////[HttpGet] //[HttpGet]
//public async Task LoadTradesToHistory(string figi) //public async Task LoadTradesToHistory()
//{ //{
// try // try
// { // {
// using var context1 = await _dbContextFactory.CreateDbContextAsync(); // using var context1 = await _dbContextFactory.CreateDbContextAsync();
// context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; // context1.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
// var data = await context1.InstrumentTrades // var data = await context1.InstrumentTrades
// .Where(c => c.Figi == figi) // //.Where(c => c.Figi == figi)
// .OrderBy(c => c.BoughtAt) // //.OrderBy(c => c.BoughtAt)
// .Select(c => new PriceChange() // .Select(c => new PriceChange()
// { // {
// Figi = figi, // Figi = c.Figi,
// Ticker = c.Ticker, // Ticker = c.Ticker,
// Time = c.BoughtAt, // Time = c.BoughtAt,
// Value = c.Price, // Value = c.Price,
// IsHistoricalData = true // IsHistoricalData = true,
// Count = (long)c.Count,
// Direction = (int)c.Direction,
// }) // })
// .ToArrayAsync(); // .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.PriceChanges.AddRangeAsync(data);
// await context1.SaveChangesAsync(); // await context1.SaveChangesAsync();
// } // }