Multi asset data dictionary - how to create?

Hi,

Course: Swing trading strategy



Question: Section 19.9 makes reference to multi_asset_data_2020_2021.bz2 pickle file.

I am unable to find reference how this was created so that I can create with my set of tickers.Checked blog with no luck.



Any assistance code sample template to use would be appreciated.Spent a number of days trying to figure it out. Still learning :slight_smile:

Thanks!

 

Hi Luis,



You can try the following code to download multiple asset's OHLCV data in one place.



import pandas as pd



tickers = ['BAC', 'C', 'META', 'GOOGL', 'INTC', 'KO', 'NFLX', 'TWTR', 'V', 'XRX']

import yfinance as yf

stock_data = {}

for t in tickers:

    stock_data[t] = yf.download(t,'2020-1-1', auto_adjust=True)


To print

stock_data





To store it in a pickle file, you can use the following code



import pickle



with open('stock_data.bz2', 'wb') as file:

    pickle.dump(stock_data,file)





And for reading the pickle, you can use the following code.



with open('stock_data.bz2', 'rb') as file:

    stock_data_from_pickle = pickle.load(file)

To print    

stock_data_from_pickle   

Thanks, but want to store my stock history locally as a dictionary. I am able to create dictionary but unable to put column headings with ohlc column names like the pickle files used in the lesson 

Hi Luis,



So in my case, I can access the data of Bank of America by just typing,



stock_data['BAC']



If I want to see the columns, then I would check with the following command,



stock_data['BAC'].columns



And let's say I want to rename the "Open"  and High column of "BAC", to "BAC_Open" and "BAC_High", then I will write it as,



stock_data['BAC'].rename(

    columns=({ 'Open': 'BAC_Open', 'High': 'BAC_High'}), 

    inplace=True,

)





Hope this helps.

Rekhit - thanks for the feedback. I appreciate it. I created the pickle file after merging my csv files. I read it into stock_data as example. I tried your suggestion stock_data['AA'] I get an error

KeyError: 'A'

Tried with another symbol from my pickle: AA

KeyError: 'AA'



I am using python ver 3.9.5

Hi Luis,



It is a bit difficult to know how this error was caused, without understanding the pickle structure. A common cause for "KeyError:" could be the column is spelt differently, as Python is case-sensitive. 



If it is possible, can you upload your file to a cloud, like google drive and share the link here? Or if you like, you can mail the file to quantra@quantinsti.com and we will try to figure out the error.



Hope this helps.

Hi Rekhit,
Basically trying to find out how the pickle file from the course was created. I am trying to duplicate it from my local csv files with ohlcv columns. I have the screener tweaked to work from csv and it is very inefficient.
 
pickle file: 
multi_asset_data_2020_2021.bz2
found in: swing trading course 
 Section 19.9 makes reference to multi_asset_data_2020_2021.bz2 pickle file.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Hey Luis,



You can refer to the notebook Fetching Data for Multiple Stocks to understand how the pickle file was created.



Since you are using CSV files from your local device, you can use the pandas read_csv method instead of yfinance's download method to fetch data.



Hope this helps.



Thanks,

Rushda Ansari

Hi Rushda, 
No issue using yfinance but when reading local csv doesn't work. I have my own stock history in csv and don't want to reach any threshold limit downloading from yfinance. Code from link you provided is appreciated. My csv is per stock with ohlcv
Here is snippet of my code that doesn't have same output format I am seeking. Using Python 3.9.5 and maybe I am overlooking something??
stocks = {}
filenames = glob.glob(folder_path + "/*.csv")
stocks = pd.concat((pd.read_csv(i) for i in filenames)).reset_index(drop=True)
stocks['Date'] = pd.to_datetime(stocks['Date'])
stocks
Output: 
:-(
 
 
 
 
 

Hey Luis,



I've simply tweaked the code used in the Fetching Data for Multiple Stocks notebook to address your query, here's what it looks like:

# List of all the filenames
tickers = ['AAPL.csv', 'META.csv']

# Specify the path to the folder that contains all files
path = '../data_modules/'

# Dictionary to store the data
stock_data = {}

# Loop for fetching the data
for t in tickers:
    stock_data[t] = pd.read_csv(path + t)

# To print the stock data for all tickers
stock_data

After the code mentioned above you can apply the same code as cell 4 of the Fetching Data for Multiple Stocks notebook to store the data in a pickle file.



Hope you find this helpful.



Thanks,

Rushda Ansari

It helped - ty