4jano20
4jano20

Reputation: 35

Loop to import and save each stock with its respective name through Yahoo Finance

I have a doubt: I am using Yahoo Finance to import information about some stocks. I have a "ticker" list where I have the names of the stocks to import and I use pdr.get_data_yahoo. My code is the following:

import pandas as pd
import datetime as dt
import pandas_datareader as pdr 

ticker = ['AAPL', 'AMZN', 'FB'] # Acciones

start = dt.datetime(2020, 11, 21) # Desde
end = dt.datetime(2020, 12, 4) # Hasta

AAPL = pdr.get_data_yahoo('AAPL', start = start, end = end) # a)
AMZN = pdr.get_data_yahoo('AMZN', start = start, end = end) # b)
FB   = pdr.get_data_yahoo('FB', start = start, end = end) # c)

Instead of importing stock by stock as in a), b) and c), how could I do a loop or iteration to save the information of each action in a variable with its respective name (equal to the str of the ticker). That is, I want to be able to modify and / or add actions to the ticker and that each one is saved with its respective name

I was trying a loop like

ticker = ['AAPL', 'AMZN', 'FB', 'MSFT', 'AAL']
for ticker in tickers:
ticker[i] = pdr.get_data_yahoo(ticker, start = start, end = end)

Upvotes: 0

Views: 787

Answers (2)

furas
furas

Reputation: 143216

You have two problems

  • first: ticker is list so you can't use it as dictonary ticker[i].

  • second: you use tickers (with s) but you don't have this variable.

You simply mess two similar names. I will use name all_tickers and ticker to better show problem.

And use dictionary for results

all_tickers = ['AAPL', 'AMZN', 'FB', 'MSFT', 'AAL']
results = {}

for ticker in all_tickers:
    results[ticker] = pdr.get_data_yahoo(ticker, start=start, end=end)

EDIT:

Don't use variables AAPL, AMZN and FB but keep it in dictinary as results["AAPL"], results["AMZN"], results["FB"]

Usually you will work with all tickets like this

for ticker in all_tickers:
    print(ticker, results[ticker])

or

max_value = {}

for ticker in all_tickers:
    max_value[ticker] = max( results[ticker] )
    #print('max value for', ticker, 'is', max_value[ticker])

for ticker in all_tickers:
    print('max value for', ticker, 'is', max_value[ticker])

or

my_favorite = ['AMZN', 'FB']

for ticker in my_favorite:
    print(ticker, results[ticker])

etc.

If you add new ticket to all_tickers then rest code will work the same.


Eventually you could add all to pandas.DataFrame and then you could work with all values even without for-loop.

Upvotes: 2

Harshana
Harshana

Reputation: 5496

You can use a dictionary and save the ticker data with the ticker name in it.

Example:

import pandas as pd
import datetime as dt
import pandas_datareader as pdr 

ticker = ['AAPL', 'AMZN', 'FB'] # Acciones
ticker_data = {}
start = dt.datetime(2020, 11, 21) # Desde
end = dt.datetime(2020, 12, 4) # Hasta

for ticker in tickers:
    ticker_data[ticker] = pdr.get_data_yahoo(ticker, start = start, end = end)

print(ticker_data)

Upvotes: 0

Related Questions