Skip to content
Project: Enriching Stock Market Data using the OpenAI API
  • AI Chat
  • Code
  • Report
  • 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.


    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 ...


    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 ...

    Before you start

    In order to complete the project you will need to create a developer account with OpenAI and store your API key as an environment variable. Instructions for these steps are outlined below.

    Create a developer account with OpenAI

    1. Go to the API signup page.

    2. Create your account (you'll need to provide your email address and your phone number).

    1. Go to the API keys page.

    2. Create a new secret key.

    1. Take a copy of it. (If you lose it, delete the key and create a new one.)

    Add a payment method

    OpenAI sometimes provides free credits for the API, but it's not clear if that is worldwide or what the conditions are. You may need to add debit/credit card details.

    The API costs $0.002 / 1000 tokens for GPT-3.5-turbo. 1000 tokens is about 750 words. This project should cost less than 1 US cents (but if you rerun tasks, you will be charged every time).

    1. Go to the Payment Methods page.

    2. Click Add payment method.

    1. Fill in your card details.

    Add an environmental variable with your OpenAI key

    1. In Workspace, click on "Environment," in the left sidebar.

    2. Click on the plus button next to "Environment variables" to add environment variables.

    3. In the "Name" field, type "OPENAI_API_KEY". In the "Value" field, paste in your secret key.

    1. Click "Create", then you'll see the following pop-up window. Click "Connect," then wait 5-10 seconds for the kernel to restart, or restart it manually in the Run menu.
    # Start your code here!
    import os
    import pandas as pd
    from openai import OpenAI
    # Instantiate an API client
    client = OpenAI(api_key=os.environ["OPEN_AI"])
    # Read in the two datasets
    nasdaq100 = pd.read_csv("nasdaq100.csv")
    price_change = pd.read_csv("nasdaq100_price_change.csv")
    # Add symbol into nasdaq100
    nasdaq100 = nasdaq100.merge(price_change[["symbol", "ytd"]], on="symbol", how="inner")
    # Preview the combined dataset
    # Loop through the NASDAQ companies
    for company in nasdaq100["symbol"]:
        # Create a prompt to enrich nasdaq100 using OpenAI
        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, Financial.
        # Create a request to the completions endpoint
        response =
            messages=[{ "role": "user", "content": prompt}],
        # Store the output as a variable called sector
        sector = response.choices[0].message.content
        # Add the sector for the corresponding company
        nasdaq100.loc[nasdaq100["symbol"] == company, "Sector"] = sector
    # Count the number of sectors
    # Prompt to get stock recommendations
    prompt = f'''Provide summary information about Nasdaq-100 stock performance year to date (YTD), recommending the three best sectors               and three or more companies per sector.
                Company data: {nasdaq100} 
    # Get the model response
    response =
            messages=[{ "role": "user", "content": prompt}],
    # Store the output as a variable and print the recommendations
    stock_recommendations = response.choices[0].message.content