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 - ==== Check if the input parameters for BuyStopLimit are correct =======
if (StopLimitPrice >= (Price-StopLevel)) // Check StopLimitPrice value
{
Alert("If BuyStopLimit is set, StopLimitPrice must be lower than Price");
return(0);
}
if (Price <= Ask) // Check Price value
{

Alert("When setting the BuyStopLimit the price of Price must be higher than Ask");
return(0);
}
if (TakeProfit - StopLimitPrice <= StopLevel) // Check TakeProfit value
{
Alert("TakeProfit too close");
return(0);
}
if (StopLimitPrice - StopLoss <= StopLevel) // Check StopLoss value
{
Alert("StopLoss too close");
return(0);
}
// - 2 - ================= End of Block =======================

// - 3 - ==== Waiting for the price to reach the StopLimitPrice value =============
Comment("The MT5_BuyStopLimit_SellStopLimit script expects the price to reach ",
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 - ================= End of Block =======================

// - 4 - ==== The desired price has been reached - place a BuyLimit order ================
while (!FatalError && !Res && !IsStopped())
{
Res = OpenOrder(OP_BUYLIMIT, StopLimitPrice, TakeProfit, StopLoss);
Sleep(1000);
}
return(0);
// - 4 - ================= End of Block =======================
}

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 - ==== Check correctness of input parameters for SellStopLimit =======
if (StopLimitPrice <= (Price+StopLevel)) // Check StopLimitPrice value
{
Alert("If SellStopLimit is set, StopLimitPrice must be higher than Price");
return(0);
}
if (Price >= Bid) // Check Price value
{
Alert("If SellStopLimit is set, the price of Price must be lower than Bid");
return(0);
}
if (StopLimitPrice - TakeProfit <= StopLevel) // Check TakeProfit value
{
Alert("TakeProfit too close");
return(0);
}
if (StopLoss - StopLimitPrice <= StopLevel) // Check StopLoss value
{
Alert("StopLoss too close");
return(0);
}
// - 5 - ================= End of Block =======================

// - 6 - ==== Waiting for the price to reach the StopLimitPrice value =============
Comment("The MT5_BuyStopLimit_SellStopLimit script expects the price to reach the value",
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 - ================= End of Block =======================

// - 7 - ==== The desired price has been reached - place a SellLimit order ================
while (!FatalError && !Res)
{
Res = OpenOrder(OP_SELLLIMIT, StopLimitPrice, TakeProfit, StopLoss);
Sleep(1000);
}
return(0);
}
// - 7 - ================= End of Block =======================

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 - Type of order being set: 0 - BuyStopLimit, 1 - SellStopLimit
- StopLimitPrice = 1.46 - the price at which the Limit order will be set
- Price = 1.47 - the price at which the Limit order will be set
- 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.

Leave a Reply

Back to top button