Skip to content

The Yahoo Finance API is like a tool that lets people get information about the stock market. It’s like looking up how much your favorite toy costs or checking the score of a game. The Yahoo Finance API gives people real-time and past data about stocks, bonds, and other things people invest in. People can use the Yahoo Finance API to create websites or apps that help them understand the stock market better or even buy and sell stocks automatically. The Yahoo Finance API is used by a lot of popular financial websites and apps, so it’s really helpful for people who want to know more about the stock market.

Step 1. Create a new file inside the app folder.

Step 2. You will need access to the db_connect class and yfinance library. Make sure you add yfinance to your library repository and you freeze it to the requirements document.

To use the yfinance library on Render, you need to set the environment variable PYTHON_VERSION to 3.11.0 on the environment page of your Render configuration. By default, the version is set to 3.7, but you should set it to the version of Python you are using in your IDE. Make sure you use the correct three-digit version number, such as 3.7.2 or 3.11.0, depending on the version you are using.

From the command prompt:

pip install yfinance
pip freeze > requirements.txt

Then add this code to the top of your file.

from app.db_connect import connect
import yfinance as yf   
# note that yf is an alias

For the next steps you will be completing a four functions – put them in this order on the page.

Function 2. Get Ticker(s) Symbols from Ticker Table

# get the tickers from the ticker table
def get_tickers():
    conn = connect()
    with conn.cursor() as cur:
        cur.execute("SELECT ticker_symbol FROM ticker")
        return cur.fetchall()

Function 3. Get the Stock Price from Yahoo Finance

def get_stock_price(symbol):
   ticker_yahoo = yf.Ticker(symbol)
   data = ticker_yahoo.history()
   last_quote = (data.tail(1)['Close'].iloc[0])
   return last_quote

Function 4. Update the Ticker Price in Ticker Table

This is just like get_tickers, except this is an update sql statement.

def update_price(price, symbol):
    conn = connect()
    cur = conn.cursor()
    sql = f"UPDATE ticker SET current_price = {price} WHERE ticker_symbol LIKE '{symbol}'"
        print("Didn't work")

Function 1. Control Function Otherwise Known as Main in Most Python Apps

def post_ticker_prices():
    tickers = get_tickers()
    for ticker in tickers:
        symbol = ticker['ticker_symbol']
        price = get_stock_price(ticker['ticker_symbol'])

        update_price(price, symbol)

Build the Route

Then build the route for ticker_price_update. Make sure you add the update_ticker_price to the top of the so you have access to it.

from app.update_ticker_price import post_ticker_prices

Then add the route.

def ticker_price_update():
    return redirect(url_for('tickers'))

Create a Button

For you last step you call the function from route button. Add a button at the top of your tickers.html page. I put it next to the Add Ticker button, make sure it is inside the <h1> tags. Then call the url for ticker price update route.

<a role="button" class="btn btn-warning" aria-disabled="true" href="{{ url_for('ticker_price_update') }}">Update Ticker Price</a>

API Ticker Helpful Tips

If you get an error you might want to check your ticker symbol is correct. Go to and search for the ticker symbol to verify it is correct.

  1. For the code on your page, make sure you give the route name a good name that is not being used in other places (ie. update_ticker_price)
  2. When you import your code to use on route page, be sure to include the app in front of the code page (ie from app.ticker_page import *)
  3. When you update your database, you do not need to return anything, instead, you need to commit the changes you just made. Example: conn.commit()
  4. In the update sql statement, your integers and decimals do not need quotes, but your strings do. If you use single quotes on the outside, use double quotes on the inside and vice versa. Also for integers and decimals use = sign, but for strings use LIKE. Example sql = ‘blah blah WHERE user_id = 1 AND symbol LIKE “DIS” ‘ In python code this would look like: ‘WHERE user_id = {user_id} AND symbol LIKE “{symbol}”‘

Turn In

Submit the API assignment by uploading the code to GitHub and ensuring that it is successfully deployed on Render. Verify that the project is functioning correctly on Render before submitting it.

Once the project is ready, provide the URLs for both the Render deployment and GitHub repository to complete the submission.

Here is a working version of the application (user: admin pass: pass1)