Partner Center Find a Broker

I’m sure you’re all excited to find out how you can program your forex robot to take trades so I’m gonna be teaching you all about it right here, right now! But since this is a pretty huge topic, I’ll be dividing this tutorial into three parts.

Let’s start off by taking a look at the syntax and the components needed to execute this command. Opening an order with your forex EA can be done with the OrderSend function and it has the following inputs:

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

Say what?!

coffee order
“May I take your order?”

I assure you, it’s not as complicated as it looks. Just think of it as ordering a beverage from your favorite coffee shop and specifying the size, flavor, and number of espresso shots or how you’d like the milk, foam, whipped cream to be. One large non-fat, no whip, soy milk, double shot, hazelnut latte comin’ right up!

Besides, you don’t even need to memorize all the inputs since you can find the sequence in the built-in MQL reference right on your MT4 or MT5 forex trading platform. No need to worry about the colors too, as those are automatically updated when you write functions or data types while coding.

Now let’s take a look at the inputs one by one.

Symbol: This simply refers to the currency pair that you’d like to trade with your expert advisor. To make things easier, you can just write the Symbol () function for this input so that trades are automatically opened on the currency pair you’re currently watching.

Cmd: This is actually robot-speak for command, which stands for the position you want to take, whether it’s long or short. If you want to go long, the proper command is OP_BUY (Yep, all capital letters!). If you want to short, the proper command is OP_SELL.

Buy limit orders need the command OP_BUYLIMIT while buy stop orders use the command OP_BUYSTOP. Similarly, sell limit orders can be set using OP_SELLLIMIT and sell stop orders can be placed using OP_SELLSTOP.

Volume: This stands for the lot size you want to use. This can be declared as a variable and assigned a value in your EA’s initialization part and you can simply put the variable name for this input.

Price: For market orders, you can specify if you want to get in the trade with the ask or bid price. For buy orders, just input “Ask” and for sell orders, just type “Bid”. You can also specify stop or limit entries by indicating the number of pips away from the current market price you want to have your orders on.

Slippage: This usually depends on the currency pair you’re trading or the broker you’re working with but you can just provide an estimate of the usual amount of slippage incurred so your forex robot can make the necessary adjustments. Just make sure you also take into consideration the kind of system that you have and that your trades might not get filled in a fast-moving market if your slippage allowance is too small.

Stop loss and profit target: Now this might seem like pretty straightforward stuff, and you’re probably thinking that you simply need to input the number of pips for your stops and targets. Or that you just have to key in the variable names you’ve specified for your stop loss and profit target in the initialization section. It gets a little trickier than that, actually. Can anybody guess why?

Well, unless anyone can come up with the correct answer, I think I’m just gonna leave y’all guessing until we cover this section next week. Ha! See you then!