From fcf686a9d51df3128482f6e03242f7373da16a57 Mon Sep 17 00:00:00 2001 From: vlad zverzhkhovskiy Date: Fri, 12 Sep 2025 10:24:30 +0300 Subject: [PATCH] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D0=B0=D0=B2=D0=B0?= =?UTF-8?q?=D1=80=D0=B8=D0=B9=D0=BD=D0=BE=D0=B9=20=D0=BF=D1=80=D0=BE=D0=B4?= =?UTF-8?q?=D0=B0=D0=B6=D0=B8=20=D0=B0=D0=BA=D1=82=D0=B8=D0=B2=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Declisions/Dtos/CachedValue.cs | 14 ++++ .../Services/Cache/PriceHistoryCacheUnit2.cs | 8 +++ KLHZ.Trader.Core/Exchange/Services/Trader.cs | 70 ++----------------- 3 files changed, 28 insertions(+), 64 deletions(-) create mode 100644 KLHZ.Trader.Core.Math/Declisions/Dtos/CachedValue.cs diff --git a/KLHZ.Trader.Core.Math/Declisions/Dtos/CachedValue.cs b/KLHZ.Trader.Core.Math/Declisions/Dtos/CachedValue.cs new file mode 100644 index 0000000..d82f824 --- /dev/null +++ b/KLHZ.Trader.Core.Math/Declisions/Dtos/CachedValue.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace KLHZ.Trader.Core.Math.Declisions.Dtos +{ + public class CachedValue + { + public DateTime Time { get; init; } + public decimal Value { get; init; } + } +} diff --git a/KLHZ.Trader.Core.Math/Declisions/Services/Cache/PriceHistoryCacheUnit2.cs b/KLHZ.Trader.Core.Math/Declisions/Services/Cache/PriceHistoryCacheUnit2.cs index bff206d..8295da5 100644 --- a/KLHZ.Trader.Core.Math/Declisions/Services/Cache/PriceHistoryCacheUnit2.cs +++ b/KLHZ.Trader.Core.Math/Declisions/Services/Cache/PriceHistoryCacheUnit2.cs @@ -1,5 +1,7 @@ using KLHZ.Trader.Core.Contracts.Declisions.Interfaces; using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces; +using KLHZ.Trader.Core.Math.Declisions.Dtos; +using System.Collections.Concurrent; namespace KLHZ.Trader.Core.Math.Declisions.Services.Cache { @@ -46,6 +48,7 @@ namespace KLHZ.Trader.Core.Math.Declisions.Services.Cache private readonly object _locker = new(); private readonly decimal[] Prices = new decimal[_arrayMaxLength]; private readonly DateTime[] Timestamps = new DateTime[_arrayMaxLength]; + private readonly ConcurrentDictionary> TimeWindows = new(); private int _length = 0; private int _pointer = -1; @@ -53,6 +56,11 @@ namespace KLHZ.Trader.Core.Math.Declisions.Services.Cache private long _asksCount = 1; private long _bidsCount = 1; + public ValueTask AddDataToTimeWindowCache(string key, CachedValue data, TimeSpan window) + { + return ValueTask.CompletedTask; + } + public ValueTask AddData(INewPrice priceChange) { if (priceChange.Figi != Figi) return ValueTask.CompletedTask; diff --git a/KLHZ.Trader.Core/Exchange/Services/Trader.cs b/KLHZ.Trader.Core/Exchange/Services/Trader.cs index abf3481..d17f66c 100644 --- a/KLHZ.Trader.Core/Exchange/Services/Trader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/Trader.cs @@ -34,7 +34,6 @@ namespace KLHZ.Trader.Core.Exchange.Services private readonly string _bigWindowProcessor = nameof(Trader) + "_big"; private readonly string _smallWindowProcessor = nameof(Trader) + "_small"; - private readonly double _buyStopLength; private readonly decimal _futureComission; private readonly decimal _shareComission; private readonly decimal _accountCashPart; @@ -59,7 +58,6 @@ namespace KLHZ.Trader.Core.Exchange.Services _accountCashPart = options.Value.AccountCashPart; _accountCashPartFutures = options.Value.AccountCashPartFutures; _tradingInstrumentsFigis = options.Value.TradingInstrumentsFigis; - _buyStopLength = (double)options.Value.StopBuyLengthMinuts; foreach (var lev in options.Value.InstrumentsSettings) { @@ -78,9 +76,6 @@ namespace KLHZ.Trader.Core.Exchange.Services private async Task ProcessPrices() { var buffer = new LinkedList<(DateTime, double)>(); - var tradesBufferBuys = new LinkedList<(DateTime, double)>(); - var tradesBufferSells = new LinkedList<(DateTime, double)>(); - var tradesRelBuffer = new LinkedList<(DateTime, decimal)>(); while (await _pricesChannel.Reader.WaitToReadAsync()) { var message = await _pricesChannel.Reader.ReadAsync(); @@ -94,8 +89,6 @@ namespace KLHZ.Trader.Core.Exchange.Services try { - //await ProcessDeferredLongOpens(message, currentTime); - //await ProcessDeferredLongCloses(message, currentTime); if (message.Figi == "FUTIMOEXF000") { var windowMaxSize = 1000; @@ -107,10 +100,9 @@ namespace KLHZ.Trader.Core.Exchange.Services } var state = ExchangeScheduler.GetCurrentState(message.Time); await ProcessClearing(data, state, message); - //await SellOldAssetsIfCan(message); ProcessOpeningStops(message, currentTime); - await ProcessNewPriceIMOEXF(data, state, message, windowMaxSize, buffer, tradesBufferBuys, tradesBufferSells, tradesRelBuffer); + await ProcessNewPriceIMOEXF(data, state, message, windowMaxSize, buffer); } } catch (Exception ex) @@ -123,6 +115,10 @@ namespace KLHZ.Trader.Core.Exchange.Services private async Task SellAssetsIfNeed(INewPrice message) { + if (!BotModeSwitcher.CanSell()) + { + return; + } var accounts = _tradeDataProvider.Accounts.Values.ToArray(); var assetType = _tradeDataProvider.GetAssetTypeByFigi(message.Figi); foreach (var acc in accounts) @@ -169,9 +165,7 @@ namespace KLHZ.Trader.Core.Exchange.Services private async Task ProcessNewPriceIMOEXF((DateTime[] timestamps, decimal[] prices) data, ExchangeState state, - INewPrice message, int windowMaxSize, LinkedList<(DateTime time, double val)> areasBuffer, - LinkedList<(DateTime time, double val)> tradesBufferBuys, LinkedList<(DateTime time, double val)> tradesBufferSells, - LinkedList<(DateTime time, decimal val)> tradesRelBufferSells) + INewPrice message, int windowMaxSize, LinkedList<(DateTime time, double val)> areasBuffer) { var res = TradingEvent.None; var resultMoveAvFull = MovingAverage.CheckByWindowAverageMean(data.timestamps, data.prices, @@ -185,58 +179,6 @@ namespace KLHZ.Trader.Core.Exchange.Services await LogPrice(message, _smallWindowProcessor, resultMoveAvFull.smallWindowAv); } - //var oldTotalSales = (decimal)tradesBufferSells.Sum(s => s.val); - //var oldTotalBuys = (decimal)tradesBufferBuys.Sum(s => s.val); - //var oldTotalTrades = oldTotalSales + oldTotalBuys; - - //if (message.Direction == 1) - //{ - // tradesBufferBuys.AddLast((message.Time, message.Count)); - - // if (tradesBufferBuys.Last != null && tradesBufferBuys.First != null - // && tradesBufferBuys.Last.Value.time - tradesBufferBuys.First.Value.time > TimeSpan.FromSeconds(60)) - // { - // tradesBufferBuys.RemoveFirst(); - // } - //} - - //if (message.Direction == 2) - //{ - // tradesBufferSells.AddLast((message.Time, message.Count)); - - // if (tradesBufferSells.Last != null && tradesBufferSells.First != null - // && tradesBufferSells.Last.Value.time - tradesBufferSells.First.Value.time > TimeSpan.FromSeconds(60)) - // { - // tradesBufferSells.RemoveFirst(); - // } - //} - - //var totalSales = (decimal)tradesBufferSells.Sum(s => s.val); - //var totalBuys = (decimal)tradesBufferBuys.Sum(s => s.val); - //var totalTrades = totalSales + totalBuys; - //var tradesRelation = -100m; - //var oldTradesRelation = -100m; - //await LogPrice(message, "tradesvolume", totalTrades); - - //if (totalTrades > 0 && oldTotalTrades > 0) - //{ - // tradesRelation = (totalBuys - totalSales) / totalTrades; - // oldTradesRelation = (oldTotalBuys - oldTotalSales) / oldTotalTrades; - - // tradesRelBufferSells.AddLast((message.Time, tradesRelation - oldTradesRelation)); - - // if (tradesRelBufferSells.Last != null && tradesRelBufferSells.First != null - // && tradesRelBufferSells.Last.Value.time - tradesRelBufferSells.First.Value.time > TimeSpan.FromSeconds(10)) - // { - // tradesRelBufferSells.RemoveFirst(); - // } - - // if (tradesRelBufferSells.Count > 0) - // { - // await LogPrice(message, "tradesrelation", tradesRelBufferSells.Sum(e => e.val) / tradesRelBufferSells.Count); - // } - //} - var areasRel = -1m; if (ShapeAreaCalculator.TryGetAreasRelation(data.timestamps, data.prices, message.Value, TimeSpan.FromMinutes(15), out var rel)) {