Conditions satisfied several times!!! :/

 

Guys, I have a problem (BTW, this problem occour several times in several several conditions)...

Look my code..

   if(Fast_MovingAverage[1] > Slow_MovingAverage[1]  &&  Fast_MovingAverage[0] < Slow_MovingAverage[0])
     {
      Trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, 0.01, Ask, 0, 0, "");
     };
   
   if(Fast_MovingAverage[1] < Slow_MovingAverage[1]  &&  Fast_MovingAverage[0] > Slow_MovingAverage[0])
     {
      Trade.PositionOpen(Symbol(), ORDER_TYPE_SELL, 0.01, Bid, 0, 0, "");
     };


Several positions are opened when, actually, I'd like was opened only one position. How to fix this problem?
 
PS: This code was wrote in "OnTick"

 
JHenry:

Guys, I have a problem (BTW, this problem occour several times in several several conditions)...

Look my code..


Several positions are opened when, actually, I'd like was opened only one position. How to fix this problem?
 
PS: This code was wrote in "OnTick"

I am assuming that you are testing that on an account that allows hedging.

Add something like this

   int totalForSymbol=0;
      for (int i=0; i<PositionsTotal(); i++)
         if (PositionGetSymbol(i)==_Symbol) totalForSymbol++;

at the beginning of that code and the before each Trade.PositionOpen() check if(totalForSymbol<1)

 
mladen:

I am assuming that you are testing that on an account that allows hedging.

Add something like this

at the beginning of that code and the before each Trade.PositionOpen() check if(totalForSymbol<1)

Yeah! I already using this ideia!!! xDDD

But, and I want open two positions? Three? Four? Five...

So, this is not a good filter...

I'm thinking about to create a filter with time. Something like this: "If a position was opened in the last 1 min, no open more none position."

In this case, I need to create a chronometer that start with 00:00:00 and start the acconting with the opening of a new position and every time that new position is opened, so the chronometer restart the accounting from zero.

This a good ideia!! I think...

So, in this case, I need to create a chronometer and I don't how do it... kkkk 

 
JHenry:

Yeah! I already using this ideia!!! xDDD

But, and I want open two positions? Three? Four? Five...

So, this is not a good filter...

I'm thinking about to create a filter with time. Something like this: "If a position was opened in the last 1 min, no open more none position."

In this case, I need to create a chronometer that start with 00:00:00 and start the acconting with the opening of a new position and every time that new position is opened, so the chronometer restart the accounting from zero.

This a good ideia!! I think...

So, in this case, I need to create a chronometer and I don't how do it... kkkk 

Then replace the if(totalForSymbol<1) with if(totalForSymbol<2) or if(totalForSymbol<3) or if(totalForSymbol<nnn) and so on ...whatever number you wish to set as the maximum opened at the same time