Getting information with OrderSelect() – MQL4 for Complete Beginners Tutorial Part 20

How do we track market orders? Manually it’s easy – you just look at the information provided at the bottom of the terminal. But what if we want to do it from inside a Forex-robot?

Well, it’s also pretty simple – we just need to use the OrderSelect() function to select the order and then we are able to use a range of functions to find out additional information about our order. Today I will show you how to use OrderSelect(), OrderStopLoss(), OrderTakeProfit(). 

Also, I will give you an extremely useful tip on how to check if an order has been closed or not using the OrderCloseTime() function. This part will be very important when we get to coding our Expert Advisor in Section 3 of the course.

Source code beneath the video

The video below is usually part of a paid course. Here you have the chance to see it for free. Simply enter your details and click “Play”.

 

This video is part of our Algorithmic Trading course. If you like it, you can check out by clicking the button below:

Algorithmic Trading Course

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial20.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 5 seconds

bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);

if(res == false)
{
Alert(“Error selecting order!”);
}
else
{
Alert(“Order selected successfully”);
//now we can work with the selected order
//the selection has been saved by the terminal

Alert(“Information about order #”, ticket, “:”);
Alert(“Instrument: “, OrderSymbol());
Alert(“Type: “, OrderType());
Alert(“Open Time: “, OrderOpenTime());
Alert(“Open Price: “, OrderOpenPrice());
Alert(“Volume: “, OrderLots());
Alert(“StopLoss: “, OrderStopLoss());
Alert(“TakeProfit: “, OrderTakeProfit());
Alert(“Comment: “, OrderComment());
Alert(“OrderCloseTime: “, OrderCloseTime()); //if closed then > 0, if not closed == 0
Alert(“OrderClosePrice: “, OrderClosePrice()); //if closed then price @ close,
//if not closed then possible price to close
Alert(“Profit: “, OrderProfit());
}

}

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

Code for 5-digit brokers

//+------------------------------------------------------------------+
//| Tutorial20.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 5 seconds

bool res;
res = OrderSelect(ticket, SELECT_BY_TICKET);

if(res == false)
{
Alert(“Error selecting order!”);
}
else
{
Alert(“Order selected successfully”);
//now we can work with the selected order
//the selection has been saved by the terminal

Alert(“Information about order #”, ticket, “:”);
Alert(“Instrument: “, OrderSymbol());
Alert(“Type: “, OrderType());
Alert(“Open Time: “, OrderOpenTime());
Alert(“Open Price: “, OrderOpenPrice());
Alert(“Volume: “, OrderLots());
Alert(“StopLoss: “, OrderStopLoss());
Alert(“TakeProfit: “, OrderTakeProfit());
Alert(“Comment: “, OrderComment());
Alert(“OrderCloseTime: “, OrderCloseTime()); //if closed then > 0, if not closed == 0
Alert(“OrderClosePrice: “, OrderClosePrice()); //if closed then price @ close,
//if not closed then possible price to close
Alert(“Profit: “, OrderProfit());
}

}

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

What are you waiting for?

START LEARNING FOREX TODAY!

Muhammad Awais

4 Responses to “Getting information with OrderSelect() – MQL4 for Complete Beginners Tutorial Part 20”

September 24, 2014 at 1:12 am, Ron said:

Hello Kirill, I saw on the video that OrderType() remain 0. shouldn’t we see OP_BUY ?
Thanks,Ron

Reply

October 17, 2014 at 6:27 pm, Kirill said:

Hi Ron,

That’s normal, because in MQL4 OP_BUY is represented by the number 0, OP_SELL is represented by the number 1.

Both OP_BUY and OP_SELL are merely convenient words that people understand. During compilation MQL4 converts them into 0 and 1 respectively. That’s why you see numbers in the output. More info: http://docs.mql4.com/constants/tradingconstants/orderproperties

Cheers,
Kirill

Reply

September 25, 2014 at 2:20 am, tablet8 said:

Hello
Please Help Me, I asked everyone but no answer
my question:
suppose we have put a buylimit order in our EA, now if my buylimit was activated by market ,I want the EA to open a sell stop order.
—————————————————————————————————————————————
how can I code this?
—————————————————————————————————————————————–
it’s almost 1 week I learning and looking for solution but no success

Reply

October 17, 2014 at 6:32 pm, Kirill said:

Hi tablet8,

Unfortunately, there is no simple answer to this question. I am assuming that this is required for a martingale strategy. You would have to create a module that monitors previous orders and based on their performance creates new ones. Trust me, when written properly, the code for this module will take a couple hundred lines of code.

Possibly, we will look at something similar in the Premium MQL4 course if I find a way to explain the algorithm simply.

Kind regards,

Kirill

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: