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!

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: