Testing Visualization: Functionality Enhancement: 1. Motivation
Testing Visualization: Functionality Enhancement: 1. Motivation
Page 1 of 9
2. Technical Aspects
We will start coding with problem definition, as usual. What result do we want to obtain? - As said before, we need realization of something analoguous to "Trade" tab, i.e., to visualize information about all open positions and pending orders. - Information must always be updated and be well readable. - The program must be highly easy to use - its conection to the Expert Advisor may not need many efforts. Now we can discuss technical aspects. How will the program be formed: as an indicator or as an external function library? It is obvious that indicator in a separate window could be the best solution. It is always visible, does not hatch the price chart and is updated on every tick. But we then face a new problem when using the indicator - it takes all data about orders from the normal terminal, not from the 'virtual' (test) terminal, i.e., while testing, it will show information about the current state of the account without any reaction to the testing itself. So we will have to find an alternative, a kind of a back way, to solve our problem. We will still create an indicator, but it will only fulfil the function of a "chart subwindow creator". It will not show anything, i.e., we will always have a 'reserved' part of the chart. This is necessary so that the created subwindow could be saved under a unique name in template tester.tpl or in template <expert_name>.tpl. Showing and updating of information will be assigned to a function library formed an included file.
3. Putting in Use
First of all, a chart template must be created that will use the visualizer. Just adjust the chart (colors, grid, scale), attach vTerminal.mq4 to the chart and use the "Save Template" command:
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 2 of 9
Window "Save as Template" For visualization purposes, the template named tester.tpl or <expert_name>.tpl will be used. It is this name that must be used to save the template.
... double _Commission = 0.0; string _Comment = ""; datetime _Expiration = -1; #include <VisualTestingTools.mq4> int start() { if ( FastEMAPeriod >= SlowEMAPeriod ) { return(-1); }
Now we can use the functions in any part of the program. Call for one of them - vTerminalInit() - will be placed in the Expert Advisor's init() function since it must be called only once for its task is to create objects to show information. Another one vTerminalRefresh() - must be called at every tick. It will update information about open positions. Code of the EA that uses the functions will look like this:
... double _Commission = 0.0; string _Comment = ""; datetime _Expiration = -1; #include <VisualTestingTools.mq4> int init() { if ( FastEMAPeriod >= SlowEMAPeriod ) { return(-1); } vTerminalInit(); return(0); } int start() { if ( FastEMAPeriod >= SlowEMAPeriod ) { return(-1); }
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 3 of 9
You can download Expert Advisor CrossMACD_DeLuxe_Visual.mq4 where all necessary changes have already been made.
5. Testing
Now let us look what we have got. First, new external variables have been added in the Expert Advisor:
New external variables are available after the VisualTestingTools.mq4 file has been added to the Expert Advisor's code Their destination is intuitive, but I would like to give more details about them: - Rows - the maximum amount of lines shown in the display. For the most strategies, 10 lines will be enough; - BigText - set true if you want to enlarge the fonts; - SignalPoints - distance in points, starting from which fields Stop Loss, Take Profit and Open Price for pending orders will be highlighted. For example, if SignalPoints = 10, then the Stop Loss field will change its color when the price is 10 points from the Stop Loss of the position; - MainColor - color of the terminal heading and of the bar informing about the account (Balance, Equity, ...). - BuyColor - color of Buy-position information; - BuyOPColor - color of the Open Price field, which will replace the main color when the current price appcoraches to the Open level of Buy Stop and Buy Limit; - BuySLColor - color, which will replace the main color when the price approaches to the Stop Loss level of Buy positions; - BuyTPColor - color, which will replace the main color when the price approaches to the Take Profit level of Buy positions. - SellOPColor, SellSLColor and SellTPColor - the same for Sell positions. Let us set MainColor = Black and press OK. The Expert is now ready for testing. Before pressing the Start button, check "Visualization" and enjoy the results:
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 4 of 9
Until there are no open positions, our "terminal" shows only heading and the account status bar. Let us wait until a signal appears and the Expert opens a position.
A Sell position is opened as the first one. As you can see, the line is red. Please direct your attention to the account status bar. It contains everything we get used to see in the "Trade" tab of the terminal. The entire information is updated every tick, so you can see how the position's profit changes:
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 5 of 9
When price approaches to the Stop Loss level of the position, the StopLoss field changes its color to that preset by user:
6. Possible Problems
One more feature of using the tester.tpl with indicator vTerminal should be mentioned: During testing, the function that must find the indicator's window (WindowFind()) is unabled. This is why, the subwindow number is explicitly set in the code:
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 6 of 9
If the template contains other indicators to be drawn in a separate window, the entire information about them will be shown in the upper window. I.e., you have either to place vTerminal on the top or write win = subwindow number; instead of win = 1; . No other problems were found when working with the indicator.
7. Conclusion
To draw a beautiful conclusion, I would like just to show some screenshots.
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 7 of 9
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 8 of 9
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007
Page 9 of 9
PDF Creator: PDF4U Pro DEMO Version. If you want to remove this line, please purchase the full version http://articles.mql4.com/302 11/5/2007