Analyzing Bars backwards in EA

 

Hello Everyone,

 This could be a really silly question but I really couldn't find the way to do it. I am trying to create an EA. after analyzing some bars, it decides to open and order or not. Let's say it opens an order. somehow if my EA stops or metatrader shuts down, when I reopened it, I want to find the bars that cause to open the order. So I look for the orders and find my order (that EA just opened) and I want to start analyzing back the bars. How will I do that ? how can I find my orders opened bar and start going back on bars.

for ex:

I am running on H1 period and look for the bars at 12:00, 13:00, 14:00 and 15:00 then I decide to open an order at 16:00. the important bars are through 12:00 to 15:00 and I want to identify these bars again. so when re-run the EA first I  will find the order and find its opened date, detect which bar is this date belong, and start the go backwards on bars before that date. 

I thought using Time array etc but, while I was trying to find the bars, what would happen if a new bar comes up? shifts will be changed and my search will be illogical.

I also do not want to write this information to a file. it requires a too much time and a lot of exception handling etc.

thanks. 

 
paranoyakX:

I thought using Time array etc but, while I was trying to find the bars, what would happen if a new bar comes up? shifts will be changed and my search will be illogical.


A new bar shouldn't come up while an EA is executing its code. It works with the data loaded when OnTick() is called.

Not sure what would happen if RefreshRates() is used though.

 
GumRai: Not sure what would happen if RefreshRates() is used though.

Although, it does not happen often (in fact, very seldom), when using RefreshRates(), I have had the experience of the bar data being shifted due to a new bar forming. To get around this problem, I try to copy all the necessary data into variables, at the beginning, and then not relying on the History Data Arrays or equivalent functions again during the rest of the event execution. This way, even if in my code I require using the RefreshRates(), I can still safely refer to the initial conditions.

@paranoyakX: So, irrespective of the use of the RefreshRates() or not, you can try a similar approach. In fact, in MQL5, you do not have the the same ability as MQL4 and you HAVE to copy the data to your own arrays in order to process it. So, by doing it this way, you are in fact a few steps closer to be able to make your code compatible to both MQL4 and MQL5.

However, the best solution, is not to code all this in the EA, but instead to code most of that logic in an Indicator, which does not suffer from this problem, because the approach is totally different. Try to program as much as possible of the Signal logic in an indicator. This is also useful because you can then use and test the indicator visually until it is working exactly as you want it to. One more use of the indicator solution, is that it will provide the signaling not only for the EA but also for manual trading. The EA then, just has to focus on the trading management and not the signalling.

 
paranoyakX:

[...] I thought using Time array etc but, while I was trying to find the bars, what would happen if a new bar comes up? shifts will be changed and my search will be illogical.

What you are asking is potentially so simple that I wonder if I am missing the point. Given a previous OrderSelect(), you can look up the bar-shift associated with its OrderOpenTime() simply by doing the following:

int BarShiftOfOrderOpen = iBarShift(Symbol(), Period(), OrderOpenTime(), false);

For example:

  • H1 chart
  • Current time is 16:32
  • Order was opened at 14:23
  • The above code should return BarShiftOfOrderOpen = 2, i.e. the H1 bar at 14:00, and Time[BarShiftOfOrderOpen] will be 14:00
 
jjc: What you are asking is potentially so simple that I wonder if I am missing the point. Given a previous OrderSelect(), you can look up the bar-shift associated with its OrderOpenTime() simply by doing the following:

For example:

  • H1 chart
  • Current time is 16:32
  • Order was opened at 14:23
  • The above code should return BarShiftOfOrderOpen = 2, i.e. the H1 bar at 14:00, and Time[BarShiftOfOrderOpen] will be 14:00

Yes, that is exactly what OP is saying, but what he is questioning, is not how to do it, but how to prevent it becoming invalid due to a bar shift caused by a new incoming tick forming a new bar, while he is processing the data.

However, that should NOT happen, except when you use RefreshRates(). For that possibility (which is seldom), I have suggested the alternative methods in the same way that MQL5 does it.

 
FMIC:

but how to prevent it becoming invalid due to a bar shift caused by a new incoming tick forming a new bar

Where does the OP say that? That might be the question, but I think that you are hearing hoof-beats and thinking of zebras rather than horses. The mention of persisting the EA state to a file across restarts of MT4 suggests to me that the question is in fact much, much simpler.
 

Hello guys,

 thank you very much for the answers,

 @FMIC, interestingly I never thought using indicators and as you say this could be much more easier if I create and use one but of course this will bring new questions, like how will find the indicator value that pairs with my order etc. Also I will not do that process every time, just OnInit() once. so maybe using an EA is ok for this.

@jjc, thank you for your solution too. I was actually a little bit worry about the "new bar" but as you all say, while EA running there won't be shift changing (and I am not using RefreshRates() function) so I can use your code too. I just curious now after what @FMIC said, would it be nicer if I create this as an indicator?

 
jjc:
Where does the OP say that? That might be the question, but I think that you are hearing hoof-beats and thinking of zebras rather than horses. The mention of persisting the EA state to a file across restarts of MT4 suggests to me that the question is in fact much, much simpler.

Even GumRai picked up on that and quoted him, which I will do again here:

paranoyakX:

I thought using Time array etc but, while I was trying to find the bars, what would happen if a new bar comes up? shifts will be changed and my search will be illogical.

 
paranoyakX:

 @FMIC, interestingly I never thought using indicators and as you say this could be much more easier if I create and use one but of course this will bring new questions, like how will find the indicator value that pairs with my order etc. Also I will not do that process every time, just OnInit() once. so maybe using an EA is ok for this.

@jjc, thank you for your solution too. I was actually a little bit worry about the "new bar" but as you all say, while EA running there won't be shift changing (and I am not using RefreshRates() function) so I can use your code too. I just curious now after what @FMIC said, would it be nicer if I create this as an indicator?

Don't mind my suggestion if it is too complicated for you! Do it in which ever way is easiest for you now and then when you are more comfortable with it, try the other way too

PS! You should not be putting any logic code in your OnInit(), that is only where you initialise things and never anything to do with data from a history data series arrays. All your logic and main code should be in the OnTick() or in the OnCalculate().

 
FMIC:

Even GumRai picked up on that and quoted him, which I will do again here:

You and Gumrai are making a massive assumption about the meaning of "if a new bar comes up" which the evidence does not yet warrant. The OP has not yet said "if a new bar comes up during OnStart".

I suspect that paranoyakX is storing trade/signal details in global variables in the EA. The issue, I suspect, is not new bars forming during a call to OnStart(), but instead new bars forming during the EA's lifetime

 
jjc:

You and Gumrai are making a massive assumption about the meaning of "if a new bar comes up" which the evidence does not yet warrant. The OP has not yet said "if a new bar comes up during OnStart".

I suspect that paranoyakX is storing trade/signal details in global variables in the EA. The issue, I suspect, is not new bars forming during a call to OnStart(), but instead new bars forming during the EA's lifetime

There is no OnStart() in EA's but I get what you meant. However, the OP even in response to your comments said and I quote:

I was actually a little bit worry about the "new bar" but as you all say, while EA running there won't be shift changing (and I am not using RefreshRates() function)

Reason: