What is wrong with my TypicalPrice function?

 

Hi All,

I keep looking at this and can't work out what I have done wrong. I have written a function to populate the array t with TypicalPrice based on high, low and close (h, l and c).

// The typical price is the average of hlc.  &t needs to be length long
void TypicalPrice(double h[], double l[], double c[], double &t[], int length)
{
   if (ArraySize(h) < length)
      Alert("Insufficent Bars in Chart");
      
   ArrayResize(t, length);
   
   for(int i=0; i<length; i++)
   {
      t[i] = (h[i] + l[i] + c[i])/3;
   }

   return;
}

The problem is that when I call it along with the inbuilt function iRSI, I get different results. Ie the two alerted values are different numbers. Any help would be appreciated.

int barCount = Bars;
double typicalPrice[];
ArrayResize(typicalPrice, barCount);
TypicalPrice(High, Low, Close, typicalPrice, barCount);

Alert(iRSIOnArray(typicalPrice, 0, 300, 1));
Alert(iRSI(NULL,0,300,PRICE_TYPICAL,1));
 
Please use this to post code . . . it makes it easier to read.

 
Will do. Would you like me to fix it so as to help answer the question?
 
stewart:
Will do. Would you like me to fix it so as to help answer the question?

I'm sure fixing it will encourage people to try and help you as it will then be easier to read your code.
 

Done. Thanks. I'd never used that button before.

Hope to get some responses :)

 
stewart: I keep looking at this and can't work out what I have done wrong. I have written a function to populate the array t with TypicalPrice based on high, low and close (h, l and c).
try making typicalPrice[] a series array like iRSIOnArray expects.
 

Thanks. One thing I am unclear on is whether a 'series array' is just about the direction of data in the array (ie element 0 is current, 1 is last bar and so on) or whether it has some further intrinsic meaning. In my example I would have thought typicalPrice[] was already a series array as it runs in the same direction as Close[] etc. I think I may have something wrong here with my understanding. Any ideas or references would be appreciated. I have read the help for ArraySetAsSeries() which didn't answer my question really.

So does ArrayAsSeries() just reverse the order of elements, or does it do something more intrinsic?

Hoping you can shed some light on this.

Thanks,

Stewart

 
stewart:

Thanks. One thing I am unclear on is whether a 'series array' is just about the direction of data in the array (ie element 0 is current, 1 is last bar and so on) or whether it has some further intrinsic meaning. In my example I would have thought typicalPrice[] was already a series array as it runs in the same direction as Close[] etc. I think I may have something wrong here with my understanding. Any ideas or references would be appreciated. I have read the help for ArraySetAsSeries() which didn't answer my question really.

So does ArrayAsSeries() just reverse the order of elements, or does it do something more intrinsic?


If you were to write price data to a standard array you would start at 0, then 1, then 2, etc . . . with the newest always going to the last element, when you ran out of space you would use ArrayResize and add more free elements to the end, then you could carry on adding to the end. But arrays holding price don't work like that, they must be made to work differently, the most current bar is in position 0 not in Arraysize - 1

All ArraySetAsSeries() does is "If the set parameter has the TRUE value, the array will be indexed in a reversed order, i.e., the last element has a zero index." but if you are adding data to your Array and need to grow it to handle more elements you need to manage it, it will no be managed for you. So to add free elements (ArrayResize) to a Series array if we simply use ArrayResize we will add elements next to the cell that holds the oldest element, we don't want to do that. So we need to make the Array NOT a series array, resize it and then turn it back into a series array. (By the way, a similar technique can be used to create a FIFO array)

 
stewart: So does ArrayAsSeries() just reverse the order of elements, or does it do something more intrinsic?
  1. For your problem, all it does is reverse the order of the elements. Read the rsi on array. It processes elements left to right (oldest to newest) but you filled in the array newest (0) to oldest (n) so you got the wrong results.
  2. As RaptorUK says it also interacts when expanding the size of the array, no need to move elements up. From my code just like indicator buffers expand:
    bool    ResizeBuffer(double& buffer[], int size){
        if (ArraySize(buffer) != size){
            ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
            if (ArrayResize(buffer, size) <= 0){ DisableTrading(
                "ArrayResize [1] failed: " + GetLastError() );      return(false);  }
            ArraySetAsSeries(buffer, true);
        }
        return(true);
    }
    
 

Hi there stewart,

  double typicalPrice[];
  ArrayResize(typicalPrice, Bars);
  ArraySetAsSeries (typicalPrice, true);
  
  for(int i = 0; i < Bars; i++)
     {
      typicalPrice[i] = (High[i] + Low[i] + Close[i])/3;
     }
   
   Alert("RSI Array ",iRSIOnArray(typicalPrice, 0, 300, 1));
   Alert("iRSI ",iRSI(Symbol(),Period(),300,PRICE_TYPICAL, 1));

 

Guys,

Many thanks for your help on this.

I have learned from this is that ArraySetAsSeries() actually affects the array in a way I hadn't expected. It is not reversing the order of the current elements at all but only changing the way the indexing is used to address the data in the for-loop before iRSIOnArray() is called.

In the above example posted by onewithzachy, if you remove the ArraySetAsSeries() then it doesn't work - as could be expected. One other thing I find interesting is that iRSIOnArray() actually doesn't care if it is a series array or not. It only cares that, forgetting indexing, in memory the data runs in the correct direction. This can be verified by adding either a ArraySetAsSeries (typicalPrice, true) or ArraySetAsSeries (typicalPrice, false) immediately before iRSIOnArray() and realising that it makes no difference to the answer. It only matters whether typicalPrice is flagged as a series array or not as the data is added to it.

Thanks again guys. This has made my day. If you're ever in London, drop me a line and I'll buy you a beer.

Stewart

Reason: