Фикс бага с потоком данных
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 Ticker { 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 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; }
}
}

View File

@ -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;

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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)
{

View File

@ -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())
{

View File

@ -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();
// }