The correct code is:
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==m_symbol.Name()) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());
The correct code is:
for(int i=PositionsTotal()-1;i>=0;i--){ ulong iTicket=PositionGetTicket(i); if(PositionSelectByTicket(iTicket)){ if(PositionGetString(POSITION_SYMBOL)==SName){ if(!trade.PositionClose(iTicket,ULONG_MAX)){ Print("PositionClose error ",trade.ResultRetcode()); return; } } } }It does the the same, closes every position after the first opened. Need to filter positions by its symbol.
I wrote this way: It does the the same, closes every position after the first opened. Need to filter positions by its symbol .
Your code ( # 2 ) IS NOT UNIVERSAL: it will not work on hedge accounts (your code will work only on netting accounts).
And here is the filter:
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties if(m_position.Symbol()==m_symbol.Name()) if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.PositionClose ",m_position.Ticket());
- 2020.02.10
- www.mql5.com
Your code ( # 2 ) IS NOT UNIVERSAL: it will not work on hedge accounts (your code will work only on netting accounts).
And here is the filter:
I wrote this way: It does the the same, closes every position after the first opened. Need to filter positions by its symbol.
Your code is the same as mine's but in a more complex way, and for the purpose we need a forward counter loop.
for(int i=PositionsTotal()-1;i>=0;i--){ // returns the number of current positions ulong iTicket=PositionGetTicket(i); if(PositionSelectByTicket(iTicket)){ // selects the position by index for further access to its properties if(PositionGetString(POSITION_SYMBOL)==SName){ // SName=SymbolName(s,true) into the SymbolsTotal loop. if(!trade.PositionClose(iTicket,ULONG_MAX)){ // close a position by the specified m_symbol Print("PositionClose error ",trade.ResultRetcode()); return; } } }
Your code is the same as mine's but in a more complex way, and for the purpose we need a forward counter loop.
My code is vice versa easy.
As for the cycle: if you delete positions, the loop should GO TO ZERO:
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
this is the law.
My code is vice versa easy.
As for the cycle: if you delete positions, the loop should GO TO ZERO:
this is the law.
That works for a grid expert no more. Isn't a rule. I need to close older, this is FIRST.
That works for a grid expert no more. Isn't a rule. I need to close older, this is FIRST.
As for the cycle: if you delete positions, the loop should GO TO ZERO :
for ( int i= PositionsTotal ()- 1 ; i>= 0 ; i-- ) // returns the number of current positions
this is the law.
If you need to find an "older" or "younger" position - do a search BY OPENING TIME of the position. This is the second law :)
As for the cycle: if you delete positions, the loop should GO TO ZERO :
this is the law.
If you need to find an "older" or "younger" position - do a search BY OPENING TIME of the position. This is the second law :)
We are forgetting this:
if(PositionsTotal())>1){ Loop should close first }
When there appears a new position on a working symbol the loop should close the first one cause positionstotal couldn't be more than one.
But the thing is the advisor is closing EVERY POSITION after the first one on ANY SYMBOL. So the symbol filter isn't working properly.
We are forgetting this:
When there appears a new position on a working symbol the loop should close the first one cause positionstotal couldn't be more than one.
But the thing is the advisor is closing EVERY POSITION after the first one on ANY SYMBOL. So the symbol filter isn't working properly.
Did I understand your desire correctly? (I warn you, I used telepathy only 1%) - we opened position No. 1 by the symbol "A", opened position No. 2 by the symbol "B" and if you opened position No. 3 by the symbol "A", then you need to close position No. 1.
- 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, have an expert to work every symbol from a single chart and tried to build a tiny code to close at reversal signal but unsuccessful:
for(int i=0;i<PositionsTotal();i++){ if(PositionSelect(SName)){ // Filter by Symbol. if(PositionsTotal()>1){ // If (symbol) PositionsTotal()>1 if(!trade.PositionClose(SName,ULONG_MAX)){ // Close older. Print("PositionClose error ",trade.ResultRetcode()); return; } } } }
What it makes is closing every position >1 independently of the symbol. Hope someone can help, thank you in advance.