Hi Pork,
Yes, I did run it. The if IS executed and buff is loaded. Without the "#" in the printf I can see the file contents in the Zorro window, even though it doesn't show in the log since it isn't active yet. For example, this works:
#define H1 (BarPeriod/60)
bool FirstTime=true;
function run() {
BarPeriod = 60;
TimeFrame = H1;
StartDate = 20080102;
EndDate = 20080131;
string buff;
if(FirstTime){
buff = file_content("History\\atestfile.txt");
printf("\nFirstTime-Bar,buff=%i,%s",Bar,buff);
FirstTime=false;
}
}
If you prefer the int route, this also works:
#define H1 (BarPeriod/60)
int FirstTime=0;
function run() {
BarPeriod = 60;
TimeFrame = H1;
StartDate = 20080102;
EndDate = 20080131;
string buff;
if(FirstTime==0){
buff = file_content("History\\atestfile.txt");
printf("\nFirstTime-Bar,buff=%i,%s",Bar,buff);
FirstTime=1;
}
}
The reason your var version prints in the log is because as Spirit said you're entering the if twice. The first time on the first run with firsttime initialized to 0, and the second time on the second run since on the first run prices haven't been loaded yet and so setting firsttime to priceClose() sets it again to 0.
If your point is you need to see the buff printout in the log, you'll need to delay until the log is active. As you found out, using empty priceClose() works. This is another way that allows you to pick the bar:
#define H1 (BarPeriod/60)
int FirstTime;
function run() {
BarPeriod = 60;
TimeFrame = H1;
StartDate = 20080102;
EndDate = 20080131;
string buff;
if(is(INITRUN)) FirstTime = 0;
if(FirstTime==1){
buff = file_content("History\\atestfile.txt");
printf("#\nFirstTime-Bar,buff=%i,%s",Bar,buff);
}
FirstTime += 1;
}
HTH.