FileSeek()ing Troubles

 

I'm really confused about this block of code below. I wanted it to either rewrite the last line on the file or add a new line. But, it is always rewriting.

                        //----------Check for new bar -----------||
                        if(TimeStamp != iTime(SymbolArray[i],Periods[PeriodIndex], 0) ){    //this condition should be met on the first start() iteration

                                TimeStamp = iTime(SymbolArray[i],Periods[PeriodIndex], 0);

                                LastTickString = MakeInfoString(SymbolArray[i],Periods[PeriodIndex] ,0);
                                FileSeek(Handle,0, SEEK_END); 
                                FileWrite(Handle, LastTickString);                              
                        }       

                        //----------Check for different close-----------||
                        else if(LastClosePrice != iClose(SymbolArray[i],Periods[PeriodIndex] ,0) ){   //if not a newbar, but a new tick, we should delete the last line and rewrite             

                                int ByteCount = StringLen(LastTickString) + 2 ; //count number of bytes to go back

                                LastTickString = MakeInfoString(SymbolArray[i],Periods[PeriodIndex] ,0);
                                FileSeek(Handle,-1*ByteCount, SEEK_END);
                                FileWrite(Handle, LastTickString);
                        }

Full code is attached. It writes bar 0 of 6 timeframes per symbol to files. Then, I wanted it to either update the closing prices or write a new line if a new bar comes in. Somehow there is always one line in each file.

It is meant to be run in as an indicator.


Any help would be appreciated.

Files:
myind.mq4  8 kb
 

Have you removed the line from your start() function?

FileDelete(FileNames[PeriodIndex]);    //JUST FOR TESTING !!!!
 
Ovo:

Have you removed the line from your start() function?


Thanks Ovo. That was why I had only one line.


Now I can proceed with even more bug squashing.

 
BenLinus:


I'm really confused about this block of code below. I wanted it to either rewrite the last line on the file or add a new line. But, it is always rewriting.

Why are you using a negative offset?

FileSeek(Handle,-1*ByteCount, SEEK_END);
 
Thirteen:

Why are you using a negative offset?

Though weird, that is how it works.
 
Thirteen:

Why are you using a negative offset?


I want to rewrite the last line in the file -- which, its not doing. I tried this strategy in a simpler code that handles only the current chart and it worked.


I'm still attempting to make it update the last line properly.


Sample output:

20130909,15:50,0.9548,0.9550,0.9548,0.9550,3
20130909,15:51,0.9549,0.9549,0.9549,0.9549,1
20130909,15:51,0.9549,0.9549,0.9548,0.9548,2
20130909,15:51,0.9549,0.9549,0.9548,0.9548,2
 

Btw, I'm thinking of reading a book about debugging techniques (much later). Probably, my code is not very well written and convenient to debug.

 
BenLinus:


Btw, I'm thinking of reading a book about debugging techniques (much later). Probably, my code is not very well written and convenient to debug.


I may save you time -there is no debugging ability for MT4 code. All you can do is to place Print() statement everywhere to monitor values during a program run.
 
BenLinus:


Btw, I'm thinking of reading a book about debugging techniques (much later). Probably, my code is not very well written and convenient to debug.


If you do, pass on the ideas ^_^

Currently I do these 3

1: "test early, test often"

2: stick Alerts everywhere showing the value of important variables.. along with what I think it ought to be.

3: stick this code after every section:

                        int error = GetLastError();
                        if(error!=0) Alert(error," after CalcLots");
 
You can also pause the tester in visual mode, manually (press pause key) or programmatically (my PauseTest())
 
Ovo: Though weird, that is how it works.
FileSeek - MQL4 Documentation from the beginning, the end

Are you sure? 5 from the beginning is 5 in. -5 would be before the start, impossible. 5 in from the end is toward the beginning -5 would be beyond the end.
Reason: