What's the best practice for handling repeated true if conditions due to multiple ticks during one period?

To add comments, please log in or register
darkenedforest
36
darkenedforest  
Suppose you want to do some action where if on the previous candle of some period indicator a == some value and indicator b<c then open a long position.

Because Ontick() runs many times during the current period, when that if statement is true, it will trigger on each tick.  So, it would open a long position on each tick.

So obviously, you could test if a position is open and not open a new one of one is open.

But the question is more of a general nature about the tick causing multiple instances of a condition being true.

What is the best way to handle that if you are looking for some action to happen only once when a condition is true?

Do you need to create a bool and set it to true when the condition is met, and include boolConditionFlag==false in your if statement?

It just seems like you'd have a lot of boolean flags and there has got to be a more elegant way.

In my head, it seems there would be an event handler such as onPeriordClose().  Alas, that doesn't seem to exist.  So I am left to wonder, how is this generally accomplished?

William Roeder
21808
William Roeder  
  1. There is no onPeriodClose because you can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

    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 2010.06.06

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum 2014.04.04

  2. Don't look at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

  3. Both
roshjardine
28
roshjardine  
darkenedforest:
Suppose you want to do some action where if on the previous candle of some period indicator a == some value and indicator b<c then open a long position.

Because Ontick() runs many times during the current period, when that if statement is true, it will trigger on each tick.  So, it would open a long position on each tick.

So obviously, you could test if a position is open and not open a new one of one is open.

But the question is more of a general nature about the tick causing multiple instances of a condition being true.

What is the best way to handle that if you are looking for some action to happen only once when a condition is true?

Do you need to create a bool and set it to true when the condition is met, and include boolConditionFlag==false in your if statement?

It just seems like you'd have a lot of boolean flags and there has got to be a more elegant way.

In my head, it seems there would be an event handler such as onPeriordClose().  Alas, that doesn't seem to exist.  So I am left to wonder, how is this generally accomplished?

use global variable, change when condition is met, set it back when you are done with the outcome of your expected condition

darkenedforest
36
darkenedforest  

roshjardine

This is exactly what I was describing, albeit less succinct.  But you still have the situation where your condition tests true, so you have to include your global variable in the test condition.  Which works for a simple situation, but it just seems that you could end up with a lot of global variables, and "did it get set at the right time and set back at the right time".  All to overcome this problem of testing the same criteria on every tick, when you are really looking for it one time at each period of a time frame.  But I suppose that's how you do it.


William Roeder

There is no onPeriodClose because you can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

Not really meaning a candle as much as the time of a peroid.  i.e. M1 has a peroid of one minute, so at 12:01:00 the period of 12:00:00 closed.  Possibly you mean the same thing by Always use time?  But how do you use time?  I read the paragraph description for OnTimer previously because I thought that would be the trick to testing a condition every minute, but it lead me to believe that it was for notification event.  However, further investigation seems like maybe OnTimer could work for such a situation.  Is that what you meant by Always use time?

roshjardine
28
roshjardine  
darkenedforest:

roshjardine

This is exactly what I was describing, albeit less succinct.  But you still have the situation where your condition tests true, so you have to include your global variable in the test condition.  Which works for a simple situation, but it just seems that you could end up with a lot of global variables, and "did it get set at the right time and set back at the right time".  All to overcome this problem of testing the same criteria on every tick, when you are really looking for it one time at each period of a time frame.  But I suppose that's how you do it.


William Roeder

There is no onPeriodClose because you can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

Not really meaning a candle as much as the time of a peroid.  i.e. M1 has a peroid of one minute, so at 12:01:00 the period of 12:00:00 closed.  Possibly you mean the same thing by Always use time?  But how do you use time?  I read the paragraph description for OnTimer previously because I thought that would be the trick to testing a condition every minute, but it lead me to believe that it was for notification event.  However, further investigation seems like maybe OnTimer could work for such a situation.  Is that what you meant by Always use time?

then use OOP approach if you don't want the burden of lot global variables...
William Roeder
21808
William Roeder  

darkenedforest: Always use time?  But how do you use time? 

I read the paragraph description for OnTimer previously because I thought that would be the trick to testing a condition every minute, but it lead me to believe that it was for notification event.  However, further investigation seems like maybe OnTimer could work for such a situation.  Is that what you meant by Always use time?

  1. Do you see those lines that are blue and underlined? They are links. Click on them and they take you to more information. (#1.1 last link)
  2. It is a notification event, for equal times. Timing of new bars are not. (#1.1 second paragraph)
roshjardine
28
roshjardine  
William Roeder:
  1. Do you see those lines that are blue and underlined? They are links. Click on them and they take you to more information. (#1.1 last link)
  2. It is a notification event, for equal times. Timing of new bars are not. (#1.1 second paragraph)
i saw William's code..he's right..use static variable in your function that needs to be run once..it's common in many programming.. and also capture the time of first condition and open position..hope this helps
To add comments, please log in or register