Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. fivo
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    F
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 5
    • Best 2
    • Groups 0

    fivo

    @fivo

    2
    Reputation
    446
    Profile views
    5
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    fivo Unfollow Follow

    Best posts made by fivo

    • RE: Order Validity

      You can still used dates to set the validity of every order to exactly one bar. Let's say your candles are 60 minutes long.

      dt = self.datas[0].datetime.datetime()
      self.sell(exectype=bt.Order.Limit,
                size=self.position.size,
                price=self.ema[0],
                valid=dt + datetime.timedelta(minutes=60))
      

      If you want to make it dependent on your data, pass timeframe_in_m to your strategy.

      posted in General Discussion
      F
      fivo
    • RE: Anyone use backtrader to do live trading on Bitcoin exchange?

      @fivo said in Anyone use backtrader to do live trading on Bitcoin exchange?:

      There seems to be a problem when fetching live data. The backfilling works correctly, but once the live data is used the feeds receives the data of the current minute (assuming we are trading minute data) that is not yet finished. Essentially the opening value is correct, but the rest (close high low vol) is not.

      It doesn't seem to be ccxt as the following little script produces the correct values. I think the code around this line seems to be fetching data as soon as some data of the currently running minute is available. I will try look into it.

      # !/usr/bin/env python
      # -*- coding: utf-8; py-indent-offset:4 -*-
      
      from datetime import datetime, timedelta
      import time
      
      import ccxt
      
      binance = ccxt.binance()
      
      while True:
          fromdate = datetime.utcnow() - timedelta(minutes=2)
          since = int((fromdate - datetime(1970, 1, 1)).total_seconds() * 1000)
          ohlcvs = binance.fetch_ohlcv('BTC/USDT', '1m', since=since, limit=1)
          if not ohlcvs:
              continue
      
          ohlcv = ohlcvs[0]
      
          print(datetime.utcfromtimestamp(ohlcv[0]/1000), ohlcv[1], ohlcv[2],
                ohlcv[3], ohlcv[4], ohlcv[5])
      
          time.sleep(1)
      

      Can anybody reproduce this issue?

      posted in General Discussion
      F
      fivo

    Latest posts made by fivo

    • RE: Anyone use backtrader to do live trading on Bitcoin exchange?

      @mula29 I have a pull request pending that fixes the issue, but be careful it crucially depends on the time stamp being from the beginning of the candle.

      posted in General Discussion
      F
      fivo
    • RE: Anyone use backtrader to do live trading on Bitcoin exchange?

      @fivo said in Anyone use backtrader to do live trading on Bitcoin exchange?:

      There seems to be a problem when fetching live data. The backfilling works correctly, but once the live data is used the feeds receives the data of the current minute (assuming we are trading minute data) that is not yet finished. Essentially the opening value is correct, but the rest (close high low vol) is not.

      It doesn't seem to be ccxt as the following little script produces the correct values. I think the code around this line seems to be fetching data as soon as some data of the currently running minute is available. I will try look into it.

      # !/usr/bin/env python
      # -*- coding: utf-8; py-indent-offset:4 -*-
      
      from datetime import datetime, timedelta
      import time
      
      import ccxt
      
      binance = ccxt.binance()
      
      while True:
          fromdate = datetime.utcnow() - timedelta(minutes=2)
          since = int((fromdate - datetime(1970, 1, 1)).total_seconds() * 1000)
          ohlcvs = binance.fetch_ohlcv('BTC/USDT', '1m', since=since, limit=1)
          if not ohlcvs:
              continue
      
          ohlcv = ohlcvs[0]
      
          print(datetime.utcfromtimestamp(ohlcv[0]/1000), ohlcv[1], ohlcv[2],
                ohlcv[3], ohlcv[4], ohlcv[5])
      
          time.sleep(1)
      

      Can anybody reproduce this issue?

      posted in General Discussion
      F
      fivo
    • RE: Anyone use backtrader to do live trading on Bitcoin exchange?

      Hi everybody,

      @Ed-Bartosh great work.

      There seems to be a problem when fetching live data. The backfilling works correctly, but once the live data is used the feeds receives the data of the current minute (assuming we are trading minute data) that is not yet finished. Essentially the opening value is correct, but the rest (close high low vol) is not.

      With the following code:

      # !/usr/bin/env python
      # -*- coding: utf-8; py-indent-offset:4 -*-
      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      import sys
      import time
      from datetime import datetime, timedelta
      
      import backtrader as bt
      import ccxt
      
      #pylint: disable=E1101,E1123
      
      class TestStrategy(bt.Strategy):
      
          def start(self):
              self.counter = 0
              print('START')
      
          def prenext(self):
              self.counter += 1
              print('prenext len %d - counter %d' % (len(self), self.counter))
      
          def __init__(self):
              pass
      
          def next(self):
              print('------ next len %d - counter %d' % (len(self), self.counter))
      
              self.counter += 1
      
              print('*' * 5, 'NEXT:', bt.num2date(self.data0.datetime[0]),
                    self.data0._name, self.data0.open[0], self.data0.high[0],
                    self.data0.low[0], self.data0.close[0], self.data0.volume[0],
                    bt.TimeFrame.getname(self.data0._timeframe), len(self.data0))
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
      
          exchange = sys.argv[1] if len(sys.argv) > 1 else 'binance'
          symbol = sys.argv[2] if len(sys.argv) > 2 else 'BTC/USDT'
      
          hist_start_date = datetime.utcnow() - timedelta(minutes=3)
          data = bt.feeds.CCXT(exchange=exchange,
                               symbol=symbol,
                               timeframe=bt.TimeFrame.Minutes,
                               fromdate=hist_start_date,
                               ohlcv_limit=999)
      
          cerebro.adddata(data)
          cerebro.addstrategy(TestStrategy)
          cerebro.run()
      

      I get the following output:

      START
      ------ next len 1 - counter 0
      ***** NEXT: 2018-03-09 14:40:00  9090.0 9115.35 9080.0 9114.99 30.29132 Minute 1
      ------ next len 2 - counter 1
      ***** NEXT: 2018-03-09 14:41:00  9112.25 9148.0 9090.11 9102.02 73.48228 Minute 2
      ------ next len 3 - counter 2
      ***** NEXT: 2018-03-09 14:42:00  9109.99 9110.0 9097.0 9097.0 35.020203 Minute 3
      ------ next len 4 - counter 3
      ***** NEXT: 2018-03-09 14:43:00  9099.0 9100.0 9090.43 9090.43 2.0025 Minute 4
      ------ next len 5 - counter 4
      ***** NEXT: 2018-03-09 14:44:00  9099.0 9099.0 9095.0 9095.0 0.058638 Minute 5
      ------ next len 6 - counter 5
      ***** NEXT: 2018-03-09 14:45:00  9088.78 9088.78 9088.78 9088.78 0.135 Minute 6
      ------ next len 7 - counter 6
      ***** NEXT: 2018-03-09 14:46:00  9099.0 9100.9 9099.0 9100.9 0.0327 Minute 7
      

      The first 2 minutes are correct, because they have completely finished. Afterwards the values are off because the minute data is obtained when the minute is still running. This is essentially some 1off error. If you want to check for yourself binance - BTC/USDT

      I also checked for bitfinex (python script.py bitfinex2 BTC/USD). Same issue.

      I don't know if this is related to the ccxt/backtrader branch or related to ccxt master. @Ed-Bartosh let me know if you want me to look into it.

      posted in General Discussion
      F
      fivo
    • RE: Order Validity

      You can still used dates to set the validity of every order to exactly one bar. Let's say your candles are 60 minutes long.

      dt = self.datas[0].datetime.datetime()
      self.sell(exectype=bt.Order.Limit,
                size=self.position.size,
                price=self.ema[0],
                valid=dt + datetime.timedelta(minutes=60))
      

      If you want to make it dependent on your data, pass timeframe_in_m to your strategy.

      posted in General Discussion
      F
      fivo
    • Indicator values before period kicks in

      I want to know how to calculate initial values for an indicator that don't fall yet into the period. So for example if my period is 12 and my indicator uses recursive values how and where do I specify the first 11 eleven values of the indicator. Let's take the EMA as an example. I am aware that bt already implements a solution for the EMA, but lets just take it as an example.

      class EMA(bt.Indicator):
          lines = ('ema',)
          params = (('period', 12),
      
          def __init__(self):
              self.addminperiod(self.p.period)
              self.smtfactor = 2.0 / (1.0 + self.p.period)
      
              #Where do I put this part
              # here I refer to data[0] data[1] and ema[0] ema[1] to the first values in the whole dataset
              self.ema[0] = self.data[0]
              for i in range(1, self.p.period):
                  self.ema[i] = self.ema[i-1] * ( 1.0 - self.smtfactor) + self.data[i] * self.smtfactor
      
          def next(self):
              self.ema[0] = self.ema[-1] * ( 1.0 - self.smtfactor) + self.data[0] * self.smtfactor
      posted in General Code/Help
      F
      fivo