Help Me Understand Iterating Over 'N' Periods For Indicator Calculations - page 2

 

I'm just going to collect some info here because I think the issue is a lot deeper than some suggest. If you have a suggestion that isn't "parse someone elses badly/non-commented code", feel free to add something to the convo.


Seems I'm not the only one trying to understand the event handlers and price arrays in this beast. Hey, lets look at OnCalculate and how it doesn't match its documented behavior -- https://www.mql5.com/en/forum/152462 - was this fixed in MT5? Beats me, because the numbers I'm getting seem similar.


Okay, surely its easy to figure out a simple array iterator, right? Yeah, why not.


For an Array with 5 elements and some fake range data therein: { 5, 10, 4, 8, 15 }


You want to sum up the whole shebang and show a current sum (excluding the current bar that hasn't closed yet), sure, easy-peasy!


// All price arrays have been indexed properly and variables declared before use

// The below codeblock is nested within int OnCalculate(){  < my crap code here >  } event handler


startIndex = rates_total - 1; // Start at last indexed bar


for(int i = startIndex; i >= 0; i--) // Decrement from highest indexed bar to 0
{
   RangeArray[i] = HighArray[i] - LowArray[i]; // Calculate range from stored high and low prices
  

   MyIndicatorBuffer[i] = RangeArray[i]; // Plot it!

}

Super awesome! It does what I ask and using our sample range array I defined above -- we see the numbers plotted.


But uh-oh, what if we want to do some things to a select number of bars as we iterate through our price history?


BEATS ME SHERLOCK -- but hey, I'll try it anyway since I'm freewheeling here

// All price arrays have been indexed properly and variables declared before use

// The below codeblock is nested within int OnCalculate(){  < my crap code here >  } event handler

startIndex = rates_total - 1; // Start at last indexed bar

for(int i = startIndex; i >= 0; i--) // Decrement from highest indexed bar to 0
{
   RangeArray[i] = HighArray[i] - LowArray[i]; // Calculate range from stored high and low prices

   // MY SUPER EDUCATED GUESS -- is to insert an inner loop here that does some junk before allowing the next iteration of the 'master' loop

  for(int startIndex = who the hell knows; endIndex >= someEffingValue; startIndex--) // Why decrment? To follow what the master loop is doing

  {

       < Some wonderful fictional code that does things that make sense >

      MyIndicatorBuffer[i] = < magical output for 'n' subset of total bars >; // Plot it, you wizard!

  }

  

   //MyIndicatorBuffer[i] = RangeArray[i]; // Plot it!

}

Yeah, if it was only so simple.

So taking my example array - { 5, 10, 4, 8, 15 }

I know I have 5 stored elements.

I know that it is ordered from index 0 to 4, with the last value being the most current closed bar.

If I wanted to use an operation to sum two of the values as we iterate, I'd use the known start point to figure out new start/end indexes to do this:

so summing two values at index 0, and index 1 would be { 5, 10, 4, 8, 15 } = 5 + 10 = 15

summing two values at index 2 and index 3 would be { 5, 10, 4, 8, 15 } = 4 + 8 = 12

And updating our indicator line with those summed values is a simple matter... just the actual loops isn't.


Anyway, still working it out, using all the tools at my disposal, which really means having to re-invent the wheel since nobody can be bothered to write a simple tutorial on Event Handling and nested loops.


Frustrated as always, xoxoxox

 
tradertimm:

 

So taking my example array - { 5, 10, 4, 8, 15 }

I know I have 5 stored elements.

I know that it is ordered from index 0 to 4, with the last value being the most current closed bar.

If I wanted to use an operation to sum two of the values as we iterate, I'd use the known start point to figure out new start/end indexes to do this:

so summing two values at index 0, and index 1 would be { 5, 10, 4, 8, 15 } = 5 + 10 = 15

summing two values at index 2 and index 3 would be { 5, 10, 4, 8, 15 } = 4 + 8 = 12

And updating our indicator line with those summed values is a simple matter... just the actual loops isn't.


Anyway, still working it out, using all the tools at my disposal, which really means having to re-invent the wheel since nobody can be bothered to write a simple tutorial on Event Handling and nested loops.


Frustrated as always, xoxoxox


   int array[]={5,10,4,8,15},size=ArraySize(array);
   for(int i=0;i<size;i+=2)
     {
      int sumOfPairs=0;
      for(int j=i;j<i+2 && j<size;j++)
         sumOfPairs+=array[j];
      printf("sum of pairs = %d",sumOfPairs);
     }
  
 
Ernst Van Der Merwe:

   int array[]={5,10,4,8,15},size=ArraySize(array);
   for(int i=0;i<size;i+=2)
     {
      int sumOfPairs=0;
      for(int j=i;j<i+2 && j<size;j++)
         sumOfPairs+=array[j];
      printf("sum of pairs = %d",sumOfPairs);
     }
  

Thank you for your code comment, I need to work through my garbage code to see where I've gone wrong on my nested loops. I'll report back on my status.

Appreciate it.

Tim