If you’re new to this series and want to check it out from the beginning, you can find that here.
Part 19 was the continuation of our OpenTrade() discussion that started in Part 17. More specifically, we discussed MQL4‘s OrderSend() function. In this part, we’ll continue with the OpenTrade() function. I’m not sure if we’ll be able to wrap it up here or not.
The OrderSend() function returns an integer. If the order was successful, it will return the order ticket number. If unsuccessful, it will return a -1. I have set the integer variable Ticket to the returned value of the function. After the OrderSend() line, we test the variable Ticket to see if it is equal or greater than 0. If it is, we will proceed to set the TP & SL for the trade.
As usual we must test whether the trade context is busy before we proceed. You’ll notice this line:
You’ll recall from here that this positions the order pointer to the current trade so we can use the OrderPrice() function to identify the actual price at which our trade was executed in the OrderModify() function. SELECT_BY_TICKET is a predefined integer constant that designates that we are selecting the order via Ticket number rather than by position (SELECT_BY_POS) as we did in the IsThereAnOpenTrade() function.
Now that we have all our info assembled, we can call the OrderModify() function:
ModifyResult = OrderModify(Ticket,OrderPrice(), NormalizeDouble(SL,Digits), NormalizeDouble(TP,Digits),0,Blue);
The OrderModify() function uses the following parameters:
Stop Loss Price
Take Profit Price
The Ticket Number was picked up from the OrderSend() function.
The Order Price was picked up by selecting the order (via Ticket) and reading the OrderPrice().
The Stop Loss Price was calculated earlier.
As was the Take Profit Price.
The Expiration is a datetime type value that represents the expiration date/time of a pending order.
The Arrow Color designates the color of the arrow that will show on the chart at the price/time of the modification. As with the OrderSend(), I rather non-creatively use blue for a long trade and red for a short trade.
The OrderModify() function returns a boolean value showing the success or failure of the function. You can test the function directly if you want to:
if(!OrderModify(Ticket,OrderPrice(), NormalizeDouble(SL,Digits), NormalizeDouble(TP,Digits),0,Blue)) Alert("Stop Loss and Take Profit not set on order ",Ticket);
Which is the way I usually do it. I included the boolean variable ModifyResult in this case in an attempt to make it more clear:
ModifyResult = OrderModify(Ticket,OrderPrice(), NormalizeDouble(SL,Digits), NormalizeDouble(TP,Digits),0,Blue); if(!ModifyResult) Alert("Stop Loss and Take Profit not set on order ",Ticket);
Either of these constructs will work. In my production programming, I prefer the former, but the latter may be easier to read. And who said programming was not art? 🙂
Also note the negation operator (!) in the if statement. In an if() statement, you are telling the computer to execute the line(s) following the if() only if the statement in the parentheses is true. The negation operator reverses the result of the boolean following it. In this case, the if() statement will execute if ModifyResult is false.
Another thing to notice is the Alert() function. This is a two-fold function. It will pop-up an alert box on the screen and sound an annoying alert sound and it will print a line in your experts log. The idea in this case is to warn the trader that he has an order in place with no Stop Loss or Take Profit. The nice thing about the Alert() and Print() functions is you can string variables, constants, functions, whatever in the parentheses connected with commas and the function will interpret them all properly. Just remember to put spaces between your numbers (” “).
I’d say that this is a good place to break until tomorrow. Thanks for your attention and follow me on Twitter.
Latest posts by admin (see all)
- How To Plan a Trade From Start to Finish - May 3, 2016
- How To Trade The Eur/Usd Right Now - April 29, 2016
- Eur/Usd Could Move Higher Based off of Support Pin Bar - February 19, 2016
Winner’s Edge Trading, as seen on: