Multiple assets and multiple indicators for each asset
-
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()
-
@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.