Count bars since ... ...

 
Hello guys! I have one question and I need some help please. I have made a EA based on several indicators for entry/exit/TP/SL etc.  My issue is that it is opening too many trades. What I mean is : I have one 2 lines cross indicator  as one of the conditions for opening a trade. If crossing up + another conditions open buytrade, if crossing down+another conditons opensell. So lets suppose that indicator crossing up, all other conditions are met, the trade is opened. After few candles TP is hit and trade is closed. But the indicator did not flip the other way yet, and then another trade is opened. This is what I want to stop in certain conditions. For example if indicator flipped 7 candles ago , do not open any trade.  What I mean is that for example a trend is forming, all my indicators are agreeing for opening a trade, the trade is opened, and TP is hit after 4-5 candles. And another order is opened but maybe the trend is nearly over, and I am entering close to the bottom. This I want to change. To create a condition in such way that if my indicator flipped [7] candles ago, not to open a trade. How can I count the bars since indicators flipped on one side
 
Daniel cioca the indicator did not flip the other way yet, and then another trade is opened.
  1. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 (2017)


  2. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

 
William Roeder #:
  1. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 (2017)


  2. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)

Thanks!

I find it difficult like this. Is there any way to count the candles from a last crossover of two MA for example, until present candle?

 
Daniel cioca #:

Thanks!

I find it difficult like this. Is there any way to count the candles from a last crossover of two MA for example, until present candle?

if you post the signal part of your code (or an example of it) we can show you how to make it correct and its easy its not complicated

 
Khuman Bakhramirad #:

if you post the signal part of your code (or an example of it) we can show you how to make it correct and its easy its not complicated

when you say "signal", you mean the opening conditions? 

Like this ? 

// Open buy order
   if( Count.Buy()<1&&tsi>sig && ma<close&& gBuyTicket ==0 && close1<close && (ind_buffer0>ind_buffer2 || ind_buffer1>ind_buffer2 ))
 
Daniel cioca #:

when you say "signal", you mean the opening conditions? 

Like this ? 

I manage to avoid opening another trade, by inserting another condition for buy/sell signals. Like if the current price is too far ( ATR*1.5) from the base line, do not open trade. 

It is a little better, profit wise, but still NOT satisfied with the outcome.

I would still like to try the version to count the candles, if anyone can point me to a direction how to achieve this. ( don't want the code, but an idea, where to start)


Like Ex : if ma5 == ma35  ... extract time when that event happened.

and then substract it from current time, and the result divide by _Period. This should give the number of bars since the event? 

 
Daniel cioca #:

I manage to avoid opening another trade, by inserting another condition for buy/sell signals. Like if the current price is too far ( ATR*1.5) from the base line, do not open trade. 

It is a little better, profit wise, but still NOT satisfied with the outcome.

I would still like to try the version to count the candles, if anyone can point me to a direction how to achieve this. ( don't want the code, but an idea, where to start)


Like Ex : if ma5 == ma35  ... extract time when that event happened.

and then substract it from current time, and the result divide by _Period. This should give the number of bars since the event? 


How to extract event time ??

If you have event time and want to find how many bars passed since use iBarShift and it will give you the exact candles number
If you have the candles count and want the time of that even use iTime()
 
Khuman Bakhramirad #:
If you have event time and want to find how many bars passed since use iBarShift and it will give you the exact candles number
If you have the candles count and want the time of that even use iTime()

But I dont have event time. That is my question. How to catch event time. 

Cause I need to somehow to catch the time of the two lines crossing, and then count from that time.

"line1==line2 .. remember what time is it know "  -- how do I put that into code? .... :)

tried something..but it not will do anything... 

datetime present;
   datetime time;
   double bars;
   if (sig==tsi)
     {
      time=iTime(_Symbol,0,1);
     }
   present=TimeCurrent();
   bars= (double)((time-present)/_Period);
Actually I dont the answer to that. I would like you to point me to where should I read so I can answer to that myself.
 
  1.    if (sig==tsi)

    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 (2013)

  2.    bars= (double)((time-present)/_Period);
    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (20 June 2006)
              No candle if open = close ? - MQL4 programming forum
 
William Roeder #:
  1. Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 (2013)

  2. This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
              "Free-of-Holes" Charts - MQL4 Articles (20 June 2006)
              No candle if open = close ? - MQL4 programming forum

Ok..thanks...and   3)..... I was waiting for 3) ..where point me somewhere..:) 

 
Daniel cioca #: . I was waiting for 3) ..where point me somewhere..:) 

bars=iBarShift(time)

Reason: