переход на секционированные стаканы
test / deploy_trader_prod (push) Successful in 8m4s Details

main
vlad zverzhkhovskiy 2025-10-20 16:32:32 +03:00
parent 9a5fedb41c
commit f49d4a9134
7 changed files with 53 additions and 41 deletions

View File

@ -3,43 +3,43 @@
public static class BotModeSwitcher public static class BotModeSwitcher
{ {
private readonly static object _locker = new(); private readonly static object _locker = new();
private static bool _canSell = true; private static bool _canClose = true;
private static bool _canPurchase = true; private static bool _canOpen = false;
public static bool CanSell() public static bool CanClose()
{ {
lock (_locker) lock (_locker)
return _canSell; return _canClose;
} }
public static bool CanPurchase() public static bool CanOpen()
{ {
lock (_locker) lock (_locker)
return _canPurchase; return _canOpen;
} }
public static void StopSelling() public static void StopClosing()
{ {
lock (_locker) lock (_locker)
_canSell = false; _canClose = false;
} }
public static void StopPurchase() public static void StopOpening()
{ {
lock (_locker) lock (_locker)
_canPurchase = false; _canOpen = false;
} }
public static void StartSelling() public static void StartClosing()
{ {
lock (_locker) lock (_locker)
_canSell = true; _canClose = true;
} }
public static void StartPurchase() public static void StartOpening()
{ {
lock (_locker) lock (_locker)
_canPurchase = true; _canOpen = true;
} }
} }
} }

View File

@ -9,11 +9,15 @@
public const string DisableTrading = "Стоп торги"; public const string DisableTrading = "Стоп торги";
public const string EnableTrading = "Старт торги"; public const string EnableTrading = "Старт торги";
public const string DisableSelling = "Выключить продажи"; public const string DisableClosing = "Выключить продажи";
public const string EnableSelling = "Включить продажи"; public const string DisableClosing2 = "Выключить закрытия";
public const string EnableClosing = "Включить продажи";
public const string EnableClosing2 = "Включить закрытия";
public const string DisablePurchases = "Выключить покупки"; public const string DisableOpening = "Выключить покупки";
public const string EnablePurchases = "Включить покупки"; public const string DisableOpening2 = "Выключить открытия";
public const string EnableOpening = "Включить покупки";
public const string EnableOpening2 = "Включить открытия";
} }
} }
} }

View File

@ -59,6 +59,8 @@ namespace KLHZ.Trader.Core.DataLayer
{ {
entity.HasKey(e1 => new { e1.Time, e1.Id }); entity.HasKey(e1 => new { e1.Time, e1.Id });
entity.Ignore(e1 => e1.IsHistoricalData); entity.Ignore(e1 => e1.IsHistoricalData);
entity.Property(p => p.Id)
.ValueGeneratedOnAdd();
entity.Property(e => e.Time) entity.Property(e => e.Time)
.HasConversion( .HasConversion(
v => v.ToUniversalTime(), v => v.ToUniversalTime(),
@ -77,6 +79,8 @@ namespace KLHZ.Trader.Core.DataLayer
modelBuilder.Entity<OrderbookElement>(entity => modelBuilder.Entity<OrderbookElement>(entity =>
{ {
entity.HasKey(e1 => new { e1.Time, e1.Id }); entity.HasKey(e1 => new { e1.Time, e1.Id });
entity.Property(p => p.Id)
.ValueGeneratedOnAdd();
entity.Property(e => e.Time) entity.Property(e => e.Time)
.HasConversion( .HasConversion(
v => v.ToUniversalTime(), v => v.ToUniversalTime(),

View File

@ -139,7 +139,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
var lastUpdateDict = new Dictionary<string, PriceChange>(); var lastUpdateDict = new Dictionary<string, PriceChange>();
var pricesBuffer = new List<Trade>(); var pricesBuffer = new List<Trade>();
var orderbookItemsBuffer = new List<OrderbookItem>(); var orderbookItemsBuffer = new List<OrderbookElement>();
var lastWrite = DateTime.UtcNow; var lastWrite = DateTime.UtcNow;
await foreach (var response in stream.ResponseStream.ReadAllAsync()) await foreach (var response in stream.ResponseStream.ReadAllAsync())
{ {
@ -162,7 +162,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
if (response.Orderbook != null) 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, Count = a.Quantity,
Price = a.Price, Price = a.Price,
@ -172,7 +172,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
Time = response.Orderbook.Time.ToDateTime().ToUniversalTime(), Time = response.Orderbook.Time.ToDateTime().ToUniversalTime(),
}).ToArray(); }).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, Count = a.Quantity,
Price = a.Price, Price = a.Price,
@ -208,7 +208,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
lastWrite = DateTime.UtcNow; lastWrite = DateTime.UtcNow;
if (orderbookItemsBuffer.Count > 0) if (orderbookItemsBuffer.Count > 0)
{ {
await context.OrderbookItems.AddRangeAsync(orderbookItemsBuffer); await context.OrderbookElements.AddRangeAsync(orderbookItemsBuffer);
orderbookItemsBuffer.Clear(); orderbookItemsBuffer.Clear();
} }
if (pricesBuffer.Count > 0) if (pricesBuffer.Count > 0)

View File

@ -719,7 +719,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
{ {
var stops = st.GetStops(PositionType.Long); var stops = st.GetStops(PositionType.Long);
if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) if (!message.IsHistoricalData && BotModeSwitcher.CanOpen())
{ {
var accounts = _portfolioWrapper.Accounts var accounts = _portfolioWrapper.Accounts
.Where(a => !a.Value.Assets.ContainsKey(message.Figi)) .Where(a => !a.Value.Assets.ContainsKey(message.Figi))
@ -740,7 +740,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
) )
{ {
var stops = st.GetStops(PositionType.Short); var stops = st.GetStops(PositionType.Short);
if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) if (!message.IsHistoricalData && BotModeSwitcher.CanOpen())
{ {
var accounts = _portfolioWrapper.Accounts var accounts = _portfolioWrapper.Accounts
.Where(a => !a.Value.Assets.ContainsKey(message.Figi)) .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 (result[TradingEvent.CloseLong] >= Constants.UppingCoefficient)
{ {
if (!message.IsHistoricalData && BotModeSwitcher.CanSell()) if (!message.IsHistoricalData && BotModeSwitcher.CanClose())
{ {
var assetsForClose = _portfolioWrapper.Accounts var assetsForClose = _portfolioWrapper.Accounts
.SelectMany(a => a.Value.Assets.Values) .SelectMany(a => a.Value.Assets.Values)
@ -772,7 +772,7 @@ namespace KLHZ.Trader.Core.Exchange.Services
if (result[TradingEvent.CloseShort] >= Constants.UppingCoefficient) if (result[TradingEvent.CloseShort] >= Constants.UppingCoefficient)
{ {
if (!message.IsHistoricalData && BotModeSwitcher.CanPurchase()) if (!message.IsHistoricalData && BotModeSwitcher.CanClose())
{ {
var assetsForClose = _portfolioWrapper.Accounts var assetsForClose = _portfolioWrapper.Accounts
.SelectMany(a => a.Value.Assets.Values) .SelectMany(a => a.Value.Assets.Values)

View File

@ -42,7 +42,7 @@ namespace KLHZ.Trader.Core.Exchange.Utils
internal static bool IsOperationAllowed(IManagedAccount account, decimal boutPrice, decimal count, internal static bool IsOperationAllowed(IManagedAccount account, decimal boutPrice, decimal count,
decimal accountCashPartFutures, decimal accountCashPart) decimal accountCashPartFutures, decimal accountCashPart)
{ {
if (!BotModeSwitcher.CanPurchase()) return false; if (!BotModeSwitcher.CanOpen()) return false;
var balance = account.Balance; var balance = account.Balance;
var total = account.Total; var total = account.Total;

View File

@ -45,35 +45,39 @@ namespace KLHZ.Trader.Core.TG.Services
case "/start": case "/start":
{ {
var replyKeyboardMarkup = new ReplyKeyboardMarkup(new[] { var replyKeyboardMarkup = new ReplyKeyboardMarkup(new[] {
new KeyboardButton[] { Constants.BotCommandsButtons.EnableSelling, Constants.BotCommandsButtons.DisableSelling}, new KeyboardButton[] { Constants.BotCommandsButtons.EnableClosing2, Constants.BotCommandsButtons.DisableClosing2},
new KeyboardButton[] { Constants.BotCommandsButtons.EnablePurchases, Constants.BotCommandsButtons.DisablePurchases}}); new KeyboardButton[] { Constants.BotCommandsButtons.EnableOpening2, Constants.BotCommandsButtons.EnableOpening2}});
await botClient.SendMessage(update.Message.Chat, "Принято!", replyMarkup: replyKeyboardMarkup); await botClient.SendMessage(update.Message.Chat, "Принято!", replyMarkup: replyKeyboardMarkup);
break; break;
} }
case Constants.BotCommandsButtons.EnableSelling: case Constants.BotCommandsButtons.EnableClosing:
case Constants.BotCommandsButtons.EnableClosing2:
{ {
BotModeSwitcher.StartSelling(); BotModeSwitcher.StartClosing();
await botClient.SendMessage(update.Message.Chat, "Продажи начаты!"); await botClient.SendMessage(update.Message.Chat, "Закрытия разрешены!");
break; break;
} }
case Constants.BotCommandsButtons.DisableSelling: case Constants.BotCommandsButtons.DisableClosing:
case Constants.BotCommandsButtons.DisableClosing2:
{ {
BotModeSwitcher.StopSelling(); BotModeSwitcher.StopClosing();
await botClient.SendMessage(update.Message.Chat, "Продажи остановлены!"); await botClient.SendMessage(update.Message.Chat, "Закрытия запрещены!");
break; break;
} }
case Constants.BotCommandsButtons.EnablePurchases: case Constants.BotCommandsButtons.EnableOpening:
case Constants.BotCommandsButtons.EnableOpening2:
{ {
BotModeSwitcher.StartPurchase(); BotModeSwitcher.StartOpening();
await botClient.SendMessage(update.Message.Chat, "Покупки начаты!"); await botClient.SendMessage(update.Message.Chat, "Открытия начаты!");
break; break;
} }
case Constants.BotCommandsButtons.DisablePurchases: case Constants.BotCommandsButtons.DisableOpening:
case Constants.BotCommandsButtons.DisableOpening2:
{ {
BotModeSwitcher.StopPurchase(); BotModeSwitcher.StopOpening();
await botClient.SendMessage(update.Message.Chat, "Покупки остановлены!"); await botClient.SendMessage(update.Message.Chat, "Открытия остановлены!");
break; break;
} }
case "скинуть IMOEXF": case "скинуть IMOEXF":