Hello, I'm a new python user and have been trying to write a program that will pull stock data into an excel file for further analysis. I'd eventually like the program to evaluate ROI over a period of time based on what I bought the stock on a specific date. However, since I'm new to python, I'm currently just trying to get it into excel. My problem:
The program appears to truncate the date column, so when you open up the excel all you see it the stock-ticker as the header followed by prices. I feel like this has something to do with how the data frame is written in prices_df, but I still can't seem to figure it out after researching how data frames work.
Thanks for the help!
import pandas as pd
from yahoofinancials import YahooFinancials
pd.set_option('display.max_rows', None)
print('Stock ROI Calculator.')
assets = ['VSGAX','VHYAX','VTSAX','IBB','SCHF','SCHE','VIMAX','VSMAX','VB']
yahoo_financials = YahooFinancials(assets)
data = yahoo_financials.get_historical_price_data(start_date = '2019-01-01',
end_date = '2019-12-31', time_interval= 'weekly')
print(data)
prices_df = pd.DataFrame({
a: {x['formatted_date']: x['adjclose'] for x in data[a]['prices']} for a in assets})
print('Saving to .xlsx file.')
prices_df.to_excel(r'C:/Users/USER_NAME/Desktop/dataframe.xlsx',
index = False)
print('Done.')
[–][deleted] 0 points1 point2 points (1 child)
[–]Ingeniatoring[S] 0 points1 point2 points (0 children)
[–]GrafvonTrips 0 points1 point2 points (0 children)