Array problem

 

The code I'm using is (stripped version) this:


#property copyright "Copyright © 2006, MetaQuotes Software Corp."

#property link      "http://www.metaquotes.net"


...


string sTrend[];


...


int start()

   {

   int i,counted_bars=IndicatorCounted();

   double shortPSAR, longPSAR;


   if(Bars<=100) return(0);

   if(counted_bars<0) return(-1);

   if(counted_bars>0) counted_bars--;

   i=Bars-counted_bars;


   while(i>=0)

      {

      shortPSAR=iSAR(NULL,0,0.004,0.2,i);

      longPSAR=iSAR(NULL,0,0.0005,0.2,i);

      

      if (iHigh (NULL,0,i)<shortPSAR && iHigh (NULL,0,i)<shortPSAR) sTrend[i]="DOWN";

      else if (shortPSAR<iLow(NULL,0,i) && longPSAR<iLow(NULL,0,i) ) sTrend[i]="UP";

      else sTrend[i]="";


      Print ("sTrend["+(i+1)+"] = "+sTrend[i+1]+ "    sTrend["+i+"] =  "+sTrend[i]); 


      i--;

      }

   return(0);

   }


When the indicator runs, the print statement will show this (example):


sTrend[32] =    sTrend[31] =    ==> basically no trend according to the PSAR rule

sTrend[31] = UP   sTrend[30] = UP   ==> SO WHY DID sTrend[31] CHANGE VALUE AND MIMIC sTrend [30]


or similar


sTrend[145] = DOWN    sTrend[144] = DOWN   ==> basically DOWN trend according to the PSAR rule

sTrend[144] = UP   sTrend[143] = UP   ==> SO WHY DID sTrend[144] CHANGE VALUE AND MIMIC sTrend [144143]


This basically happens for every change and I loose the transistion from one state to another for some reason (I want to store the history of a systems state).


I do not see anything in the loop that could change the sTrend [31] value from EMPTY to UP after i decreases by 1 or change sTrend[144] value from DOWN to UP after i decreases by 1. 


There's only one assignement and yet the value of a previous assigned element in the array changed?


Any help would be appreciated.


Yves

 

string sTrend[]; --->> string sTrend[1000];

 

So i need to pre-allocate memory and I guess up to the maximum number of bars in the graph right?


Tested in and that did it... thanks for a really fast reply ;-)


There's a maxbars in history (= 512000) and maxbars in chart (65000) in the global options.


Should i set the value for my chart somewhere to be safe not to go over the preallocated 1000?

 
yvesgazin wrote >>

So i need to pre-allocate memory and I guess up to the maximum number of bars in the graph right?

Tested in and that did it... thanks for a really fast reply ;-)

There's a maxbars in history (= 512000) and maxbars in chart (65000) in the global options.

Should i set the value for my chart somewhere to be safe not to go over the preallocated 1000?

It's right.

You can allocate memory dynamically.

/////////////////////////////////////

int start()

{

if( ArraySize(sTrend) < Bars ) ArrayResize(sTrend, Bars );

/////////////////////////////////////

Reason: