Navigation

    Backtrader Community

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

    kicgahi

    @kicgahi

    0
    Reputation
    2
    Profile views
    4
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    kicgahi Unfollow Follow

    Latest posts made by kicgahi

    • RE: logging timestamp produces an entirely different number

      @rajanprabu said in logging timestamp produces an entirely different number:

      dt = dt or self.datas[0].datetime.date(0)

      thank you so much for the help, it does work now!

      posted in General Code/Help
      K
      kicgahi
    • logging timestamp produces an entirely different number

      hi, i'm still pretty new to using backtrader and haven't really gotten the grasp of it. I've been trying to log my order executions but the log time is always completely off. My data was fed in using this code

      fromdate = datetime.datetime.strptime('2020-1-01', '%Y-%m-%d')
      todate = datetime.datetime.strptime('2021-01-08', '%Y-%m-%d')
      
      data = bt.feeds.GenericCSVData(
          dataname='2021_hourly.csv', 
          dtformat=2, 
          compression=30, 
          timeframe=bt.TimeFrame.Minutes, 
          fromdate=fromdate, 
          todate=todate)
      
      cerebro.adddata(data)
      
      

      and my data looks something like this. The first column corresponds to the datetime for each row

      Screenshot 2021-01-10 at 3.17.28 AM.png

      but somehow when i try to log with the following code

      def log(self, txt, dt=None):
              ''' logging function for this strat ''
              dt = datetime.datetime.fromtimestamp(self.data.datetime[0])
              print('%s, %s' % (dt, txt))
              print('')
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  return
          
              if order.status in [order.Completed, order.Canceled, order.Margin]:
                  if order.isbuy():
                      self.log('Buy Executed, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm))
                      self.buyprice = order.executed.price
                      self.buycomm = order.executed.comm
                  else:
                      self.log('Sell Executed, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm))
      
                  self.bar_executed = len(self)
      
              self.order = None
      

      it produces an output like this in which the dates and time are completely off
      Screenshot 2021-01-10 at 3.20.10 AM.png

      if i change dt to the following instead

      dt = self.data.datetime[0]
      

      the output will produce the date as this number which is completely different from the unix datetime fed into cerebroScreenshot 2021-01-10 at 3.22.52 AM.png

      could anyone help identify what the problem is? would really appreciate it, thanks!

      posted in General Code/Help
      K
      kicgahi
    • RE: signals stop showing after a certain date

      @vladisld said in signals stop showing after a certain date:

      cerebro.broker.setcash(1000000)

      thanks for the help, it does work now!

      posted in General Code/Help
      K
      kicgahi
    • signals stop showing after a certain date

      pretty new to this. followed some guide online and i managed to write out this code which seems to work fine until after july. I'm just trying to create a chart with the 14 day RSI as an indicator to signal buy (rsi<30) and sell (rsi>70) points.

      here is my code

      import backtrader as bt
      import datetime
      
      class RSIStrategy(bt.Strategy):
      
          def __init__(self):
              self.rsi = bt.talib.RSI(self.data, period=14)
      
          def next(self):
              if self.rsi < 30 and not self.position:
                  self.buy(size=1)
              
              if self.rsi > 70 and self.position:
                  self.close()
      
      
      cerebro = bt.Cerebro()
      
      fromdate = datetime.datetime.strptime('2020-01-01', '%Y-%m-%d')
      todate = datetime.datetime.strptime('2020-12-25', '%Y-%m-%d')
      
      data = bt.feeds.GenericCSVData(dataname='2020_hourly.csv', dtformat=2, compression=60, timeframe=bt.TimeFrame.Minutes, fromdate=fromdate, todate=todate)
      
      cerebro.adddata(data)
      
      cerebro.addstrategy(RSIStrategy)
      
      cerebro.run()
      
      cerebro.plot()
      

      the data import is fine, double checked that so it shouldn't be the problem. but the chart it produces looks like this

      Screenshot 2020-12-28 at 3.31.23 AM.jpg

      as you can see, after around end july, the signals stop appearing even though there are certain points where the rsi is clearly below 30 (some even below 20) or over 70.

      could anyone help identify what the problem is? would really appreciate it, thanks!

      posted in General Code/Help
      K
      kicgahi