For Loop Backtesting - Unexpected Results

 

Hello all,

I was hoping for some help regarding an issue I am having getting a 'for loop' to function correctly while using the Strategy Tester (it works exactly as intended live).

for(int i = 0; i < BarsBack; i++)
   {
   Sum += (High[i] - Low[i]);
   }


Simply put, I want to sum up the difference between the high and low of each bar for a set amount of bars back, however when I run my EA in the backtester, it produces unusually high values.

I don't see any obvious reason why it should do this given that A- it works fine live, and B- I use the same calculation elsewhere in the code but for volume, which works fine both live and in the backtester.


Any help or advice on this would be greatly appreciated!


Thanks,

Shawn

 

Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (17 November 2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (18 April 2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (7 April 2010)

 

Wow, what a smarmy reply. I have to say, I've read a lot of threads on this forum and there is a noticable trend of rude and unhelpful responses to people's queries. What gives?


Anyway, no I don't expect anyone to debug my code, I have already debugged and tested extensively and can confirm that my code produces no errors. However, as I am a beginner when it comes to coding, I cannot understand why this might be.

double Bar1 = (High[1] - Low[1]);
double Bar2 = (High[2] - Low[2]);
double Bar3 = (High[3] - Low[3]);
double Bar4 = (High[4] - Low[4]);
double Bar5 = (High[5] - Low[5]);

double Sum = (Bar1+Bar2+Bar3+Bar4+Bar5);

//The above is essentially what I want my loop to accomplish. In one example it produces a value of 0.00496.

for(int i = 0; i < 5; i++)
   {
   Sum += (High[i] - Low[i]);
   }

//Instead of 0.00496, the loop produces 12882. Significantly higher than expected.
 

Hey Shawnlinus do you have examples of the values your getting? (Also timeframe,currency pair, <deleted>?)

 
Bryan Torres #:

Hey Shawnlinus do you have examples of the values your getting? (Also timeframe,currency pair, <deleted>?)

I doubt that he is still waiting for a reply after 4 months.

The most likely solution is quite simple

for(int i = 0; i < BarsBack; i++)
   {
   Sum += (High[i] - Low[i]);
   }

Probably Sum figures in a number of calculations and is not set back to zero.

Sum=0;
for(int i = 0; i < BarsBack; i++)
   {
   Sum += (High[i] - Low[i]);
   }