Maintaining continuity across platform termination and EA recompilation.

 

I've got an EA which opens a trade for me and sets a target level which it monitors. It closes the trade when that level is approached and certain other criteria are reached - so it's not as simple as setting a take profit on the trade. I use the EA for a number of different currency pairs but there is never more than one trade on a given pair.

My concern is how I maintain this arrangement when I either close metatrader or recompile the EA. I'm guessing I should do the following:

1. On deinit() use FileWrite to store the ticket number and the target level. Give the file a name which is unique to the currency pair.

2. On init() use FileOpen to restore the data.

Is that the correct approach?

 

It's one option. You'll be better off in the long run if you design stand-alone functions which can search open or history trades in-order to continue where it left off. Currently I use GlobalVariableSet to save the state of my EA's. Globals would be enough to save your status if it's not an array. Save to file will work for arrays. But both would not recover in cases of hard-drive crash.

Lastly, there are some things which cannot be retrieved by using an OrderSelect loop. Like Last-Error or #-of-Requotes. Or extern Optionals which changes within the EA.

 

GlobalVariableSet is not reliable. If the OS crashes, terminal will NOT write them to disk.

"On deinit() use FileWrite" is not reliable for the same reason. You MUST write the file after any modifications to ram.

  1. You knew the TP level when you opened, so why can't you calculate it again? iBarShift tells you when it opened, calculated it from there.
  2. Set the TP 20 pips above the real TP. No permanent storage required. And if you loose connection the order will be closed.
 
WHRoeder:

GlobalVariableSet is not reliable. If the OS crashes, terminal will NOT write them to disk.

"On deinit() use FileWrite" is not reliable for the same reason. You MUST write the file after any modifications to ram.

Very much agree. I'll add that I don't like global variables for this purpose because gvariables.ini is undocumented. I much prefer something where I have the option of inspecting and modifying the file by hand before restarting MT4.

You knew the TP level when you opened, so why can't you calculate it again? iBarShift tells you when it opened, calculated it from there.

Well, yes, but there are plenty of scenarios where it may be difficult or impossible to re-calculate a historic s/l or t/p, or other characteristics of the order.

A simple example of something which is difficult in MT4 is anything involving indicator values from a higher timeframe. Let's say that your EA is running on an H1 chart, and you set the s/l or t/p of an order to the current moving average of the D1 data. When you call iMA() you will get a value based on the current incomplete D1 bar. If in the future you try to recalculate the s/l historically using iMA(), you will get a value based on the day-end D1 data, not the intra-bar value at the time when you placed the order. It's not impossible to recalculate the historic intra-bar moving average for the higher timeframe, but it's not easy. And this sort of thing becomes progressively harder as you use more complex indicators.

 
I could recalculate the target level as things stand currently, although it would require a little bit of reworking of my code, and as jjc says, it might get harder as I develop my EA in the future. Alternatively I might just set the take profit to a level representing a profit twice that of my target profit. If the tp gets hit there without my managed closure I don't mind. Then when I reactivate the EA it can redetermine the target profit from the tp. Anyway it's a lot clearer now, thanks.