Not able to generate the data file like shown in capstone data

Course Name: Swing Trading Strategies, Section No: 23, Unit No: 5, Unit type: Notebook

Dear Sir,

I am trying to generate the data for capstone_data_2020_2021.bz2.

Kindly help me to change the data for my selected scrips

Bhaumik Mehta

 

 

Hello Bhaumik,

It's really amazing to see that you're attempting to solve the capstone project!

Unlike most data files, the data provided for the capstone project is stored as a pickle file. Hence the file extension .bz2 in 'capstone_data_2020_2021.bz2'.

In order to generate a file of a similar type, you can firstly download the data of the shortlisted scrips as a dataframe.

For example: 

data = yf.download(['AAPL', 'AMZN', 'FB', 'MSFT', 'TSLA', 'GOOG' ], period = '1mo', interval = '1h')

Then you can use the following lines of code, to save the data as a pickle file.

import pickle

with open('filename.bz2', 'wb') as file:
    pickle.dump(data, file)

Later, to read the pickle file, simply use the pickle.load() function as follows:

with open('filename.bz2', 'rb') as file:
    multi_asset_data = pickle.load(file)

To learn more about Pickle and its uses in Python, you can check out this blog.
Apart from this, if you need help with fetching the data for scrips, you can refer to this article.
I hope this was helpful. Regards.

Dear Sir,



I am still having some problem. I am trying to run capstone project solution when I am runing the file as under: dataframe.pdf - Google Drive



Kindly advise so that I can run the file.

You can try the below code to get the prices in the same format. The format of the data is stock ticker as key for dictionary and price data as its value. So you have to store the prices in the value and set the ticker as the key as shown below.

 

import yfinance as yf

stock_tickers = ['AAPL',"AMZN"]

multi_asset_data = {}

for ticker in stock_tickers:
    multi_asset_data[ticker] = yf.download(ticker, '2019-1-1', auto_adjust=True)
     
multi_asset_data

You can change the period and data frequency as per your requirement.



I hope this helps.



 

Thank you very much my problem has been solved now.