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.
filter_top_n()
, weight_equally()
, and
run_backtest()
are self-explanatoryInstall from GitHub:
# install.packages("devtools")
::install_github("alb3rtazzo/PortfolioTesteR") devtools
library(PortfolioTesteR)
# Load sample data
data(sample_prices_weekly)
# Calculate 12-week momentum
<- calc_momentum(sample_prices_weekly, lookback = 12)
momentum
# Select top 10 momentum stocks
<- filter_top_n(momentum, n = 10)
top_momentum
# Weight selected stocks equally
<- weight_equally(top_momentum)
weights
# Run backtest
<- run_backtest(
result 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
sourcescalc_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 seriesfilter_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 conditionsweight_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 Parityrun_backtest()
- Execute strategy backtestanalyze_performance()
- Detailed performance
metricsprint()
- Display backtest resultssummary()
- Show detailed statisticsplot()
- Visualize performanceThe package includes three datasets for immediate testing: -
sample_prices_weekly
- Weekly prices for 20 stocks
(2017-2020) - sample_prices_daily
- Daily prices for
detailed analysis - sample_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.