//+------------------------------------------------------------------+ //| My-Functions.mqh | //| Copyright 2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property strict // Include necessary libraries #include #include #include #include #include #include #include CTrade trade; // For order operations // Chart control bool removeFromThisChartOnly = true; // Time Functions datetime timetostartopenchart = TimeCurrent(); datetime timetostartclosechart = TimeCurrent(); // Trade limits int max_trades_same_type = 3; // Maximum number of trades of same type // Account tracking double MaxAccountBalance = AccountInfoDouble(ACCOUNT_BALANCE); double InitialAccountBalance = AccountInfoDouble(ACCOUNT_BALANCE); double MinAccountBalance = 1000000; // Minimum account balance threshold // Symbol and source arrays string symbols[] = { "XAUUSD", "XAGUSD", "XTIUSD", "USDJPY", "EURJPY", "GBPJPY", "CHFJPY", "USDCHF", "XAUEUR","XAUGBP","NZDUSD", "USDCAD", "AUDUSD", "EURUSD", "EURGBP", "EURNZD", "GBPUSD", "NZDCAD", "GBPNZD", "GBPAUD", "EURAUD", "NZDCHF" }; string sources[] = {"Own", "News", "Younus"}; // Maximum 9 int count_symbols = ArraySize(symbols); // Number of symbols in array int count_sources = ArraySize(sources); // Number of sources in array // Telegram bot configuration CCustomBot bot; string InpToken = "8181067618:AAGqA6S1y59CG5JgxX-c2rcgEt0kvpgCBvY"; // UK Bot int check_telegram_updates = 2150; // Update check interval in milliseconds int capture_previous_update_id = 0; ulong chat_id = 0; //+------------------------------------------------------------------+ //| Order Placement Functions (MQL5 version) | //+------------------------------------------------------------------+ void showbalance() { double CurrentAccountBalance = AccountInfoDouble(ACCOUNT_BALANCE); if (MaxAccountBalance < CurrentAccountBalance) MaxAccountBalance = CurrentAccountBalance; if (MinAccountBalance > CurrentAccountBalance) MinAccountBalance = CurrentAccountBalance; Comment ("Initial Account Balance was ",InitialAccountBalance,", Minimum Balance was ",MinAccountBalance,", Max Balance was ", MaxAccountBalance,", Current Balance is ",CurrentAccountBalance," and Current Equity is ",AccountInfoDouble(ACCOUNT_EQUITY)); } void chartopenclose() { // Open charts for positions if(TimeCurrent()-timetostartopenchart > 20) { Print("Trying to Open Chart in Use"); timetostartopenchart = TimeCurrent(); for(int i = PositionsTotal()-1; i >= 0; i--) { string position = PositionGetSymbol(i); if(position != NULL && position != "") { bool found = false; long ID = ChartFirst(); while(ID >= 0) { if(ChartSymbol(ID) == position) { found = true; break; } ID = ChartNext(ID); } if(!found) { Print("Opening chart for position: ", position); ChartOpen(position, PERIOD_CURRENT); } } } } // Close unused charts if(TimeCurrent() - timetostartclosechart > 30) { Print("Trying to Close Chart not in use"); timetostartclosechart = TimeCurrent(); long ID_chart = ChartFirst(); ID_chart = ChartNext(ID_chart); // Skip the first chart while(ID_chart >= 0) { bool shouldClose = true; string chartSymbol = ChartSymbol(ID_chart); string gvName = "EA_Active_"+IntegerToString(ID_chart); // 1. Check for open positions for(int i = PositionsTotal()-1; i >= 0; i--) { if(PositionGetSymbol(i) == chartSymbol) { shouldClose = false; Print("Keeping chart with open position: ", chartSymbol); break; } } // 2. Check for active EA using global variable if(shouldClose && GlobalVariableCheck(gvName)) { shouldClose = false; Print("Keeping chart with active EA: ", chartSymbol); } // 3. Final closing decision if(shouldClose) { Print("Closing unused chart: ", chartSymbol); ChartClose(ID_chart); } // Get next chart ID_chart = ChartNext(ID_chart); } } } long GetChartIDSymbol(string symb) { bool found = false; long ID = ChartFirst(); while (ID >= 0) { if (ChartSymbol(ID) == symb) { found = true; break; } ID = ChartNext(ID); } return ID; } void SendBuyOrder(string SymbolForOrder, double VolumeofTrade, double sl_points, double tp_points, long Magic) { double ask = SymbolInfoDouble(SymbolForOrder, SYMBOL_ASK); double buyprice = NormalizeDouble(ask, _Digits); double sl = buyprice - PipsToPrice(sl_points, SymbolForOrder); double tp = buyprice + PipsToPrice(tp_points, SymbolForOrder); if (sl_points==0) sl=0; if (tp_points==0) tp=0; MqlTradeRequest request1 = {}; MqlTradeResult result1 = {}; request1.action = TRADE_ACTION_DEAL; request1.symbol = SymbolForOrder; request1.volume = VolumeofTrade; request1.type = ORDER_TYPE_BUY; request1.price = buyprice; request1.sl = sl; request1.tp = tp; request1.deviation = 10; // Lower deviation request1.magic = Magic; request1.type_filling = ORDER_FILLING_IOC; // Add this if your broker requires it if (OrderSend(request1, result1)) { Print("Buy order sent successfully. Ticket: ", result1.order); } else { Print("Failed to send buy order. Error: ", result1.retcode); } } void SendBuyLimitOrder(string SymbolForOrder, double VolumeofTrade, double sdforbuy, double sl_points, double tp_points, long Magic) { double buyprice = NormalizeDouble(SymbolInfoDouble(SymbolForOrder, SYMBOL_ASK), _Digits); // Get the current ask price double buyLimitPrice = buyprice - PipsToPrice(sdforbuy, SymbolForOrder); double sl = buyLimitPrice - PipsToPrice(sl_points, SymbolForOrder); double tp = buyLimitPrice + PipsToPrice(tp_points, SymbolForOrder); if (sl_points==0) sl=0; if (tp_points==0) tp=0; MqlTradeRequest request1 = {}; MqlTradeResult result1 = {}; request1.action = TRADE_ACTION_PENDING; // Use TRADE_ACTION_PENDING for a pending order (Limit) request1.symbol = SymbolForOrder; // Symbol of the instrument request1.volume = VolumeofTrade; // Trade volume request1.type = ORDER_TYPE_BUY_LIMIT; // Buy Limit order type request1.price = buyLimitPrice; // Buy Limit price request1.sl = sl; request1.tp = tp; request1.deviation = 10; // Maximum allowed price deviation request1.magic = Magic; // Magic number for the order request1.type_filling = ORDER_FILLING_IOC; // Add this if your broker requires it // Send the Buy Limit order if (trade.OrderSend(request1, result1)) { Print("Buy Limit order placed successfully. Ticket: ", result1.order); } else { Print("Failed to place Buy Limit order. Error: ", result1.retcode); } } void SendBuyStopOrder(string SymbolForOrder, double VolumeofTrade, double sdforbuy, double sl_points, double tp_points, long Magic) { double buyprice = NormalizeDouble(SymbolInfoDouble(SymbolForOrder, SYMBOL_ASK), _Digits); // Get the current ask price double buyStopPrice = buyprice + PipsToPrice(sdforbuy, SymbolForOrder); double sl = buyStopPrice - PipsToPrice(sl_points, SymbolForOrder); double tp = buyStopPrice + PipsToPrice(tp_points, SymbolForOrder); if (sl_points==0) sl=0; if (tp_points==0) tp=0; MqlTradeRequest request1 = {}; MqlTradeResult result1 = {}; request1.action = TRADE_ACTION_PENDING; // Use TRADE_ACTION_PENDING for a pending order (Limit) request1.symbol = SymbolForOrder; // Symbol of the instrument request1.volume = VolumeofTrade; // Trade volume request1.type = ORDER_TYPE_BUY_STOP; // Buy Limit order type request1.price = buyStopPrice; // Buy Limit price request1.sl = sl; request1.tp = tp; request1.deviation = 10; // Maximum allowed price deviation request1.magic = Magic; // Magic number for the order request1.type_filling = ORDER_FILLING_IOC; // Add this if your broker requires it // Send the Buy Limit order if (trade.OrderSend(request1, result1)) { Print("Buy Limit order placed successfully. Ticket: ", result1.order); } else { Print("Failed to place Buy Limit order. Error: ", result1.retcode); } } void SendSellOrder(string SymbolForOrder, double VolumeofTrade, double sl_points, double tp_points, long Magic) { double bid = SymbolInfoDouble(SymbolForOrder, SYMBOL_BID); double sellprice = NormalizeDouble(bid, _Digits); double sl = sellprice + PipsToPrice(sl_points, SymbolForOrder); double tp = sellprice - PipsToPrice(tp_points, SymbolForOrder); if (sl_points == 0) sl = 0; if (tp_points == 0) tp = 0; MqlTradeRequest request1 = {}; MqlTradeResult result1 = {}; request1.action = TRADE_ACTION_DEAL; request1.symbol = SymbolForOrder; request1.volume = VolumeofTrade; request1.type = ORDER_TYPE_SELL; request1.price = sellprice; request1.sl = sl; request1.tp = tp; request1.deviation = 10; // Low deviation to avoid rejection request1.magic = Magic; request1.type_filling = ORDER_FILLING_IOC; Print("New wala"); if (OrderSend(request1, result1)) { Print("Sell order sent successfully. Ticket: ", result1.order); } else { Print("Failed to send sell order. Error: ", result1.retcode); } } void SendSellLimitOrder(string SymbolForOrder, double VolumeofTrade, double sdforsell, double sl_points, double tp_points, long Magic) { double sellprice = NormalizeDouble(SymbolInfoDouble(SymbolForOrder, SYMBOL_BID), _Digits); // Get the current ask price // Normalize the Buy Limit price to the instrument's digit precision double sellLimitPrice = sellprice + PipsToPrice(sdforsell, SymbolForOrder); double sl = sellLimitPrice + PipsToPrice(sl_points, SymbolForOrder); double tp = sellLimitPrice - PipsToPrice(tp_points, SymbolForOrder); if (sl_points==0) sl=0; if (tp_points==0) tp=0; MqlTradeRequest request1 = {}; MqlTradeResult result1 = {}; request1.action = TRADE_ACTION_PENDING; // Use TRADE_ACTION_PENDING for a pending order (Limit) request1.symbol = SymbolForOrder; // Symbol of the instrument request1.volume = VolumeofTrade; // Trade volume request1.type = ORDER_TYPE_SELL_LIMIT; // Buy Limit order type request1.price = sellLimitPrice; // Buy Limit price request1.deviation = 10; // Maximum allowed price deviation request1.magic = Magic; // Magic number for the order // Send the Buy Limit order if (trade.OrderSend(request1, result1)) { Print("Buy Limit order placed successfully. Ticket: ", result1.order); } else { Print("Failed to place Buy Limit order. Error: ", result1.retcode); } } void SendSellStopOrder(string SymbolForOrder, double VolumeofTrade, double sdforsell, double sl_points, double tp_points, long Magic) { double sellprice = NormalizeDouble(SymbolInfoDouble(SymbolForOrder, SYMBOL_BID), _Digits); // Get the current ask price // Normalize the Buy Limit price to the instrument's digit precision double sellStopPrice = sellprice - PipsToPrice(sdforsell, SymbolForOrder); double sl = sellStopPrice + PipsToPrice(sl_points, SymbolForOrder); double tp = sellStopPrice - PipsToPrice(tp_points, SymbolForOrder); if (sl_points==0) sl=0; if (tp_points==0) tp=0; MqlTradeRequest request1 = {}; MqlTradeResult result1 = {}; request1.action = TRADE_ACTION_PENDING; // Use TRADE_ACTION_PENDING for a pending order (Limit) request1.symbol = SymbolForOrder; // Symbol of the instrument request1.volume = VolumeofTrade; // Trade volume request1.type = ORDER_TYPE_SELL_STOP; // Buy Limit order type request1.price = sellStopPrice; // Buy Limit price request1.deviation = 10; // Maximum allowed price deviation request1.magic = Magic; // Magic number for the order // Send the Buy Limit order if (trade.OrderSend(request1, result1)) { Print("Buy Limit order placed successfully. Ticket: ", result1.order); } else { Print("Failed to place Buy Limit order. Error: ", result1.retcode); } } bool CheckBuyOpenOrders(long MagicBuy) { for(int i=0; i0) { if(PositionGetInteger(POSITION_MAGIC)==MagicBuy && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) return true; } } return false; } bool CountBuyOpenOrders(string symb) { int ordercount = 0; for(int i=0; i0) { if(PositionGetString(POSITION_SYMBOL)==symb && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) ordercount++; } } return (ordercount < max_trades_same_type); } bool CheckBuyStopOpenOrders(long MagicBuy) { for(int i=0; i0) { if(OrderGetInteger(ORDER_MAGIC)==MagicBuy && OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_STOP) return true; } } return false; } bool CheckBuyLimitOpenOrders(long MagicBuy) { for(int i=0; i0) { if(OrderGetInteger(ORDER_MAGIC)==MagicBuy && OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_BUY_LIMIT) return true; } } return false; } void CloseBuyOrders(long MagicBuy) { Comment("Buy Orders are Closed"); for(int i=PositionsTotal()-1; i>=0; i--) { ulong ticket=PositionGetTicket(i); if(ticket>0) { if(PositionGetString(POSITION_SYMBOL)!=_Symbol && removeFromThisChartOnly) continue; if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY && PositionGetInteger(POSITION_MAGIC)==MagicBuy) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); request.action = TRADE_ACTION_DEAL; request.symbol = PositionGetString(POSITION_SYMBOL); request.volume = PositionGetDouble(POSITION_VOLUME); request.position = ticket; request.deviation = 0; request.magic = MagicBuy; request.type = ORDER_TYPE_SELL; double bid = SymbolInfoDouble(request.symbol,SYMBOL_BID); request.price = bid; if (!OrderSend(request, result)) { Print("OrderSend failed: ", GetLastError()); } else { Print("Order sent. Result code: ", result.retcode); } } } } } bool CheckSellOpenOrders(long MagicSell) { for(int i=0; i0) { if(PositionGetInteger(POSITION_MAGIC)==MagicSell && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) return true; } } return false; } bool CountSellOpenOrders(string symb) { int ordercount = 0; for(int i=0; i0) { if(PositionGetString(POSITION_SYMBOL)==symb && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL) ordercount++; } } return (ordercount < max_trades_same_type); } bool CheckSellStopOpenOrders(long MagicSell) { for(int i=0; i0) { if(OrderGetInteger(ORDER_MAGIC)==MagicSell && OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL_STOP) return true; } } return false; } bool CheckSellLimitOpenOrders(long MagicSell) { for(int i=0; i0) { if(OrderGetInteger(ORDER_MAGIC)==MagicSell && OrderGetInteger(ORDER_TYPE)==ORDER_TYPE_SELL_LIMIT) return true; } } return false; } void CloseSellOrders(long MagicSell) { Comment("Sell Orders are Closed"); for(int i=PositionsTotal()-1; i>=0; i--) { ulong ticket=PositionGetTicket(i); if(ticket>0) { if(PositionGetString(POSITION_SYMBOL)!=_Symbol && removeFromThisChartOnly) continue; if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL && PositionGetInteger(POSITION_MAGIC)==MagicSell) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); request.action = TRADE_ACTION_DEAL; request.symbol = PositionGetString(POSITION_SYMBOL); request.volume = PositionGetDouble(POSITION_VOLUME); request.position = ticket; request.deviation = 0; request.magic = MagicSell; request.type = ORDER_TYPE_BUY; double ask = SymbolInfoDouble(request.symbol,SYMBOL_ASK); request.price = ask; if (!OrderSend(request, result)) { Print("OrderSend failed: ", GetLastError()); } else { Print("Order sent. Result code: ", result.retcode); } } } } } bool CheckOpenOrders(long Magic) { // Check positions for(int i=0; i0 && PositionGetInteger(POSITION_MAGIC)==Magic) return true; } // Check pending orders for(int i=0; i0 && OrderGetInteger(ORDER_MAGIC)==Magic) return true; } return false; } void ClosePendingOrders(long Magic) { Comment("All Pending Orders are Closed"); for(int i=OrdersTotal()-1; i>=0; i--) { ulong ticket=OrderGetTicket(i); if(ticket>0 && OrderGetInteger(ORDER_MAGIC)==Magic) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); request.action = TRADE_ACTION_REMOVE; request.order = ticket; if (!OrderSend(request, result)) { Print("OrderSend failed: ", GetLastError()); } else { Print("Order sent. Result code: ", result.retcode); } } } } void CloseAllOrders(long Magic) { Comment("All Orders are Closed"); // Close positions first for(int i=PositionsTotal()-1; i>=0; i--) { ulong ticket=PositionGetTicket(i); if(ticket>0) { if(PositionGetString(POSITION_SYMBOL)!=_Symbol && removeFromThisChartOnly) continue; if(PositionGetInteger(POSITION_MAGIC)==Magic) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); request.action = TRADE_ACTION_DEAL; request.symbol = PositionGetString(POSITION_SYMBOL); request.volume = PositionGetDouble(POSITION_VOLUME); request.position = ticket; request.deviation = 0; request.magic = Magic; if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY) { request.type = ORDER_TYPE_SELL; request.price = SymbolInfoDouble(request.symbol,SYMBOL_BID); } else { request.type = ORDER_TYPE_BUY; request.price = SymbolInfoDouble(request.symbol,SYMBOL_ASK); } if (!OrderSend(request, result)) { Print("OrderSend failed: ", GetLastError()); } else { Print("Order sent. Result code: ", result.retcode); } } } } // Then remove pending orders ClosePendingOrders(Magic); } bool lastlosscheck() { if(HistorySelect(0, TimeCurrent())) { int total = HistoryDealsTotal(); if(total > 0) { ulong ticket = HistoryDealGetTicket(total-1); if(ticket > 0) { double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT); return (profit < 0); } } } return false; } long CreateMagicNumber(string source, string symb, string type, int ordernum) { int sourcenumber = 9900000; int symbnumber = 99900; int typenumber = 99; // Find source index for (int x = 0; x < count_sources; x++) { if (source == sources[x]) sourcenumber = 1000000 * (x + 1); } // Find symbol index for (int x = 0; x < count_symbols; x++) { if (symb == symbols[x]) symbnumber = 30000 + (x + 1) * 100; } // Set type number if (type == "Buy") typenumber = 10; if (type == "Sell") typenumber = 20; // Combine all components long magicnumber = sourcenumber + symbnumber + typenumber + ordernum; return magicnumber; } string GetMagicSource(long Magic) { string source = "None"; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 0, 2); int sourcemagic = (int)StringToInteger(tempmagic) / 10; source = sources[sourcemagic - 1]; } return source; } string GetMagicSymbol(long Magic) { string symbol = "None"; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 2, 3); int symbolmagic = (int)StringToInteger(tempmagic) - 300; symbol = symbols[symbolmagic - 1]; } return symbol; } string GetMagicType(long Magic) { string type = "None"; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 5, 1); type = "Buy"; if (tempmagic == "2") type = "Sell"; } return type; } int GetMagicOrderNum(long Magic) { int ordernum = 0; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 6, 1); ordernum = (int)StringToInteger(tempmagic); } return ordernum; } int GetMagicSourceIndex(long Magic) { int source = 0; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 0, 2); int sourcemagic = (int)StringToInteger(tempmagic) / 10; source = sourcemagic - 1; } return source; } int GetMagicSymbolIndex(long Magic) { int symbol = 0; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 2, 3); int symbolmagic = (int)StringToInteger(tempmagic) - 300; symbol = symbolmagic - 1; } return symbol; } int GetMagicTypeNum(long Magic) { int type = 0; if (Magic>1000000 && Magic <9999999) { string tempmagic = IntegerToString(Magic); tempmagic = StringSubstr(tempmagic, 5, 1); if (tempmagic == "2") type = 1; } return type; } string toUpper(string text) { StringToUpper(text); return text; } double PipsToPrice(double pips, string symb) { double price; if (symb == "XAUUSD" || symb == "XTIUSD" || symb == "XAUEUR" || symb == "XAUGBP") { price = pips / 100; return price; } if (symb == "USDJPY" || symb == "EURJPY" || symb == "GBPJPY" || symb == "CHFJPY" || symb == "XAGUSD") { price = pips / 1000; return price; } if (symb == "USDCAD" || symb == "AUDUSD" || symb == "EURUSD" || symb == "EURGBP" || symb == "EURNZD" || symb == "USDCHF" || symb == "GBPUSD" || symb == "NZDCAD" || symb == "NZDUSD" || symb == "GBPNZD" || symb == "GBPAUD" || symb == "EURAUD" ) { price = pips / 100000; return price; } return 0; } int telegram_to_mt4() { CJAVal js; string headers; char post[], result[]; int res = WebRequest("GET", "https://api.telegram.org/bot" + InpToken + "/getUpdates?offset=-1", "", NULL, 10000, post, ArraySize(post), result, headers); string server_response_data = CharArrayToString(result); js.Deserialize(result); string update_id_str = js["result"][0]["update_id"].ToStr(); int update_id = (int)StringToInteger(update_id_str); string chat_id_str = js["result"][0]["message"]["chat"]["id"].ToStr(); chat_id = (ulong)chat_id_str; string output[]; string output2[]; if (capture_previous_update_id < update_id) { capture_previous_update_id = update_id; string chat_message_text = js["result"][0]["message"]["text"].ToStr(); string chat_message_caption = js["result"][0]["message"]["caption"].ToStr(); StringSplit(chat_message_text, StringGetCharacter("\n", 0), output); StringSplit(chat_message_caption, StringGetCharacter("\n", 0), output2); if (res == -1) { Print("Error in WebRequest. Error code = ", GetLastError()); MessageBox("Add the address in the list of allowed URLs on tab 'Expert Advisors'", "Error", MB_ICONINFORMATION); } else { // Save chat ID int filehandle11 = FileOpen("chat_id.txt", FILE_WRITE | FILE_TXT); FileWriteString(filehandle11, IntegerToString(chat_id)); FileClose(filehandle11); // Save message details int filehandle2 = FileOpen("message_details.txt", FILE_WRITE | FILE_CSV); int count_lines = ArraySize(output); for (int x = 0; x < count_lines; x++) { FileWriteString(filehandle2, output[x]); FileWriteString(filehandle2, ","); } int count_lines2 = ArraySize(output2); for (int x = 0; x < count_lines2; x++) { FileWriteString(filehandle2, output2[x]); FileWriteString(filehandle2, ","); } FileClose(filehandle2); } } return capture_previous_update_id; } double TruncateNumber(double number) { // Multiply, truncate, then divide to keep 3 decimals double truncated = MathFloor(number * 1000.0) / 1000.0; return truncated; } double updatetextboxdouble(string ssparam, string boxname, double boxvaluecurrent) { double boxvalue=boxvaluecurrent; if(ssparam == boxname) { string volumeText = ObjectGetString(0, boxname, OBJPROP_TEXT); boxvalue = StringToDouble(volumeText); ObjectSetString(0, boxname, OBJPROP_TEXT, string(boxvalue)); } return boxvalue; } string updatetextboxstring(string s2param, string boxname, string boxvaluecurrent) { string boxvalue=boxvaluecurrent; if(s2param == boxname) { string volumeText = ObjectGetString(0, boxname, OBJPROP_TEXT); ObjectSetString(0, boxname, OBJPROP_TEXT, boxvalue); } return boxvalue; } void createLabel(CLabel &labelCtrl, CAppDialog &parent, string name, string text, int startx, int endx, int starty, int endy) { labelCtrl.Create(0,name,0,startx,starty,endx,endy); parent.Add(labelCtrl); labelCtrl.Text(text); } void createInputBox(CEdit &editCtrl, CAppDialog &parent, string name, string text, int startx, int endx, int starty, int endy) { editCtrl.Create(0,name,0,startx,starty,endx,endy); parent.Add(editCtrl); editCtrl.Text(string(text)); } string SecondsToHMS(int totalSeconds) { int hours = totalSeconds / 3600; int minutes = (totalSeconds % 3600) / 60; int seconds = totalSeconds % 60; return StringFormat("%02d:%02d:%02d", hours, minutes, seconds); } int GetTextWidth(string text, int fontSize = 10) { // Average width per character for basic fonts double avgCharWidth = fontSize * 0.6; // You can tweak this multiplier if needed return (int)(StringLen(text) * avgCharWidth); } bool isbasecurrency(string symbol, string currency) { if (StringLen(symbol) < 6 || StringLen(currency) != 3) return false; // Basic validation string base = StringSubstr(symbol, 0, 3); // First 3 letters are base currency return (base == currency); } void CheckAndDeleteOldPendingOrders() { int totalOrders = OrdersTotal(); for (int i = totalOrders - 1; i >= 0; i--) { ulong ticket = OrderGetTicket(i); if (OrderSelect(ticket)) { int type = (int)OrderGetInteger(ORDER_TYPE); long magic = OrderGetInteger(ORDER_MAGIC); datetime openTime = (datetime)OrderGetInteger(ORDER_TIME_SETUP); // Correct method to get order setup time // Check if it's a pending order with non-zero magic number if ((type == ORDER_TYPE_BUY_LIMIT || type == ORDER_TYPE_BUY_STOP || type == ORDER_TYPE_SELL_LIMIT || type == ORDER_TYPE_SELL_STOP) && magic != 0) { datetime elapsed = TimeCurrent() - openTime; if (elapsed > 600) { trade.OrderDelete(ticket); // Delete order if older than 10 minutes } } } } }