Sunday, February 1, 2009

Education MQL II. Lesson 9

Hello dear readers. Today we will write an expert and a custom indicator. The expert will deal with the use of an indicator.



9.1. The algorithm and code indicator



The indicator uses two arrays of data, it consists of two lines. Both lines are moving averages. First we construct the average of the maximum price, the second from the floor. For this purpose we use a standard extended moving average indicator. It is this kind:

iMAEx (period, ma_method, ma_shift, applied_price, shift)



As an indicator, we have an external variable, it defines the period average.



/ * [[

Name: = urovni

Author: = forextimes

Link: = artur@fxtest.ru

Separate Window: = NoFirst

Color: = Blue

First Draw Type: = Line

First Symbol: = 217

Use Second Data: = Yes

Second Color: = Red

Second Draw Type: = Line

Second Symbol: = 218

]] * /

Inputs: per (21);



Variable: shift (0), HMa (0), lMa (0);



SetLoopCount (0);

/ / Loop from first bar to current bar (with shift = 0)

For shift = Bars-1 Downto 0 Begin



HMa = iMAEx (per, MODE_EMA, 0, PRICE_HIGH, shift);

LMa = iMAEx (per, MODE_EMA, 0, PRICE_LOW, shift);



SetIndexValue (shift, HMa);

SetIndexValue2 (shift, LMa);

End;



The algorithm is very simple, so I did not write an explanation.



9.2. The algorithm and code expert



The expert verifies the existence of trends and track the status of the price indicator on the line. The presence of a very primitive trend is verified by checking the last three value moving average, if each following the previous trend is more bullish, or bearish. If the bull trend price lower than the average user indicator, is buying, if the price trend bear more top line indicator sells.



Withdrawal shall be effected as to stop orders and under the condition, condition of entry is to the opposite line of custom indicator.



There are two external variables, the first -defines the period indicator, - defines the period of determining the average trend.



It is necessary to limit the time for the opening position. This is done in order not to open multiple positions in one bar. The condition has the form:



if curtime-LastTradeTime <4 * 3600 then exit;

figure before the <* 3600>, the value taymfrema Hours



/ * [[

Name: = expertUrovni

Author: = forextimes

Link: = fxtest.ru

Lots: = 1.00

Stop Loss: = 30

Take Profit: = 50

Trailing Stop: = 0

]] * /

defines: per (5), perMa (21);

var: HMa0 (0), LMa0 (0), Ma1 (0), Ma2 (0), Ma3 (0), cnt (0);



if curtime-LastTradeTime <4 * 3600 then exit;



/ *



HMa0 = iCustom ( "urovni", per, MODE_FIRST, 0);

LMa0 = iCustom ( "urovni", per, MODE_SECOND, 0);



* /



HMa0 = iMAEx (per, MODE_EMA, 0, PRICE_HIGH, 0);

LMa0 = iMAEx (per, MODE_EMA, 0, PRICE_LOW, 0);



Ma1 = iMAEx (perMa, MODE_EMA, 0, PRICE_close, 1);

Ma2 = iMAEx (perMa, MODE_EMA, 0, PRICE_close, 2);

Ma3 = iMAEx (perMa, MODE_EMA, 0, PRICE_close, 3);



if TotalTrades = 0 then (



if Ma1-Ma2> 0 and Ma2-Ma3> 0 and ask
then (SetOrder (op_buy, lots, ask, 3, bid-stoploss * point, bid + takeprofit * point, blue); exit;);



if Ma3-Ma2> 0 and Ma2-Ma1> 0 and bid> HMa0

then (SetOrder (op_sell, lots, bid, 3, ask + stoploss * point, ask-takeprofit * point, red); exit;);

);

if TotalTrades> 0 then

(

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 bid> HMa0 then

(

CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), Bid, 3, Violet);



Exit;

);



);



If OrderValue (cnt, VAL_TYPE) = OP_SELL then

(

if ask
(

CloseOrder (OrderValue (cnt, VAL_TICKET), OrderValue (cnt, VAL_LOTS), ask, 3, Violet);



Exit;

);



);



);

);)





As testing expert with the use of custom indicators is very slow, variable, using a custom indicator is commented out. Comments can be removed for inspection of the indicator.



The next lesson will also be practical if you have the desire to implement an interesting algorithm, be sure to write.





Halhalyan Arthur

Technical support for traders



No comments: