Skip to content

Enriching stock market data using Open AI API

The Nasdaq-100 is a stock market index made up of 101 equity securities issued by 100 of the largest non-financial companies listed on the Nasdaq stock exchange. It helps investors compare stock prices with previous prices to determine market performance.

In this project you are provided with two CSV files containing Nasdaq-100 stock information:

  • nasdaq100.csv: contains information about companies in the index such as symbol, name, etc.
  • nasdaq100_price_change.csv: contains price changes per stock across periods including (but not limited to) one day, five days, one month, six months, one year, etc.

As an AI developer, you will leverage the OpenAI API to classify companies into sectors and produce a summary of sector and company performance for this year.

CSV with Nasdaq-100 stock data

In this project, you have available two CSV files nasdaq100.csv and nasdaq100_price_change.csv.

nasdaq100.csv

symbol,name,headQuarter,dateFirstAdded,cik,founded AAPL,Apple Inc.,"Cupertino, CA",,0000320193,1976-04-01 ABNB,Airbnb,"San Francisco, CA",,0001559720,2008-08-01 ADBE,Adobe Inc.,"San Jose, CA",,0000796343,1982-12-01 ADI,Analog Devices,"Wilmington, MA",,0000006281,1965-01-01 ...

nasdaq100_price_change.csv

symbol,1D,5D,1M,3M,6M,ytd,1Y,3Y,5Y,10Y,max AAPL,-1.7254,-8.30086,-6.20411,3.042,15.64824,42.99992,8.47941,60.96299,245.42031,976.99441,139245.53954 ABNB,2.1617,-2.21919,9.88336,19.43286,19.64241,68.66902,23.64013,-1.04347,-1.04347,-1.04347,-1.04347 ADBE,0.5409,-1.77817,9.16191,52.0465,38.01522,57.22723,21.96206,17.83037,109.05718,1024.69214,251030.66399 ADI,0.9291,-4.03352,2.58486,3.65887,5.01602,17.02062,8.09735,63.42847,92.81874,286.77518,26012.63736 ...
# Start your code here!
import os
import pandas as pd
import openai

# Continue coding here
openai.api_key = os.environ["OPENAI_API_KEY"]
nasdaq100 = pd.read_csv("nasdaq100.csv")
nasdaq100
# Use as many cells as you like
#Create a dataframe of the nasdaq100_price_change dataset.
#Extract only the symbol and ytd columns from the nasdaq100_price_change and store it in nasdaq_ytd.
#Merge the nasdaq100 and nasdaq_ytd dataframes by the symbol column, assign a suffix.
nasdaq100_price_change = pd.read_csv("nasdaq100_price_change.csv")
nasdaq_ytd = nasdaq100_price_change.loc[:,["symbol", "ytd"]]
nasdaq100 = nasdaq100.merge(nasdaq_ytd, on = "symbol", suffixes= ("", "_y"))
#Drop any repeating ytd column
nasdaq100.drop([col for col in nasdaq100.columns if col.endswith("_x") or col.endswith("_y")], axis=1, inplace=True)
nasdaq100
#Create a for loop that iterates over all the instances of the "symbol" column in the nasdaq100 dataframe.
for company in nasdaq100["symbol"]:
    #The prompt classifies each instance in the symbol column at the nasdaq100 dataframe according to it's sector.
    prompt = f'''Classify company {company} into one of the following sectors. Answer only with the sector name: Technology, Consumer Cyclical, Industrials, Utilities, Healthcare, Communication, Energy, Consumer Defensive, Real Estate or Financial.'''
    #Create the completion and pass the prompt into the content at the dictionarie.
    response = openai.ChatCompletion.create(model = "gpt-3.5-turbo", 
                                            messages =[{"role":"user","content":prompt}], temperature = 0.5)
    #Extract the responses
    sector = response.choices[0].message.content
    #Create a new column called "sector" in which passes the sector response in each comapny instance
    nasdaq100.loc[nasdaq100["symbol"] == company, "sector"] = sector
nasdaq100
#Use the Open API to analyze the nasdaq100 dataset
prompt = f'''Provide summary information about Nasdaq-100 {nasdaq100} stock performance YTD, recommending the three best sectors and three or more companiees per sector.'''
response = openai.ChatCompletion.create(model = "gpt-3.5-turbo",
                                       messages = [{"role":"user", "content":prompt}],
                                       temperature = 0.0)
stock_recommendations = response.choices[0].message.content
print(stock_recommendations)