Hi Guys,
I am having problems with arrays, I know where the problem is but I don't understand why this is happening.
As an example, I wrote this little study:
function preMain() setPriceStudy(false);
var myarray = new Array();
var ii = null;
function main() {
nTime = getValue("rawtime",0); //get current time value
barnday = Math.round(getCurrentBarIndex()-getFirstBarIndexOfDay(nTime)); // get the current daily bar index (first bar of the day = 0, second = 1, etc.)
if(barnday == null) return;
if(barnday==0) ii=0; //if this is the first bar of the day, ii = 0
dim=myarray.push(2*ii); //I add the element 2*ii to myarray. The dimension of myarray is stored in dim.
ii++; //I increase ii by 1.
return new Array( ii, dim);
}
This works very well. The values for ii reset at the beginning of each day, while the dim keeps growing.
Now, say that I want to kill the array at the beginning of every new day. I try to do that and it doesn't work.
If I try to CREATE a new array within the "if" statement, i.e., If modify the if statement in the code above like this:
if(barnday==0) {
var myarray = new Array();
ii=0; //if this is the first bar of the day, ii = 0
}
I get the error "TypeError: myarray has no properties",
It doesn't let me create an array in a conditional statement and call it later! Is it worried that I might call it before even creating it? I just want to destroy it and start a new one everyday. So, my two questions: a) Any ideas of why this is happening and b) what I can do to resolve my problem?
Thanks a lot!
Silvia.
I am having problems with arrays, I know where the problem is but I don't understand why this is happening.
As an example, I wrote this little study:
function preMain() setPriceStudy(false);
var myarray = new Array();
var ii = null;
function main() {
nTime = getValue("rawtime",0); //get current time value
barnday = Math.round(getCurrentBarIndex()-getFirstBarIndexOfDay(nTime)); // get the current daily bar index (first bar of the day = 0, second = 1, etc.)
if(barnday == null) return;
if(barnday==0) ii=0; //if this is the first bar of the day, ii = 0
dim=myarray.push(2*ii); //I add the element 2*ii to myarray. The dimension of myarray is stored in dim.
ii++; //I increase ii by 1.
return new Array( ii, dim);
}
This works very well. The values for ii reset at the beginning of each day, while the dim keeps growing.
Now, say that I want to kill the array at the beginning of every new day. I try to do that and it doesn't work.
If I try to CREATE a new array within the "if" statement, i.e., If modify the if statement in the code above like this:
if(barnday==0) {
var myarray = new Array();
ii=0; //if this is the first bar of the day, ii = 0
}
I get the error "TypeError: myarray has no properties",
It doesn't let me create an array in a conditional statement and call it later! Is it worried that I might call it before even creating it? I just want to destroy it and start a new one everyday. So, my two questions: a) Any ideas of why this is happening and b) what I can do to resolve my problem?
Thanks a lot!
Silvia.
Comment