How To Use yfinance/Python To Get Average Daily Range

Guide on How-To Get the Average Daily Range using yfinance API through python.

PYTHON

1/28/20251 min read

Step 1: Install yfinance
  1. Go to your console and install yfinance with pip install

  1. Import yfinance into your python file

import yfinance as yf

Step 2: Get Ticker Data using yfinance

> pip install yfinance

  1. Define Ticker (NQ -> NQ=F / ES -> ES=F)

ticker = "ES=F"

  1. Get the daily range

data = yf.download(ticker, start='2024-01-01', end='2025-01-01', interval='1d')

  1. Import pyplot to use for creating graphs

import matplotlib.pyplot as plt

  1. Download the daily data over the full year to variable named 'data'

data['Daily_Range'] = data['High'] - data['Low']

  1. Get average range and print / plot it

average_daily_range = data['Daily_Range'].mean()

print(f"Average Daily Range: {average_daily_range:.2f}")

plt.figure(figsize=(12, 8))

plt.plot(data.index, data['Daily_Range'], label='Daily Range', alpha=0.6, color='yellow')

plt.axhline(y=average_daily_range, color='red', linestyle='--', label=f'Avg Daily Range: {average_daily_range:.2f}')

plt.xlabel('Date')

plt.ylabel('Range')

plt.title(f'{ticker} Daily Range')

plt.xticks(rotation=45)

plt.legend()

plt.tight_layout()

plt.show()

Step 3: Run Code

Video Tutorial

Video on how to get these statistics in python then use it to code an indicator in TradingView