Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Problem with resampling Binance data from text

    General Code/Help
    1
    2
    88
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Heinrich last edited by

      Hello everybody! I'm new to Backtrader and run into the first problem. I try to import/ resample generic Binance data from text in the following shape(how to upload the .csv file?):

      1608573600000,1.5505,1.5505,1.5505,1.5505,0.44
      1608573660000,1.5505,1.5505,1.5505,1.5505,0.0
      1608573720000,1.5512,1.5516,1.5512,1.5516,0.4
      1608573780000,1.5516,1.5516,1.5516,1.5516,0.0
      1608573840000,1.5555,1.5555,1.5555,1.5555,0.1
      1608573900000,1.5555,1.5555,1.5555,1.5555,0.0
      1608573960000,1.5555,1.5555,1.5555,1.5555,0.0
      1608574020000,1.559,1.559,1.559,1.559,0.32
      1608574080000,1.5609,1.5609,1.5609,1.5609,0.08
      1608574140000,1.5609,1.5609,1.5609,1.5609,0.0
      1608574200000,1.5516,1.5516,1.5512,1.5512,0.4
      1608574260000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574320000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574380000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574440000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574500000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574560000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574620000,1.5512,1.5512,1.5512,1.5512,0.0
      1608574680000,1.5512,1.5512,1.5512,1.5512,0.0

      I changed data-resample a bit:

      #!/usr/bin/env python
      # -*- coding: utf-8; py-indent-offset:4 -*-
      ###############################################################################
      #
      # Copyright (C) 2015-2020 Daniel Rodriguez
      #
      # This program is free software: you can redistribute it and/or modify
      # it under the terms of the GNU General Public License as published by
      # the Free Software Foundation, either version 3 of the License, or
      # (at your option) any later version.
      #
      # This program is distributed in the hope that it will be useful,
      # but WITHOUT ANY WARRANTY; without even the implied warranty of
      # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      # GNU General Public License for more details.
      #
      # You should have received a copy of the GNU General Public License
      # along with this program.  If not, see <http://www.gnu.org/licenses/>.
      #
      ###############################################################################
      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      
      import argparse
      from _datetime import datetime
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      
      
      class BinanceOHLCV(btfeeds.GenericCSVData):
          params = dict(
              dtformat=lambda x: datetime.utcfromtimestamp(int(x) / 1000),
              headers=False,
              openinterest=-1
          )
      
      
      def runstrat():
          args = parse_args()
      
          # Create a cerebro entity
          cerebro = bt.Cerebro(stdstats=False)
      
          # Add a strategy
          cerebro.addstrategy(bt.Strategy)
      
          # Load the Data
          datapath = args.dataname or '../../../data_2/KSM-BNB--1m--binance--2020-12-21--2021-03-24.csv'
          data = BinanceOHLCV(
              dataname=datapath,
              name=datapath.split('--')[0].split('/')[-1].replace("-", ""))
      
          # Handy dictionary for the argument timeframe conversion
          tframes = dict(
              minutely=bt.TimeFrame.Minutes,
              daily=bt.TimeFrame.Days,
              weekly=bt.TimeFrame.Weeks,
              monthly=bt.TimeFrame.Months)
      
          # New resampler
          cerebro.resampledata(
              data,
              timeframe=tframes[args.timeframe],
              compression=args.compression)
      
          # Run over everything
          cerebro.run()
      
          # Plot the result
          cerebro.plot(style='bar')
      
      
      def parse_args():
          parser = argparse.ArgumentParser(
              description='Resample down to minutes')
      
          parser.add_argument('--dataname', default='', required=False,
                              help='File Data to Load')
      
          parser.add_argument('--timeframe', default='minutely', required=False,
                              choices=['minutely', 'daily', 'weekly', 'monthly'],
                              help='Timeframe to resample to')
      
          parser.add_argument('--compression', default=1, required=False, type=int,
                              help='Compress n bars into 1')
      
          return parser.parse_args()
      
      if __name__ == '__main__':
          runstrat()
      

      python data-resample-binance.py --dataname 'path_to_file' --compression 100
      leads to daily bars with (100 Minutes) in the plot.

      With the timeframe is commented out

      cerebro.resampledata(
              data,
              # timeframe=tframes[args.timeframe],
              compression=args.compression)
      

      python data-resample-binance.py --dataname 'path_to_file' --compression 100
      now leads to correct bars, but I keep the preset daily timeframe (100 days) in the plot. It keeps the daily timeframe in a strategy/ next() as well.

      Can somebody point me in the right direction? Thanks in advance!

      H 1 Reply Last reply Reply Quote 0
      • H
        Heinrich @Heinrich last edited by

        @heinrich
        Here the corresponding data: https://drive.google.com/file/d/1jWMhpJGeXE8i_mU7rpgWEZ6Dk3uyiD8y/view?usp=sharing in case somebody would like to have look.
        The data should be continues. I got them via one of the ccxt examples.
        I started directly with the IB and the CCXTStore in live mode and I am impressed how easy it is to implement multivariate and multi-frequency models. Something around these lines,

        store = CCXTStore(exchange='binance', currency='BNB', config=config, retries=5, debug=False)
        data = store.getdata(dataname="BTC/USDT", name="BTCUSDT", timeframe=timeframe,
                                 fromdate=hist_start_date,
                                 compression=5, ohlcv_limit=ohlcv_limit, drop_newest=False)
        data1 = store.getdata(dataname="BTC/USDT", name="BTCUSDT", timeframe=timeframe,
                                 fromdate=hist_start_date,
                                 compression=15, ohlcv_limit=ohlcv_limit, drop_newest=False)
        

        works nicely(Besides the given granularity and the reliance on the OHLCV bars).
        But I struggle a bit with the import from text.
        Related to this, shouldn't in the provided data-resample.py example,

        python data-resample/data-resample.py --timeframe daily --compression 7
        and
        python data-resample/data-resample.py --timeframe weekly --compression 1
        yield the same result?res_backtrader.png

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors