using MtApi5; using Microsoft.AspNetCore.Mvc; using System.Reflection; using System.Globalization; var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseUrls("http://0.0.0.0:8080"); builder.Services.AddSingleton(sp => { var client = new MtApi5Client(); try { var prop = typeof(MtApi5Client).GetProperty("CommandTimeout"); if (prop != null && prop.CanWrite) { prop.SetValue(client, 120000); } else { var field = typeof(MtApi5Client).GetField("_command_timeout", BindingFlags.NonPublic | BindingFlags.Instance); field?.SetValue(client, 120000); } Console.WriteLine("CommandTimeout set to 120000ms"); } catch (Exception ex) { Console.WriteLine($"Could not set CommandTimeout: {ex.Message}"); } return client; }); builder.Services.AddSingleton(new SemaphoreSlim(1, 1)); builder.Services.AddHostedService(); var app = builder.Build(); app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { var logger = context.RequestServices.GetRequiredService().CreateLogger("GlobalExceptionHandler"); var exception = context.Features.Get()?.Error; if (exception != null) { logger.LogError(exception, "Unhandled exception while processing {Method} {Path}", context.Request.Method, context.Request.Path); } context.Response.StatusCode = StatusCodes.Status500InternalServerError; await context.Response.WriteAsJsonAsync(new { detail = "The bridge could not process the request.", status = StatusCodes.Status500InternalServerError }); }); }); // API Key 认证 const string API_KEY = "UiHMqtaYLZzwBdcuS4RFmEGhgDO8N2eI"; app.Use(async (context, next) => { var key = context.Request.Headers["X-API-Key"].FirstOrDefault() ?? context.Request.Query["key"].FirstOrDefault(); if (key != API_KEY) { context.Response.StatusCode = 401; await context.Response.WriteAsync("Unauthorized"); return; } await next(); }); app.MapGet("/health", async (MtApi5Client mt, SemaphoreSlim gate) => { await gate.WaitAsync(); try { var connected = mt.ConnectionState == Mt5ConnectionState.Connected; return connected ? Results.Ok(new { status = "healthy", mt5_connected = true, mt5_version = "unknown", api_version = "1.0.0" }) : Results.Json(new { status = "disconnected", mt5_connected = false, mt5_version = "unknown", api_version = "1.0.0" }, statusCode: StatusCodes.Status503ServiceUnavailable); } finally { gate.Release(); } }); app.MapGet("/account", async (MtApi5Client mt, SemaphoreSlim gate) => { await gate.WaitAsync(); try { var login = (ulong)mt.AccountInfoInteger(ENUM_ACCOUNT_INFO_INTEGER.ACCOUNT_LOGIN); var leverage = (uint)mt.AccountInfoInteger(ENUM_ACCOUNT_INFO_INTEGER.ACCOUNT_LEVERAGE); var tradeAllowed = mt.AccountInfoInteger(ENUM_ACCOUNT_INFO_INTEGER.ACCOUNT_TRADE_ALLOWED) != 0; var tradeExpert = mt.AccountInfoInteger(ENUM_ACCOUNT_INFO_INTEGER.ACCOUNT_TRADE_EXPERT) != 0; var currency = mt.AccountInfoString(ENUM_ACCOUNT_INFO_STRING.ACCOUNT_CURRENCY) ?? "USD"; var server = mt.AccountInfoString(ENUM_ACCOUNT_INFO_STRING.ACCOUNT_SERVER) ?? ""; var name = mt.AccountInfoString(ENUM_ACCOUNT_INFO_STRING.ACCOUNT_NAME) ?? ""; var company = mt.AccountInfoString(ENUM_ACCOUNT_INFO_STRING.ACCOUNT_COMPANY) ?? ""; var balance = mt.AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_BALANCE); var equity = mt.AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_EQUITY); var profit = mt.AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_PROFIT); var margin = mt.AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_MARGIN); var marginFree = mt.AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_MARGIN_FREE); var marginLevel = mt.AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE.ACCOUNT_MARGIN_LEVEL); return Results.Ok(new { data = new[] { new { login, leverage, trade_allowed = tradeAllowed, trade_expert = tradeExpert, currency, currency_digits = 2, server, name, company, balance, equity, profit, margin, margin_free = marginFree, margin_level = marginLevel } }, count = 1, format = "json" }); } finally { gate.Release(); } }); app.MapGet("/symbols/{symbol}", async (string symbol, MtApi5Client mt, SemaphoreSlim gate, ILoggerFactory loggerFactory) => { if (string.IsNullOrWhiteSpace(symbol)) return Results.BadRequest(new { detail = "Symbol is required." }); await gate.WaitAsync(); try { var digits = (int)mt.SymbolInfoInteger(symbol, ENUM_SYMBOL_INFO_INTEGER.SYMBOL_DIGITS); var spreadFloat = mt.SymbolInfoInteger(symbol, ENUM_SYMBOL_INFO_INTEGER.SYMBOL_SPREAD_FLOAT) != 0; var spread = (int)mt.SymbolInfoInteger(symbol, ENUM_SYMBOL_INFO_INTEGER.SYMBOL_SPREAD); var point = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_POINT); var bid = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_BID); var ask = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_ASK); var volumeMin = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_VOLUME_MIN); var volumeMax = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_VOLUME_MAX); var volumeStep = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_VOLUME_STEP); var contractSize = mt.SymbolInfoDouble(symbol, ENUM_SYMBOL_INFO_DOUBLE.SYMBOL_TRADE_CONTRACT_SIZE); string description = "", currencyBase = "", currencyProfit = "", path = ""; try { description = mt.SymbolInfoString(symbol, ENUM_SYMBOL_INFO_STRING.SYMBOL_DESCRIPTION) ?? ""; } catch { } try { currencyBase = mt.SymbolInfoString(symbol, ENUM_SYMBOL_INFO_STRING.SYMBOL_CURRENCY_BASE) ?? ""; } catch { } try { currencyProfit = mt.SymbolInfoString(symbol, ENUM_SYMBOL_INFO_STRING.SYMBOL_CURRENCY_PROFIT) ?? ""; } catch { } try { path = mt.SymbolInfoString(symbol, ENUM_SYMBOL_INFO_STRING.SYMBOL_PATH) ?? ""; } catch { } return Results.Ok(new { data = new[] { new { name = symbol, description, digits, point, bid, ask, spread, spread_float = spreadFloat, volume_min = volumeMin, volume_max = volumeMax, volume_step = volumeStep, trade_contract_size = contractSize, currency_base = currencyBase, currency_profit = currencyProfit, category = path } }, count = 1, format = "json" }); } catch (Exception ex) { loggerFactory.CreateLogger("SymbolsEndpoint").LogError(ex, "Failed to read symbol info for {Symbol}", symbol); return Results.Problem("Failed to read symbol info.", statusCode: StatusCodes.Status502BadGateway); } finally { gate.Release(); } }); app.MapGet("/symbols/{symbol}/tick", async (string symbol, MtApi5Client mt, SemaphoreSlim gate) => { if (string.IsNullOrWhiteSpace(symbol)) return Results.BadRequest(new { detail = "Symbol is required." }); await gate.WaitAsync(); try { var tick = mt.SymbolInfoTick(symbol); if (tick == null) return Results.NotFound(new { detail = $"Tick for {symbol} not found" }); return Results.Json(new { data = new[] { new { time = tick.time.ToString("yyyy-MM-ddTHH:mm:ss"), bid = tick.bid, ask = tick.ask, last = tick.last, volume = tick.volume, time_msc = tick.time.ToString("yyyy-MM-ddTHH:mm:ss.fff000"), flags = 6, volume_real = tick.volume_real } }, count = 1, format = "json" }); } finally { gate.Release(); } }); app.MapGet("/rates/from-pos", async (string symbol, string timeframe, int start_pos, int count, MtApi5Client mt, SemaphoreSlim gate, ILoggerFactory loggerFactory) => { if (string.IsNullOrWhiteSpace(symbol)) return Results.BadRequest(new { detail = "Symbol is required." }); if (!TryParseTimeframe(timeframe, out var tf)) return Results.BadRequest(new { detail = $"Unsupported timeframe '{timeframe}'." }); if (start_pos < 0) return Results.BadRequest(new { detail = "start_pos must be greater than or equal to 0." }); if (count <= 0 || count > 10000) return Results.BadRequest(new { detail = "count must be between 1 and 10000." }); await gate.WaitAsync(); try { const int chunkSize = 1000; var allRates = new List(); int remaining = count; int currentPos = start_pos; while (remaining > 0) { int chunk = Math.Min(remaining, chunkSize); var result = mt.CopyRates(symbol, tf, currentPos, chunk, out MqlRates[]? rates); Console.WriteLine($"CopyRates pos={currentPos} chunk={chunk} result={result}, rates={(rates != null ? rates.Length.ToString() : "null")}"); if (rates != null && rates.Length > 0) { allRates.AddRange(rates); currentPos += rates.Length; remaining -= rates.Length; if (rates.Length < chunk) break; } else { break; } } var data = allRates.Select(r => new { time = r.time.ToString("yyyy-MM-ddTHH:mm:ss"), r.open, r.high, r.low, r.close, tick_volume = r.tick_volume, r.spread, real_volume = r.real_volume }).ToArray(); return Results.Ok(new { data, count = data.Length, format = "json" }); } catch (Exception ex) { loggerFactory.CreateLogger("RatesEndpoint").LogError(ex, "Failed to read rates for {Symbol} {Timeframe}", symbol, timeframe); return Results.Problem("Failed to read rates.", statusCode: StatusCodes.Status502BadGateway); } finally { gate.Release(); } }); app.MapGet("/positions", async (MtApi5Client mt, SemaphoreSlim gate) => { await gate.WaitAsync(); try { var total = mt.PositionsTotal(); var positions = new List(); for (int i = 0; i < total; i++) { var ticket = mt.PositionGetTicket(i); if (ticket == 0) continue; positions.Add(new { ticket, symbol = mt.PositionGetString(ENUM_POSITION_PROPERTY_STRING.POSITION_SYMBOL), type = (int)mt.PositionGetInteger(ENUM_POSITION_PROPERTY_INTEGER.POSITION_TYPE), volume = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_VOLUME), price_open = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_PRICE_OPEN), sl = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_SL), tp = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_TP), price_current = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_PRICE_CURRENT), swap = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_SWAP), profit = mt.PositionGetDouble(ENUM_POSITION_PROPERTY_DOUBLE.POSITION_PROFIT), comment = mt.PositionGetString(ENUM_POSITION_PROPERTY_STRING.POSITION_COMMENT), magic = (ulong)mt.PositionGetInteger(ENUM_POSITION_PROPERTY_INTEGER.POSITION_MAGIC) }); } return Results.Ok(new { data = positions, count = positions.Count, format = "json" }); } finally { gate.Release(); } }); app.MapGet("/orders", async (string? symbol, MtApi5Client mt, SemaphoreSlim gate) => { await gate.WaitAsync(); try { var total = mt.OrdersTotal(); var orders = new List(); for (int i = 0; i < total; i++) { var ticket = mt.OrderGetTicket(i); if (ticket == 0) continue; var orderSymbol = mt.OrderGetString(ENUM_ORDER_PROPERTY_STRING.ORDER_SYMBOL); if (!string.IsNullOrWhiteSpace(symbol) && orderSymbol != symbol) continue; orders.Add(new { ticket, symbol = orderSymbol, type = (int)mt.OrderGetInteger(ENUM_ORDER_PROPERTY_INTEGER.ORDER_TYPE), volume_initial = mt.OrderGetDouble(ENUM_ORDER_PROPERTY_DOUBLE.ORDER_VOLUME_INITIAL), price_open = mt.OrderGetDouble(ENUM_ORDER_PROPERTY_DOUBLE.ORDER_PRICE_OPEN), sl = mt.OrderGetDouble(ENUM_ORDER_PROPERTY_DOUBLE.ORDER_SL), tp = mt.OrderGetDouble(ENUM_ORDER_PROPERTY_DOUBLE.ORDER_TP), magic = (ulong)mt.OrderGetInteger(ENUM_ORDER_PROPERTY_INTEGER.ORDER_MAGIC), comment = mt.OrderGetString(ENUM_ORDER_PROPERTY_STRING.ORDER_COMMENT) }); } return Results.Ok(new { data = orders, count = orders.Count, format = "json" }); } finally { gate.Release(); } }); app.MapPost("/order/check", async (TradeRequestDto body, MtApi5Client mt, SemaphoreSlim gate) => { var validationError = ValidateTradeRequest(body); if (validationError != null) return Results.BadRequest(new { detail = validationError }); var request = ToMqlTradeRequest(body); await gate.WaitAsync(); try { mt.OrderCheck(request, out MqlTradeCheckResult? checkResult); if (checkResult == null) return Results.Ok(new { data = new { retcode = 0u, balance = 0.0, equity = 0.0, profit = 0.0, margin = 0.0, margin_free = 0.0, margin_level = 0.0, comment = "Check failed" }, count = 1, format = "json" }); return Results.Ok(new { data = new { retcode = checkResult.Retcode, balance = checkResult.Balance, equity = checkResult.Equity, profit = checkResult.Profit, margin = checkResult.Margin, margin_free = checkResult.Margin_free, margin_level = checkResult.Margin_level, comment = checkResult.Comment ?? "" }, count = 1, format = "json" }); } finally { gate.Release(); } }); app.MapPost("/order/send", async (TradeRequestBody body, MtApi5Client mt, SemaphoreSlim gate) => { var validationError = ValidateTradeRequest(body.request); if (validationError != null) return Results.BadRequest(new { detail = validationError }); var request = ToMqlTradeRequest(body.request); await gate.WaitAsync(); try { mt.OrderSend(request, out MqlTradeResult? result); if (result == null) return Results.Ok(new { data = new { retcode = 0u, order = 0ul, comment = "Send failed" }, count = 1, format = "json" }); return Results.Ok(new { data = new { retcode = result.Retcode, order = result.Order, comment = result.Comment ?? "" }, count = 1, format = "json" }); } finally { gate.Release(); } }); app.MapGet("/history/deals", async (string date_from, string date_to, string? symbol, MtApi5Client mt, SemaphoreSlim gate) => { if (!TryParseClientDate(date_from, out var fromDt) || !TryParseClientDate(date_to, out var toDt)) return Results.BadRequest(new { detail = "date_from and date_to must be valid ISO-8601 or yyyy-MM-dd values." }); if (fromDt > toDt) return Results.BadRequest(new { detail = "date_from must be earlier than or equal to date_to." }); await gate.WaitAsync(); try { mt.HistorySelect(fromDt, toDt); var total = mt.HistoryDealsTotal(); var dealList = new List(); for (int i = 0; i < total; i++) { var ticket = mt.HistoryDealGetTicket(i); if (ticket == 0) continue; var dealSymbol = mt.HistoryDealGetString(ticket, ENUM_DEAL_PROPERTY_STRING.DEAL_SYMBOL); if (!string.IsNullOrWhiteSpace(symbol) && dealSymbol != symbol) continue; var dealTime = MtTimeToDateTime(mt.HistoryDealGetInteger(ticket, ENUM_DEAL_PROPERTY_INTEGER.DEAL_TIME)); dealList.Add(new { ticket, time = dealTime.ToString("yyyy-MM-ddTHH:mm:ss"), entry = (int)mt.HistoryDealGetInteger(ticket, ENUM_DEAL_PROPERTY_INTEGER.DEAL_ENTRY), magic = (ulong)mt.HistoryDealGetInteger(ticket, ENUM_DEAL_PROPERTY_INTEGER.DEAL_MAGIC), volume = mt.HistoryDealGetDouble(ticket, ENUM_DEAL_PROPERTY_DOUBLE.DEAL_VOLUME), price = mt.HistoryDealGetDouble(ticket, ENUM_DEAL_PROPERTY_DOUBLE.DEAL_PRICE), profit = mt.HistoryDealGetDouble(ticket, ENUM_DEAL_PROPERTY_DOUBLE.DEAL_PROFIT), commission = mt.HistoryDealGetDouble(ticket, ENUM_DEAL_PROPERTY_DOUBLE.DEAL_COMMISSION), swap = mt.HistoryDealGetDouble(ticket, ENUM_DEAL_PROPERTY_DOUBLE.DEAL_SWAP), symbol = dealSymbol, comment = mt.HistoryDealGetString(ticket, ENUM_DEAL_PROPERTY_STRING.DEAL_COMMENT) ?? "" }); } return Results.Ok(new { data = dealList, count = dealList.Count, format = "json" }); } finally { gate.Release(); } }); app.MapGet("/gvar", async (HttpContext context, SemaphoreSlim gate) => { var mt = context.RequestServices.GetRequiredService(); await gate.WaitAsync(); try { var total = mt.GlobalVariablesTotal(); var vars = new List(); for (int i = 0; i < total; i++) { var name = mt.GlobalVariableName(i) ?? ""; var value = mt.GlobalVariableGet(name); vars.Add(new { name, value }); } return Results.Ok(new { data = vars, count = vars.Count, format = "json" }); } finally { gate.Release(); } }); app.MapGet("/gvar/{name}", async (HttpContext context, string name, SemaphoreSlim gate) => { if (string.IsNullOrWhiteSpace(name)) return Results.BadRequest(new { detail = "Global variable name is required." }); var mt = context.RequestServices.GetRequiredService(); await gate.WaitAsync(); try { if (!mt.GlobalVariableCheck(name)) return Results.NotFound(new { detail = $"GlobalVariable '{name}' not found" }); var value = mt.GlobalVariableGet(name); return Results.Ok(new { name, value }); } finally { gate.Release(); } }); app.MapPost("/gvar/{name}", async (HttpContext context, string name, SemaphoreSlim gate) => { if (string.IsNullOrWhiteSpace(name)) return Results.BadRequest(new { detail = "Global variable name is required." }); var mt = context.RequestServices.GetRequiredService(); var body = await context.Request.ReadFromJsonAsync(); if (body == null || double.IsNaN(body.value) || double.IsInfinity(body.value)) return Results.BadRequest(new { detail = "Invalid body." }); await gate.WaitAsync(); try { mt.GlobalVariableSet(name, body.value); return Results.Ok(new { name, value = body.value }); } finally { gate.Release(); } }); app.MapDelete("/gvar/{name}", async (HttpContext context, string name, SemaphoreSlim gate) => { if (string.IsNullOrWhiteSpace(name)) return Results.BadRequest(new { detail = "Global variable name is required." }); var mt = context.RequestServices.GetRequiredService(); await gate.WaitAsync(); try { if (!mt.GlobalVariableCheck(name)) return Results.NotFound(new { detail = $"GlobalVariable '{name}' not found" }); mt.GlobalVariableDel(name); return Results.Ok(new { deleted = name }); } finally { gate.Release(); } }); app.Run(); DateTime MtTimeToDateTime(long mtTime) { return new DateTime(1970, 1, 1).AddSeconds(mtTime); } bool TryParseTimeframe(string timeframe, out ENUM_TIMEFRAMES parsed) { parsed = timeframe switch { "TIMEFRAME_M1" => ENUM_TIMEFRAMES.PERIOD_M1, "TIMEFRAME_M5" => ENUM_TIMEFRAMES.PERIOD_M5, "TIMEFRAME_M15" => ENUM_TIMEFRAMES.PERIOD_M15, "TIMEFRAME_M30" => ENUM_TIMEFRAMES.PERIOD_M30, "TIMEFRAME_H1" => ENUM_TIMEFRAMES.PERIOD_H1, "TIMEFRAME_H4" => ENUM_TIMEFRAMES.PERIOD_H4, "TIMEFRAME_D1" => ENUM_TIMEFRAMES.PERIOD_D1, _ => default }; return timeframe is "TIMEFRAME_M1" or "TIMEFRAME_M5" or "TIMEFRAME_M15" or "TIMEFRAME_M30" or "TIMEFRAME_H1" or "TIMEFRAME_H4" or "TIMEFRAME_D1"; } bool TryParseClientDate(string value, out DateTime parsed) { return DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out parsed) || DateTime.TryParseExact(value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out parsed); } string? ValidateTradeRequest(TradeRequestDto request) { if (!Enum.IsDefined(typeof(ENUM_TRADE_REQUEST_ACTIONS), (int)request.action)) return "action is invalid."; if (string.IsNullOrWhiteSpace(request.symbol)) return "symbol is required."; if (request.volume is not null && (!double.IsFinite(request.volume.Value) || request.volume.Value <= 0)) return "volume must be a positive number."; if (request.price is not null && (!double.IsFinite(request.price.Value) || request.price.Value < 0)) return "price must be a non-negative number."; if (request.sl is not null && (!double.IsFinite(request.sl.Value) || request.sl.Value < 0)) return "sl must be a non-negative number."; if (request.tp is not null && (!double.IsFinite(request.tp.Value) || request.tp.Value < 0)) return "tp must be a non-negative number."; if (request.order_type is not null && !Enum.IsDefined(typeof(ENUM_ORDER_TYPE), (int)request.order_type.Value)) return "order_type is invalid."; if (request.deviation is not null && request.deviation.Value > 100000) return "deviation is unreasonably large."; return null; } MqlTradeRequest ToMqlTradeRequest(TradeRequestDto r) { return new MqlTradeRequest { Action = (ENUM_TRADE_REQUEST_ACTIONS)r.action, Symbol = r.symbol, Volume = r.volume ?? 0, Type = (ENUM_ORDER_TYPE)(r.order_type ?? 0), Price = r.price ?? 0, Sl = r.sl ?? 0, Tp = r.tp ?? 0, Magic = r.magic ?? 0, Comment = r.comment ?? "", Order = r.order ?? 0, Position = r.position ?? 0, Deviation = r.deviation ?? 0 }; } class TradeRequestDto { public uint action { get; set; } public string symbol { get; set; } = ""; public double? volume { get; set; } public uint? order_type { get; set; } public double? price { get; set; } public double? sl { get; set; } public double? tp { get; set; } public ulong? magic { get; set; } public string? comment { get; set; } public ulong? order { get; set; } public ulong? position { get; set; } public ulong? deviation { get; set; } } class TradeRequestBody { public TradeRequestDto request { get; set; } = new(); } class GvarSetDto { public double value { get; set; } } class MtConnectionService : BackgroundService { private readonly MtApi5Client _client; private readonly ILogger _logger; private readonly TimeSpan _connectTimeout; private const int Port = 8228; public MtConnectionService(MtApi5Client client, ILogger logger) { _client = client; _logger = logger; _connectTimeout = TimeSpan.FromSeconds(15); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { if (_client.ConnectionState != Mt5ConnectionState.Connected) { var firstConnect = _client.ConnectionState == Mt5ConnectionState.Disconnected; _logger.LogWarning(firstConnect ? "Connecting to MT5 via MtApi5 on port {Port}..." : "MT5 disconnected, attempting reconnect on port {Port}...", Port); var connected = await TryConnectAsync(stoppingToken); if (!connected) { _logger.LogError("MT5 is not connected. Ensure MtApi5 EA is loaded and listening on port {Port}.", Port); await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); continue; } _logger.LogInformation(firstConnect ? "MT5 Bridge ready on http://localhost:8080" : "MT5 reconnected."); } await Task.Delay(TimeSpan.FromSeconds(30), stoppingToken); } } private async Task TryConnectAsync(CancellationToken stoppingToken) { if (_client.ConnectionState == Mt5ConnectionState.Connected) { return true; } var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); EventHandler? handler = null; handler = (_, e) => { _logger.LogInformation("MT5 Connection: {Status}", e.Status); if (e.Status == Mt5ConnectionState.Connected || e.Status == Mt5ConnectionState.Failed || e.Status == Mt5ConnectionState.Disconnected) { tcs.TrySetResult(e.Status); } }; _client.ConnectionStateChanged += handler; try { _client.BeginConnect(Port); using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); timeoutCts.CancelAfter(_connectTimeout); using var registration = timeoutCts.Token.Register(() => tcs.TrySetCanceled(timeoutCts.Token)); var status = await tcs.Task; return status == Mt5ConnectionState.Connected; } catch (OperationCanceledException) when (!stoppingToken.IsCancellationRequested) { _logger.LogWarning("Timed out waiting {TimeoutSeconds}s for MT5 connection state change.", _connectTimeout.TotalSeconds); return false; } finally { _client.ConnectionStateChanged -= handler; } } }