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/

    Multiple assets and multiple indicators for each asset

    Indicators/Strategies/Analyzers
    indicator lines
    2
    2
    66
    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.
    • Daniel Cunha
      Daniel Cunha last edited by

      Suppose that i have i=1,..,n assets (close price only), and for each asset i I have j=1,..,k macro indicators.

      I want to make a strategy that gives a score to each of the n assets. The score is a function of the value of the k indicators.

      I have tried to add all asset closes and macro indicators at once using bt.feeds.PandasData but im not sure if this is the best way.

      One obvious issue with my approach is that i have to know the index of each self.datas which is cumberstone.

      Is there an easier way to build my data feed and use it inside my strategy?

      import pandas as pd
      import os
      import backtrader as bt
      
      class DataFeed(bt.feeds.PandasData):
      
          params = (
              ('datetime', None),
              ('open', None),
              ('high', None),
              ('low', None),
              ('close', 'Close'),
              ('openinterest', None),
          )
      
      class Teste(bt.Strategy):
          def __init__(self):
              teste=1
              
      if __name__ == '__main__':
          # initialize cerebro
          cerebro = bt.Cerebro()
      
          # add strategy
          cerebro.addstrategy(Teste)
      
          # add target assets to cerebro
          target_asset_df = pd.read_excel(os.path.join(os.getcwd(), 'data', 'inputs_swap.xlsx'), sheet_name='target_assets').dropna()
          target_asset_df = target_asset_df.set_index('Date')
      
          for asset in target_asset_df.columns:
              loop_df = target_asset_df[[asset]]
              loop_df.columns = ['Close']
              df_tot_feed = DataFeed(dataname=loop_df)
              cerebro.adddata(df_tot_feed, name=asset)
      
          # add inputs
          inputs_df = pd.read_excel(os.path.join(os.getcwd(), 'data', 'inputs_swap.xlsx'), sheet_name='inputs').dropna()
          inputs_df = inputs_df.set_index('Date')
      
          for input in inputs_df.columns:
              loop_df = inputs_df[[input]]
              loop_df.columns = ['Close']
              df_tot_feed = DataFeed(dataname=loop_df)
              cerebro.adddata(df_tot_feed, name=input)
      
          # run cerebro
          cerebro.run()
      
      
      run-out 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @Daniel Cunha last edited by

        @daniel-cunha Add your 1-k indicators to your asset in pandas before loading, then load your close + k indicators together as one data feed. You will then have a data line for each asset that has the 1-k indicators in the same line.

        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(); }); }