PortfolioTesteR is an R package designed for students and individual investors to test investment strategies using clear, readable code. Unlike complex quantitative libraries, PortfolioTesteR uses intuitive function names that read like plain English, making strategy development accessible to everyone.
- English-like syntax: Functions like
filter_top_n()
,weight_equally()
, andrun_backtest()
are self-explanatory - Educational focus: Perfect for learning quantitative investing without getting lost in code complexity
- Comprehensive toolkit: 70+ functions covering everything from technical indicators to risk analysis
- Fast backtesting: Optimized for testing strategies on large stock universes
- Ready-to-use data: Includes sample price data to start testing immediately
- Realistic results: Handles warmup periods, transaction tracking, and proper return calculations
Install from GitHub:
# install.packages("devtools")
devtools::install_github("alb3rtazzo/PortfolioTesteR")
library(PortfolioTesteR)
# Load sample data
data(sample_prices_weekly)
# Calculate 12-week momentum
momentum <- calc_momentum(sample_prices_weekly, lookback = 12)
# Select top 10 momentum stocks
top_momentum <- filter_top_n(momentum, n = 10)
# Weight selected stocks equally
weights <- weight_equally(top_momentum)
# Run backtest
result <- run_backtest(
prices = sample_prices_weekly,
weights = weights,
initial_capital = 100000,
name = "Simple Momentum"
)
# View results
print(result)
summary(result)
plot(result)
sql_adapter()
- Load from SQLite databasescsv_adapter()
- Load from CSV filesload_mixed_symbols()
- Combine different data sources
calc_momentum()
- Price momentumcalc_rsi()
- Relative Strength Indexcalc_moving_average()
- Simple moving averagescalc_stochastic_d()
- Stochastic oscillatorcalc_rolling_volatility()
- Rolling historical volatility (std / range / MAD / abs-return)calc_distance()
- Distance between price series
filter_top_n()
- Select top/bottom N stocksfilter_above()
- Select above thresholdfilter_below()
- Select below thresholdfilter_between()
- Select within rangecombine_filters()
- Combine multiple conditionsapply_regime()
- Filter based on market conditions
weight_equally()
- Equal weight (1/N)weight_by_signal()
- Weight by signal strengthweight_by_rank()
- Weight by rankingweight_by_volatility()
- Inverse volatility weightingweight_by_hrp()
- Hierarchical Risk Parity
run_backtest()
- Execute strategy backtestanalyze_performance()
- Detailed performance metricsprint()
- Display backtest resultssummary()
- Show detailed statisticsplot()
- Visualize performance
The package includes three datasets for immediate testing:
sample_prices_weekly
- Weekly prices for 20 stocks (2017-2020)sample_prices_daily
- Daily prices for detailed analysissample_sp500_sectors
- Sector classifications
Load them with:
data(sample_prices_weekly)
data(sample_prices_daily)
data(sample_sp500_sectors)
Every function includes detailed documentation with examples:
?run_backtest
?calc_momentum
?filter_top_n
This package is for educational and research purposes only. Past performance does not guarantee future results. Always conduct your own research before making investment decisions. The author is not responsible for any financial losses incurred from using this software.
Alberto Pallotta
MIT License
We welcome contributions! Please feel free to submit issues and pull requests on GitHub.