OrderClose – MQL4 for Complete Beginners Tutorial Part 18

OrderCloseWelcome back!

In Tutorial 15 we learned how to open market orders from Scripts. For an Algorithmic Trading System to make profits it also has to close orders. Therefore, closing orders is equally important and today I will show you how it’s done.

The OrderClose Function

When executing new orders we used the OrderSend function. Similarly, to close an existing order we need to use the OrderClose function. OrderClose is an MQL4 trade function which takes the following parameters:
bool OrderClose(
int ticket, // ticket
double lots, // volume
double price, // close price
int slippage, // slippage
color arrow_color // color
);

In today’s tutorial we will discuss each of these parameters individually in detail. Nevertheless, you can always look for additional information on the MetaQuotes’ official website: OrderClose Reference

Video Tutorial

Additionally, in this tutorial we will further continue working with order ticket numbers. Knowing how to handle tickets is a useful skill. It will come in handy when we start programming expert advisors in section 3 of this course.

The source code is located below the video. Feel free to let me know if you have any questions by using the comments section at the end of this post.

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

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial18.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));

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

//+------------------------------------------------------------------+
//| Tutorial18.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));

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: