I'm trying to write a program where the user inputs multiple stocks tickers that will (eventually) get graphed.
import pandas as pd
import pandas_datareader.data as web
from datetime import datetime, timedelta
tickers = ['QQQ', 'SQQQ', 'RYCVX', 'RYCWX']
days = (datetime.now() - timedelta(5)).strftime('%Y-%m-%d')
data = {}
for t in tickers:
data[t] = web.DataReader (t, 'yahoo', days, end = None)
Result:
{'QQQ': High Low ... Volume Adj Close
Date ...
2019-08-14 185.949997 182.419998 ... 51000500 182.759995
2019-08-15 183.589996 181.160004 ... 36685000 182.550003
2019-08-16 185.949997 184.100006 ... 32781400 185.479996
2019-08-19 188.830002 187.500000 ... 22494100 188.429993
[4 rows x 6 columns],
'SQQQ': High Low Open Close Volume Adj Close
Date
2019-08-14 36.900002 35.000000 35.320000 36.730000 32427000 36.730000
2019-08-15 37.689999 36.230000 36.520000 36.849998 19184200 36.849998
2019-08-16 35.939999 34.830002 35.939999 35.110001 14002600 35.110001
2019-08-19 33.970001 33.220001 33.590000 33.529999 11745300 33.529999,
'RYCVX': High Low Open Close Volume Adj Close
Date
2019-08-14 89.489998 89.489998 89.489998 89.489998 0 89.489998
2019-08-15 90.290001 90.290001 90.290001 90.290001 0 90.290001
2019-08-16 92.489998 92.489998 92.489998 92.489998 0 92.489998
2019-08-19 94.290001 94.290001 94.290001 94.290001 0 94.290001,
'RYCWX': High Low Open Close Volume Adj Close
Date
2019-08-14 9.92 9.92 9.92 9.92 0 9.92
2019-08-15 9.83 9.83 9.83 9.83 0 9.83
2019-08-16 9.59 9.59 9.59 9.59 0 9.59
2019-08-19 9.40 9.40 9.40 9.40 0 9.40}
This creates a dictionary where tickers are keys and the stock data are the values. My original idea was to remove the key:value pair and assign to a variable but I have just learned my original idea was bad practice. What I now need to know how to do is the following:
- I need to be able to call a column value from a specific key such as the "High" column of the key QQQ. What I would normally do to call a column if it was assigned to a variable:
QQQ["High"]
- I need a loop to iterate a new column to every dataframe. What I would do if the df was assigned to a variable to create a new column:
QQQ['15MA'] = QQQ['Close'].rolling(15).mean()
How do I adapt the calling and iterating to a dictionary?