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

Forex RobotGood Monday morning to all. It’s a beautiful day here on the farm. It was pretty stormy over the weekend, but it’s cleared up nicely.

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

In Part 16 we wrote the NewBar() function to tell us if we’ve already taken a particular signal or not.  Now we’re about to tackle the most tedious function of all – mostly due to MT4 quirks – the OpenTrade() function.

In looking over the code in Part 15, I realized that I made another mistake. I’m not sure how this happened, but I’m sure it’s confused some of you. In the start() function is the following code:

 

 

if(iTime(NULL,0,0) > LastTradeTime)
    {
    //Set LastTradeTime to iTime(NULL,0,0)
    LastTradeTime = TimeCurrent();
    //Enter code to trigger a trade
    OpenTrade(CrossOver);
    }

I’m not sure exactly where that came from (I should probably look back at prior articles), but the if statement should contain the NewBar() function and we need to set LastTradeTime to iTime(NULL,0,0) rather than TimeCurrent() like so (that was my first error):

if(NewBar())
    {
    //Set LastTradeTime to iTime(NULL,0,0)
    LastTradeTime = iTime(NULL,0,0);
    //Enter code to trigger a trade
    OpenTrade(CrossOver);
    }

Functionally, the code in place of NewBar() will work, I just want it to be more self-explanatory. Plus I really like the idea of getting my ideas down on paper and fleshing out the details later – hence the NewBar() function.

Well, if it weren’t for my faux pas, we might actually get this project done. So, on to the OpenTrade() function.

The OrderSend() function is used to open trades. MT4 is not very forgiving when it comes to trade opening code. Everything must be very precise so I always have all the numbers (stop losses and take profits) calculated before attempting to open the trade.  Of course, if we’re using a market order, we can’t pre-calculate the entry prices. To be sure the latest price is being used to open the market order, I use the pre-defined variables Bid and Ask in the actual OrderSend() function (Bid for opening Sell orders, Ask for opening Buy orders)

When we’re ready to open our order, we must start by ensuring the Trade Context is open – in other words, we have to be sure it’s not already trying to open a trade in another EA or pair. We’ll use the built-in function IsTradeContextBusy() to make that determination. We’ll set up a loop so the EA will wait for the Trade Context to be available, then we will need to refresh the rates using RefreshRates() to be sure we have the latest data in all the pre-defined variables before attempting to enter the trade.

Another quirk of MT4 is that the prices must have precisely the correct number of digits after the decimal to correspond with the pair on which you are opening the trade. We will use the NormalizeDouble() function to take care of that – along with the Digits pre-defined variable – which contains the appropriate number of digits for the current pair on the current platform.  There has been some controversy in the MQL4 programmer’s forums regarding the use of NormalizeDouble(). I just ignore the controversy. The function has always worked properly for me.  Early in my MQL4 programming career, I spent quite a bit of time experimenting with it and discovered that it’s worth the typing trouble to just use it every time you must send a price to an MT4 function. I also use it when displaying the prices on the chart – it’s quite convenient.

A broker-related quirk of MT4 is that some brokers will not allow you to set the SL and TP of your trade when you enter the trade. With those brokers, you must use the OrderModify() function to set them after the trade is opened.  Once again, I opt for the solution that works (in my experience) in all cases – setting the SL and TP after the trade is entered. I haven’t come up with a good reason not to do it that way. If any of my readers have a different opinion on this, I would love to hear about it.

I think I’ve covered all the quirks with which we need to deal. So, if I haven’t made any more errors, maybe we can finish the OpenTrade() function in our next installment. Have a great day and thanks for your attention.

Tim

 

here_for_you_001

1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube
Connect_With_Winners_Edge

Get trade set ups everyday!

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.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news