Could you please give me some advice on this code?

 

Hi, thank you for taking the time to read this thread.

I added a new function to this program so that it only closes its own orders when trading and does not close other orders that were opened manually or opened by other expert advisors. By adding this function, the program makes only one order during Backtest and is not able to make any more trades in the Backtest. No error message can be seen in the "Journal" section. Do you know where the problem lies in the code?

Regards,
Rahub


The following function is newly added to the code (highlighted, Ln26-41 & Ln309-319):

void CloseAllOrders(int MagicNumber, const string symbol)
{
   for(int b = 0; a < 5; b++)
   {
      for (int i = 0;i < OrdersTotal(); i++)
      {  
         bool Check = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if (Check && OrderMagicNumber() == MagicNumber && OrderSymbol() == symbol)
         {
            if (OrderType() == OP_BUY) if (!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID), 5000, clrAqua)) Print("Can't close all orders" + IntegerToString(GetLastError()));
            if (OrderType() == OP_SELL) if (!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK), 5000, clrAqua)) Print("Can't close all orders" + IntegerToString(GetLastError()));
            else if (OrderType() >= 2) if (!OrderDelete(OrderTicket())) Print("Can't close all orders" + IntegerToString(GetLastError()));
         }
      }
   }
}
			.
			.
			.
			.
			.
			.
			.
			.
			
if (OpenOrderProfits(1313,Symbol()) > TotalInvested() * TakeProfit)

            {
               Ticket = 0;
               CloseAllOrders(1313,Symbol());
               check = False;
            }
            if (OpenOrderProfits(1313,Symbol()) < -AccountBalance() * Risk/100)   
            { 
               Ticket = 0;
               CloseAllOrders(1313,Symbol());
               check = False;


            

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
Symbol Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
Rsi-MarTest.mq4  22 kb
 
Roja Rohub: Do you know where the problem lies in the code?
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)