Forex RobotIf 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);


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

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

           LastTradeTime = iTime(NULL,0,0);


           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):


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


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


      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.

      ATRPips = ATR / AdjPoint;
      SLNewPips = ATRPips * ATRSLFactor;
      TPNewPips = ATRPips * ATRTPFactor;
      }  //if(UseATR)
      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.

Please leave a comment below if you have any questions!

Also, please give this strategy a 5 star if you enjoyed it!





With over 50+ years of combined trading experience, Trading Strategy Guides offers trading guides and resources to educate traders in all walks of life and motivations. We specialize in teaching traders of all skill levels how to trade stocks, options, forex, cryptocurrencies, commodities, and more. We provide content for over 100,000+ active followers and over 2,500+ members. Our mission is to address the lack of good information for market traders and to simplify trading education by giving readers a detailed plan with step-by-step rules to follow.