From yfinance to pypsx: backtest PSX instead of US stocks
You reached for yfinance because the data was there. Here's the one-for-one migration to backtest the market you actually live in.
If you learned to backtest in Python, you almost certainly learned on yfinance
and US tickers, because that’s where the free data was. pypsx gives you the
same shape for PSX. Here’s the swap.
Downloading history
# yfinance
import yfinance as yf
df = yf.download("AAPL", period="10y")
# pypsx
import pypsx
df = pypsx.download("OGDC", period="10y")
Same period shorthands (1y, 5y, 10y, max), same pandas DataFrame with
Open, High, Low, Close, Volume.
Indicators
pypsx.analysis ships the indicators you’d otherwise pull from ta or roll by
hand:
import pypsx.analysis as pa
rsi = pa.rsi(df, window=14)
upper, mid, lower = pa.bollinger_bands(df)
summary = pa.performance_summary(df) # sharpe, sortino, max drawdown, win rate
The part yfinance never gave you
yfinance has no execution layer, you could test an idea but never run it. With
pytrader you go from backtest to paper account with the same symbols:
from pytrader import TradingClient
client = TradingClient.from_env(paper=True)
client.place_manual_order(symbol="OGDC", side="BUY", quantity=100, order_type="MARKET")
Free market data, free sandbox. You stop backtesting a market you don’t trade and start testing the one you do.