0

Context: I'm using an API to retrieve stock data and I've created a function that loops through the elements of a list and retrieves the data for each stock in the tickerList.

Main goal: My main goal with this is so I can create individual variables for each ticker in the tickerList. Would that be possible? For example, after passing the for loop, could I create variables like historyAAPL, historyHOOD, historyTSLA... (i.e. for each element in the tickerList) and retrieve the data individually? I want to do this so that, in the future, I can send the data of each variable to a MySQL database (where each data from each stock is a table - historyAAPL would be it's own table, historyHOOD would be another table, etc). But first I would like to create the variables with the data (I've thought of using classes, but not sure if it would work for this)

Problem: I can't seem to figure out how to add the suffix on the variable history so that each suffix equals the elements on the tickerList. It seems that executing the function moduleApi will only retrieve the last element on the list (MSFT). I've also tried using locals(), but with no success as well

Any help is welcome as I'm a beginner. Also, is it a good idea to save the data as data frame to then send to a database on MySQL? Or would you recommend other format? I've also thought of using dictionaries where each key is the stock name and the values are the open, close, high, low, date values, but I've read that this format isn't ideal if we then want to push the data onto a table in MySQL

Thank you

import pandas as pd
import numpy as np
import yfinance as yf

tickerList = ("AAPL", "HOOD", "TSLA", "GOOG", "MSFT")
history = ()


def moduleApi(setTicker, setPeriod, setInterval):
    for setTicker in tickerList:
        stock = yf.Ticker(setTicker)
        history[setTicker] = pd.DataFrame(stock.history(
            period=setPeriod,
            interval=setInterval,
            group_by='ticker',
            proxy=None))

    return history[setTicker]


moduleApi(tickerList, 'max', '1d')

historyAAPL #this should retrieve the data for the ticker AAPL
  • you probably want to use a dictionary for this setting the keys as the names like 'AAPL' etc – Chris Doyle Sep 16 '21 at 21:46
  • Hello, Chris, thank you for your answer! I've added something at the end of the text saying that I've read that using dictionaries aren't recommended if we want to push that data onto a MySQL table in the future. Would that be correct? This is the source - https://newbedev.com/python-sqlite3-insert-into-table-value-dictionary-goes-here –  Sep 16 '21 at 21:47
  • I dont think thats true. Have a look at this answer,https://stackoverflow.com/a/68335209/1212401. – Chris Doyle Sep 16 '21 at 21:54
  • Thank you for that, Chris! I'll try that out for sure! :) –  Sep 16 '21 at 21:58
  • I hope by "try that out" you mean "use a dictionary". – mkrieger1 Sep 16 '21 at 22:00
  • Yes, mkrieger1, it seems that the dictionary is the way to go :) Thank you for the input! –  Sep 16 '21 at 22:03
  • Generally when you are thinking of using variables with different names to hold the same kind of data, what you really should use is a dictionary, which effectively gives you a collection of custom-named variables in a single container. However, in your case, you probably should think a little further about data normalization for storage. When you store this data in a database, it should really all go into a single table, with the ticker ID as one of the columns. So you may want to adopt that approach in pandas too -- one big table, with ticker ID as one of the columns. – Matthias Fripp Sep 16 '21 at 22:06
  • Thank you for your help, @MatthiasFripp! You're right, at first I thought it would be best to create individual tables for each stock, but your suggestion seems to be the appropriate approach for this! Would it then be possible to have a column with the stock ID (such as AAPL) and then a sub-column that hold the values for open, close, high and low prices as well as the date? –  Sep 16 '21 at 22:10
  • It sounds like you would do best to create a table with these columns: stock ID, date, open, close, high, low. The stock ID and the date are your index or key values: they identify each data point. Then the open, close, high and low are the data you are storing. As needed, you can retrieve rows for particular stock IDs and/or dates. You don't need subrows (those don't really exist in a SQL table) -- just a single big table with these columns. Then you select the rows you want for any particular analysis. – Matthias Fripp Sep 16 '21 at 22:15
  • Awesome, will do! Thank you once again @MatthiasFripp! –  Sep 16 '21 at 22:17

1 Answers1

0

I don't see a reason to not use a dictionary, but you can dynamically add local or global variables by adding them to locals() or globals() as you rightfully already found out.

It can look like this:

values = {}
for ticker in tickerList:
    values[f'history{ticker}'] = history[ticker]
locals().update(values)

# you can now access historyTSLA

You might want to create a separate namespace for this

from types import SimpleNamespace
...
tickers = SimpleNamespace(**values)

# you can now access your variables
# as attributes on the tickers namespace 
# tickers.historyTSLA

For completeness, if you're deciding to use a dictionary you can easily and dynamically retrieve key and value to add them to your database, that's not possible if you have them as variables or as attributes on a namespace.

values.items() # returns list of tuples

for ticker, history in values.items():
   ...
ScientiaEtVeritas
  • 5,158
  • 4
  • 41
  • 59
  • Thank you for your answer! I thought using a dictionary for this purpose wasn't recommended, but I'll try it out now! I wasn't aware that we couldn't add variables to the database, so I guess the dictionary is the way to go for this situation. Again, I appreciate your help :) –  Sep 16 '21 at 22:02