Reversal close on grid

 

Hi there, tried two different ways to close grid positions on reversal signal but result is disastrous, the entry condition is based on oscillators so the idea is quite simple: buy low and sell high.

As a grid, it could be that there are several positions into one direction and then it appears the opposite signal. Instead of a StopLoss, I'm looking to close positions in the wrong direction by "reversal close".

These are the lines:

      for(int i=PositionsTotal()-1;i>=0;i--){
         ulong iTicket=PositionGetTicket(i);
         if(PositionGetString(POSITION_SYMBOL)==_Symbol){
            if(ReversalClose==true){
               if(BuyOrders>0&&SellOrders>0){
                  if(!trade.PositionClose(iTicket,ULONG_MAX)){
                     Print("PositionClose error ",trade.ResultRetcode());
                     return;
                     }
                  else{j--;}
                  }
               }

When there are opened orders in both directions, close from the first one so then it would be only one opened order (last one which is new signal).

Also wrote into a forward counter loop through FIFO rules too but result is the same: curve going down.

Actually think there must be some flaw in this module cause in the right way there should be some profitable deals especially into the flat but the advisor is just closing losing orders.

Hope someone can help in this matter.

 

When there is a trading system: when a trading signal occurs, it is necessary to close opposite positions, I use a universal code - a code that can close OR BUY positions OR SELL positions:

//+------------------------------------------------------------------+
//| Close positions                                                  |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
  {
   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() && m_position.Magic()==InpMagic)
            if(m_position.PositionType()==pos_type)
               if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol
                  Print(__FILE__," ",__FUNCTION__,", ERROR: ","BUY PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());
  }
 
Vladimir Karputov:

When there is a trading system: when a trading signal occurs, it is necessary to close opposite positions, I use a universal code - a code that can close OR BUY positions OR SELL positions:

Thank you Vlad, I will try this.

 
David Diez :

Thank you Vlad, I will try this.

I am not 'Vlad' - I am 'Vladimir'. (Help: these are two different male names)

 
Vladimir Karputov:

I am not 'Vlad' - I am 'Vladimir'. (Help: these are two different male names)

Ok, sorry then.

Reason: