Hi Gary,
I finally found time to get back here...
first thanks for your source and your comments...gave me lots of thoughts...and brainwrenching..
Now I will give it back to you...
garyfritz wrote:I guess I never posted it, did I? Probably just as well, because it was an ad-hoc hack (an ad-hack?) as I figured out what I was doing. I'm not sure it's going to help you any, in fact I suspect it might confuse the issue more than it helps, but here 'tis.
No confusing at all..

at least you documented it a little...
garyfritz wrote:I'm not sure how applicable that ZigZag example is. It's a specialized tester written ground-up for ZZ. Some of the concepts will be similar but the general solution I have in mind (assuming we can actually make it work!!!) will be much more difficult.
The ZigZag is only a prove of concept...in the source there is also a MACD example and a Fantasies example commented out. I also like the visualization part of this concept.
But it is only a really simple version of that what we want to accomplish here.
garyfritz wrote:Here's my idea: we want this to work for any EA. The first thing I would do is to write a "wrapper" library that people would call instead of calling the MQL code directly. So instead of calling OrderSend() they would call BTOrderSend() or SHFOrderSend() or whatever. (Unfortunately I don't think MQL supports replacing the original OrderSend() with our own OrderSend() and still allowing us to access the original OrderSend().) So e.g. Steve would write his EAs as before, but calling functions with the BT (backtester) prefix.
I also think that this would be the way to go (Wrapper Library for Functions with equal Naming + prefix).
I did some little tests with creating some Functions with the same Naming as OrderSend, OrdersTotal... this seems not to work as the compiler and our beloved Empty4 Platform does not support any kind of overriding/overloading MQL functions.
garyfritz wrote:In the "wrapper" library, each BTxxx() function would just call the xxx() function. You would load in the "wrapper" library when you just wanted to run the EA normally.
Then the BackTester library would provide a different set of BTxxx() functions, but instead of calling the xxx() functions directly, it would perform the backtesting function.
Yep this kind of logic could work...
another approach/idea could be that we only have one library and we could use a external Parameter "backtesting=true/false" to switch between the modes backtesting/real trading.
garyfritz wrote:But actually....... now that I think through this a bit.....
The "wrapper" library and BTxxx() functions may not be necessary! Let's say you write your EA using the normal OrderSend() function. Everything works normally, and for the most part you don't have to do anything differently.
But the Backtester will run as an **indicator***, NOT an EA! You will copy your EA code into an indicator to test it. So it doesn't NEED to call EA functions like OrderSend(), and in fact it would probably be an error if it did. So the Backtester library will provide functions that replace the order functions used in an EA. Instead of re-calling the "real" OrderSend(), the backtester OrderSend() will record the details of the order. Then as the EA-turned-indicator executes, the Backtester will keep a list of all open orders and open positions, track any positions that open and close, and "do the right thing" as order prices are hit, etc. It will track the open and closed equity and probably chart it and/or dump it to a file for analysis.
Make sense so far? This approach has the huge advantage that it can be applied to ANY existing EA, with very little change to the EA source code. You'll probably have to make a few changes as you convert your EA into a backtester-indicator -- copy the EA source into an indicator, add some #include lines, add a BT_init() call in init(), add a BT_start() call in start(), things like that -- but that's a lot more straightforward than the "replace all xxx() calls by BTxxx()" approach that I envisioned originally.
It's a nice Idea, but as I said above no function overloading of any kind in Empty4. This is also not possible in Indicators and in scripts...it just won't let you compile...
To run the EA as Indicator could still be possible but we would still need the library.
garyfritz wrote:So ... if you want to start making progress on this, the first thing we need to do is to draw up a list of all the EA functions (and anything else) we'll need to replace. Anything that is EA-specific, anything that we'll need to re-implement to track orders & positions, etc. That will include all OrderXXX() and AccountXXX() functions... anything else?
I think that the OrderXXX() and AccountXXX() functions should be enough...no other Function as of now comes to my mind.
garyfritz wrote:Then we'll need to start designing the logic behind them. That will be the tricky bit.
In the example of the article I posted he used an array just to get hold of the signals to buy, sell and close of the orders.
I also think that we should have a table (array) where all open/pending orders are stored that we can access through the library.
The Problem I see here is that within an indicator...also with a library that we call inside an indicator we have no access at all to the normal AccountXXX() functions and therefore need also to code the logic of this. This also means that we need Parameters to collect (accountsize, leverage,...)
garyfritz wrote:Indicators execute very differently to EAs. EA start() gets called on every tick in realtime, and that's what the user's EA start() function will assume.
Indicator start() functions get called once per bar for history, and then once per tick in realtime, and also several recent bars may get re-called whenever it feels like it. That's messy and we don't want the backtester to work that way. We don't care about realtime updates of the chart, we're just interested in history.
I think the way to work this is that the backtester provides its own start() function, and then the user (when he converts his EA to a backtester-indicator) renames his start() to EA_start(). Then the backtester start() "simulates" the once-per-tick start() call of an EA by calling EA_start() for each bar.
This could be a reasonable solution...just need to check it though.
garyfritz wrote:I don't know if there is any way to step through tick-by-tick history on, say, an H4 chart. Maybe we could do M1-level backtesting -- step through the M1 data in the chart and call EA_start() once for each minute, instead of once for each tick the way an actual realtime EA would execute.
I don't think tick-by-tick is available in indicators...rather bar-by-bar, but I am not sure myself. If we have the logic in the indicator this should not be a great problem since the indicator also inits with each change of the period...so we can see what timeframe would be appropriate for our testing purposes.
garyfritz wrote:Hmm, problem. The EA is likely to refer to High, Low, Close, etc. In realtime EA execution, those refer to the current tick. How do we get them to refer to the H/L/C of the backtested M1 bar that we're calling EA_start() on?
Bigger problem: the EA will probably use iClose(), iHigh(), etc. The EA will expect those to be relative to realtime. I.e. shift=0 will refer to the currently-building bar. But when we're calling EA_start() on a historic M1 bar, shift=0 needs to refer to the current M1 bar. So I suspect the backtester will have to re-implement iClose() &etc. That's a problem because, unlike OrderSend() &etc, we still want to call the original iClose(). So we might have to implement wrapper functions for iClose() & similar, and EA writers will have to use (or convert when they use the backtester) BT_iClose(), BT_iOpen(), etc.
Ewww. And we'll have to do similar things for Time[], TimeCurrent(), and probably others. Ick. This is going to be messier than I hoped.

That is really a Problem and would complicate things a lot.
The easy way of copying and pasting an EA into an Indicator and editing a few lines is not going to happen as I see now. The limitation and problems we face are not easy to overcome and would take a lot of work.
I think we should reassess what we want to accomplish.
Our goal as I understand it is to be able to test multi-currency and multi-timeframe strategies and to have a result if this strategy could/would be successful or not.
What about another way of doing this...
The rule sets of a strategy are most times not that complex...and could be transfered to an indicator with much less effort. Why not split the strategy in two pieces...
- 1st part is a Custom Indicator with the rule sets that rely on indicator- or chart-action (like in the article I posted before)... this could work like a signaling service (buy/sell/closeBuy/closeSell).
- 2nd Part would be an EA that has the rule sets for Order/Trade/Money Management .
Both could communicate with each other via Global Variables or via File read/write and can also log their results in one log/csv that can later be analyzed with Excel.
We still could have a library for the EA Part where we can switch the Order Part from backtesting to real trading.
The Rule set and indicator calls from the Indicator should be easy to implement into the final EA if needed...or you just release both Files.
I know that we maybe loose Steve with this concept. But eventually we could find a solution that his EAShell can still be used or we setup a new Base for the EA Shell. This Part depends upon Steve if he wants to jump on the wagon...
What do you think about this? Or is this too far away from what you want to achieve?
happy trading,
NeoTrader
-