Understanding how to manage open orders is essential in automated trading. In MQL4, you can modify and close orders using specific functions. This guide will demonstrate how to identify, modify, and close orders using a robust method.
To modify an existing order, you must first find it. The following example shows how to iterate through all orders and modify them based on certain conditions:
// Check if there are open orders
if(OrdersTotal() > 0) {
// Iterate through all orders in reverse order
for(int i = OrdersTotal() - 1; i >= 0; i--) {
// Select each order by position
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
// Check if the selected order is for the current symbol
if(OrderSymbol() == Symbol()) {
// Insert your order modification logic here
// For example, modifying stop loss and take profit
// OrderModify(OrderTicket(), newPrice, newStopLoss, newTakeProfit, 0);
}
}
}
}
This code snippet checks all open orders and selects each one to see if it matches the current symbol (currency pair). You can then insert your logic to modify these orders as needed.
Similarly, to close an order, you need to find it first. The following example demonstrates how to iterate through orders and close them:
// Check if there are open orders
if(OrdersTotal() > 0) {
// Iterate through all orders in reverse order
for(int i = OrdersTotal() - 1; i >= 0; i--) {
// Select each order by position
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
// Check if the selected order is for the current symbol
if(OrderSymbol() == Symbol() && (OrderType() == OP_SELL)) {
// Insert your order closing logic here
// For example, closing the order completely
int closemyorder=OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
// Use Ask price to close the sell trade
}
}
}
}
This code iterates through all orders and selects those that match the current symbol, providing an opportunity to close them as required.
OrderSelect()
is crucial to manage a specific order.OrderSymbol()
against Symbol()
to ensure you're working with orders for the current chart's symbol.Last page we recommended visiting our page for avoiding the EA placing multiple orders, so we will incorporate that added bit of code into this bit as well:
#property copyright "Copyright 2023, www.mql4trader.com."
#property link "https://www.mql4trader.com"
#property version "1.00"
#property strict
#include
#include
///////////////////////////////////
// External items go below here... These are items that can be altered by the user via expert properties.
///////////////////////////////////
input double LotSize = 0.1;
extern bool TradingEnabled = true;
/////////////////////////////////
/// Global variables go here ////
/////////////////////////////////
double CalculateSpread()
{ return Ask - Bid; }
bool MyFirstCondition=false;
////////////////////////////////////
// Expert initialization function //
// Initialization code goes here //
///////////////////////////////////
int OnInit()
{ return(INIT_SUCCEEDED); }
/////////////////////////////////
// Deinitialization code here //
////////////////////////////////
void OnDeinit(const int reason)
{
}
// Here we are adding a check to see if any orders are open for the symbol our EA is working on... We call upon this later on in our code when we want it to run the check for us...
int OrdersForCurrentSymbolAndType(int type) {
int count = 0;
for (int i = 0; i < OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderType() == type) {
count++;
}
}
return count;
}
///////////////////////////////
// OnTick Function goes here //
//////////////////////////////
void OnTick()
{
// Trading logic here///
double testMovingAverage50=iMA(NULL,0,50,0,MODE_EMA,0,1);
double testMovingAverage200=iMA(NULL,0,200,0,MODE_EMA,0,1);
double testMovingAverage800=iMA(NULL,0,800,0,MODE_EMA,0,1);
double testRSILASTBAR=iRSI(NULL,0,14,0,1);
double testRSI5BARSBACK=iRSI(NULL,0,14,0,5);
if(testMovingAverage200 < testMovingAverage800)
{
MyFirstCondition=true;
}
else
{
MyFirstCondition=false;
}
if(TradingEnabled && MyFirstCondition && testMovingAverage50 < testMovingAverage200 && testRSI5BARSBACK > 70 && testRSILASTBAR < 70 && OrdersForCurrentSymbolAndType(OP_SELL) == 0)
//We have added a check if any sell orders are already open. You would do the opposite check for a buy trade: OrdersForCurrentSymbolAndType(OP_BUY) == 0...
{
int SELLTRADE=OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0,0, "MyFirstSellTrade", 0, 0, Red);
}
else
{
Print("Trade not executed...");
}
if (!MyFirstCondition)
{ Print("The first condition is not true"); }
////////////////////////////////////
// Lets add in a order closing here
////////////////////////////////////
// Check if there are open orders
if(OrdersTotal() > 0) {
// Iterate through all orders in reverse order
for(int i = OrdersTotal() - 1; i >= 0; i--) {
// Select each order by position
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
// Check if the selected order is for the current symbol and for the type of order we are looking to close...
if(OrderSymbol() == Symbol() && (OrderType() == OP_SELL)) {
if(testMovingAverage50 > testMovingAverage200) {
int closemyorder=OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
//We have now told the EA our final condition... that if the 50 period EMA goes above the 200 period EMA, that it must close any sell trade open for this symbol...
}
}
}
}
}
return;
}
Effectively managing orders is a vital skill in MQL4 programming. Using the methods shown, you can iterate through, identify, and manage orders for efficient trading automation.
NEXT UP: Using Built in Indicators in MQL4