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
Go to your console and install yfinance with pip install
Import yfinance into your python file
import yfinance as yf
Step 2: Get Ticker Data using yfinance
> pip install yfinance
Define Ticker (NQ -> NQ=F / ES -> ES=F)
ticker = "ES=F"
Get the daily range
data = yf.download(ticker, start='2024-01-01', end='2025-01-01', interval='1d')
Import pyplot to use for creating graphs
import matplotlib.pyplot as plt
Download the daily data over the full year to variable named 'data'
data['Daily_Range'] = data['High'] - data['Low']
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
