Thursday, February 26, 2009

Writing Experts for MetaTrader. Lesson number 18

Education MQL II. Lesson 18

Good day dear readers! Today we write a very simple expert. But with this idea appealed to me for several clients. This is a tilt in the moving average trend.

Algorithm
The general idea of this. Moving Average with a large period (> 50). Take the value at the last bar, it shall be deemed to be current. And the importance of a few bars earlier, for example 5. For example, we have upward trend, and my MA (70) tilted upwards. Then we have AI-hypotenuse, a few bars back (5)-horizontal cathetus, and the difference between the last value of AI and AI value of 5 bars back vertical cathetus formed a rectangular triangle. And the idea of slope can be described as the angle between the AI (hypotenuse) and the horizontal cathetus. But we already have all but a few.

First, the units of vertical cathetus - points, and horizontal bars.
Secondly, although we have AI and a great period, however, is not straight, so the angle between the curve and the straight (horizontal cathetus) have not entirely correct.
And, thirdly, it is enough to change the scale of the graphs as a visual change the angle.

For all the above, I decided to leave the angles in degrees, for determining the amount of tilt MA. The best and easiest way to determine the slope of AI, I decided to choose the definition of the value of vertical cathetus in points. Ie , for example, we always take the difference between the previous value of AI and the value of 4 bar back and get some value greater than this value, the greater the slope.
As an additional signal to the input will determine whether the current price is above Amnesty for Long and lower for shorts.

So the final algorithm.
Log in long exercised if there is a difference between the value of AI in the past, a bar and 4 bars back more than the indicated value and the closing price of the last bar more AI in the last bar at 10 points or more.
Log In short, vice versa, respectively.

Conditional access is carried out by reverse cross-AI price. Ie condition for the closure of Long is the excess value above the cost AI closure.

In the external set period and the average value of points defining the trend.

Conclusion
Although very drodaunom expert pointed to the history of (euro H4), a positive result. For H1 and below I have not selected settings, but I think the effectiveness of the expert will worsen as you move into smaller frames.

/ * [[
Name: = NaklonMa
Author: = forextimes
Link: = fxtest.ru
Lots: = 1.00
Stop Loss: = 50
Take Profit: = 1000
Trailing Stop: = 100
]] * /
defines: tr (10), per (70);
var: cnt (0), k (0);

k = 0; / / obnudenie flag that determines the trend

if iMA (per, MODE_SMA, 1)-iMA (per, MODE_SMA, 4)> tr * point then (k = 1;) / / condition for vozhodyaschy trend
if iMA (per, MODE_SMA, 1)-iMA (per, MODE_SMA, 4) <-tr * point then (k =- 1 ;};// condition on nizhodyaschy trend

if TotalTrades = 0 then (/ / if no open positions ...

if k = 1 and c [1]-iMA (per, MODE_SMA, 1)> 10 * point then
(Setorder (op_buy, lots, ask, 3, bid-StopLoss * point, bid + TakeProfit * point, blue);
exit;);
if k =- 1 and iMA (per, MODE_SMA, 1)-c [1]> 10 * point then
(Setorder (op_sell, lots, bid, 3, ask + StopLoss * point, ask-TakeProfit * point, red);
exit;);
/ / check for an additional condition and placement of orders
);

if TotalTrades> 0 then / / we are going to check on the condition of closure, if there are open positions
(
for cnt = 1 to TotalTrades
(
if OrderValue (cnt, VAL_TYPE) <= OP_SELL and
OrderValue (cnt, VAL_SYMBOL) = Symbol then
(
If OrderValue (cnt, VAL_TYPE) = OP_BUY then
(

if c [1]
(
CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), Bid, 3, Violet);
Exit;
);

);

If OrderValue (cnt, VAL_TYPE) = OP_SELL then
(

if c [1]> iMA (per, MODE_SMA, 1) then

(
CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), ask, 3, Violet); / / exit condition of the Short

Exit;
);
);
);
);)




Company «Fxtest»
Halhalyan Arthur
Technical support for traders
artur@fxtest.ru

No comments: