Risk warning

All articles, strategies and indicators are just reflecting a single traders opinion and should be viewed as that.
I advice everybody to trade with a DEMO account!

Sunday, May 22, 2011

How to write to a file with CTL

Well first of all you have to create a file. And you should create it in a directory where you have administrator rights. Go to your directory my documents. Next create a text file in that directory. For example

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

10 comments:

  1. 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.

    Kes

    ReplyDelete
  2. Writing to files should not be excessive done, because it tends to freeze your charts.

    Writing some special information is ok, but don't write values with every tick or every bar on the lower timeframes.

    ReplyDelete
  3. Would there be a possible problem for a 15min TF?
    Is it better to write to the debug window?
    Thanks.

    Kes

    ReplyDelete
  4. Logging all needed values is ok while you are backtesting and debugging.

    When 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.

    ReplyDelete
  5. 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?

    Thanks for all your help.

    Kes.

    ReplyDelete
  6. Hi,

    Yes you have to convert all numbers to string before writing them to a file.

    Use numbertostring(....)

    Good luck

    ReplyDelete
  7. Hy!

    Thanks 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.

    ReplyDelete
  8. My CTL IDE version is 1.0.39.18.

    ReplyDelete
  9. ...and my other problem, not about the file:

    strategy 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.

    ReplyDelete
  10. First problem:

    Did 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

    ReplyDelete