Download MetaTrader 5

FileSeek()ing Troubles

To add comments, please log in or register
Ben Linus
245
Ben Linus  

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
Ex Ovo Omnia
3354
Ex Ovo Omnia  

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

FileDelete(FileNames[PeriodIndex]);    //JUST FOR TESTING !!!!
Ben Linus
245
Ben Linus  
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.

GreenMoney
379
GreenMoney  
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);
Ex Ovo Omnia
3354
Ex Ovo Omnia  
Thirteen:

Why are you using a negative offset?

Though weird, that is how it works.
Ben Linus
245
Ben Linus  
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
Ben Linus
245
Ben Linus  

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.

Ex Ovo Omnia
3354
Ex Ovo Omnia  
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.
Ian Flanagan
549
Ian Flanagan  
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");
whroeder1
16076
whroeder1  
You can also pause the tester in visual mode, manually (press pause key) or programmatically (my PauseTest())
whroeder1
16076
whroeder1  
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.
12
To add comments, please log in or register