Skip to content

TradePruf is a flexible and extensible backtesting framework that allows you to test trading strategies across multiple asset classes including stocks, cryptocurrencies, and ETFs. It provides detailed performance metrics, portfolio management capabilities, and interactive visualizations.

License

Notifications You must be signed in to change notification settings

arman-bd/tradepruf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TradePruf - Python Trading Backtester

πŸš€ Overview

TradePruf is a flexible and extensible backtesting framework that allows you to test trading strategies across multiple asset classes including stocks, cryptocurrencies, and ETFs. It provides detailed performance metrics, portfolio management capabilities, and interactive visualizations.

🚨 Disclaimer

This project was developed in a very short period with heavy use of AI assistance. While the code is functional and well-structured, it should be used for educational and research purposes only. Trading financial instruments involves substantial risk of loss. Past performance is not indicative of future results.

✨ Features

  • Multiple asset classes support (Stocks, Crypto, ETFs)
  • Built-in trading strategies:
    • Simple Moving Average (SMA) Crossover
    • Exponential Moving Average (EMA)
    • Relative Strength Index (RSI)
    • Moving Average Convergence Divergence (MACD)
    • Bollinger Bands
    • ATR Trailing Stop
  • Portfolio backtesting with custom allocation
  • Detailed performance metrics:
    • Total/Annual Returns
    • Sharpe Ratio
    • Maximum Drawdown
    • Win Rate
    • Trade Statistics
  • Interactive visualizations:
    • Equity curves
    • Drawdown analysis
    • Monthly returns heatmap
    • Asset allocation
  • Flexible data granularity (1m to 1wk)
  • Position sizing and risk management
  • Extensible strategy framework

πŸ›  Installation

  1. Clone the repository:
git clone https://github.com/arman-bd/tradepruf.git
cd tradepruf
  1. Create and activate a virtual environment using uv:
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
uv pip install -e ".[dev]"

πŸ“Š Quick Start

Single Asset Backtest

# Basic backtest
tradepruf backtest --symbol BTC-USD --asset-type crypto --strategy sma

# With visualization
tradepruf backtest \
    --symbol AAPL \
    --asset-type stock \
    --strategy macd \
    --start-date 2023-01-01 \
    --end-date 2024-01-01 \
    --charts interactive

Portfolio Backtest

  1. Create a portfolio configuration file:
{
    "initial_capital": 100000,
    "position_size": 0.1,
    "max_positions": 10,
    "start_date": "2023-01-01",
    "end_date": "2024-01-01",
    "interval": "1h",
    "assets": [
        {
            "symbol": "BTC-USD",
            "type": "crypto",
            "strategy": "sma",
            "params": {
                "short_window": 20,
                "long_window": 50
            }
        },
        {
            "symbol": "ETH-USD",
            "type": "crypto",
            "strategy": "rsi",
            "params": {
                "period": 14,
                "oversold": 30,
                "overbought": 70
            }
        },
        {
            "symbol": "AAPL",
            "type": "stock",
            "strategy": "macd"
        }
    ]
}
  1. Run the backtest:
tradepruf backtest-portfolio --portfolio portfolios/btc-eth-aapl.json --charts interactive --enhanced-analysis

Result

     Portfolio Configuration     
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ Parameter       ┃       Value ┃
┑━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩
β”‚ Initial Capital β”‚ $100,000.00 β”‚
β”‚ Position Size   β”‚        0.20 β”‚
β”‚ Max Positions   β”‚           5 β”‚
β”‚ Start Date      β”‚  2023-01-01 β”‚
β”‚ End Date        β”‚  2024-01-01 β”‚
β”‚ Interval        β”‚          1h β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      Assets                                      
┏━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Symbol  ┃ Type   ┃ Strategy ┃ Extra Params                                     ┃
┑━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
β”‚ BTC-USD β”‚ crypto β”‚ sma      β”‚ {"short_window": 20, "long_window": 50}          β”‚
β”‚ ETH-USD β”‚ crypto β”‚ rsi      β”‚ {"period": 14, "oversold": 30, "overbought": 70} β”‚
β”‚ AAPL    β”‚ stock  β”‚ macd     β”‚ "N/A"                                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
================================================================================
=== Starting portfolio backtest with 3 assets ===
================================================================================
Fetched 8676 bars for BTC-USD
Date range: 2023-01-01 00:00:00+00:00 to 2023-12-31 23:00:00+00:00
Fetched 8675 bars for ETH-USD
Date range: 2023-01-01 00:00:00+00:00 to 2023-12-31 23:00:00+00:00
Fetched 1742 bars for AAPL
Date range: 2023-01-03 09:30:00-05:00 to 2023-12-29 15:30:00-05:00
================================================================================
=== Generating signals ===
================================================================================
Generated SMA Crossover signals: Buy=111, Sell=110
Generated signals for BTC-USD using SMA Crossover
Generated RSI values: Min=2.86, Max=98.35
Generated RSI signals: Buy=117, Sell=116
Generated signals for ETH-USD using RSI Strategy
Generated MACD signals: Buy=71, Sell=71
Generated signals for AAPL using MACD Strategy

 Portfolio Backtest Results 
┏━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ Metric        ┃    Value ┃
┑━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
β”‚ Total Return  β”‚   24.12% β”‚
β”‚ Annual Return β”‚   24.20% β”‚
β”‚ Sharpe Ratio  β”‚     0.12 β”‚
β”‚ Max Drawdown  β”‚   38.12% β”‚
β”‚ Total Trades  β”‚      299 β”‚
β”‚ Win Rate      β”‚   47.83% β”‚
β”‚ Average Win   β”‚  $443.41 β”‚
β”‚ Average Loss  β”‚ $-253.45 β”‚
β”‚ Volatility    β”‚  177.96% β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

                      Asset Performance Summary                      
┏━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ Asset   ┃ Strategy      ┃ Type   ┃ Trades ┃        P&L ┃ Win Rate ┃
┑━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━┩
β”‚ BTC-USD β”‚ SMA Crossover β”‚ crypto β”‚    111 β”‚ $16,305.44 β”‚    35.1% β”‚
β”‚ ETH-USD β”‚ RSI Strategy  β”‚ crypto β”‚    117 β”‚  $5,112.17 β”‚    65.8% β”‚
β”‚ AAPL    β”‚ MACD Strategy β”‚ stock  β”‚     71 β”‚  $2,705.18 β”‚    38.0% β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Β» View Event Journal

Analysis Dashboard Analysis Dashboard contains some interactive charts and tables to help you understand the performance of your portfolio. Here are some screenshots of the dashboard:

Screenshot 1 Screenshot 2
Screenshot 3 Screenshot 4

Β» View Analysis Dashboard

πŸ“ˆ Custom Strategies

Create your own strategy by extending the base Strategy class:

from strategies.base import Strategy, SignalType

class MyCustomStrategy(Strategy):
    def __init__(self, param1: int = 10, param2: float = 2.0):
        super().__init__("My Custom Strategy")
        self.param1 = param1
        self.param2 = param2
    
    def generate_signals(self, data: pd.DataFrame) -> pd.Series:
        signals = pd.Series(SignalType.HOLD, index=data.index)
        # Your strategy logic here
        return signals

πŸ“Š Visualization Options

  • interactive: Display charts in browser
  • html: Save interactive charts as HTML files
  • png: Save static chart images
  • none: No visualization (default)

πŸ” Performance Metrics

  • Total Return
  • Annual Return
  • Sharpe Ratio
  • Maximum Drawdown
  • Win Rate
  • Average Win/Loss
  • Total Trades
  • Volatility
  • Per-asset Performance

πŸ“ Todo

  • Add more technical indicators
  • Implement portfolio optimization
  • Add transaction costs and slippage
  • Real-time trading capabilities
  • Enhanced risk management features
  • Machine learning strategy support
  • More visualization options
  • Performance optimization for large datasets

⚠️ Limitations

  • Limited to yfinance data
  • No support for options or futures
  • Basic position sizing
  • No margin trading simulation
  • Historical data limitations

πŸ“œ License

MIT License - see LICENSE for details

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ™ Acknowledgments

  • YFinance for market data
  • Plotly and Matplotlib for visualizations
  • Click for CLI interface
  • Rich for terminal formatting
  • All the AI tools that helped in development

⚠️ Risk Warning

This software is for educational purposes only. Trading financial instruments carries a high level of risk and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade, you should carefully consider your investment objectives, level of experience, and risk appetite.


Note: This project was developed with the assistance of AI tools. While efforts have been made to ensure accuracy and reliability, users should conduct their own testing and validation before using in any real-world applications.

About

TradePruf is a flexible and extensible backtesting framework that allows you to test trading strategies across multiple asset classes including stocks, cryptocurrencies, and ETFs. It provides detailed performance metrics, portfolio management capabilities, and interactive visualizations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages