As I’ve mentioned in my previous entry on opening orders with your forex expert advisor, setting stop losses and profit targets usually entails a few more extra steps than just putting a value or variable in the parameters. Setting buy or sell limit orders, as well as putting in buy or sell stop orders, also require additional lines of code.
If you’ve guessed that this is because one pip for a particular currency pair isn’t always equal to one pip for another, then you’re almost there! After all, one pip for EUR/USD or most dollar pairs is 0.0001 while one pip for USD/JPY and other yen crosses is 0.01. Of course if you’d like to run your EA on just a single pair, then you can simply multiply your stop or target with either 0.01 or 0.0001 to get the point value.
But if you’d like to run your forex EA on different currency pairs, you would need to make sure that your specified stop loss and profit target is appropriate for the current pair your forex robot is trading. Otherwise, you might end up with stop losses that are too wide or profit targets that are too tiny.
Luckily for you, the gods of the forex robot world have already anticipated this dilemma and created a pre-set variable called Point, which tracks the point value of the currency you’re watching.
“Aha! Problem solved. Now we can just multiply the Point variable with the stop loss or profit target size…”
Hold on right there! We ain’t done yet! Keep in mind that the input for the stop loss and profit target requires an actual price instead of just the number of pips or point values. With that, you’d actually need to make use of a few arithmetic operations in order to specify your stop or target relative to your trade entry price.
For market entries, you can use the following stops and targets, provided that you have declared and initialized your StopLoss and ProfitTarget variables earlier in the program.
Stop loss for buy order: Ask – (StopLoss * Point)
Profit target for buy order: Ask + (ProfitTarget * Point)
Stop loss for sell order: Bid + (StopLoss * Point)
Profit target for sell order: Bid – (ProfitTarget * Point)
Now lemme bring up the OrderSend function syntax right back here to see where we’re at:
OrderSend (string symbol, int cmd, double volume, double price, int slippage, double stoploss, double profittarget, string comment, int magic, datetime expiration);
Now the last three components aren’t really essential ones but you can’t just leave them blank. The string comment input is just for you to keep track of which EA has executed this particular order (in case you have multiple ones running at the same time) and if you don’t need a value for this one, just type NULL.
Next up we have int magic, which isn’t even remotely as magical as it sounds. Just like the comment string variable, this can serve as your index in keeping track of which EA executed the trade order. As with the other inputs, you can declare and initialize this at the start of your program and simply indicate the variable name in this portion.
Lastly, the datetime expiration portion can be used for pending orders and you can input the number of seconds for which limit or stop orders will wait to get triggered. Take note that for market orders, you don’t need to put anything in this field.
Everyone keeping up so far? Don’t be shy to post any questions you’ve got right here! Next week we’ll take a look at a few examples so make sure you’ve mastered this lesson on setting orders with your EA!