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/

    How to Feed Backtrader Alternative Data

    General Code/Help
    3
    15
    9022
    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
      cwse last edited by

      Hi there,

      I am new to backtrader, it seems a powerful platform!

      I have a pre-written trading algorithm which modifies the pandas dataframes for many stocks, adding additional columns to the standard OHLCV dataframe. Many new columns are added for the time series to calculate different characteristics.

      What is the best way to bring these dataframes, with my columns, into Back Trader? Can I just use the standard "data= bt.feeds.PandasData(..."?

      How can I then call these columns in "Def next()" so that I can say things like "if Column X > Column Y Then BUY BUY BUY!!" ...i.e. self.order = self.buy().

      Appreciate your help! Hopefully I don't need to re-write my code and can just pull the dataframes into Backtrader!

      Thank you!
      cwse

      1 Reply Last reply Reply Quote 1
      • I
        Ivan last edited by

        Hi,
        seems to me that you could use GenericCSVData feature described here:
        https://www.backtrader.com/docu/datafeed.html#genericcsvdata

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          Each and every lines object in the platform can get the lines definition extended.

          GenericCSVData can be extended: Docs - Extending a Data Feed

          And the same principles apply to PandasData. Due to the nature of a dataframe and to look for things in the dataframe by name, the class features also a datafields atttribute which names the fields (columns in the dataframe).

          You should also add the new fields to the params with the expected position or expect autodetection by means of the name.

          1 Reply Last reply Reply Quote 1
          • C
            cwse last edited by

            @administrators @backtrader is there a way to do it without importing CSVs? I have many dataframes and it would be more efficient to load the dataframes direct.

            Thanks!

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              Yes. That was the intention of the answer.

              You have to add the additional lines. The Docs - Extending a Data Feed gives you an example of how to add an additional line.

              As stated above and due to the nature of a DataFrame, the PandasData class carries a datafields attribute, to which you add the extra names (this aids in mapping column names to actual fields)

              And the parameters tells where to find them (or to try to find them).

              There was an issue some time ago where this was discussed: https://github.com/mementum/backtrader/issues/65

              1 Reply Last reply Reply Quote 0
              • C
                cwse last edited by

                Awesome, got it thank you!

                1 Reply Last reply Reply Quote 0
                • C
                  cwse last edited by

                  Hi, I am using the "Extending teh pandasDataDeed #65" approach with the following class for two new columns at the right hand side of OHLCV:

                  class CustomDataLoader(btfeeds.PandasData):
                      lines = ('TOTAL_SCORE','Beta',)
                      params = (('TOTAL_SCORE',-1),
                          ('Beta',-1)
                      )
                  
                      datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])
                  

                  This works file. But are there any issues that I DONT have an "Openinterest" column in my data source? I am not seeing any errrors but worried that backtrader may be taking the "Total Score" column as the Openinterest?

                  Thanks!

                  1 Reply Last reply Reply Quote 0
                  • B
                    backtrader administrators last edited by

                    If in doubt disable that column by passing None for it.

                    See the rereference of PandasData here:

                    • https://www.backtrader.com/docu/dataautoref.html?highlight=pandas
                    1 Reply Last reply Reply Quote 0
                    • C
                      cwse last edited by

                      So is this correct??

                      class CustomDataLoader(btfeeds.PandasData):
                          lines = ('TOTAL_SCORE','Beta',)
                          params = (('Open_Interest", None),
                              ('TOTAL_SCORE',-1),
                              ('Beta',-1)
                          )
                      
                          datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])
                      
                      B 1 Reply Last reply Reply Quote 0
                      • B
                        backtrader administrators @cwse last edited by backtrader

                        @cwse openinterest is the name in the lines hierarchy of backtrader.

                        1 Reply Last reply Reply Quote 0
                        • C
                          cwse last edited by

                          Meaning?? Is my code correct? If not perhaps you can kindly advise how it should be?

                          1 Reply Last reply Reply Quote 0
                          • B
                            backtrader administrators last edited by

                            You said you wanted to ignore openinterest, but your code contains Open_Interest, which is obviously not the same. And that's what the answer pointed out.

                            C 1 Reply Last reply Reply Quote 0
                            • C
                              cwse @backtrader last edited by

                              @backtrader said in How to Feed Backtrader Alternative Data:

                              code contains Open_Interest, which is obviously not the same. And that's what the answer pointed out.

                              Yes, I do want to ignore it. You said to pass "None" so I am trying to!! I have obviously done it wrong, so please correct me! Thanks, CWSE

                              B 1 Reply Last reply Reply Quote 0
                              • B
                                backtrader administrators @cwse last edited by

                                @cwse The name of the line is openinterest and not Open_Interest.

                                1 Reply Last reply Reply Quote 0
                                • C
                                  cwse last edited by

                                  perfect, so this is it:

                                  class CustomDataLoader(btfeeds.PandasData):
                                      lines = ('TOTAL_SCORE','Beta',)
                                      params = (
                                          ('openinterest',None), #None= column not present
                                          ('TOTAL_SCORE',-1),
                                          ('Beta',-1)
                                      )
                                      datafields = btfeeds.PandasData.datafields + (['TOTAL_SCORE','Beta'])
                                  
                                  1 Reply Last reply Reply Quote 0
                                  • 1 / 1
                                  • First post
                                    Last post
                                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors