Education MQL II. Lesson number 14
Hello dear readers. I received several experts algorithms in future releases, I always realizing some of them. In the same issue and would like to talk about parts of the source code, which is the same in all the experts. Someone will say that each individual expert, someone that all is very much in common, but accurately, that some moments are absolutely identical. This is a test of free margins, limiting trade expert at the time (trading session, or restriction made on weekdays), treylingstop etc. These fragments of code experts, and will be discussed.
14.1 Limitation of DC
The work of experts at the DC limitations 2, more precisely, this limitation is not specific to the experts, but in general to work through the DTs.
1) restrictions on the free margins: If FreeMargin <1000 then Exit;
2) 10 second timeout, after the opening of the position: If ((CurTime-LastTradeTime) <10
14.2 The ability to verify
At the beginning of an expert it is possible to check on
1) Correct exposure of the foot If StopLoss <10> 100 * point then exit;
2) Check the number of bars stories (for the test of history) If Bars <200 then Exit;
3) To play a position you can use the structure kinds If TotalTrades <1 then (
or
If TotalTrades = 1 then exit;
4) To start trailing If TrailingStop> 0 then (
14.3 The inspections and restrictions on time
Sometimes there is a need to test an expert at some certain period of history, the conditions can be read as follows
If Year <> 2003 Then Exit; With this limitation, testing expert will take place only in 2003
If Year> = 2004 And Month> 2 Then Exit; testing expert by a certain date, in this case until the end of February 2004.
Conditions on the test of time within the day.
If Hour <10> 19 Then Exit; Trade will be in the range from 10 to 19 on-time MP.
If Minute = 10 Then (Action will take place at 10 minutes each hour for the time MT.
If Seconds = 10 Then (Same for seconds.
Also in MT can be done on-time at the time of the day, there are a function of Day of week. It can take values from 1 to 7, respectively, days of the week, but the numbering starts with the resurrection. Ie 1-Sunday 2 - Monday, etc.
If DayOfWeek = 3 Then Exit; expert will not work on Tuesdays.
14.4 Treylingstopy
Of course it makes no sense to write a new treylingstop for each expert, you can embed the code in an expert if you want.
If TrailingStop> 0 then (
for cnt = 1 to TotalTrades
begin
/ * If it is long position? * /
If Ord (cnt, VAL_TYPE) = OP_BUY then / * long position * /
(
/ * Is there any profit from this deal more than the trailing stop? * /
If (Bid-Ord (cnt, VAL_OPENPRICE))> (TrailingStop * Point) then
(
/ * Position is greater than the value of profit trailing stop then check whether one's feet? * /
If Ord (cnt, VAL_STOPLOSS) <(Bid-TrailingStop * Point) then
(
/ * Purse stoploss to Bid-Trailing Stop * /
ModifyOrder (Ord (cnt, VAL_TICKET), Ord (cnt, VAL_OPENPRICE), Bid-TrailingStop * Point, Ord (cnt, VAL_TAKEPROFIT), Red);
Exit;
);
);
);
/ * This is a short position? * /
If Ord (cnt, VAL_TYPE) = OP_SELL then
(
/ * Is there any profit from this deal more than the trailing stop? * /
If (Ord (cnt, VAL_OPENPRICE)-Ask)> (TrailingStop * Point) then
(
/ * Position is greater than the value of profit trailing stop then check whether one's feet * /
If Ord (cnt, VAL_STOPLOSS)> (Ask + TrailingStop * Point) or Ord (cnt, VAL_STOPLOSS) = 0 then
(
/ * Purse stoploss * /
ModifyOrder (Ord (cnt, VAL_TICKET), Ord (cnt, VAL_OPENPRICE), Ask + TrailingStop * Point, Ord (cnt, VAL_TAKEPROFIT), Red);
Exit;
);
);
);
end;
);
I am confident that this is not difficult to harvest, the process of writing experts only slightly improved and simplified.
In the next issue we will write an expert or indicator algorithm which seems to me more interesting.
Company
Halhalyan Arthur
Technical support for traders
artur@fxtest.ru
Halhalyan Arthur
Technical support for traders
artur@fxtest.ru
No comments:
Post a Comment