CSV candle logging and more !!, code giveaway
#1
Star 
Although things can be logged in the databases
There still a lot of people in love with spreadsheets (excel, calc, etc)
So i made a comma value logger, that can easily be integrated in your own strategies.

  • A filename is automatically generated (based upon when you started), or it can be set manual
    In the later case, previous files with such a name would be deleted.
  • It logs candles numbered with date time info, close low high  etc etc
  • If you wish you can add additional values to your log file, in the update function add them as extralog (be sure to coma seperate them).
  • And since you investigating strategies you can also optionally prefix the filename, in the refresh call.
  • Also if you like headers in your file there is an option to set them as well.

  • Different linebreaks for linux vs Windows ea \n vs \r\n\  (for the notepad users )
  • The data collumns C,D,E,F can be directly put in LibreOffice Calc stock chart !!  (they are in the right order   (code updated)
Here is my code :
which can be placed on top in your own strategies code

Code:
var fs = require('fs');
var CVCandleLogger =
{
    linebreaktype:'',
    CandleNr:0,
    firstrun: true,
    stratname: '',
    filename: '',
    // one could log additional commma seperated info (like math results or buy sell info, using extralog)
    update: function (candle, extralog = '',stratname='') {
        this.CandleNr++;

        if (this.filename === '') //base it upon creation time if no filename was givven
        {
            var today = new Date();
            var dd = String(today.getDate()).padStart(2, '0');
            var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
            var yyyy = today.getFullYear();
            var hh = String(today.getHours()).padStart(2, '0');
            var mm = String(today.getMinutes()).padStart(2, '0');
            today = yyyy + '-' + mm + '-' + dd + ' ' + hh + mm;
            this.filename = stratname + ' log ' + today + '.log';
        }

        if (this.firstrun) {
            var isWin = process.platform === "win32";
            if(isWin)this.linebreaktype='\r\n';else this.linebreaktype='\n';

            fs.access(this.filename, (err) => {
                if (!err) {
                    console.log('previous log file name existed, deleting it');
                    fs.unlink(this.filename);
                }
                console.log('New candle data log :', this.filename);
            });
            this.firstrun = false;
        };
        var moment = String(candle.start);
        console.log(moment);
        var timestamp = moment.substring(0, moment.lastIndexOf(' GMT'));
        
        var logline = this.CandleNr+','+ timestamp + ',' + candle.open + ',' + candle.close +','+ candle.high + ',' + candle.low + ','  + candle.vwp + ',' + candle.volume + ',' + candle.trades
        if (extralog !== '') logline = logline + ',' + extralog;
        logline=logline+this.linebreaktype;
        fs.appendFile(this.filename, logline, function (err) {if (err) throw err;});
    },

    writeheader: function(extralog='',stratname='')
    {
        if (this.filename === '') //base it upon creation time if no filename was givven
        {
            var today = new Date();
            var dd = String(today.getDate()).padStart(2, '0');
            var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
            var yyyy = today.getFullYear();
            var hh = String(today.getHours()).padStart(2, '0');
            var mm = String(today.getMinutes()).padStart(2, '0');
            today = yyyy + '-' + mm + '-' + dd + ' ' + hh + mm;
            this.filename = stratname + ' log ' + today + '.log';
        }
        if (this.firstrun) {
            fs.access(this.filename, (err) => {
                if (!err) {
                    console.log('previous log file name existed, deleting it');
                    fs.unlink(this.filename);
                }
                console.log('New candle data log :', this.filename);
            });
            this.firstrun = false;
        };

        logline ='CandleNr, date,time, open , close,high ,low,vwp ,volume , trades' ;
        if (extralog!=='')logline=logline+','+extralog;
        fs.appendFile(this.filename, logline, function (err) {if (err) throw err;});
    }

};

if you put above code on top in your strategy just below :  var config=require('../core/util/js').getconfig();

then only one line is enough to get you started with it

    /* CHECK */
    check: function (candle) {

        CVCandleLogger.update(candle); //minimal sample  find the log outputs in your gekko folder !

 ...
..

oh and the function CVCandlelogger.writeheader();
can be placed in your init part of your strategy, its not required but might be handy for excel / calc.
  Reply


Messages In This Thread
CSV candle logging and more !!, code giveaway - by PGTART - 08-28-2019, 04:09 PM

Forum Jump:


Users browsing this thread: