How to pass a collection of stocks through your Python code

Options for passing around stock information within Python code:

  1. Raw dictionary
    Creating a dictionary with stocks as keys and stock characteristic as values, passing the dictionary around as a function input parameter or in a pickle file.
  2. List of lists
    Doing the same as above with stock saved as a list of characteristics and assigned to a variable of which the name is a reference to a stock. Then create a list of lists as the collection of stocks and pass it around as above.
  3. Dictionary of class objects
    Creating a class for stocks, creating a class instance for every stock and storing every stock characteristic as a class instance attribute. Then placing the stock class object in a dictionary and moving the dictionary around as above.

Analysis:

Option 1 is possible, but it's more cumbersome as shown in this video.

Option 2 feels natural. It is the same as having documents on your desk that you might want to use later. You place the documents in a pile (list of lists) and browse through the pile from up to down to get the right document. However, a binary search method is always faster as shown in this video.

Option 3 is the most convenient way for passing around the information. You can store the dictionary of stock class objects in a pickle file, load the pickle file where ever you want and search for stock x within the dictionary which is exponentially faster than searching in a list as shown in this video. If you found stock x within the dictionary then you can browse through its characteristic (current price, full name, size, etc) by using the different class attributes of that stock object class instance.

Options for referring to the stock within the collection:

  1. Using the stock name
    Disadvantage: Stock names can have different spellings on different websites.
  2. Using the stock symbol
    Disadvantage: Different websites use different symbols.
  3. Creating a reference number
    Disadvantage: You create an identification number for every stock class instance and refer to the identity number before you can make use of an attribute of the stock class instance.

Choosing 2 times for option 3 leads to the following code:

import randomclass StockObjectCreation:
def __init__(self, stock_id, company_name, symbol, price, change):
self.si = stock_id
self.cn = company_name
self.sb = symbol
self.pr = price
self.ch = change


stocks = {}
for index, row in dataframe_of_stocks.iterrows():
s = str(random.random())
s = s[-15:] # take last 15 random figures, so remove the comma
stock = StockObjectCreation(s, row[1], row[2], row[3], row[4])
stocks[s] = stock
with open('stock_data.pkl', 'wb') as output:
pickle.dump(stocks, output, pickle.HIGHEST_PROTOCOL)

print(stocks)

The output is a dictionary with as keys random numbers and as values the stock class object. The output looks something like this:

Output:
{'074518663003896': <__main__.StockObjectCreation object at 0x7fec5a15f940>, '579310584644404': <__main__.StockObjectCreation object at 0x7fec5a52c460>, '121838623686386': <__main__.StockObjectCreation object at 0x7fec5a52cdf0>}

The stocks are saved within the dictionary as a class object of which the attributes can be called through stock.si, stock.cn, stock.sb, etc. While all the different stocks can be called with the same name reference the stock objects are different objects in memory as you can see in the human-unfriendly reference Python regurgitates: “object at 0x7fec5a15f940”. The information can be retrieved in other places within the code in the following way:

with open('stock_data.pkl', 'rb') as input:
list_stocks = pickle.load(input)

stock = list_stocks["383119374356758"]
stock_name = stock.cn
print(stock_name)
Output:
AT&T