Close at opposite signal

 

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.

 

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());
 
Vladimir Karputov:

The correct code is:

I wrote this way:
      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.
 
David Diez :
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 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());
Close at opposite signal
Close at opposite signal
  • 2020.02.10
  • www.mql5.com
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: What it ma...
 
Vladimir Karputov:

Your code ( ) IS NOT UNIVERSAL: it will not work on hedge accounts (your code will work only on netting accounts).

And here is the filter:

Ok I'll try your code and let you know something, but it's essentially the same tan I wrote and the loop must be forward counter.
 
Vladimir Karputov:

Your code ( ) IS NOT UNIVERSAL: it will not work on hedge accounts (your code will work only on netting accounts).

And here is the filter:

David Diez:
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;
        }
      }
    }
 
David Diez :

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.

 
Vladimir Karputov:

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.

 
David Diez :

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 :)

 
Vladimir Karputov:

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.

 
David Diez :

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.

Reason: