Python for Stocks | Part 1: Pulling historical data from Yahoo Finance

← Go home

TLDR;

Intentions and Outcomes

Objective

  • pull stock data over a period of time
  • Libraries

  • yfinance
  • datetime
  • pandas
  • pandas_datareader
  • Results

  • prompt to input a ticker symbol
  • prompt to input a starting year
  • defined variables and constants
  • pulled data from yfinance
  • store data in a DataFrame
  • Full script
    # -> libraries
    import datetime as dt
    import pandas as pd
    import yfinance as yf
    from pandas_datareader import data as pdr
    
    
    # -> setup
    yf.pdr_override()  # workaround for yfinance
    
    # -> asking for the inputs
    stock = input("Enter a stock ticker symbol: ")
    starting_year = input("Enter starting year: ")
    print("stock: $" +stock) 
    print("starting_year: " +starting_year)
    
    # -> definitions
    start_year = int(starting_year)
    START_MONTH = 1
    START_DAY = 1
    
    start = dt.datetime(start_year, START_MONTH, START_DAY)
    now = dt.datetime.now()
    
    # -> setting up dataframe
    df = pdr.get_data_yahoo(stock, start, now)
    df
    


    🚨

    This series, I go through learning Python for the purpose of applying it for stock market analysis. Follow along this walkthrough and try it yourself!

    Part 1: Pulling the ticker symbol and historical data

    Let’s begin by creating a python file named “stock_picker” in one of two ways, you can choose:

    Option 1: Create a new file named: stock_picker.py

    To run the code in this file, in your terminal you type in: python stock_picker.py

    Option 2: Create a new file named: stock_picker.ipynb

    To run this code in a Jupyter Notebook which allows you to run python code in sections at a time.


    Next, let’s import the libraries we will be using for to grab stock data from Yahoo Finance and store it in a DataFrame.

    The syntax is pretty much: import [library name] as [your shorthand name for it]

    # -> libraries
    import datetime as dt
    import pandas as pd
    import yfinance as yf
    from pandas_datareader import data as pd

    To install these libraries locally, run these commands in your terminal

    pip install datetime
    pip install pandas
    pip install yfinance
    pip install pandas-datareader


    The setup we want to achieve here is to pull data from Yahoo Finance (where we will need to provide three parameters: stock ticker symbol, starting date, ending date) and throw the data into a DataFrame. Let’s start by asking for the inputs: stock and starting_year

    # -> setup
    yf.pdr_override()  # workaround for yfinance
    
    # -> asking for the inputs
    stock = input("Enter a stock ticker symbol: ")
    starting_year = input("Enter starting year: ")
    print("stock: $" +stock) 
    print("starting_year: " +starting_year)

    When we run this part of the script, it should prompt us to enter a ticker symbol.

    In this example, we enter: TSLA

    Then it should prompt us to enter a starting year:

    In this example, we enter: 2020

    The result should print in your terminal:

    stock: $TSLA
    starting_year: 2020


    Next, we will define our variables and constants that will be passed to Yahoo Finance. This is where we will be using our imported libraries declared earlier.

    datetime = for assisting with date and time related data

    pandas / pandas_datareader = for extracting data from the Internet into pandas DataFrame

    # -> definitions
    start_year = int(starting_year)
    START_MONTH = 1
    START_DAY = 1
    
    start = dt.datetime(start_year, START_MONTH, START_DAY)
    now = dt.datetime.now()
    
    # -> setting up dataframe
    df = pdr.get_data_yahoo(stock, start, now)
    df

    When we run this code with our TSLA and 2020 input, we should end up with a DataFrame that looks something like this:

    As we can see here, the DataFrame has returned us TSLA stock performance from 2020-01-01 to “now” which is today’s date when I ran the script. Yahoo Finance has provided us the stock’s price for: Open, High, Low, Close, Adjusted Close, and Volume traded.

    From here, when we call our df we can play with this data to run some maths against and make some charts.

    If you managed to get to this point, you should now be able to grab stock data in python for yourself. This is the starting point from where we will build upon. Stay tuned!