Support with PositionClose for Expert Advisor (MT5)

To add comments, please log in or register
sadema3
87
sadema3  

Dear community,

I am trying to program an Expert Advisor in Metatrader 5.

A buy or sell trade is to be carried out on the basis of different indicators.

I am currently having problems closing a trade and I am stuck. Can someone please give me a reminder?

1. it is successfully initialized

2. A trade is opened

3. The signal values ​(MACD, ADX, etc.) ​and the number of open "Buy" and "Sell" positions are checked as comments in the chart

4. The trade should be closed based on different criteria. Below is the code, in yellow you can find the mentioned request.

I would be very grateful for your support.

Best wishes,

sadema3 

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Properties
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Properties
  • www.mql5.com
Price chart drawing. If false, drawing any price chart attributes is disabled and all chart border indents are eliminated, including time and price scales, quick navigation bar, Calendar event labels, trade labels, indicator and bar tooltips, indicator subwindows, volume histograms, etc. Scrolling the chart horizontally using the left mouse...
Files:
Vladimir Karputov
Moderator
207337
Vladimir Karputov  

Read the help more carefully:

It is necessary to send a ticket, not a serial number.

Closes a position with the specified ticket.

bool  PositionClose(
   const ulong   ticket,                  // position ticket
   ulong         deviation=ULONG_MAX      // deviation
   )


Error:

// Close Buy positions
   if(BuyPositions >= 1)
     {
      for(int i = PositionsTotal()-1; i>=0; i--)
        {
         string symbol=PositionGetSymbol(i);
         if(_Symbol == symbol)
            ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         if(PARvalue < price0 ||EMAvalue < price0 ||ADXvalue < ADXlow ||
            DIPvalue < DIMvalue ||MACDvalue < SIGNALvalue ||RSIvalue > RSIlow)
           {
            CTrade trade;
            trade.PositionClose(i);
           }
        } // End for
     } // End if

Not error:

// Close Buy positions
   if(BuyPositions >= 1)
     {
      for(int i = PositionsTotal()-1; i>=0; i--)
        {
         string symbol=PositionGetSymbol(i);
         if(_Symbol == symbol)
            ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
         if(PARvalue < price0 ||EMAvalue < price0 ||ADXvalue < ADXlow ||
            DIPvalue < DIMvalue ||MACDvalue < SIGNALvalue ||RSIvalue > RSIlow)
           {
            CTrade trade;
            trade.PositionClose(PositionTicket);
           }
        } // End for
     } // End if
sadema3
87
sadema3  
Vladimir Karputov:

Read the help more carefully:

It is necessary to send a ticket, not a serial number.

Closes a position with the specified ticket.


Error:

Not error:

Thanks Vladimir for your support.


I changed the code to

trade.PositionClose(PositionTicket);


but by debugging the complete code, I got these failures:

- "PositionTicket" - undeclared identifier

- "PositionClose" - ambiguous call to overloaded function 


Do you have another advice?

Thank you very much.

Best regards,

sadema3

Vladimir Karputov
Moderator
207337
Vladimir Karputov  
sadema3 :

Thanks Vladimir for your support.


I changed the code to


but by debugging the complete code, I got these failures:

- "PositionTicket" - undeclared identifier

- "PositionClose" - ambiguous call to overloaded function  


Do you have another advice?

Thank you very much.

Best regards,

sadema3

Modified version:

// Close Buy positions
   if(BuyPositions >= 1)
     {
      for(int i = PositionsTotal()-1; i>=0; i--)
        {
         string symbol=PositionGetSymbol(i);
         ulong PositionTicket=0;
         if(symbol==Symbol())
           {
            PositionTicket=PositionGetInteger(POSITION_TICKET);
            if(PositionTicket>0)
               if(PARvalue < price0 ||EMAvalue < price0 ||ADXvalue < ADXlow ||
                  DIPvalue < DIMvalue ||MACDvalue < SIGNALvalue ||RSIvalue > RSIlow)
                 {
                  CTrade trade;
                  trade.PositionClose(PositionTicket);
                 }
           }
        } // End for
     } // End if
lippmaje
1210
lippmaje  
Vladimir Karputov:

Modified version:

((I think you were mistaking i with PositionTicket.)) And it should be checked if the position type is a Buy.

// Close Buy positions
   if(BuyPositions >= 1)
     {
      for(int i = PositionsTotal()-1; i>=0; i--)
        {
         string symbol=PositionGetSymbol(i);
         ulong PositionTicket=0;
         if(symbol==Symbol())
           {
            PositionTicket=PositionGetInteger(POSITION_TICKET);
            if(PositionTicket>0 && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
               if(PARvalue < price0 ||EMAvalue < price0 ||ADXvalue < ADXlow ||
                  DIPvalue < DIMvalue ||MACDvalue < SIGNALvalue ||RSIvalue > RSIlow)
                 {
                  CTrade trade;
                  trade.PositionClose(PositionTicket);
                 }
           }
        } // End for
     } // End if
Vladimir Karputov
Moderator
207337
Vladimir Karputov  
lippmaje :

I think you were mistaking i with PositionTicket . And it should be checked if the position type is a Buy.

Read post

You probably wedged into our conversation at the moment when I was editing the code

But checking the type of position (BUY) is good advice.

Support with PositionClose for Expert Advisor (MT5)
Support with PositionClose for Expert Advisor (MT5)
  • 2020.07.27
  • www.mql5.com
Dear community, I am trying to program an Expert Advisor in Metatrader 5...
sadema3
87
sadema3  
Vladimir Karputov:

Read post

You probably wedged into our conversation at the moment when I was editing the code

But checking the type of position (BUY) is good advice.

Thank you for your support :) 

I will modify and check if it is working. 

Best regards,

sadema3

sadema3
87
sadema3  
sadema3:

Thank you for your support :) 

I will modify and check if it is working. 

Best regards,

sadema3

Thanks Vladimir, it is working fine. Your support is very appreciated :) 

To add comments, please log in or register