Simple Expert Advisor Issue .. Help pleaseee

I am trying to build a very simple EA that monitors all open positions and then closes a position if that one reaches 10 pips drawdown …

I am familiar with MQL5 and have written many codes with it, though I am not a professional programmer … the EA is simply not closing positions at 10 pips, sometimes 1 sometimes 2 sometimes 4 …

Can anyone take a quick look at the code to perhaps see where a mistake might be ? I am trading GOLD so the pips are calculated as the difference between open and current price * 10 …

I appreciate all help and suggestions !

#include <Trade\Trade.mqh>

// Create an instance of CTrade class
CTrade trade;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   // Initialization code
   EventSetTimer(300); // Set timer to trigger every 5 minutes (300 seconds)
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Deinitialization code
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // Check if the current time is after 01:00:00
   datetime currentTime = TimeCurrent();
   MqlDateTime currentTimeStruct;
   TimeToStruct(currentTime, currentTimeStruct);
   int hour = currentTimeStruct.hour;

   if(hour < 1) return;

  
   // Loop through all open positions
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      // Get the symbol of the position
      string symbol = PositionGetSymbol(i);

      // Select the position by symbol
      if(PositionSelect(symbol))
        {
         double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         double currentPrice = 0;
         long positionType = PositionGetInteger(POSITION_TYPE);

         // Get current price based on position type
         if(positionType == POSITION_TYPE_BUY)
            currentPrice = SymbolInfoDouble(symbol, SYMBOL_BID);
         else if(positionType == POSITION_TYPE_SELL)
            currentPrice = SymbolInfoDouble(symbol, SYMBOL_ASK);

         // Calculate pips
         double pips = (currentPrice - openPrice) * 10;

         // For sell positions, pips should be negative
         if(positionType == POSITION_TYPE_SELL)
            pips = -pips;
            
         // Close position if pips reach -10
         if(pips >= -10)
           {
            trade.PositionClose(PositionGetInteger(POSITION_TICKET));
            Print("Closed position: ", PositionGetInteger(POSITION_TICKET));

           }
        }
     }
  }
//+------------------------------------------------------------------+
//| TEST                                                |
//+------------------------------------------------------------------+
void OnTimer()
  {
  
   // Check if the current time is after 01:00:00
   datetime currentTime = TimeCurrent();
   MqlDateTime currentTimeStruct;
   TimeToStruct(currentTime, currentTimeStruct);
   int hour = currentTimeStruct.hour;

   if(hour < 1) return;

   
   // Open a buy and sell order with no stop loss or take profit
   string symbol = _Symbol;
   double lotSize = 0.1; // Example lot size

   // Open a buy order
   trade.Buy(lotSize, symbol);

   // Open a sell order
   trade.Sell(lotSize, symbol);
  }
//+------------------------------------------------------------------+

Hi @pilgrimprayers, Finally I can have coding related topic in BP :smiley:

Look at this part

if(pips >= -10)

it should be

if(pips <= -10)

Your logic is wrong.
Next, when you mention about closing bellow 10 pips, be careful most broker identify 1 pip = 10 points for now. Some will have 1 pip = 100 points (XAU exness)

So if you want to close after 10 pips DD your code need to be
if(pips <= -100)

1 Like

Oh wow how silly of me … I feel so slow !

Thank you so much for that, I will try out the code as soon as I get home!

Greatly appreciated:)

1 Like