MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

How to send a Buy or Sell Order in MQL4

Introduction

If you're starting with automated trading on the MetaTrader 4 platform using MQL4, one of the first things you'll need to learn is how to send trading orders. In this guide, we'll break down the process step by step.

Understanding the OrderSend() Function

The heart of sending orders in MQL4 is the OrderSend() function. This function has several parameters, each conveying specific information about the order you wish to place.

int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE );

It might look overwhelming at first, but don't worry! We'll go over the most essential parameters and explain their purposes.

Placing a Buy Order

A Buy order means you believe the price will go up. To place a Buy order, you use the OP_BUY operation type. Here's a simple example:

double lotSize = 0.1;
int ticket = OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "Buy Order");

Notice that we omitted some parameters and kept only the essential ones. We'll explain why shortly.

Placing a Sell Order

A Sell order means you believe the price will go down. To place a Sell order, you use the OP_SELL operation type. Here's how you can do it:

double lotSize = 0.1;
int ticket = OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "Sell Order");

Essential Parameters Explained

  • symbol: The currency pair or instrument you want to trade. Symbol() is a built-in function that returns the current instrument.
  • cmd: The type of operation. OP_BUY for buying and OP_SELL for selling.
  • volume: How much you want to trade, given in lots.
  • price: The price you want to buy/sell at. Ask for buying and Bid for selling.
  • slippage: The maximum allowed difference (in pips) between the price you want and the price you get.

We omitted some parameters in our examples for simplicity. For instance, we didn't set a stop loss, take profit, or other advanced features. Once you're more comfortable, you can explore these advanced parameters.

Conclusion

The OrderSend() function is your gateway to executing trades in MQL4. By understanding its core parameters, you set the foundation for building more complex and sophisticated trading algorithms. Remember, it's essential to practice and test your strategies before going live!