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/

    Using tick data and timeseries data together

    General Code/Help
    2
    2
    3048
    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.
    • C
      ct last edited by

      Hi,

      I want to use simple strategy. I tried a lot ways but I can't.

      Strategy must be use tick data (reading from csv), indicators must be use timeseries (resampled from tick) data

      When latest price changed (on tick data):
      if latest price (tick) >= indicator (ex: SMA(55)) latest bar close (based on timeseries (ex daily))
      buy()
      if latest price (tick) <= indicator (ex: SMA(20)) latest bar close (based on timeseries (ex daily))
      sell()

      Could you help me?

      Best,

      ct

      class TestStrategy(bt.Strategy):
          
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.datetime(0)
              print('%s, %s' % (dt, txt))
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # To keep track of pending orders
              self.order = None
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enougth cash
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
      
                  self.bar_executed = len(self)
      
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
      
              # Write down: no pending order
              self.order = None
      
          def next(self):
              up_value = <--- I need SMA(55) latest bar close
              down_value = <--- I need SMA(20) latest bar close
              self.log('Close, price: %.2f - up: %.2f' % (self.dataclose[0], up_value))
      
              # Check if an order is pending ... if yes, we cannot send a 2nd one
              if self.order:
                  return
      
              if self.position.size == 0 : # FLAT
                  if self.dataclose[0] > up_value:
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
                      self.order = self.buy()
                  elif self.dataclose[0] < down_value:
                      self.log('SELL CREATE, %.2f' % self.dataclose[0])
                      self.order = self.sell()
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Load the Data
          datapath = 'csv/df2_tick_reversed.csv'
      
          tick_data = btfeeds.GenericCSVData(
              dataname=datapath,
              timeframe=bt.TimeFrame.Ticks
          )
          
          # cerebro.addindicator(TURTLE, period=10)
          # period_data = cerebro.resampledata(dataname=tick_data, timeframe=bt.TimeFrame.Days)
      
          # Add the Data Feed to Cerebro
          # cerebro.adddata(tick_data)
          cerebro.resampledata(dataname=tick_data, timeframe=bt.TimeFrame.Days)
          # cerebro.adddata(period_data)
      
          # Set our desired cash start
          cerebro.broker.setcash(100000.0)
          cerebro.broker.setcommission(commission=0.001) # 0.1% ... divide by 100 to remove the %
      
          # Add a strategy
          cerebro.addstrategy(TestStrategy)
      
          # Print out the starting conditions
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
          # Run over everything
          cerebro.run()
      
          cerebro.plot(style='bar')
          # Print out the final result
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        To simulate a raw tick data one can assign the value of the tick to the fields of the OHLC. See

        • Community - backtest with tick data, get period datas

        With that in mind the value of close (for example) can be used as the tick value (And has to be added with addata)

        The 2nd stream, the resampled one, is created, as already present in the code, by issuing resampledata.

        The tick data would be in data0 and the resampled in data1

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