OrderClose Won't Work.. It Hates Me

 

Hi

I have the code below, that is a portion of code extracted from my project, which according to other examples I've read as well as the MQL4 documentation I've read, should work without a problem. I have alerts in amongst the code to inform me what is happening & the code appears to be working as it should. That is with the exception of the OrderClose function.

I have buttons on screen with the same name as the ticket number of the trade & I'm trying to close the trade with that ticket number by comparing the return of the button name (sparam) and the OrderTicket() number. That part is working, the testing of a Buy or Sell trade works, the OrderClose is not playing fair. The code isn't the finished article, it's a work in progress.

Debugging MQL4 is a nightmare compared to VBA.

Can anyone see why it's not working please?

Thanks

//+------------------------------------------------------------------+
//|                                                TradeCloser-1.mq4 |
//|                                                           Phil B |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Phil B"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
int A, C, intTicketNum, closing, buyORsell;

   if ( id == CHARTEVENT_OBJECT_CLICK ){ // Use temporarily for ease of testing
 
   C = NULL;
   closing = NULL;
      
    intTicketNum = StrToInteger(sparam);
    
      for(A = OrdersTotal() - 1; A >= 0; A --){

      if (!OrderSelect(intTicketNum,SELECT_BY_TICKET) == true) continue; 
      
         Alert("Ticket Number= " + IntegerToString(OrderTicket()));// To Be Deleted
         
         buyORsell = OrderType();
   
         if ((buyORsell == 0) && (intTicketNum == OrderTicket())){// Buy Trade
         
         if ( ! OrderClose(OrderTicket(), OrderLots(), Bid, 5) == true){
            Alert("Error Closing Buy Trade " + IntegerToString(GetLastError()));}
             
            Alert("Order Type = BUY "+ IntegerToString(buyORsell));// To Be Deleted
            buyORsell = NULL;
            
            break;}
         
         else if ((buyORsell == 1) && (intTicketNum == OrderTicket())){// Sell Trade
         
         if ( ! OrderClose(OrderTicket(), OrderLots(), Ask, 5) == true){
            Alert("Error Closing Sell Trade " + IntegerToString(GetLastError()));}
            
            Alert("Order Type = SELL "+ IntegerToString(buyORsell));// To Be Deleted
            buyORsell = NULL;
            
            break;}
      }
         
   }
   
   return;
   
  }
//+------------------------------------------------------------------+
 

I have not looked closely at your code, but I see that it is an indicator.

An indicator cannot open, modify or close trades so you will need to rewrite your code as an EA.

 
GumRai:

I have not looked closely at your code, but I see that it is an indicator.

An indicator cannot open, modify or close trades so you will need to rewrite your code as an EA.

Hi GumRai

That'll be it then.. Now I'm venturing into unknown territory again

Thanks for the reply

Phil

 

Just to let you know that it's all working now

Thanks for your pointer :)

Reason: