38 — Python Power Tools

pandas-ta & yfinance#

PythonPractice 1 min read
◆ The PatternTechnical indicators + market data in one-liners

yfinance downloads free historical market data from Yahoo Finance. pandas-ta computes 130+ technical indicators as DataFrame operations. Together, they're the fastest way to go from idea to analysis for any market strategy.

// Price data with technical indicators
CategoryIndicatorspandas-ta function
TrendSMA, EMA, MACDta.sma(), ta.ema(), ta.macd()
MomentumRSI, Stochastic, CCIta.rsi(), ta.stoch(), ta.cci()
VolatilityBollinger, ATR, Keltnerta.bbands(), ta.atr()
VolumeOBV, VWAP, MFIta.obv(), ta.vwap(), ta.mfi()
Pattern bridge: These are the same indicators explored in the Markets → Indicators collection — RSI, MACD, Bollinger Bands and the rest — but here you can compute them yourself in Python and feed them into ML models as features.
Worked example

The same 20 closing prices, two “RSI(14)”s. Wilder’s smoothing gives 52.01; a plain 14-day average of gains and losses gives 63.46. Libraries and platforms differ in exactly these choices — and Wilder’s version also depends on how much history came before, because its average never fully forgets.

Where it misleads
  • Indicators computed on short histories have not “warmed up”; discard the first few dozen values.
  • Two libraries with the same indicator name can give different numbers; pin the implementation and test it against a known reference.
  • Downloaded prices may be adjusted or unadjusted for splits and dividends; indicators on mixed data are wrong.
In code
close = pd.Series([44.3, 44.1, 44.2, 43.6, 44.3, 44.8, 45.1, 45.4, 45.8, 46.1,
                   45.9, 46.2, 45.6, 46.3, 46.3, 46.0, 46.4, 46.2, 45.6, 46.2])
d = close.diff()
up, down = d.clip(lower=0), -d.clip(upper=0)
def rsi(u, v): return (100 - 100 / (1 + u / v)).iloc[-1]
wilder = rsi(up.ewm(alpha=1 / 14, adjust=False).mean(), down.ewm(alpha=1 / 14, adjust=False).mean())
simple = rsi(up.rolling(14).mean(), down.rolling(14).mean())   # "RSI" with a plain average
Python, in your browser — edit the code and run it again

Assumes import numpy as np and import pandas as pd (and SciPy where imported). Each snippet simulates or makes up its own data, as the comments say.

The same idea with a library
# pandas-ta + yfinance — quick market analysis
import yfinance as yf
import pandas_ta as ta

# Download data
df = yf.download('AAPL', start='2023-01-01')

# Add indicators as columns
df.ta.sma(length=20, append=True)
df.ta.rsi(length=14, append=True)
df.ta.macd(append=True)
df.ta.bbands(length=20, append=True)

# Or run a full strategy
df.ta.strategy('all')  # adds 130+ indicators

# Quick signal
df['signal'] = (df['RSI_14'] < 30).astype(int)
print(df[['Close', 'SMA_20', 'RSI_14']].tail())
Sources
  • J. W. Wilder Jr., New Concepts in Technical Trading Systems, Trend Research, 1978
  • pandas-ta documentation, github.com/twopirllc/pandas-ta
  • Technical Analysis from A to Z (2nd ed.), S. B. Achelis, McGraw-Hill, 2000

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 1
← Previous
Optuna
Open in the full reader, with the topic sidebar