My EA can't differentiate between the MB_YES & MB_NO with Messagebox

 

This has genuinely got to be one of the must ludicrous, most ridiculous situations I have even been in with this coding language.

Is there any reason as to why my trades are closing, regardless as to the button pressed on the prompted  Messagebox - despite the fact my EA is only supposed to close trade when the  IDYES  button is selected?

I click the  IDYES  button, my trade liquidates, I click the  IDNO  button, my trade still liquidates.

Why is my EA doing things I have not told it to do? The word "fuming" cannot express enough how I'm feeling.


//+------------------------------------------------------------------+
//|                                          initialization_test.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

datetime LastActiontime;
bool totalOrders = OrdersTotal();
double currencyConversion;
double tradeSpread;
int ordersTotal;

int OnInit()
  {
//---

  LastActiontime=Time[0];
  
  if (totalOrders == 0){
      totalOrders = true;
  }
   

//---
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
//---
   int LotSize = 1;
   int RewardFactor = 3;
   int stopLossPoints = 200;
   double entryPriceBid = MarketInfo(Symbol(),MODE_BID);
   double spread = MarketInfo(Symbol(), MODE_SPREAD);
   double tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE);
   color sellcolor = clrGreen;
   bool Newbar = true;
   currencyConversion = 100000/(tickvalue * 100000);
   tradeSpread = (spread * tickvalue)*LotSize;
   
   if(LastActiontime!=Time[0])
   if(OrdersTotal() == 0)
   if(totalOrders == true){
   
      bool OpenShort = OrderSend(Symbol(),OP_BUY,LotSize,MarketInfo(Symbol(),MODE_BID),100,((entryPriceBid/Point)-(stopLossPoints))*Point,((entryPriceBid/Point)+(stopLossPoints*RewardFactor))*Point,"Spread Charge £"+DoubleToStr((spread * tickvalue)*LotSize,2),Period(),0,sellcolor);
      LastActiontime=Time[0];    
      if (OpenShort == true){
         bool msgBox = MessageBox("Please confirm the risk on this trade with the margin at the bottom of the terminal."+
         "\n"+
         "\nWould you like to close this trade?"+
         "\n"+
         "\n£"+DoubleToStr(((tickvalue * LotSize)*stopLossPoints)+ tradeSpread,2),"Information | ", MB_YESNO|MB_ICONQUESTION|MB_TOPMOST);
         
         if (msgBox = IDYES){
         
            ordersTotal = OrdersTotal();   
            for(int b=ordersTotal-1;b>=0;b--){
            
               if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES)==true){
               
                  bool closeOrder = OrderClose(OrderTicket(),LotSize,MarketInfo(Symbol(),MODE_BID),300,clrAqua);
                  Comment("This trade has closed");

               }  
            }
         
         }

         Print("The tick value used for this EA is £ "+DoubleToStr(tickvalue , 5)+" and the conversion is "+DoubleToStr(currencyConversion,5));
         SendMail("You have opened a position","You have opened a position");
      }
        
   }
   
   
   
   
  }
//+------------------------------------------------------------------+
Timeline for Messagebox is closing trades regardless of "Yes" or "No" button clicked
Timeline for Messagebox is closing trades regardless of "Yes" or "No" button clicked
  • stackoverflow.com
Stack Overflow | The World’s Largest Online Community for Developers
 
BCCI:

This has genuinely got to be one of the must ludicrous, most ridiculous situations I have even been in with this coding language.

Is there any reason as to why my trades are closing, regardless as to the button pressed on the prompted  Messagebox - despite the fact my EA is only supposed to close trade when the  IDYES  button is selected?

I click the  IDYES  button, my trade liquidates, I click the  IDNO  button, my trade still liquidates.

Why is my EA doing things I have not told it to do? The word "fuming" cannot express enough how I'm feeling.


yes because your code is wrong the msgBox returns an Integer not a bool.

so change it to this format   int msgBox = MessageBox..........

then check the value of msgBox  and not IDYES

https://docs.mql4.com/common/messagebox
MessageBox - Common Functions - MQL4 Reference
MessageBox - Common Functions - MQL4 Reference
  • docs.mql4.com
MessageBox - Common Functions - MQL4 Reference
 
Paul Anscombe:

yes because your code is wrong the msgBox returns an Integer not a bool.

so change it to this format   int msgBox = MessageBox..........

then check the value of msgBox  and not IDYES

https://docs.mql4.com/common/messagebox

Thanks Paul, it's incredible what you miss when you're burning the candle at not only both ends but down the middle as well! :)