You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
thanks for the advice, here is a very simple version of my code, this is not my real code but I hope this will be more clear.
as I said this is not the real code, finding pattern etc is just an example. the reason I opened this thread is DetectExistingPattern() function
I have already given you sample code on how to run code on the first run of OnTick(), so now update your code post accordingly so that your DetectExistingPattern() runs in the first call of OnTick() and not in the OnInit().
Also, you cannot use "Bars" to detect a New Bar. That is incorrect. You should use either "Time[0]" or "SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE )". Here is an example:
I have already given you sample code on how to run code on the first run of OnTick(), so now update your code post accordingly so that your DetectExistingPattern() runs in the first call of OnTick() and not in the OnInit().
Also, you cannot use "Bars" to detect a New Bar. That is incorrect. You should use either "Time[0]" or "SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE )". Here is an example:
thanks @FMIC, I added the simple code because @jjc asked for it. By the way, I learned using Bars from the from as I remember, everybody was writing that way so I did. Why it is wrong ? Does it can work improperly ?
ps: I use metatrader 4, so it is mql4 right ?
thanks @FMIC, I added the simple code because @jjc asked for it. By the way, I learned using Bars from the from as I remember, everybody was writing that way so I did. Why it is wrong ? Does it can work improperly ?
ps: I use metatrader 4, so it is mql4 right ?
FMIC:
No! On the forum, you will find that many users have shown that the "Bars" method is very old and unreliable because the number of bars can be changed by the "Options" for charts or it can be constant when the maximum number of bars per chart has been reached. So, it is wrong to use it. Use either "Time[0]" or if you want it to be compatible with MQL5 as well, use "SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE )". Please see my example.
as I said this is not the real code, finding pattern etc is just an example. the reason I opened this thread is DetectExistingPattern() function
Returning to your original question... In this example, one option would be that you change your CheckTrendPattern() function so that it can be told to start at a specific bar. Instead of using the fixed range of bars 1 to 10, you instead use an offset of 1 to 10 from a specified start point.
When there is no existing order, and you want to check whether the pattern is currently met, you then use CheckTrendPattern(0) instead of CheckTrendPattern().
When starting up, if you have an existing order then you can look for the pattern which triggered it as follows. This is the same suggestion which I made at outset: you get the bar index which is equivalent to the open time of the existing order, and start looking backwards from there.
However, as FMIC says, it is preferable to do this check in the first call to OnTick() rather than in OnInit(). My own reasons for suggesting this have nothing to do with "holding up the initialisation". Instead, I would be concerned that, when MT4 is re-starting with the EA already attached to a chart, you don't yet 100% reliably have an order list from the broker or an updated bar history.
Another consideration which you may want to bear in mind is the following scenario, all happening during the time covered by a single bar:
You may want to check closed orders as well as open orders, to prevent this.
Returning to your original question... In this example, one option would be that you change your CheckTrendPattern() function so that it can be told to start at a specific bar. Instead of using the fixed range of bars 1 to 10, you instead use an offset of 1 to 10 from a specified start point.
When there is no existing order, and you want to check whether the pattern is currently met, you then use CheckTrendPattern(0) instead of CheckTrendPattern().
When starting up, if you have an existing order then you can look for the pattern which triggered it as follows. This is the same suggestion which I made at outset: you get the bar index which is equivalent to the open time of the existing order, and start looking backwards from there. However, as FMIC says, it is preferable to do this check in the first call to OnTick() rather than in OnInit(). My own reasons for suggesting this have nothing to do with "holding up the initialisation". Instead, I would be concerned that, when MT4 is re-starting with the EA already attached to a chart, you don't yet 100% reliably have an order list from the broker or an updated bar history.
Another consideration which you may want to bear in mind is the following scenario, all happening during the time covered by a single bar:
Now it is more clear why I should do it in OnInit, reliability is the most important point of course and I did not think about your consideration warning. this part is really important, I could open redundant orders, maybe rare but possible. I have to handle this in some ways. really thank you so much.
@WHRoeder your new bar check control is so simple and beautiful. I added your method to my code, thank you.
Hello everyone,
as @FMIC adviced, I write and indicator that finds my pattern, give a signal and a few more data that will use for my orders (I might open more than one order at difference prices). Now I want to use it in my EA but I have a question, I need your experience actually. When I get a signal from my indicator, I will open an order or put an order (sell limit or buy limit etc) if my mt4 crashes for any reason when I reopen it, how can I find that opened order is belong to which signal in my indicator. is it Ok to use bar shift for that ? as we talked before, I can find order creation time, then find that bar shift and should I try to get indicator value by using this shift ? How can I match this order and indicator shift ?
thanks.
Yes, you can use iBarShift(), just as jjc has explained to you in an earlier post to then reference your indicator's data via iCustom() function.