C:\Users\user\Documents\testfile.txt
A small strategy will show exactly how you can write something to that file.
strategy sample_ema_printfile;
input period = 10, lots = 1;
begin
series ma;
file myfile;
ma := ema(close, period);
openfile(myFile, "C:\Users\user\Documents\testfile.txt",app);
if crossup (close, ma) then printf(myFile,"crossup occurred");
if crossdown(close, ma) then printf(myFile,"crossdown occurred");
closefile(myFile);
end.
The openfile instruction has the following structure
Input: f(file), path(string), openmode(number);for openmode you can use app or trunc
app stands for append, this means add
trunc stands for truncate, this means delete content of file first
Thank you Ctlprogrammer, this is just what i was looking for, could not find this info anywhere! Just begining to learn programming CTL, your examples are very helpful.
ReplyDeleteKes
Writing to files should not be excessive done, because it tends to freeze your charts.
ReplyDeleteWriting some special information is ok, but don't write values with every tick or every bar on the lower timeframes.
Would there be a possible problem for a 15min TF?
ReplyDeleteIs it better to write to the debug window?
Thanks.
Kes
Logging all needed values is ok while you are backtesting and debugging.
ReplyDeleteWhen you are running the strategies on your real account i would reduce the logging amount to a minimum. I am logging entries, changing of stops, and exits on a 30 min chart and my charts "survive" a week without freezing.
Can someone tell me, when writing data to files, do you have convert numbers to string or something, when trying to check values, say of a SMA?
ReplyDeleteThanks for all your help.
Kes.
Hi,
ReplyDeleteYes you have to convert all numbers to string before writing them to a file.
Use numbertostring(....)
Good luck
Hy!
ReplyDeleteThanks for your post, but I've got a problem; the compiler says "unexpected file". Do you know what's the matter?
strategy filelog;
begin
file myFile;
openfile(myFile, "C:\tozsde\log.txt",app);
printf(myFile,"hello");
closefile(myFile);
end.
The log.txt is exist.
Thank you.
My CTL IDE version is 1.0.39.18.
ReplyDelete...and my other problem, not about the file:
ReplyDeletestrategy proba1;
input lots=1, firstrun=0;
begin
if firstrun=0 then
begin
lots:=lots*10000;
buy(lots);
firstrun:=firstrun+1;
end;
end.
So I want to run this little script one time, but at EVERY Tick the strategy buys, BUT IT DOESN'T MULTIPLY THE LOTS. I'm confused, what's happening and why???
I'm close to hate this language :)
Thanks for your help.
First problem:
ReplyDeleteDid you create the text file before trying to access it with the small strategy? You should!
Are you sure that windows is granting you all rights in that specific folder? Better be sure and put that folder under besides your documents in windows!
Second problem:
You little strategy doesn't work because you variable firstrun will always be set to zero again.
2 solution.
1)use global variables...but you're version of CTL doesn't support that
2)use CTLDLL and write to a file the value of firstrun and read the value each time the loop runs.....insane solution CTLDLL isn't free