 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Now we know almost everything needed for creating an Expert Advisor. From trading functions we haven’t considered only OrderModify() and OrderCloseBy() functions. But before we start writing complicated Expert Advisors it is necessary to understand clearly how an Expert Advisor works on-line, being applied to an instrument’s chart, and how its work is simulated in the tester when it is backtested on the historical data.
Many traders write indicators and Expert Advisors, not realizing the difference between theory and practice. As after backtesting Expert Advisors in the tester they achieved attractive results they apply these Expert Advisors to online charts and later on they are often surprised at the difference (sometimes striking) between the results achieved online and from backtesting. That’s why first of all we will create an Expert Advisor for which we won’t blush, and which works equally well on any instrument and on any time-frame. This Expert Advisor won’t trade. Its own purpose is to keep a record of the state of a trading account, that’s why I called it BlackBox, by analogy with the equipment for recording flights of airliners.
Traders think out complicated algorithm for opening positions, even more complicated for trailing stop loss orders, check the indicators’ values on different time-frames, count the number of profitable and loss-making deals in the latest series, track a wide variety of events, that may happen to the price and trading account. But they sometimes forget about two most important events.
The first event is the coming of a new tick as the start() function of an Expert Advisor and indicator is executed precisely each time when a new tick comes. This event is evident. The second event is the closing of an old bar and formation of a new one. These two events are not equivalent with relation to writing an Expert Advisor. It is evident that the value of an indicator changes in an incomplete bar with each change of the price, that’s why most strategies use values from the latest completed bar, i.e. from the first bar.
It is necessary to define the moment when the old bar closed and the first tick of the new (zero) bar came. At this moment we estimate the current situation according to the algorithm of the Expert Advisor and take decision whether to execute or not trading operations. This event (formation of a new bar) we will check by a user-defined function isNewBar() of Boolean type, it will return true, if a new bar has formed, and false if this were not the case. If a new bar hasn’t appeared common operations are performed, that can be done on each new tick.
We create a new Expert Advisor with the help of The Initializing Wisard of the Expert Advisor. It requires no parameters for its work, that’s why we write only the name of the Expert Advisor.
|
|
|
|
In the block start() we write several lines of a simple algorithm:
|
|
|
|
We can see that on each bar the fact of appearing of a new bar is checked and in case of positive value the EveryBar() function is called. This function is called only once at the beginning of each bar (if isNewBar() is written properly) and during the time of developing of the bar isn’t caleed any more. Besides on each tick the EveryTick() function is called —any actions can be executed in it.
How can we define the appearing of a new bar? There are two standard ways.
- Each bar has such property as Time[] — the time of opening on a new bar, and if each time we ask the value Time[0] and compare it with the value that was on the previous tick, then at the moment of the new bar appearing these values won’t coincide.
- The second variant is to remember each time the value of the predefined variable Bars. When a new bar appears this value will also change.
Both variants are equal, but I will consider the second one. We write the function:
|
|
|
|
Pay attention that the variable expertBars (in which we remember the value Bars) is global. Only variables on the global level remember their values between arrivals of each tick and launch of the block start() (from which the isNewBar() function is called). If the expression expertBars!=Bars is true, it means that a new bar appeared, we will set the value res equal to true and we won’t forget to remember the value Bars at this stage — next time in the future we will compare Bars with the current value.
Thus the value res returned by the function wil be either true or false.
Next we think out the EveryBar() function, in order not to think for a long time I decided to print out each new bar in the log order list, as in the script OrderList. mq4 (the article «Orders in MetaTrader4» . ).
|
|
|
|
For this from the EveryBar()function a new function — PrintAllOrders() is called.
|
|
|
|
We only have to write the EveryTick() function. I decided to count in this function the average profit for all the ticks arrived during the bar. Fro this on each tick the current profit on the account is summarized (AccountProfit()), the number of bars is counts, and when a new bar appears the sum of profits is divided by the number of the ticks arrived, as a result we get the average profit for the bar.
|
|
|
|
We will modify slightly the EveryBar() function for calculating the average profit.
|
|
|
|
Now when a new bar appears we count the average profit and zeroise the variables SumProfit and ticksBars for further usage from a new leaf on the new bar. We launch it on the chart EURUSD M15 and see the output in the log:
|
|
|
|
Now our Expert Advisor each 15 minutes will output the data of orders and average profit for this period.
|
|
|
|
I think the logic of this Expert Advisor is clear. You can download the code of the Expert Advisor here .
Go to article «The tick indicator in a separate window».
|
|
|
|
|
|
 |
|
|
|
|
|
|
|
|
|
|
|
|
|
+7 (495) 710-76-76
|
© 1998—2008 «Alpari» |
|