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:
Muhammad Awais

3 Responses to “Using the MQL4 OrderModify function – MQL4 for Complete Beginners Tutorial Part 19”

September 15, 2014 at 9:25 am, tablet8 said:

“”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.””

I exactly nees this part, how can I buy this premium course?

Reply

September 15, 2014 at 10:23 am, Kirill said:

Hi tablet8,

I am currently working on the Algorithmic Trading For Success course. Are there any specific topics you would like me to cover in relation to Buy/Sell-Limit and Buy/Sell-Stop Orders?

Kirill

Reply

September 15, 2014 at 3:13 pm, tablet8 said:

Hi Kirill
I need such thing:
first I will open a sell or buy order(market execution) lets say with tp=10 pips and sl=20 pips,
then I need an expert to make an algorithm like this:
just as I opened my first order manually the expert should open an ??? stop order just in the middle of first order and its SL,
for example if my first order was buy ,tp=10,sl=20,
the expert should open a pending order 10 pips below the first position in form of sell stop with TP= equal to the first order SL, Sl=tp of the first order.

when the second order was executed the expert should repeat the first order with the same tp and sl but in form of by stop. and this process will continue untill all orders are hit by market

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: