Trader’s Tech – Writing Your Own EA Part 42 – EA Mods

Forex Robot

Saturday morning we left my nephew’s northeastern Indiana farm and today we’re in the beautiful (and very hot) state of Texas to see our kids. We’ll be parking our motor-home at a lake resort for a while, so that should be nice. We’ve been through Indiana, southern Illinois, Missouri and amazing Arkansas. We don’t usually get in too big a hurry when we travel but we did this time. We like to see the sights and I’m usually just too old for the 24 hours and no sleep routine. Been there, done that. :)

If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.

In Part 41, we started a couple of modifications to our EA. Due to my lack of foresight (that usually doesn’t happen ;)), we had to modify one of our include files. But that’s the nice thing about programming, you can change whatever you want to change. In this installment, we’ll continue with our modifications.

Our next modification is to the NewBar() function. Since we’re triggering our trades using a designated time frame, we have to test that time frame each time, rather than testing the current chart time frame. So this line:

   datetime CurrBarTime = iTime(NULL,0,0);

becomes:

   datetime CurrBarTime = iTime(NULL,TriggerTF,0);

and when we assign the LastTradeTime static variable in the start() function:

           LastTradeTime = iTime(NULL,0,0);

becomes:

           LastTradeTime = iTime(NULL,TriggerTF,0);

That wraps the changes necessary to use TriggerTF instead of the chart time frame. Now we’ll tackle the changes necessary to use ATR for our SL and TP.

Once again, we have a bit of a quandary. Doing this the right way requires a little more work. But in the long run, the extra work now will reduce the debugging for future modifications. As I’ve said, you will soon forget what you did here today. The only thing you’ll remember in six months is what’s in the source code. Especially if you’re a prolific programmer. That’s why I always recommend copious comments in the code. It’s also the reason I recommend taking the time to do it right – at least do it right by your own standards and codes. Then when it comes time to reuse the code or modify it in some way, you’ll know exactly what’s what.

This modification will all take place in the OpenTrade() function. The quick fix is to just reassign TPPips and SLPips with the values you will use for the TP and SL. But, in my opinion, those variables represent something different and you may want to use those variables elsewhere in the program and if you arbitrarily reassign them here that could cause you all sorts of grief if you try to use them elsewhere in the future. So I recommend creating new local variables within the OpenTrade() function to handle that task. So start with adding these declarations at the beginning of the OpenTrade() function:

int SLNewPips,TPNewPips;

Sometimes my variable naming is uninspired. ;)  Then, change the following lines:

      TP = Bid + (TPPips * AdjPoint);
      SL = Bid - (SLPips * AdjPoint):

to:

      TP = Bid + (TPNewPips * AdjPoint);
      SL = Bid - (SLNewPips * AdjPoint);

also:

      TP = Ask - (TPPips * AdjPoint);
      SL = Ask + (SLPips * AdjPoint);

to:

      TP = Ask - (TPNewPips * AdjPoint);
      SL = Ask + (SLNewPips * AdjPoint);

Now we have to assign values to TPNewPips and SLNewPips. We start by testing UseATR to see if we should use ATR to determine the SL & TP or not. We also should add declarations for a couple more variables:

double ATR;
int ATRPips;

Since the ATR is returned in price format, we will need a double to store it. Then we’ll convert it to pips to use for the SL & TP calculations. The ATR can be retrieved using MQL4′s iATR() function. Converting price to pips can be accomplished using our AdjPoint variable we assigned in the init() function. Then finding SLNewPips and TPNewPips is just a matter of multiplying ATRPips by ATRSLFactor and ATRTPFactor, respectively. So we will add the following code near the beginning of the OpenTrade() function.

   if(UseATR)
      {
      ATR=iATR(NULL,ATRTF,ATRBars,0);  
      ATRPips = ATR / AdjPoint;
      SLNewPips = ATRPips * ATRSLFactor;
      TPNewPips = ATRPips * ATRTPFactor;    
      }  //if(UseATR)
   else
      {
      SLNewPips = SLPips;
      TPNewPips = TPPips;
      }  //else

As you can see, we check the UseATR variable and if it’s false, we just assign SLNewPips to SLPips and TPNewPips to TPPips. Otherwise we do the ATR calculation. It’s as simple as that.

Well, that’s a wrap for today. Thanks for your attention and please follow me on Twitter and LinkedIn.

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!

 

 

 

Follow our social media:
1359774937_facebook 1359774984_linkedin 1359774949_twitter 1359774960_google 1359774973_rss 1360027635_youtube

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news