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

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

In Part 18, we continued talking about the OpenTrade() function. Guess what we’re going to do this time? Right, continue talking about the OpenTrade() function. I blew that whole function right in your face – all at once. Now I’m explaining what it all means.

I got through discussing the while loop that I always put before I try to enter or modify any trade. That will help you avoid the common Error 146 Trade Context Busy.

After that I enter the OrderSend() line:



Ticket = OrderSend(Symbol(),OP_BUY,NormalizeDouble(TradeSize,2),
                           NormalizeDouble(Ask,Digits),2.0,0.0,0.0,"Trade Comment",

The MT4 Editor will allow you to split up a line to make it easier to read. I don’t usually do that, but this WordPress editor chopped off the lines I didn’t split. But it’s a good example. The MT4 editor will ignore whitespace characters in your code line. Whitespace characters include spaces, tabs, newlines, carriage returns and a few others. The editor interprets a line end with the semicolon (;) or right brace (}) character. Of course, the right brace must be paired with the left brace ({).

The OrderSend() function requires the following parameters:

Volume (in lots)
Trade Price
Allowed Slippage
Stop Loss Price (0 in our case – to allow for brokers that require separate entry of SL & TP)
Take Profit Price (again, 0)
Trade Comment
Magic Number
Arrow Color

For Symbol, I used Symbol() which is the built-in variable representing the current chart symbol.

The Command is an integer that represents what type of order you are placing. It’s very nice of MQL4 to have a series of constants that we can use instead of having to remember these numbers. They do that a lot. OP_BUY is the constant for a Market Buy order. OP_SELL is for Market Sell. The others are OP_BUYLIMIT, OP_SELLLIMIT, OP_BUYSTOP and OP_SELLSTOP. For now, we’re only concerned with the market orders. We’ll discuss the Stop orders and Limit orders later. My assumption is that you are already traders and understand what that means.

The Volume must be provided in whatever your broker considers lots. Again, this is a quirk of MT4, the broker can designate what a lot is for the purposes of the MT4 platform. Most often this is a standard 100,000 notional unit lot, but don’t assume that. There are built-in variables in MT4 to allow you to manipulate that information. We’ll discuss those in future modifications of our EA (you didn’t think I was going to stop with just writing this EA, did you? ;))  You will notice that I used the NormalizeDouble() function. As I mentioned here, that will make sure that picky MT4 won’t generate an error because it only understands 1.56 and not 1.56000. Please be mindful of the fact that we haven’t really manipulated the lot size to be sure that it conforms to the minimums, maximums and step sizes required by the broker. For the purposes of our first EA, I’m assuming that you are using a valid trade size amount in the LotSize external variable.

The Trade Price is the price we hope to get for our entry. Since this is a market buy order, we are expecting to get filled at the current Ask price. Again we use NormalizeDouble() to make sure that our price conforms to MT4‘s expected price format. And once again you see the Digits pre-defined variable that allows for the differences between JPY pairs (2 or 3 digits) and the other pairs (4 or 5 digits) and the differences between brokers that display pips and those that display tenths of pips (pipettes or nano pips.)

The Allowed Slippage is the number of pips you will allow the price to be slipped to get into the trade. If the slippage is more, the trade will not enter and will generate an error.

Stop Loss Price and Take Profit Price we will enter 0 here because, in order to conform with the most situations, we’ll be entering the SL & TP using an OrderModify(). If you enter these prices here, be sure to use the NormalizeDouble() function to enter them.

The Trade Comment will be attached to your trade and will appear in the trade listing of the MT4 terminal. You can put anything you like in here. Just remember, though, the trade server can (and will) change your comments, so don’t depend on your comment to identify a certain trade. That’s why we have:

The Magic Number is the only way to consistently track your trade through the system. I usually assign a magic number to a certain strategy and the uniqueness of a particular trade can be determined by the Symbol and the Magic Number. If you want to assign a different magic number to every trade you execute, you can do that.

And finally we come to the Arrow Color. This is just the color of the arrow that appears on the chart to designate where the trade was executed. I, very un-originally, use red for sell orders and blue for buy orders. You may want to be more creative in your color selections, since you have several thousand combinations available.

Well, I guess we’ll wrap it up right here for today. Tomorrow we’ll continue with the OpenTrade() function. Thanks for your attention and 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