Monday, March 2, 2009

Writing Experts for MetaTrader. Lesson number 21

Hello dear readers. This week I have a lot of questions from you, so this lesson will be entirely devoted to responses to your questions.

Question - Answer

Andrew Sirotkin sent 2 interesting question.

1. I want a trading expert in a particular pair, but I could play on the other hand. In the expert input is subject
if TotalTrades <1>
But because I have to manually open positions on the other pair, the entrance to the expert can not be realized (because if TotalTrades already = 1) for the pair to which is attached expert.
What can I do?
.. "

Answer:

/ * [[
Name: = test
Lots: = 1.00
Stop Loss: = 0
Take Profit: = 0
Trailing Stop: = 0
]] * /
var: cnt (0), tt (0);
for cnt = 1 to totaltrades (if OrderValue (cnt, VAL_SYMBOL) = Symbol then tt = tt +1;);
if tt = 0 then (...

Thus, we count only the number of positions open for the pair to which the expert. Login is subject tt = 0 and the number of positions for the remaining pairs can be anything.

2. Can I do that would be an expert to understand what the deal was opened it, and what I discovered? This is necessary for trade opportunities and the expert hands of one couple. ... "

Answer:

Special IDs for open positions in MT no. To identify the positions of convenient size of the lot (with the DC which supports fractional dimensions). For example, you play 1 hand lead, but an expert on the same pair, you can trade 1.1 lots, or 0.9. Mm from this greatly affected, although, of course, is an imaginary solution, but nothing remains.

var: cnt (0), tt (0);
for cnt = 1 to totaltrades (if OrderValue (cnt, VAL_SYMBOL) = Symbol and
OrderValue (cnt, VAL_LOTS) = lots then tt = tt +1;);

It looks at first, but weeding is still open positions and size of lot.

Alexander E. asked.

"Can I close some open positions, such as achieving certain profit ..."

Answer:

Yes this is possible, but not all, DC.

ID Expert is roughly this:

.......
lot = 0.5;
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-OrderValue (cnt, VAL_OPENPRICE))> 20 * point then (
CloseOrder (OrderValue (cnt, VAL_TICKET), lot, Bid, 3, Violet);
Exit;
);

);
If OrderValue (cnt, VAL_TYPE) = OP_SELL then
(
if (OrderValue (cnt, VAL_OPENPRICE)-ask)> 20 * point then (
CloseOrder (OrderValue (cnt, VAL_TICKET), lot, ask, 3, Violet);
Exit;
);
);
);
);)
........

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

In this feature, the fourth parameter is a variable lot, do not confuse it with the registered variable lots.
In the variable lot earlier in the code of the expert we determine what part of the position closing. We need to remember one time using this approach, it is necessary to limit the closing of the position one time.

Until now, come questions about the timeout. Dear readers, before you ask questions, I recommend strongly to read all issues of the journal.

To comply with the timeout (10 seconds time limitation of DC), after opening a position, you should use the following link at the beginning of an expert.

if curtime-LastTradeTime <11 then exit;





Halhalyan Arthur
artur@fxtest.ru

No comments: