Using the MQL4 OrderModify function – MQL4 for Complete Beginners Tutorial Part 19

We have already discussed how to open and close market orders through a FOREX robot. Today we will learn how you can get your Algorithmic Trading system to adjust market orders. In order to do this you will need to use the OrderModify() function.

Modifying market orders is used to adjust the StopLoss (e.g. trailing stoploss), TakeProfit, and other parameters of the orders. In many trading systems it is imperative to modify existing market orders and that is why I have included this topic in this basic course.

Note that in this beginners course we will not be working with pending orders. If you want to learn about pending orders (such as Buy-Limit, Sell-Limit, Buy-Stop, and Sell-Stop) then look at my premium course Algorithmic Trading For Success.

Source code after video

Enrol in the full course here: https://www.forexboat.com/learn-mql4

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial19.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

TakeProfitLevel = Bid + TakeProfit*Point; //0.0001
StopLossLevel = Bid – StopLoss*Point;

/*
OrderSend can return:
ticket #; OR
-1 (if OrderSend failed)
*/

int ticket;
ticket = OrderSend(“EURUSD”, OP_BUY, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, “My 1st Order!”);

if(ticket < 0)
{
Alert(“Error!”);
}
else
{
Alert(“Your ticket # is: ” + string(ticket));

Sleep(5000); //delay of 2 seconds

//Modifying the order
Alert(“Modifying order…”);

bool res;
res = OrderModify(ticket, 0, Bid – 2*StopLoss*Point, Bid + 2*TakeProfit*Point, 0);

if(res == false)
{
Alert(“Error modifying order!”);
}
else
{
Alert(“Order modified successfully”);
}

/*Closing the order

Alert(“Closing order…”);

bool res;
res = OrderClose(ticket, 1.0, Bid, 10);

if(res == false)
{
Alert(“Error closing order!”);
}
else
{
Alert(“Order closed successfully”);
}
*/

}

}
//+——————————————————————+

Code for 5-digit brokers

//+------------------------------------------------------------------+
//| Tutorial19.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

//here we are assuming that the TakeProfit and StopLoss are entered in Pips
TakeProfitLevel = Bid + TakeProfit*Point*10; //0.00001 * 10 = 0.0001
StopLossLevel = Bid – StopLoss*Point*10;

/*
OrderSend can return:
ticket #; OR
-1 (if OrderSend failed)
*/

int ticket;
ticket = OrderSend(“EURUSD”, OP_BUY, 1.0, Ask, 10*10, StopLossLevel, TakeProfitLevel, “My 1st Order!”); //notice that slippage also has to be multiplied by 10

if(ticket < 0)
{
Alert(“Error!”);
}
else
{
Alert(“Your ticket # is: ” + string(ticket));

Sleep(5000); //delay of 2 seconds

//Modifying the order
Alert(“Modifying order…”);

//here we are assuming that the TakeProfit and StopLoss are entered in Pips
bool res;
res = OrderModify(ticket, 0, Bid – 2*StopLoss*Point*10, Bid + 2*TakeProfit*Point*10, 0);

if(res == false)
{
Alert(“Error modifying order!”);
}
else
{
Alert(“Order modified successfully”);
}

/*Closing the order

Alert(“Closing order…”);

bool res;
res = OrderClose(ticket, 1.0, Bid, 10*10); //notice that slippage also has to be multiplied by 10

if(res == false)
{
Alert(“Error closing order!”);
}
else
{
Alert(“Order closed successfully”);
}
*/

}

}
//+——————————————————————+

What are you waiting for?

START LEARNING FOREX TODAY!

share This:
Team ForexBoat
Team ForexBoat

Our goal is to share this passion with others and guide newbies to avoid costly mistakes. Today we want to share all our knowledge and insights, so you can take your trading skills to the next level.

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: