portwine - a clean, elegant portfolio backtester
pip install portwine
https://stuartfarmer.github.io/portwine/
Portfolio construction, optimization, and backtesting can be a complicated web of data wrangling, signal generation, lookahead bias reduction, and parameter tuning.
But with portwine, strategies are clear and written in an 'online' fashion that removes most of the complexity that comes with backtesting, analyzing, and deploying your trading strategies.
Simple Strategies
Strategies are only given the last day of prices to make their determinations and allocate weights. This allows them to be completely encapsulated and portable.
class SimpleMomentumStrategy(StrategyBase):
"""
A simple momentum strategy that:
1. Calculates N-day momentum for each ticker
2. Invests in the top performing ticker
3. Rebalances weekly (every Friday)
This demonstrates a step-based strategy implementation in a concise, easy-to-understand way.
"""
def __init__(self, tickers, lookback_days=10):
"""
Parameters
----------
tickers : list
List of ticker symbols to consider for investment
lookback_days : int
Number of days to use for momentum calculation
"""
super().__init__(tickers)
self.lookback_days = lookback_days
self.price_history = {ticker: [] for ticker in tickers}
self.current_signals = {ticker: 0.0 for ticker in tickers}
self.dates = []
def is_friday(self, date):
"""Check if given date is a Friday (weekday 4)"""
return date.weekday() == 4
def calculate_momentum(self, ticker):
"""Calculate simple price momentum over lookback period"""
prices = self.price_history[ticker]
# Need at least lookback_days+1 data points
if len(prices) <= self.lookback_days:
return -999.0
# Get starting and ending prices for momentum calculation
start_price = prices[-self.lookback_days-1]
end_price = prices[-1]
# Check for valid prices
if start_price is None or end_price is None or start_price <= 0:
return -999.0
# Return simple momentum (end/start - 1)
return end_price / start_price - 1.0
def step(self, current_date, daily_data):
"""
Process daily data and determine allocations
"""
# Track dates for rebalancing logic
self.dates.append(current_date)
# Update price history for each ticker
for ticker in self.tickers:
price = None
if daily_data.get(ticker) is not None:
price = daily_data[ticker].get('close', None)
# Forward fill missing data
if price is None and len(self.price_history[ticker]) > 0:
price = self.price_history[ticker][-1]
self.price_history[ticker].append(price)
# Only rebalance on Fridays
if self.is_friday(current_date):
# Calculate momentum for each ticker
momentum_scores = {}
for ticker in self.tickers:
momentum_scores[ticker] = self.calculate_momentum(ticker)
# Find best performing ticker
best_ticker = max(momentum_scores.items(),
key=lambda x: x[1] if x[1] != -999.0 else -float('inf'))[0]
# Reset all allocations to zero
self.current_signals = {ticker: 0.0 for ticker in self.tickers}
# Allocate 100% to best performer if we have valid momentum
if momentum_scores[best_ticker] != -999.0:
self.current_signals[best_ticker] = 1.0
# Return current allocations
return self.current_signals.copy()
Breezy Backtesting
Backtesting strategies is a breeze, as well. Simply tell the backtester where your data is located with a data loader manager and give it a strategy. You get results immediately.
universe = ['MTUM', 'VTV', 'VUG', 'IJR', 'MDY']
strategy = SimpleMomentumStrategy(tickers=universe, lookback_days=10)
data_loader = EODHDMarketDataLoader(data_path='../../../Developer/Data/EODHD/us_sorted/US/')
backtester = Backtester(market_data_loader=data_loader)
results = backtester.run_backtest(strategy, benchmark_ticker='SPY')
Streamlined Data
Managing data can be a massive pain. But as long as you have your daily flat files from EODHD or Polygon saved in a directory, the data loaders will manage the rest. You don't have to worry about anything except writing code.
Effortless Analysis
After running a strategy through the backtester, put it through an array of analyzers that are simple, visual, and clear. You can easily add your own analyzers to discover anything you need to know about your portfolio's performance, risk management, volatility, etc.
Check out what comes out of the box:
Equity Drawdown Analysis
EquityDrawdownAnalyzer().plot(results)
Monte Carlo Analysis
MonteCarloAnalyzer().plot(results)
Seasonality Analysis
EquityDrawdownAnalyzer().plot(results)
With more on the way!
Docs
Metadata
Release files for portwine 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| portwine-0.1.5.tar.gz | 180.6 kB | Without provenance | Details for portwine-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| portwine-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for portwine-0.1.5-py3-none-any.whl |
Total release size: 390.9 kB
Release files / portwine-0.1.5.tar.gz
| Download URL | portwine-0.1.5.tar.gz |
|---|---|
| Size | 180.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5a128fc99b2fbd6f276066d2121e46bbb87d733bc432de6c3755ca9a1724ddfc
|
|
BLAKE2b-256 checksum How to use checksums |
206f93fcbfdeeaeb23d2334889ae7afa16b6bc0d9cc3879388ff6a2ba9d474ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.12.10 Darwin/24.6.0
|
Release files / portwine-0.1.5-py3-none-any.whl
| Download URL | portwine-0.1.5-py3-none-any.whl |
|---|---|
| Size | 210.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c7d4ef155914f3ffb4a3c283ba23653a2e3845f228f5d8b1cfb824e79bb6cae
|
|
BLAKE2b-256 checksum How to use checksums |
8225f19247e83b95ab2bcf010d90f332990fffa3660b5937f5c61080a05e4d7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.4 CPython/3.12.10 Darwin/24.6.0
|
Security for portwine 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.