Place a pending order (either Buy Limit or Sell Limit) in MetaTrader 4 with specified Stop Loss (SL) and Take Profit (TP) levels.

Main menu

Pages

Place a pending order (either Buy Limit or Sell Limit) in MetaTrader 4 with specified Stop Loss (SL) and Take Profit (TP) levels.

 


Below is a more detailed MQL4 script to place a pending order (either Buy Limit or Sell Limit) in MetaTrader 4 with specified Stop Loss (SL) and Take Profit (TP) levels.


//+------------------------------------------------------------------+ //| PendingOrder.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 PendingPrice = 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 input bool IsBuyLimit = true; // True for Buy Limit, False for Sell Limit //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Check if there are enough funds to place the order if(AccountFreeMarginCheck(Symbol(), IsBuyLimit ? OP_BUYLIMIT : OP_SELLLIMIT, LotSize) < 0) { Print("Not enough margin to place the order"); return(INIT_FAILED); } // Determine order type int orderType = IsBuyLimit ? OP_BUYLIMIT : OP_SELLLIMIT; // Place a pending order int ticket = OrderSend(Symbol(), orderType, LotSize, PendingPrice, Slippage, StopLoss, TakeProfit, "Pending Order", 0, 0, clrGreen); if(ticket < 0) { Print("Error placing order: ", GetLastError()); return(INIT_FAILED); } else { Print("Pending 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.
    • PendingPrice: The price at which the pending 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.
    • IsBuyLimit: A boolean flag to determine whether the order is a Buy Limit (true) or a Sell Limit (false).
  2. OnInit() Function:

    • This function initializes the expert advisor. It checks if there are enough funds to place the order using AccountFreeMarginCheck.
    • It determines the order type (OP_BUYLIMIT or OP_SELLLIMIT) based on the IsBuyLimit input.
    • If there are enough funds, it places a pending 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.

Customization:

  • Modify the input parameters (LotSize, PendingPrice, StopLoss, TakeProfit, Slippage, IsBuyLimit) as needed before attaching the EA to the chart.
  • This script will place a pending order (Buy Limit or Sell Limit) with the specified parameters when the EA is initialized.

Comments