|
|
|
|
|
|
|
|
Limit losses and let your profit grow
Why haven’t we at once created the file of signals in our script (at its completion in deinit())? There are two reasons for this:
- our signals are detected only when the arrow SmartArrow moves on the chart, and as signals can be rather rare, not always we can “jump” from one signal to another while scrolling the chart. That’s why we have to apply the hop, step and jump technique – i.e. to make an intermediate stop (setting an extra signal). If such a function were built in the terminal MT4 we could put marks on the chart with one click.
- if it is required to plot too many signals for testing, apparently there will be pauses between working sessions with the script. We can create several *.csv files and join them with the help of Excel.
Let’s create a separate script for recording signals in a file. We adjust the location of arrows on the chart, delete unnecessary (induced extra signals) and launch the script ArrowsToFile.mq4. To write this script I copied ready parts of the code from WriteFile.mq4 , only having changed a little the init() function.
|
|
|
|
The code for recording the function in a file isn’t difficult; please note that before recording two barrage checks for the object type and name are performed.
|
|
|
|
To test the script we will add a trend line with the name SmartArrow555 on the chart.
|
|
|
|
We launch the script on the chart; my 12 arrows were processed.
|
|
|
|
We can see that there appeared to be three objects on the chart which were not arrows, also we see that our arrows were processed (sorted) by name, and not by time. In the future we may need the operation of sorting. We open the file in Excel:
|
|
|
|
It is so, signals are listed not quite in chronological order. Now we can send our file to a trader we know by email, or upload it onto a forum. But besides, there is another remarkable possibility in MT4 which other programs don’t have. You may explain arbitrarily long in written form or by phone what you want to show somebody, but seeing is believing. MT4 gives such a possibility with the help of templates. We call the menu on the chart with the help the right mouse button and save the chart under a wanted name.
|
|
|
|
The saved template we will find in one of the folders in MT4:
|
|
|
|
We send this file to another person who places it into the same folder on his computer, opens the template (signals.tpl) and sees in his terminal the full copy of your chart. At that we are over with recording signals in a file, and now we have to learn how to read these data from our file.
If there is an operation of recording data in a file, there must be the opposite operation – reading from the file. Everything is clear with arrays, we can always get the size of the array and arrange the for() loop to get values of the array elements by the element’s index. But when we open a file we usually don’t know in advance how many lines or values are recorded in it. That’s why some stop-signal is required to break the cycle of reading from the file operations. We refer to the help section and see:
|
|
|
|
The FileIsEnding() function is the most suitable for ending reading from the file. We write a simple script to acquire experience; I set rigidly the name of the file for reading signals.
|
|
|
|
If it were an indicator or expert advisor this code would cause a hang-up of the terminal! Note: an infinite loop is allowed only in scripts. We have got an infinite loop as we can’t reach the end. An operation of “scrolling” the file is required, the function of reading from a text file FileReadString() is just the one that must provide it. I didn’t know how it worked and decided to check it using the trial and error method.
|
|
|
|
The file of signals is small, that’s why we must see everything in the tab «Expert». In Excel I counted 13 lines and 52 values (13*4). I launch the script on any chart and I see the log:
|
|
|
|
We can see that the expected values differ from the printed values by one. It means that the script makes a blank cycle before it determines the end of the line and the end of the file. Knowing of this will help us to fill the array of signals in the expert advisor. We will save the script as ReadFile.mq4.
We won’t write an expert advisor for testing manual signals, we will take RTS+TS3.mg4, will name it CutTheLosses.mq4 and will redefine the init() function. We will make the SignalExist() function empty so far.
|
|
|
|
If we know the structure of a text file it is not difficult to read it. Now we have the array of signals and we can proceed to testing of the expert advisor. For this we have to teach the SignalExist() function to retrieve necessary signals from Signals[]. Let’s use a simple algorithm – at each moment of time we run through all the array and compare the current time Time[0] with the time in Signals[i][0], if the values coincide we check the type of the signal and give the answer according to it.
|
|
|
|
We will change a little the GetOrder() function: we will delete the random component, we will leave the entry in the order comment untouched – we may need it in the future.
|
|
|
|
Please note we insert an additional checking for the order type into the else construction. If sometime we transfer as a parameter OP_BUYLIMIT or OP_SELL_STOP in this function, it will prevent us in the future from a mistaken opening of a sell position. (OP_SELL).
The advisor is ready, but if we launch it in the tester on EURUSD H1 (from where we took signals), nothing will come of it. We look into the tab «Log» in the tester and we see the following:
|
|
|
|
The error code (4103) confirms that it is impossible to open the file. The point is that all file operations in the tester are performed in its own folder, that’s why we will copy our file in the folder /tester/files.
|
|
|
|
Now testing of the expert advisor by the signals from the file is performed without mistakes. Moreover testing shows profit! The exotic parabolic trailing stop proves to have right to life in case of certain signals.
|
|
|
|
Now even a person who has never written a line of a code (but has read this article), can create his own file of signals, take an advisor of such class, and having sorted different systems of exit (or trailing), check his new system of entries. And if it is difficult to formalize the system of entries, perhaps it will be the only way (and not the worst, in my opinion).
All the files that were used are available here
Go to article «Global Variables».
|
|