Wednesday, February 25, 2009

Writing Experts for MetaTrader. Lesson number 11

Today we write expert, trade will take place only in the direction of the trend, the signals would be reversible (ie the open positions will rotate continuously), the nominee will be used only one moving average.

11.1. Algorithm

Play in the direction of the rolling average can be in several ways. In our nominee, we will compare the past and the average value before and if there is a difference greater number of pledged items, we will close the old position and open a new one.

Because after the closing of one position, just the opposite is opening in the seasoned expert timeout DC.

As the expert, unlike the past can be connected to multiple instruments and trade on them will take place simultaneously.

As a nominee exhibited unattainable foot for that position would be closed only on the condition. Of course this is not working expert, but we've covered here, some new approaches, methodically, he will be very useful.

I have not yet decided what will be in the next issue, so please, be active and, possibly, your algorithm will be on the next issue.

/ * [[
Name: = MA
Author: = fxtest.ru
Link: = forextimes.ru
Lots: = 1.00
Stop Loss: = 2000
Take Profit: = 2000
Trailing Stop: = 0
]] * /
defines: Npips (2), Per (21) / / 2 external variables
Npips-difference between the values of AI, Per-period MA
var: cnt (0), s (0), fb (0), fs (0), MA1 (0), MA2 (0);
if CurTime - LastTradeTime <10 then exit; / / 10 seconds timeout
MA1 = iMA (per, MODE_EMA, 1); / / write the value of AI in some variables for convenience (AI - EMA, you can change the type)
MA2 = iMA (per, MODE_EMA, 2);
s = 0, / / counter of the open positions at the beginning of every time we reset
if fb = 1 then (fb = 0; SetOrder (op_buy, lots, ask, 3, bidstoploss * point, bid + takeprofit * point, green); exit;);
/ / Open the opposite position, subject to equality (fb = 1 and fs = 1)
if fs = 1 then (fs = 0; SetOrder (op_sell, lots, bid, 3, ask + stoploss * point, ask-takeprofit * point, red); exit;);
for cnt = 1 to TotalTrades (if OrderValue (cnt, VAL_SYMBOL) = Symbol then s = s +1;);
/ / Cycle for we believe the number of open positions is on the current instrument
if s <1 then (/ / if no open positions now ...
if MA1-MA2> Npips * point then
(SetOrder (op_buy, lots, ask, 3, bidstoploss * point, bid + takeprofit * point, green); exit;);
if MA2-MA1> Npips * point then
(
SetOrder (op_sell, lots, bid, 3, ask + stoploss * point, asktakeprofit * point, red); exit;);
/ / Define the possibility to open a position, note in this block expert comes only once 1
);
if s> 0 then / / if there are open positions, then inspected for possible closure
(
for cnt = 1 to TotalTrades
(
if OrderValue (cnt, VAL_TYPE) <= OP_SELL and / / buy
or sell
OrderValue (cnt, VAL_SYMBOL) = Symbol then / /
tool matches
(
If OrderValue (cnt, VAL_TYPE) = OP_BUY then / /
if you buy ...
(
if MA2-MA1> Npips * point then / / condition for closing
(
CloseOrder (OrderValue (cnt, VAL_TICKET),
OrderValue (cnt, VAL_LOTS), Bid, 3, Violet); / / close
fs = 1 / / flag at the opening of the opposite position
Exit;
);
);
If OrderValue (cnt, VAL_TYPE) = OP_SELL then / /
if you sell
(
if MA1-MA2> Npips * point then / / condition for closing
(
CloseOrder (OrderValue (cnt, VAL_TICKET),
OrderValue (cnt, VAL_LOTS), ask, 3, Violet); / / close
fb = 1; / / flag at the opening of the opposite position
Exit;
);
);
);
);)




Company «Fxtest»
to Forex Magazine
Halhalyan Arthur
artur@fxtest.ru

No comments: