Trading Functions and Order Management in MQL4

In MQL4, trading operations are the core of algorithmic strategies. Understanding trading functions and order management is crucial for implementing and executing your trading ideas efficiently.

Key Trading Functions

MQL4 offers a set of built-in functions to perform trading operations:

Order Properties

Each order has properties that can be retrieved using built-in functions:

Example: Placing a Buy Order

double slippage = 3;
int lotSize = 1;

///For  Buy Orders...

int ticketbuy = OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, "Buy Order", 0, 0, Green);
if(ticket < 0) {
    Print("OrderSend failed with error(", GetLastError(), ")");
} else {
    Print("Buy order placed successfully with ticket number: ", ticketbuy);

///For  Sell Orders...

int ticketsell = OrderSend(Symbol(), OP_SELL, lotSize, Bid, slippage, 0, 0, "Sell Order", 0, 0, Red);
if(ticket < 0) {
    Print("OrderSend failed with error(", GetLastError(), ")");
} else {
    Print("Sell order placed successfully with ticket number: ", ticketsell);


In this example, a buy order is placed for the current symbol with a specified lot size. If the order fails, the error is printed to the terminal.

Efficient Order Management

Order management is vital for risk control and strategy optimization. Some best practices include:

Lets try this in our test EA:

Lets add in some new conditions with some indicators and order placements...

Please note that this EA is not complete and will open a new order every tick that the condition is true. If you want the EA to only open 1 order and then wait until the order has been closed before opening another, please check our page Preventing an EA from Opening Multiple Trades Simultaneously

#property copyright "Copyright 2023,"
#property link      ""
#property version   "1.00"
#property strict
#include <stderror.mqh>
#include <stdlib.mqh>

// 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)



// 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);

// We have added several indicators to be used for calculations

if(testMovingAverage200 < testMovingAverage800) 








// If moving average 200 is less than moving average 800, then MyFirstCondition will be flagged as true... If it does not prove to be true the flag will either remain false or be reset to false...

if(TradingEnabled && MyFirstCondition && testMovingAverage50 < testMovingAverage200 && testRSI5BARSBACK > 70 && testRSILASTBAR < 70)

     int SELLTRADE=OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0,0, "MyFirstSellTrade", 0, 0, Red);

Print("Trade not executed...");


// The above states that if TradingEnabled is set to true, MyFirstCondition is set to true, moving average 50 is less than moving average 200, RSI from the 5th candle back was greater than 70 and the RSI of the last bar is less than 70, the EA is to open a sell trade, using the LotSize pretermined at the very top of the EA...

if (!MyFirstCondition) 

{ Print("The first condition is not true"); }

// if MyFirstCondition is NOT true Print the message "The first condition is not true"




Trading functions are the heart of algorithmic trading in MQL4. By mastering these functions and understanding the nuances of order management, traders can ensure that their algorithmic strategies are executed smoothly and effectively, maximizing potential profits while minimizing risks.

NEXT UP: Modifying and Closing Orders