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
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
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
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
I think you were mistaking i with PositionTicket . And it should be checked if the position type is a Buy.
Read post # 1
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.
- 2020.07.27
- www.mql5.com
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 :)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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