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/

    What is a simple way to use indexes created myself?

    Indicators/Strategies/Analyzers
    2
    3
    655
    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.
    • A
      atori last edited by

      Hi. I'm Japanese boy.

      I would like to use the index created myself as pandas dataframe in the strategy.
      Is there a simple way to use indicators in strategy?

      For example, I created an index named EMA5 , but I could not reference it.
      (I understand that EMA can be created with backtrader, but I write it as an example of indicator.)

      ・df_input
      datetime stock_name close EMA5 EMA20 EMA60
      2018-08-14 S&P500 2839.9600 2837.8037 2825.0370 2783.2351
      2018-08-15 S&P500 2818.3701 2831.3258 2824.4021 2784.3870
      2018-08-16 S&P500 2840.6899 2834.4472 2825.9533 2786.2330
      2018-08-17 S&P500 2850.1299 2839.6748 2828.2559 2788.3280
      2018-08-20 S&P500 2857.0500 2845.4665 2830.9982 2790.5812

      '-----------------------------------------------------------------------
      class Bt_print(bt.Strategy):

      def next(self):
      print(str(self.datas[0].datetime.date(0)) + " price: " + str(self.data.lines.close[0]) + " EMA5: " + str(self.data.lines.EMA5[0]))

      bt_data = bt.feeds.PandasData(dataname=df_input,datetime='datetime',close='close',openinterest=None)
      cerebro.adddata(bt_data)

      cerebro.addstrategy(Bt_print)
      cerebro.run()

      '-----------------------------------------------------------------------
      AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'EMA5'

      1 Reply Last reply Reply Quote 0
      • A
        Amien Johaadien last edited by

        Hi Atori,

        You can't use the standard PandasData class for that - you need a new class with the additional lines defined. See https://www.backtrader.com/docu/extending-a-datafeed.html

        Regards,
        Amien

        1 Reply Last reply Reply Quote 0
        • A
          atori last edited by

          Thank you for your reply.
          It was solved by creating own class that extended the PandasData class.

          class Bt_print(bt.Strategy):
            def next(self):
              print(str(self.datas[0].datetime.date(0)) + " price: " + str(self.data.lines.close[0]) + " EMA5: " + str(self.data.lines.EMA5[0]))
          
          
          class PandasData_Extend(bt.feeds.PandasData):
            
            params = (
              ('EMA5', -1),
            )
            
            lines = ('EMA5',)
          
          
          bt_data = PandasData_Extend(dataname=df_input,datetime='datetime',close='close',EMA5='EMA5',openinterest=None)
          cerebro.adddata(bt_data)
          
          cerebro.addstrategy(Bt_print)
          cerebro.run()
          

          2018-08-13 price: 2821.9299 EMA5: 2836.7256
          2018-08-14 price: 2839.96 EMA5: 2837.8037
          2018-08-15 price: 2818.3701 EMA5: 2831.3258
          2018-08-16 price: 2840.6899 EMA5: 2834.4472
          2018-08-17 price: 2850.1299 EMA5: 2839.6748
          2018-08-20 price: 2857.05 EMA5: 2845.4665
          [<main.Bt_print at 0x7fcd32ebe3c8>]

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }