Trouble with sgnal given by indicator opens and closes tons of positions

 

Hi all!

I am developing a trend system that opens a position depending on the signal it receives. It turns out that I am trying to incorporate an indicator that works as exit indicator (that is, when the signal is given, the trend is supposed to be over, so the position should closed), but it turns out that there are times when the exit signal it is active at the same time as a valid setup.

Ex: signals to buy (ie, uptrend) are activated and the exit signal is activated, then the position is closed and it opens again in the same candle infinitely many times (the exit signal's indicator should say "hey, this trend is probably over, so get out!)

Oh my gosh! look at this equity curve! haha. welp, that straight line is that period of time when it opned and closed like 1000 trades.



I'll post the part of the code if anyone can give me a hand. This is all in the OnTick() function.

The idea is if I'm long and the main line is < than the signal line, then it means that the trend ir probably over and we should exit the trade (and don't open another buy trade).

In a similar way, if we are short and the main line is > than the signal line, it means that the trend is probably over and we should exit the trade (and don't open another sell trade).

The arrays that contains the information of the RVI handle are set as series.


if(tradesOpen>0)
     {
      string auxCheckTradeType=checkTradeType(); //looks for the actual trade type
      
      
      checkForExit(auxCheckTradeType);
        
      ZeroMemory(lastTick);
      SymbolInfoTick(Symbol(),lastTick);
      if((auxCheckTradeType=="buy" && lastTick.bid>=takeProfit) || (auxCheckTradeType=="sell" && lastTick.ask<=takeProfit)) trailing(auxCheckTradeType);

     }
   else
     {
      takeProfit=0;
      firstCall=true;
      
         if(volume()==0 || DV2()==0 || Spread>spread)
           {
            return;
           }
         if(DSP()==1 && iT()==1) //buy
           {
            openTrade(1);
           }

         if(DSP()==-1 && iT()==-1) //sell
           {
            openTrade(-1);
           }
     }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void checkForExit(string type)
  {
   CopyBuffer(rviHandle,0,0,3,rviMainBuff);     //if long and main<signal=>exit
   CopyBuffer(rviHandle,1,0,3,rviSignalBuff);   //if short and main>signal =>exit

   if((type=="buy" && rviMainBuff[0]<rviSignalBuff[0]) || (type=="sell" && rviMainBuff[0]>rviSignalBuff[0]))
     {
      for(int i=PositionsTotal()-1;i>=0;i--)
        {
         if(mposition.SelectByIndex(i))
           {
            if(mposition.Symbol()==Symbol() && mposition.Magic()==Magic)
              {
               mtrade.PositionClose(mposition.Ticket(),Slippage);
              }
           }
        }
     }
  }
 

Best to find another indicator to incorporate into the strategy....There are many to choose from:

Bollinger

Stochastic

RVI

EMA

etc