How to Open Orders in a Forex EA: Part III

As promised, I’ll be providing a few examples on how to set orders using your forex expert advisor in this tutorial article. But before you read along, make sure you’ve read the first and second part of this series.

All good? Lemme just bring up the OrderSend function right here so that we’ll have a reference handy for the inputs:

OrderSend (string symbol, int cmd, double volume, double price, int slippage, double stoploss, double profittarget, string comment, int magic, datetime expiration);

Now that we’re all set, let’s go through some examples so you’d get the hang of tweaking the inputs depending on what your forex system requires.

Example 1:

  • Go long or short at market for the current currency pair
  • Set a 50-pip stop and a 100-pip profit target
  • Use 0.05 lot size for each position
  • Slippage allowance of 5 pips
  • No expiration

For your buy order, you should have: 

OrderSend (Symbol(), OP_BUY, 0.05, Ask, 5, Ask – (50 * Point), Ask + (100 * Point), NULL);

And for your sell order, you should have:

OrderSend (Symbol(), OP_SELL, 0.05, BID, 5, BID + (50 * Point), Bid – (100 * Point), NULL);

Example 2:

  • Place a buy order 10 pips above the current market price or place a sell order 10 pips below the current market price
  • Set a 100-pip stop and a 200-pip profit target
  • Use a lot size of 0.01 for each position
  • Slippage allowance of 3 pips
  • No comment, Magic Number, and expiration date

For your buy order, you should have:

OrderSend (Symbol(), OP_BUYSTOP, 0.01, Ask + (10 * Point) , 3, Ask + (10 * Point) – (100 * Point), Ask + (10 * Point) + (200 * Point), NULL);

For your sell order, you should have:

OrderSend (Symbol(), OP_SELLSTOP, 0.01, Bid – (10 * Point), 3, Bid – (10 * Point) + (100 * Point), Bid – (10 * Point) – (200 * Point), NULL);

Any questions on this one? As you’ve probably noticed, since the buy or sell order has to be set 10 pips above or below the current market price, you’d have to make use of OP_BUYSTOP or OP_SELLSTOP. Aside from that, notice how the double price input also makes use of the pre-set Point variable in setting orders above or below the ask or bid price? You also gotta make sure that this same input is incorporated in setting your stop losses and profit targets!

Example 3:

  • Place a buy order 5 pips below the current market price or place a sell order 5 pips above the current market price
  • Set a 50-pip stop and a 50-pip profit target
  • Use a lot size of 0.03 per position
  • Slippage allowance of 3 pips, no expiration
  • Assign orders from this forex robot under the index 143 with a comment describing the position and EA name of MA Crossover

For your buy order, you should have:

OrderSend (Symbol(), OP_BUYLIMIT, 0.03, Ask – (5 * Point) , 3, Ask – (5 * Point) – (50 * Point), Ask – (5 * Point) + (50 * Point), “Buy limit from MA Crossover”, 143);

For your sell order, you should have:

OrderSend (Symbol(), OP_SELLLIMIT, 0.03, Bid + (5 * Point), 3, Bid + (5 * Point) + (50 * Point), Bid + (5 * Point) – (50 * Point), “Sell limit from MA Crossover”, 143);

As you’ve probably noticed, the string comment input must be placed in between quotation marks which are needed for this particular data type. While I’ve simply typed in the index as 143, you can also declare and initialize this variable at the start of your program then just input the variable name in the OrderSend function.