Trader’s Tech – Writing Your Own EA Part 21 – Functions

Forex RobotCan you believe it’s actually Friday again? Whew! Tempus fugit. Hope you’ve all had a great week. We always have great weeks here at Winner’s Edge. 🙂

If you’re new to this series and want to check it out from the beginning, you can find that here.

In Part 20, we continued our discussion of the OpenTrade() function, talking about the built-in OrderModify() function. Today, we’ll wrap the week with a wrap of the OpenTrade() topic.

In yesterday’s discussion, I talked about how we tested the Ticket number to see if the trade was successfully entered.  The OrderSend() function returns the ticket number if the trade entry was successful, otherwise it returns a -1. The if() statement is part of a construct. A construct is a group of statements that work together. The code construct following is how we did the test:


if(Ticket >= 0)
    //Valid Trade - Set SL & TP
    //Invalid Trade - Warn the operator

The if() construct includes an else statement. If the statement in the parentheses following the if() is true, it executes the code between the first set of braces. If the statement is false, it executes the code between the set of braces following the else statement. The else statement is not required if you have no code that is executed when the statement following the if evaluates as false.

For now, all that’s necessary for the else statement is an alert to the trader that the trade was not entered. The Alert() function, as I’ve said before, will pop up a dialog box, make a sound and make an entry in the Experts log.  In future versions of our EA, we will test the error code to determine why our trade didn’t enter. Some of the reasons can be handled programmatically, allowing the computer to do the job it’s meant to do; help the trader be profitable. I don’t like too many alerts when I’m using an EA. It desensitizes me to real alert situations. Then when something serious happens (you know, the whole market is sliding into oblivion or some such thing) then you won’t just click the OK button without reading the alert. 🙂

This wraps the TradeDir = 1 (buy trade) portion of the OpenTrade() function. I’m just going to point out the differences between that and the short trade – most of which will be obvious to those who are already traders.

The most obvious difference of course is the Stop Loss (SL) will now be above the Entry and the Take Profit (TP) will be below the Entry. Also, the sell trade will be opened at the Bid price and closed at the Ask price. So any reference to Bid or Ask will be changed to its opposite. Also, as I mentioned, I use Red for my short trade arrow color and Blue as my long trade arrow color. And lastly, the OP_BUY in the OrderSend() function is changed to OP_SELL. I think that pretty much covers the differences.

Which means we have pretty much wrapped the OpenTrade() function. I believe that also means we can wrap up the functions portion of this series and proceed to the actual compiling and testing of our new EA.

It being Friday afternoon and all, I think I’ll continue all this next week. Have a great weekend and thanks for your attention. Follow me on Twitter.




The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news