Coder Social home page Coder Social logo

Comments (4)

 avatar commented on May 14, 2024

I have implemented this in the modules/backfill.js. If you like it you could use it for the main master.

Whole file:

'use strict';

var moment = require('moment');
let CandlestickEvent = require('../event/candlestick_event')
var _ = require('lodash')
let resample = require('./../utils/resample')

module.exports = class Backfill {
    constructor(exchangesIterator, candleStickListener) {
        this.exchangesIterator = exchangesIterator
        this.candleStickListener = candleStickListener
    }

    async backfill(exchangeName, symbol, period, date) {
        let exchange = this.exchangesIterator.find(e => e.getName() === exchangeName)
        if (!exchange) {
            throw 'Exchange not found: ' + exchangeName
        }

        let start = moment().subtract(date, 'days');
        let results = undefined
        let totalResults = []

        do {
            console.log('Since: ' + start)
            results = await exchange.backfill(symbol, period, start)
            totalResults = totalResults.concat(results)

            let event = new CandlestickEvent(exchangeName, symbol, period, results)

            await this.candleStickListener.onCandleStick(event)

            console.log('Got: ' + results.length)

            start = _.max(results.map(r => new Date(r.time * 1000)))
        } while (results.length > 10);

        // @TODO This should be exported into a customizing/global config? 
        // Same as in the exchanges, e.g. bitmex.js
        let resamples = {};

        if (!resamples[symbol.symbol]) {
            resamples[symbol.symbol] = {}
        }

        if (!resamples[symbol.symbol]['5m']) {
            resamples[symbol.symbol]['5m'] = []
        }

        resamples[symbol.symbol]['5m'].push('15m')

        // lets wait for settle down of database insert: per design we dont know when is was inserted to database
        if (resamples[symbol['symbol']] && resamples[symbol['symbol']][period] && resamples[symbol['symbol']][period].length > 0) {
            console.log('Start Resampling')
            setTimeout(async () => {
                for (let periodTo of resamples[symbol['symbol']][period]) {
                    let resampledCandles = resample.resampleMinutes(
                        totalResults.reverse().slice(),
                        resample.convertPeriodToMinute(periodTo)
                    )
                    let event = new CandlestickEvent(exchangeName, symbol, periodTo, resampledCandles)

                    await this.candleStickListener.onCandleStick(event)

                    console.log('Resampling: ' + period + ' -> ' + periodTo)
                }
                console.log('Resampling Finished')

            }, 1000);
        }
    }
};

from crypto-trading-bot.

 avatar commented on May 14, 2024

The referenced commit does not include a resampling of the backfilled data, does it?

from crypto-trading-bot.

Haehnchen avatar Haehnchen commented on May 14, 2024

no, but resulted in that not all resampled data was saved

from crypto-trading-bot.

 avatar commented on May 14, 2024

Ah, I see okay, thanks!

I have update my coding for this. Feel free to use it, if you like it:

'use strict';

let moment = require('moment');
let ExchangeCandlestick = require('../dict/exchange_candlestick')
let _ = require('lodash')
let resample = require('./../utils/resample')

module.exports = class Backfill {
    constructor(exchangesIterator, candleImporter) {
        this.exchangesIterator = exchangesIterator
        this.candleImporter = candleImporter
    }

    async backfill(exchangeName, symbol, period, date) {
        let exchange = this.exchangesIterator.find(e => e.getName() === exchangeName)
        if (!exchange) {
            throw 'Exchange not found: ' + exchangeName
        }

        let start = moment().subtract(date, 'days');
        let candles = undefined
        let totalResults = []

        do {
            console.log('Since: ' + new Date(start).toISOString())
            candles = await exchange.backfill(symbol, period, start)
            totalResults = totalResults.concat(candles)

            let exchangeCandlesticks = candles.map(candle => {
                return ExchangeCandlestick.createFromCandle(exchangeName, symbol, period, candle)
            });

            await this.candleImporter.insertCandles(exchangeCandlesticks)

            console.log('Got: ' + candles.length + ' candles')

            start = _.max(candles.map(r => new Date(r.time * 1000)))
        } while (candles.length > 10);
        console.log('Backfill Finished');

        // @TODO This should be exported into a customizing/global config? 
        // Same as in the exchanges, e.g. bitmex.js
        let resamples = {};

        if (!resamples[symbol.symbol]) {
            resamples[symbol.symbol] = {}
        }

        if (!resamples[symbol.symbol]['5m']) {
            resamples[symbol.symbol]['5m'] = []
        }

        resamples[symbol.symbol]['5m'].push('10m')

        if (!resamples[symbol.symbol]['5m']) {
            resamples[symbol.symbol]['5m'] = []
        }

        resamples[symbol.symbol]['5m'].push('15m')

        let preparedTotalResults = totalResults.reverse().slice();
        // lets wait for settle down of database insert: per design we dont know when is was inserted to database
        if (resamples[symbol['symbol']] && resamples[symbol['symbol']][period] && resamples[symbol['symbol']][period].length > 0) {
            console.log('Start Resampling')
            setTimeout(async () => {
                for (let periodTo of resamples[symbol['symbol']][period]) {
                    let resampledCandles = resample.resampleMinutes(
                        preparedTotalResults,
                        resample.convertPeriodToMinute(periodTo)
                    )
                    let resampledCandlesticks = resampledCandles.map(candle => {
                        return ExchangeCandlestick.createFromCandle(exchangeName, symbol, periodTo, candle)
                    });
        
                    await this.candleImporter.insertCandles(resampledCandlesticks)

                    console.log('Resampling: ' + period + ' -> ' + periodTo)
                }
                console.log('Resampling Finished')

            }, 1000);
        }
    }
};

from crypto-trading-bot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.