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
#2
How can we recognize that it is PU leather or Real Leather What is PU Leather What is the Difference bw PU Leather and Real Leather, We have all answers in details, Please visit our website to get the best & honest information for all kind of leathers.
  Reply
#3
Affordable Pool Repair provides the best pool restoration, renovation, gunite pool finishes, and pool coping in the area of Boston as well as New Hampshire. Pool Deck Services
  Reply
#4
Our website is a place where every man will gain self-confidence, a guarantee of proven products and 100% understanding. We created the website because we want to help in the fight against what is not talked about loudly. Kamagra apteka
  Reply
#5
If you’re looking for where to get top-notch samples for music, then you’ve come to the right place. Producers Wave has you covered for any music genre, format, DAW, or style. All our sounds are created by professional producers and sound designers for your inspiration. They are all royalty-free for both personal and commercial use. No attribution is needed. We believe there’s bound to be something here that matches your production. hip hop sounds
  Reply
#6
Our specialty: We are a team of music and fashion lovers committed to providing a service for people just like us. We provide great pieces that balance the music and fashion sides of our clients. We love our celebrities and provide a comprehensive service that caters to VIP members outside of retail hours. We also have a master barber on staff to add that extra something to your look.We are the cultural hub that mixes music and fashion equally for people of all ages. black crossbody bag
  Reply
#7
We give women a place to shop for athletic wear that was affordable, good quality and stylish. We know that keeping up with the latest trends and styles can be pricey. That is why we are here. Trendy and affordable all in one place. We got you girls! Leggings
  Reply
#8
Buy high quality cannabis and vape proudcts from us at great prices. Weed, flowers, cbd oil, vapes, pre-rolls, extracts and have them delivered to you. cannabis seed to sale
  Reply
#9
Qikink enables anyone to sell products online without inventory, processing and logistics hurdles. Anyone can signup on Qikink.com and start selling products online. Custom T-Shirt Printing
  Reply
#10
All of our products are designed and hand crafted in Tennessee by local seamstresses.We follow God's Word and stitch a little extra love into every piece. knotted gowns
  Reply


Forum Jump:


Users browsing this thread: