Array index direction

 
I have a question about arrays, specifically when referencing bar arrays.

The code:

int h1 = High[0];
int h100 = High[100];

returns two values in the series. The first returns the LATEST bar, i.e, today's bar while h100 returns 100 bars ago. So an array of bars counts BACKWARDS from today to some date in the past.

Is this correct?

If so is there a way to copy the array and reverse the index direction such that High[0] referes to the OLDEST price and h100 referes to a price 100 bars later?

I'm writing an indicator that requires the array to be structured from oldest bar to newest bar and I could not figure out why the code was not working, until I made this discovery with a few print statements.

Any help would be appreciated.

Des
 
Desiderius,

If you show us the code you are trying to write maybe we can help you.
You can manually copy the array values to a new array, but i'm sure that you can keep your code structured the
way it is now (some minor changes) and play with the indexes.

Matias Romeo
Custom Metatrader Systems
matiasdotromeoatgmail.com
 
Matias

Sure, here is the code. Note that this is based on an array starting from x time back counting forward. So the last bar will be the last on the chart (most recent).

This code obviously does not work, because it seems the Bar array works backwards. Each time a new bar is added it now becomes [0] and the others are incremented. This seems crazy and thus causing me the problems.

Any help here would be nice. I know that Bars and IndicatorCounted() come into the picture and the code below from the help file seems to indicate that the number of recalculations can be limited using the code below.

int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for (int i = 0; i < limit; i++) {


---------------------------------------------------------

Here is the code I am trying to do. It basically counts from left to right and finds what I call a Minor high (MH) This is a bar where 2 bars (or more) before and after it have a High value lower than it.

//+------------------------------------------------------------------+
//| MHML.mq4 |
//| Des Hartman |
//| |
//+------------------------------------------------------------------+
#property link ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red

// Days to left and right for MH/ML
extern int LRBars = 2;

//---- buffers
double MH_Array[];
double ML_Array[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
// Print("Inside init() ");

//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,MH_Array);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ML_Array);

// Initiate the first value of the buffers
MH_Array[0]=High[0];
ML_Array[0]=Low[0];

return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{

// Temp holder
double tempLevel = MH_Array[0];
int leftCount = 0;
int rightCount = 0;

for (int i = 1; i < Bars-1; i++) {
// Assume no change
MH_Array[i] = MH_Array[i-1];

// Increase the rightCount if the previous bar is higher & there have been enough leftCount bars
// to make a MH possible
if( (High[i-1] > High[i]) && (leftCount > LRBars) ){
rightCount++;
}

// Increase the LeftCount if the previous bar is lower
if(High[i-1] < High[i]){
leftCount++;
}

// Move tempLine
if(High[i-1] < High[i]){
tempLevel = High[i];
}

// Move MH_Array line
if((leftCount >= LRBars) && (rightCount > LRBars)){
MH_Array[i] = tempLevel;
leftCount = 0;
rightCount =0;
}

}

//----
return(0);
}
//+------------------------------------------------------------------+
 
use ArrayGetAsSeries function for recognize array's index direction
 
Slawa

I did this, but it confirms the order

if(ArrayGetAsSeries(High)==true)
Print("array1 is indexed as a series array");
else
Print("array1 is indexed normally (from left to right)");

Result was: "array1 is indexed as a series array"

Is there a way to reverse the indexing?

Thanks
Des
 
Is there a way to reverse the indexing?

yeah. use ArraySetAsSeries. but You cannot apply left-to-right indexing to timeseries (Open, High, Low, Close, Time, Volume)
 
Slawa

Thanks. I did the following to get a left-to-right index.

if(ArrayGetAsSeries(High)==true)
Print("array1 is indexed as a series array");
else
Print("array1 is indexed normally (from left to right)");

double AllHighs[];
ArrayCopySeries(AllHighs, MODE_HIGH);

ArraySetAsSeries(AllHighs,false);

if(ArrayGetAsSeries(AllHighs)==true)
Print("array1 is indexed as a series array");
else
Print("array1 is indexed normally (from left to right)");

...........and the result is what I want.

Do you have a more detailed guide of the functions? The one line descriptions in the help file are a bit light on detail of how the functions work. Specifically how buffers are filled and used during price updates. Any help would avoid silly questions :-(

I'll work with this for now and see if I can find a way to reverse the code to use normal time series.

I'll post the final code when done.

Thanks
Des
Reason: