Backtrader Community

    • 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/

    Next() Called Twice

    General Code/Help
    1
    1
    36
    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.
    • P
      pjg last edited by

      Hi Everyone I have uploaded two 'minute level' data streams. I have added timezone to both. However, the next() function is still called twice and so each trade call is made twice. Has anyone had this problem? Here is a simple example. "Not a very smart strategy ;)"

      '''

      from __future__ import (absolute_import, division, print_function, unicode_literals)
      import datetime  # For datetime objects
      import os.path  # To manage paths
      import sys  # To find out the script name (in argv[0])
      
      # Import the backtrader platform
      import backtrader as bt
      
      
      # Create a Stratey
      class TestStrategy(bt.Strategy):
      
      def log(self, txt, dt=None):
          
          dt = dt or self.data.datetime[0]
          dt = bt.num2date(dt)
          print('%s, %s' % (dt.isoformat(), txt))
      
      
      def __init__(self):
          # Keep a reference to the "close" line in the data[0] dataseries
          self.dataclose0 = self.datas[0].close
          self.dataclose1 = self.datas[1].close
      
      
      
      def next(self):
      
         
      
          # Placing the order
          self.log('SELL CREATE %s, price = %.2f, qty = %d' % ("AAPL", self.data0.close[0], 100))
          self.sell(data=self.data0, size=100)  # Place an order for buying y + qty2 shares
          self.log('BUY CREATE %s, price = %.2f, qty = %d' % ("GOOGL", self.data1.close[0], 100))
          self.buy(data=self.data1, size=100)  # Place an order for selling x + qty1 shares
      
      
      
      
      if __name__ == '__main__':
      # Create a cerebro entity
      cerebro = bt.Cerebro()
      
      # Add a strategy
      cerebro.addstrategy(TestStrategy)
      
      #Datas are in a subfolder of the samples. Need to find where the script is
      # because it could have been called from anywhere
      modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
      datapath0 = os.path.join(modpath, 'datas/AAPL.csv')
      datapath1 = os.path.join(modpath, 'datas/GOOGL.csv')
      
       # Create a Data Feed 1
      
      data0 = bt.feeds.GenericCSVData(
          dataname=datapath0,
      
          fromdate=datetime.datetime(2019, 12, 23, 14, 30),
          todate=datetime.datetime(2021, 11, 29, 23, 51),
          timeframe = bt.TimeFrame.Minutes,
          tz=pytz.timezone('US/Eastern'),
          nullvalue=0.0,
      
          dtformat=('%Y-%m-%d %H:%M:%S'),
          datetime=0,
          high=2,
          low=3,
          open=1,
          close=4,
          volume=5,
          openinterest=-1,
      )
      
      cerebro.adddata(data0)
      
      # Create a Data Feed 2
      data1 = bt.feeds.GenericCSVData(
          dataname=datapath1,
      
          fromdate=datetime.datetime(2019, 12, 23, 14, 30),
          todate=datetime.datetime(2021, 11, 29, 23, 51),
          timeframe = bt.TimeFrame.Minutes,
          tz=pytz.timezone('US/Eastern'),
          nullvalue=0.0,
      
          dtformat=('%Y-%m-%d %H:%M:%S'),
          datetime=0,
          high=2,
          low=3,
          open=1,
          close=4,
          volume=5,
          openinterest=-1,
      )
      
      cerebro.adddata(data1)
      
      # Set our desired cash start
      cerebro.broker.setcash(100000.0)
      
      # Print out the starting conditions
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      # Run over everything
      cerebro.run(tz=pytz)
      
      # Print out the final result
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors