From f49d4a9134e4375bf3aa6f900411573b4ae5cf57 Mon Sep 17 00:00:00 2001 From: vlad zverzhkhovskiy Date: Mon, 20 Oct 2025 16:32:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D1=85=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=B5=D0=BA=D1=86=D0=B8=D0=BE=D0=BD=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=BA=D0=B0=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KLHZ.Trader.Core/Common/BotModeSwitcher.cs | 28 ++++++++-------- KLHZ.Trader.Core/Common/Constants.cs | 12 ++++--- KLHZ.Trader.Core/DataLayer/TraderDbContext.cs | 4 +++ .../Exchange/Services/ExchangeDataReader.cs | 8 ++--- KLHZ.Trader.Core/Exchange/Services/Trader.cs | 8 ++--- .../Exchange/Utils/TraderUtils.cs | 2 +- .../TG/Services/BotMessagesHandler.cs | 32 +++++++++++-------- 7 files changed, 53 insertions(+), 41 deletions(-) diff --git a/KLHZ.Trader.Core/Common/BotModeSwitcher.cs b/KLHZ.Trader.Core/Common/BotModeSwitcher.cs index 438658c..9d6b9cc 100644 --- a/KLHZ.Trader.Core/Common/BotModeSwitcher.cs +++ b/KLHZ.Trader.Core/Common/BotModeSwitcher.cs @@ -3,43 +3,43 @@ public static class BotModeSwitcher { private readonly static object _locker = new(); - private static bool _canSell = true; - private static bool _canPurchase = true; + private static bool _canClose = true; + private static bool _canOpen = false; - public static bool CanSell() + public static bool CanClose() { lock (_locker) - return _canSell; + return _canClose; } - public static bool CanPurchase() + public static bool CanOpen() { lock (_locker) - return _canPurchase; + return _canOpen; } - public static void StopSelling() + public static void StopClosing() { lock (_locker) - _canSell = false; + _canClose = false; } - public static void StopPurchase() + public static void StopOpening() { lock (_locker) - _canPurchase = false; + _canOpen = false; } - public static void StartSelling() + public static void StartClosing() { lock (_locker) - _canSell = true; + _canClose = true; } - public static void StartPurchase() + public static void StartOpening() { lock (_locker) - _canPurchase = true; + _canOpen = true; } } } diff --git a/KLHZ.Trader.Core/Common/Constants.cs b/KLHZ.Trader.Core/Common/Constants.cs index e5ec482..22fdf2d 100644 --- a/KLHZ.Trader.Core/Common/Constants.cs +++ b/KLHZ.Trader.Core/Common/Constants.cs @@ -9,11 +9,15 @@ public const string DisableTrading = "Стоп торги"; public const string EnableTrading = "Старт торги"; - public const string DisableSelling = "Выключить продажи"; - public const string EnableSelling = "Включить продажи"; + public const string DisableClosing = "Выключить продажи"; + public const string DisableClosing2 = "Выключить закрытия"; + public const string EnableClosing = "Включить продажи"; + public const string EnableClosing2 = "Включить закрытия"; - public const string DisablePurchases = "Выключить покупки"; - public const string EnablePurchases = "Включить покупки"; + public const string DisableOpening = "Выключить покупки"; + public const string DisableOpening2 = "Выключить открытия"; + public const string EnableOpening = "Включить покупки"; + public const string EnableOpening2 = "Включить открытия"; } } } diff --git a/KLHZ.Trader.Core/DataLayer/TraderDbContext.cs b/KLHZ.Trader.Core/DataLayer/TraderDbContext.cs index 0673cd7..702a8c6 100644 --- a/KLHZ.Trader.Core/DataLayer/TraderDbContext.cs +++ b/KLHZ.Trader.Core/DataLayer/TraderDbContext.cs @@ -59,6 +59,8 @@ namespace KLHZ.Trader.Core.DataLayer { entity.HasKey(e1 => new { e1.Time, e1.Id }); entity.Ignore(e1 => e1.IsHistoricalData); + entity.Property(p => p.Id) + .ValueGeneratedOnAdd(); entity.Property(e => e.Time) .HasConversion( v => v.ToUniversalTime(), @@ -77,6 +79,8 @@ namespace KLHZ.Trader.Core.DataLayer modelBuilder.Entity(entity => { entity.HasKey(e1 => new { e1.Time, e1.Id }); + entity.Property(p => p.Id) + .ValueGeneratedOnAdd(); entity.Property(e => e.Time) .HasConversion( v => v.ToUniversalTime(), diff --git a/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs b/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs index 7f6112f..7303384 100644 --- a/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/ExchangeDataReader.cs @@ -139,7 +139,7 @@ namespace KLHZ.Trader.Core.Exchange.Services var lastUpdateDict = new Dictionary(); var pricesBuffer = new List(); - var orderbookItemsBuffer = new List(); + var orderbookItemsBuffer = new List(); var lastWrite = DateTime.UtcNow; await foreach (var response in stream.ResponseStream.ReadAllAsync()) { @@ -162,7 +162,7 @@ namespace KLHZ.Trader.Core.Exchange.Services if (response.Orderbook != null) { - var asks = response.Orderbook.Asks.Take(4).Select(a => new OrderbookItem() + var asks = response.Orderbook.Asks.Take(4).Select(a => new OrderbookElement() { Count = a.Quantity, Price = a.Price, @@ -172,7 +172,7 @@ namespace KLHZ.Trader.Core.Exchange.Services Time = response.Orderbook.Time.ToDateTime().ToUniversalTime(), }).ToArray(); - var bids = response.Orderbook.Bids.Take(4).Select(a => new OrderbookItem() + var bids = response.Orderbook.Bids.Take(4).Select(a => new OrderbookElement() { Count = a.Quantity, Price = a.Price, @@ -208,7 +208,7 @@ namespace KLHZ.Trader.Core.Exchange.Services lastWrite = DateTime.UtcNow; if (orderbookItemsBuffer.Count > 0) { - await context.OrderbookItems.AddRangeAsync(orderbookItemsBuffer); + await context.OrderbookElements.AddRangeAsync(orderbookItemsBuffer); orderbookItemsBuffer.Clear(); } if (pricesBuffer.Count > 0) diff --git a/KLHZ.Trader.Core/Exchange/Services/Trader.cs b/KLHZ.Trader.Core/Exchange/Services/Trader.cs index 697a92e..0d97963 100644 --- a/KLHZ.Trader.Core/Exchange/Services/Trader.cs +++ b/KLHZ.Trader.Core/Exchange/Services/Trader.cs @@ -719,7 +719,7 @@ namespace KLHZ.Trader.Core.Exchange.Services { var stops = st.GetStops(PositionType.Long); - if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) + if (!message.IsHistoricalData && BotModeSwitcher.CanOpen()) { var accounts = _portfolioWrapper.Accounts .Where(a => !a.Value.Assets.ContainsKey(message.Figi)) @@ -740,7 +740,7 @@ namespace KLHZ.Trader.Core.Exchange.Services ) { var stops = st.GetStops(PositionType.Short); - if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) + if (!message.IsHistoricalData && BotModeSwitcher.CanOpen()) { var accounts = _portfolioWrapper.Accounts .Where(a => !a.Value.Assets.ContainsKey(message.Figi)) @@ -758,7 +758,7 @@ namespace KLHZ.Trader.Core.Exchange.Services } if (result[TradingEvent.CloseLong] >= Constants.UppingCoefficient) { - if (!message.IsHistoricalData && BotModeSwitcher.CanSell()) + if (!message.IsHistoricalData && BotModeSwitcher.CanClose()) { var assetsForClose = _portfolioWrapper.Accounts .SelectMany(a => a.Value.Assets.Values) @@ -772,7 +772,7 @@ namespace KLHZ.Trader.Core.Exchange.Services if (result[TradingEvent.CloseShort] >= Constants.UppingCoefficient) { - if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) + if (!message.IsHistoricalData && BotModeSwitcher.CanClose()) { var assetsForClose = _portfolioWrapper.Accounts .SelectMany(a => a.Value.Assets.Values) diff --git a/KLHZ.Trader.Core/Exchange/Utils/TraderUtils.cs b/KLHZ.Trader.Core/Exchange/Utils/TraderUtils.cs index 4a190b5..637cf6a 100644 --- a/KLHZ.Trader.Core/Exchange/Utils/TraderUtils.cs +++ b/KLHZ.Trader.Core/Exchange/Utils/TraderUtils.cs @@ -42,7 +42,7 @@ namespace KLHZ.Trader.Core.Exchange.Utils internal static bool IsOperationAllowed(IManagedAccount account, decimal boutPrice, decimal count, decimal accountCashPartFutures, decimal accountCashPart) { - if (!BotModeSwitcher.CanPurchase()) return false; + if (!BotModeSwitcher.CanOpen()) return false; var balance = account.Balance; var total = account.Total; diff --git a/KLHZ.Trader.Core/TG/Services/BotMessagesHandler.cs b/KLHZ.Trader.Core/TG/Services/BotMessagesHandler.cs index d0106ab..b573ff1 100644 --- a/KLHZ.Trader.Core/TG/Services/BotMessagesHandler.cs +++ b/KLHZ.Trader.Core/TG/Services/BotMessagesHandler.cs @@ -45,35 +45,39 @@ namespace KLHZ.Trader.Core.TG.Services case "/start": { var replyKeyboardMarkup = new ReplyKeyboardMarkup(new[] { - new KeyboardButton[] { Constants.BotCommandsButtons.EnableSelling, Constants.BotCommandsButtons.DisableSelling}, - new KeyboardButton[] { Constants.BotCommandsButtons.EnablePurchases, Constants.BotCommandsButtons.DisablePurchases}}); + new KeyboardButton[] { Constants.BotCommandsButtons.EnableClosing2, Constants.BotCommandsButtons.DisableClosing2}, + new KeyboardButton[] { Constants.BotCommandsButtons.EnableOpening2, Constants.BotCommandsButtons.EnableOpening2}}); await botClient.SendMessage(update.Message.Chat, "Принято!", replyMarkup: replyKeyboardMarkup); break; } - case Constants.BotCommandsButtons.EnableSelling: + case Constants.BotCommandsButtons.EnableClosing: + case Constants.BotCommandsButtons.EnableClosing2: { - BotModeSwitcher.StartSelling(); - await botClient.SendMessage(update.Message.Chat, "Продажи начаты!"); + BotModeSwitcher.StartClosing(); + await botClient.SendMessage(update.Message.Chat, "Закрытия разрешены!"); break; } - case Constants.BotCommandsButtons.DisableSelling: + case Constants.BotCommandsButtons.DisableClosing: + case Constants.BotCommandsButtons.DisableClosing2: { - BotModeSwitcher.StopSelling(); - await botClient.SendMessage(update.Message.Chat, "Продажи остановлены!"); + BotModeSwitcher.StopClosing(); + await botClient.SendMessage(update.Message.Chat, "Закрытия запрещены!"); break; } - case Constants.BotCommandsButtons.EnablePurchases: + case Constants.BotCommandsButtons.EnableOpening: + case Constants.BotCommandsButtons.EnableOpening2: { - BotModeSwitcher.StartPurchase(); - await botClient.SendMessage(update.Message.Chat, "Покупки начаты!"); + BotModeSwitcher.StartOpening(); + await botClient.SendMessage(update.Message.Chat, "Открытия начаты!"); break; } - case Constants.BotCommandsButtons.DisablePurchases: + case Constants.BotCommandsButtons.DisableOpening: + case Constants.BotCommandsButtons.DisableOpening2: { - BotModeSwitcher.StopPurchase(); - await botClient.SendMessage(update.Message.Chat, "Покупки остановлены!"); + BotModeSwitcher.StopOpening(); + await botClient.SendMessage(update.Message.Chat, "Открытия остановлены!"); break; } case "скинуть IMOEXF":