BuyStopLimit and SellStopLimit orders
With the release of the first builds of the Meta Trader 5 terminal, two more types of orders - BuyStopLimit and SellStopLimit - were added to the standard list of trade orders. BuyStopLimit - is an order to set a Buy Limit order when the price reaches a value that is above the current Ask price. SellStopLimit - is an order to set a Sell Limit order when the price reaches a value that is lower than the current Bid price.
All this will work in MT5, but at the moment none broker or the dealing center does not use MT5 for real trading, because the terminal is still being tested. But I really want to try the charms of such trading orders now, on MT4. Fortunately, all this can be done in MT4 as well. You just need a separate program (script) and not a built-in mechanism in the terminal.
Since we are implementing two types of orders, the script will logically be divided into two parts - processing BuyStopLimit and processing SellStopLimit. These parts will not intersect each other in any way. The type of operation will be specified by the user in the special external variable Type. So, to set Buy Stop Limit ordersAny other value will disable the script.
So, the block of work with BuyStopLimit:
if (Type == 0) // If BuyStopLimit is selected
{
// — 2 — ==== Проверка правильности входных параметров для BuyStopLimit =======
if (StopLimitPrice >= (Price-StopLevel)) // Check StopLimitPrice value
{
Alert(«При установке BuyStopLimit цена StopLimitPrice должна быть ниже Price»);
return(0);
}
if (Price <= Ask) // Check Price value
{
Alert(«При установке BuyStopLimit цена Price должна быть выше Ask»);
return(0);
}
if (TakeProfit — StopLimitPrice <= StopLevel) // Проверка значения TakeProfit
{
Alert(«Слишком близкий TakeProfit»);
return(0);
}
if (StopLimitPrice — StopLoss <= StopLevel) // Проверка значения StopLoss
{
Alert(«Слишком близкий StopLoss»);
return(0);
}
// — 2 — ================= Окончание блока =======================
// — 3 — ==== Ожидание достижения ценой значения StopLimitPrice =============
Comment(«Скрипт MT5_BuyStopLimit_SellStopLimit ожидает достижение ценой значения «,
Price);
ShowPrice(Price);
double NAsk = ND(MarketInfo(Symbol(), MODE_ASK));
while(!IsStopped() && NAsk < Price)
NAsk = ND(MarketInfo(Symbol(), MODE_ASK));
deinit();
if (IsStopped()) return(0);
// — 3 — ================= Окончание блока =======================
// — 4 — ==== Нужная цена достигнута — ставим ордер BuyLimit ================
while (!FatalError && !Res && !IsStopped())
{
Res = OpenOrder(OP_BUYLIMIT, StopLimitPrice, TakeProfit, StopLoss);
Sleep(1000);
}
return(0);
// — 4 — ================= Окончание блока =======================
}
Don't be embarrassed by the absence of the first block. It is not given in the article, because it contains the standard set of actions for collecting information on trading conditions. It is present in the script, of course.
In turn, the Price price should be higher than the current Ask price. TakeProfit and StopLoss parameters are also set by the user. Therefore they are also checked. Here everything is clearer - the profit should be higher than the opening price Buy Limit orders by StopLevel points, and the stop - below the opening price by the same StopLevel points.
If all the parameters are set correctly by the user, then the user passes to the third block, where the waiting for the desired price is performed. To make the waiting more informative, in the upper left corner of the screen the message about the waiting price is displayed, and a yellow horizontal line corresponding to the price Price is drawn. It is also worth noting that in the case of Buy Stop Limit all prices are specified by Ask, so it is the equality of Price and Ask prices that the script will wait for. The waiting occurs in a cycle, the exit from which is possible by two events: the user stops the script and the price reaches the desired value.
If the exit from the cycle took place because the necessary price was reached, the last, fourth block is executed, where also, in the cycle, attempts to set a Buy Limit order are made. Exit from this cycle occurs for three reasons: the occurrence of a fatal error, successful installation of the order and a stop of the script by the user.
Very similar to the first part of the script, the second part that handles SellStopLimit:
else
if (Type == 1) // If SellStopLimit is selected
{
// — 5 — ==== Проверка правильности входных параметров для SellStopLimit =======
if (StopLimitPrice <= (Price+StopLevel)) // Check StopLimitPrice value
{
Alert(«При установке SellStopLimit цена StopLimitPrice должна быть выше Price»);
return(0);
}
if (Price >= Bid) // Check Price value
{
Alert(«При установке SellStopLimit цена Price должна быть ниже Bid»);
return(0);
}
if (StopLimitPrice — TakeProfit <= StopLevel) // Проверка значения TakeProfit
{
Alert(«Слишком близкий TakeProfit»);
return(0);
}
if (StopLoss — StopLimitPrice <= StopLevel) // Проверка значения StopLoss
{
Alert(«Слишком близкий StopLoss»);
return(0);
}
// — 5 — ================= Окончание блока =======================
// — 6 — ==== Ожидание достижения ценой значения StopLimitPrice =============
Comment(«Скрипт MT5_BuyStopLimit_SellStopLimit ожидает достижение ценой значения»,
Price);
ShowPrice(Price);
double NBid = ND(MarketInfo(Symbol(), MODE_BID));
while(!IsStopped() && NBid > Price && !
NBid = ND(MarketInfo(Symbol(), MODE_BID));
deinit();
if (IsStopped()) return(0);
// — 6 — ================= Окончание блока =======================
// — 7 — ==== Нужная цена достигнута — ставим ордер SellLimit ================
while (!FatalError && !Res)
{
Res = OpenOrder(OP_SELLLIMIT, StopLimitPrice, TakeProfit, StopLoss);
Sleep(1000);
}
return(0);
}
// — 7 — ================= Окончание блока =======================
The fifth block is identical to the second with the changes inherent to the Sell Limit order. Let's remind, with this type of orders all prices are considered on Bid.
In the sixth block there is a waiting for reaching the price Price with the display of graphical and textual notification. When the Bid price reaches the Price price, the line and the message are removed (deinit function call). After that the transition to the seventh block is made.
The seventh unit, just like the fourth, tries to set an order, only it does not Buy Limitand already Sell Limit.
That's all the simple logic of the script.
When you run the script, you can set the values of the input parameters:
- Lots = 0.1 - volume of each order
• Type = 0 – Тип устанавливаемого ордера: 0 — BuyStopLimit, 1 – SellStopLimit
• StopLimitPrice = 1.46 — цена, по которой будет установлен ордер Limit
• Price = 1.47 — цена, по достижению которой будет установлен ордер Limit
- TakeProfit = 1.47 - profit level for the order
- StopLoss = 1.45 - stop order level for the order
The full source code of the script can be found here.