Thursday, February 19, 2009

Programming in MQL II: Upgrade your hands.

In the previous issue of the journal (ForexMagazine May 2004, Num 17), we talked about "unconventional" use of experts MetaTrader'a - about using them to collect market information.

But, as we have pointed out, the experts can find an array of applications: let's look at another one of the options for the use of experts. Sometimes, during discussions, trading terminal MetaTrader mention really helpful, but, unfortunately, missed opportunities that could make life easier for traders. Why not try to implement these ideas in MQL II?

One such "good stuff" to the demand by users, is a pending order of limited duration. It is possible to put a warrant, which must be present on the market only for a certain period of time. If he had not realized during this time, he was overruled.

Go to the realization of this idea in the form of an expert to MetaTrader'a. To get started, consider the option when we already have a pending order, and we want it to close at a certain time. Upon accession to the expert's schedule, we can pass it parameters. We need to pass pending order number, which must be lifted, and, indeed, the time of its repeal. Since the number of order problems should not arise, and the first option would be to specify the number pending order:

define: order_number (2000000);

But with the transfer to the program date and time of the closure order is not so simple. Since the parameters passed to the expert, can only be integer or fractional numbers, in this case we have to resort to some tricks. We will send the date and time as follows:
Individual number - year;
Then a number - a couple of "the month and day," where an entire portion of the number would mean a month, and fractional - day;

And the last number - a couple of "hours and minutes, the same way: the entire part - watch, fractional - minutes.

define: close_year (2004);
define: close_date (11.22);
define: close_time (10.01);

Then, in order to pass in some parameters of the three date and time "to turn" in the importance of an understandable language functions MQL II, pribegnem to another trick. Uniting parameters close_year, close_date and close_time one line in the format "yyyy.mm.dd hh: min". And after that, using the StrToTime () make string to the value that stores the number of seconds since 0 hours 0 minutes, 1 January 1970 year - this is the version of the storage date and time functions understandable MQL II.

Here's the text ekspertas comments that will help out in other parts of his work.

It should be noted that the code is not ideal in terms of sustainability of its work. For example, the situation is not checked when the user (input) in the setting, meaning the month and the number, the value of 99.99, the same is true of the time. For a truly commercial product should be added to the code described verification.

In addition, you can make an expert more convenient to use. Slightly changing program, we can force it to open pending order on the characterization of the price. In the case of intra day trading, closing time, you can not specify a specific date and specific time, but as the time interval of existence of the warrant - the number of minutes (or seconds) since its opening. Alternatively, you can specify the lifetime of a warrant, even in bars. Following these changes to open pending order with a limited lifetime will need to enter is not four, but only two parameters: price and term life.

There are, of course, and a small inconvenience when dealing with a pending order in this way: one window to the schedule may be assigned only to one expert. Therefore, if you want to open several such time-limited orders for the same traded instrument, you need to open multiple windows and attach them to an expert.

That's all that I would like to talk about "unconventional" use of experts to add new features to the functionality MetaTrader'a. As always, readers are invited to develop their own theme and consider ways to enhance the capacity of trading terminal MetaTrader.

/ * [[
Name: = Close Pending Orders
Author: = Copyright c 2004, Horn
Link: = alexander@indus.ru
Notes: = close the pending order with the number "order_number"
Notes: = at the time specified using the variable "close_year",
Notes: = "close_date" and "close_time".
Notes: = in "close_date" specify the month and date through the point
Notes: = in "close_time" specify the hours and minutes through the point
Lots: = 0.00
Stop Loss: = 0
Take Profit: = 0
Trailing Stop: = 0
]] * /

define: order_number (2000000);
define: close_year (2004);
define: close_date (11.22);
define: close_time (10.01);

var: bOrderPresented (false);
var: bOrderClosed (false);
var: bNoAlerts (false);
var: nTradeNumber (0);
var: tCloseTime (0);

/ / Floor function returns a portion of a fractional number
tCloseTime = StrToTime (close_year + "."
+ Floor (close_date) + "."
+ (Close_date - Floor (close_date)) * 100 + ""
+ Floor (close_time) + ":"
+ (Close_time - Floor (close_time)) * 100);

for nTradeNumber = TotalTrades downto 1 (

/ / If a warrant is pending orders to buy or sell and
/ / If the number coincides with the number allocated in the parameter expert
/ / Then we check the current time.

if ((OrderValue (nTradeNumber, VAL_TYPE) = OP_SELLLIMIT)
or (OrderValue (nTradeNumber, VAL_TYPE) = OP_SELLSTOP)
or (OrderValue (nTradeNumber, VAL_TYPE) = OP_BUYLIMIT)
or (OrderValue (nTradeNumber, VAL_TYPE) = OP_BUYSTOP))
and (OrderValue (nTradeNumber, VAL_TICKET) == order_number) then (

bOrderPresented = true;

/ / If the current time on the same or more
/ / The required closing time, then cancel the order!

if (CurTime> = tCloseTime) then (
DeleteOrder (OrderValue (nTradeNumber, VAL_TICKET), Brown);

/ / Inform the user about the successful closing of the warrant
Alert ( "Order ?" + order_number + "closed" + TimeToStr (CurTime));

bOrderClosed = true;
exit;
);
);
);

/ / Inform the user that the pending order with the number
/ / To the expert in the parameters, there is no
if (! bOrderClosed and! bOrderPresented) then (
Alert ( "the specified pending order does not exist!");
);




Alexander Ivanov
to Forex Magazine

No comments: