Hi
I'm confused about closing a trade.
I am working on a multi symbol EA. So I have an array of symbols that the code works through. I also have a corresponding array of tickets for any trades that are opened. I'm trying to close trades when exit conditions are met, using their ticket number.
Most of this is working except that I don't seem to be able to close a trade. Stepping through the code it seems that the CTrade::PositionClose(const ulong ticket,const ulong deviation) function is not seeing a ticket number, despite the watch list showing the ticket number.
This is my close trade function gets passed an index value for my array that corrisponds to the trade that needs to be closed.
So working though the debugging processes, I see for example a trade with a ticket number 11 on the chart display and the variable openTradeOrderTicket[indx]=11. So that is correct.
I then step into the trade.mqh module
on entry the debugger's watch shows ticket is still 11. However:
that PositionSelectByTicket function seems to return false and the PositionClose function ends without doing anything, the trade is left open, but trade.ResultRetcode returns 10009 that I thought meant TRADE_RETCODE_DONE, Request completed.
I'm obviously missing something, but I don't know what?
Thank you for any help given.
Further investigation seems to suggest that I don't understand the ticketnumber.
This is my code for opening the trade:
void ProcessTradeOpen(int indx, string tradeDirection, double sl) { string currentSymbol=symbolArray[indx]; double tp=0; if (tradeDirection=="LONG") { //calc lots double lots; if(!CalcLots(lastTick.bid-sl,lots)){return;} if (trade.PositionOpen(currentSymbol,ORDER_TYPE_BUY,lots,lastTick.ask,sl,0,"Ichi Breakout Strat- long trade")) { openTradeOrderTicket[indx]=trade.ResultDeal(); openTradeOrderDir[indx]="LONG"; Print(currentSymbol," we have just created a, ",openTradeOrderDir[indx], " order with ticket no :",openTradeOrderTicket[indx] ); } else { Print("PositionOpen() method failed. Return code=",trade.ResultRetcode(), ". Code description: ",trade.ResultRetcodeDescription()); } } else if (tradeDirection=="SHORT") { //calc lots double lots; if(!CalcLots(lastTick.bid-sl,lots)){return;} if (trade.PositionOpen(currentSymbol,ORDER_TYPE_SELL,lots,lastTick.ask,sl,0,"Ichi Breakout Strat - short trade")) { openTradeOrderDir[indx]="SHORT"; openTradeOrderTicket[indx]=trade.ResultDeal(); Print(currentSymbol," we have just created a, ",openTradeOrderDir[indx], " order with ticket no :",openTradeOrderTicket[indx] ); } else { Print("PositionOpen() method failed. Return code=",trade.ResultRetcode(), ". Code description: ",trade.ResultRetcodeDescription()); } } return; }
I thought trade.ResultDeal() returned the ticketnumber? but I get a different value when I step through the list of open trades and then retrieve the ticket number for my symbol.
I thought trade.ResultDeal() returned the ticketnumber. https://www.mql5.com/en/docs/standardlibrary/tradeclasses/ctrade/ctraderesultdeal says "Gets the deal ticket." I guess that is not the same?I assume I need to find a function that returns the same parameter as expected by the PositionSelectTicket() function?

- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
I'm confused about closing a trade.
I am working on a multi symbol EA. So I have an array of symbols that the code works through. I also have a corresponding array of tickets for any trades that are opened. I'm trying to close trades when exit conditions are met, using their ticket number.
Most of this is working except that I don't seem to be able to close a trade. Stepping through the code it seems that the CTrade::PositionClose(const ulong ticket,const ulong deviation) function is not seeing a ticket number, despite the watch list showing the ticket number.
This is my close trade function gets passed an index value for my array that corrisponds to the trade that needs to be closed.
So working though the debugging processes, I see for example a trade with a ticket number 11 on the chart display and the variable openTradeOrderTicket[indx]=11. So that is correct.
I then step into the trade.mqh module
on entry the debugger's watch shows ticket is still 11. However:
that PositionSelectByTicket function seems to return false and the PositionClose function ends without doing anything, the trade is left open, but trade.ResultRetcode returns 10009 that I thought meant TRADE_RETCODE_DONE, Request completed.
I'm obviously missing something, but I don't know what?
Thank you for any help given.