Pending order in MetaTrader 4 (MT4) using MQL4

Main menu

Pages

Pending order in MetaTrader 4 (MT4) using MQL4


 To create a pending order in MetaTrader 4 (MT4) using MQL4, you need to write a script or an expert advisor (EA). Below is an example of an MQL4 code snippet to place a pending buy order (buy limit) in MT4.

//+------------------------------------------------------------------+ //| BuyLimitOrder.mq4 | //| Generated by https://forexoptionst.blogspot.com | //+------------------------------------------------------------------+ #property strict // Define input parameters for the pending order input double LotSize = 1.0; // Lot size input double Price = 1.2000; // Price for the pending order input double StopLoss = 1.1950; // Stop loss level input double TakeProfit = 1.2100; // Take profit level input int Slippage = 3; // Slippage in pips //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Check if there are enough funds to place the order if(AccountFreeMarginCheck(Symbol(), OP_BUYLIMIT, LotSize) < 0) { Print("Not enough margin to place the order"); return(INIT_FAILED); } // Place a Buy Limit order int ticket = OrderSend(Symbol(), OP_BUYLIMIT, LotSize, Price, Slippage, StopLoss, TakeProfit, "Buy Limit Order", 0, 0, clrGreen); if(ticket < 0) { Print("Error placing order: ", GetLastError()); return(INIT_FAILED); } else { Print("Buy Limit order placed successfully. Ticket number: ", ticket); return(INIT_SUCCEEDED); } } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { } //+------------------------------------------------------------------+

Explanation:

  1. Inputs:

    • LotSize: The size of the lot to trade.
    • Price: The price at which the pending buy limit order will be placed.
    • StopLoss: The price at which the stop loss will be set.
    • TakeProfit: The price at which the take profit will be set.
    • Slippage: The maximum slippage allowed in pips.
  2. OnInit() Function:

    • This function initializes the expert advisor. It checks if there are enough funds to place the order using AccountFreeMarginCheck.
    • If there are enough funds, it places a buy limit order using OrderSend.
    • If the order is placed successfully, it prints the ticket number; otherwise, it prints an error message.
  3. OnDeinit() Function:

    • This function is called when the expert advisor is removed. It is left empty in this example.
  4. OnTick() Function:

    • This function is called on every tick. It is also left empty in this example.

How to use:

  1. Open the MetaEditor from MetaTrader 4.
  2. Create a new Expert Advisor and paste the code above into the editor.
  3. Save and compile the code.
  4. Attach the compiled EA to a chart in MetaTrader 4.

This script will place a buy limit order with the specified parameters when the EA is initialized. Modify the input parameters as needed before attaching the EA to the chart.

Comments