Download MetaTrader 5
To add comments, please log in or register
Have you found an interesting application? Publish it in Code Base - millions of traders will appreciate that!
mallymally
94
mallymally 2016.02.18 20:03 

How to check for second 0 of a candle in different currencys / and check a condition on the previous candle / to start an order



Background

I work on an EA to place an orders at second 0, when a condition is true.

This EA is running in one Chart – one Currency – but it checks conditions for different currencys.


TimeCurrent() Issue

  • I found out, that TimeCurrent() returns the time of the last tick of that chart the EA ist running in, when TimeCurrent() is called from OnTick().
  • And that TimeCurrent() returns the last Time that was sent by the broker with the last tick of all currencys, when TimeCurrent() is called from within OnTimer().


Condition – check the previous candle

The condition will check the last candle (index 1) , when the current candle (index 0) has opened and it's second 0.

In other words, when we are in second 0 of a new candle, the condition will check the previous candle and if the condition is true, an order will be generated.


iTime() Issue

Lets stay in one timeframe, and just look at different currencys, that are analysed by the EA, thats running in only one chart:

On my way getting these two things (second-0 and condition) together, i found out, that on some currencys the iTime() call sometimes returns the new-candle-time, or sometimes the time of the previous candle. How can that, be? The new candle has startet but, at some currencys iTime() returns the time of the old, previous candle.

My explaination is, that the metatrader 4 gets a timestamp with every tick of a certain currenys. And thats the time especially for that one tick in that one currency. So if there's no new tick, even when the new candle has startet, for that one curreny, you will get the last known candle time where the last tick was in, if you call iTime(). Is that right?


My Conclusion

So, we need the condition that checks the previous candle to start a trade.

I thought, we might do it in two cases than:

  1. when there is a tick in a currency at second 0, check condition on the previous candle
  2. when there is no tick in a currency at second 0, check condition on the current candle, because no new candle has been created by a tick. So, the old candle, thats still the current one, wont change when the next tick will come. because when a tick will come, that tick will open a new candle. The new tick belongs to the new candle. thats what i found out, is that right?


Questions

  • Do you thing case 2 will work that way? Or did I forget or miss something?
  • And how can I check that we are in second 0 ?  - I tried to check second 0 via a OnTimer() call that will be called every 200ms. Is that a good way to check second 0 of a candle, or is there a better way to do that.
Alain Verleyen
Moderator
29589
Alain Verleyen 2016.02.18 21:30  

There is no "second 0" of a candle.

So if there's no new tick, even when the new candle has startet

If there is no new tick, there is no new candle, it's how MT4 (and MT5) are working. To be clear, an example : timeframe H1, we have a candle with time 09:00, the candle with time 10:00 will only be created on the first tick after 10:00. It could be 10:00:00 or 10:00:10 or 10:01 or whatever.

From there it depends of your need how you will deal with that.

/
To add comments, please log in or register