Skip to main content

A backtesting framework for machine learning and financial trading strategies.

Project description

Financial Backtesting

A Python library for backtesting financial trading strategies using machine learning models and custom signals. This library provides a robust framework for simulating trades, analyzing performance, and visualizing results.


Features

  • Dynamic Position Sizing: Supports risk-based position sizing for better capital management.
  • Customizable Parameters: Configure stop-loss, take-profit, leverage, transaction fees, and more.
  • Comprehensive Metrics: Provides detailed performance metrics such as win rate, profit factor, drawdown, and total return.
  • Visualization: Generates plots for equity curves, drawdowns, and trade signals.
  • Trade Logging: Tracks and summarizes closed positions for detailed analysis.

Installation

Install the package using pip:

pip install financial-backtesting

Usage

1. Import the Library

from backtesting import Backtest

2. Prepare Your Data

Ensure your data is a pandas.DataFrame with at least the following columns:

  • Price Column: The column containing price data (e.g., close).
  • Date Column: The column containing date/time data (e.g., date).
  • Signals: A list or array of buy/sell signals.

3. Run a Backtest

# Example DataFrame
import pandas as pd
df = pd.read_csv("your_data.csv")

# Example signals (0 = Buy, 2 = Sell)
signals = [0, 2, 0, 2, ...]

# Initialize the backtest
backtest = Backtest(
    data=df,
    signals=signals,
    price_column="close",
    date_column="date",
    BUY_SIGNAL=0,
    SELL_SIGNAL=2
)

# Run the backtest
results = backtest.run(
    stop_loss=0.01,  # 1% stop loss
    take_profit=0.03,  # 3% take profit
    initial_capital=10000,
    leverage=2,
    quantity=1,
    transaction_fee_rate=0.001,
    dynamic_position_size=True,
    risk_per_trade=0.02,
    max_slippage=0.001,
    plot=True
)

# Print results
print(results)

API Reference

Backtest Class

Initialization

Backtest(data, signals, price_column="close", date_column="date", BUY_SIGNAL=0, SELL_SIGNAL=2)
  • data: pd.DataFrame - The input data containing price and date columns.
  • signals: List - A list of buy/sell signals.
  • price_column: str - The column name for price data.
  • date_column: str - The column name for date/time data.
  • BUY_SIGNAL: int - The value representing a buy signal.
  • SELL_SIGNAL: int - The value representing a sell signal.

Methods

  1. run() Runs the backtest with the specified parameters.

    run(
        stop_loss: float,
        take_profit: float,
        initial_capital: float = 10000,
        leverage: float = 1,
        quantity: int = 1,
        transaction_fee_rate: float = 0.002,
        dynamic_position_size: bool = False,
        risk_per_trade: float = 0.01,
        max_slippage: float = 0.00,
        start_date: str = None,
        end_date: str = None,
        plot: bool = True
    )
    
    • stop_loss: Stop-loss percentage (e.g., 0.01 for 1%).
    • take_profit: Take-profit percentage (e.g., 0.03 for 3%).
    • initial_capital: Starting capital for the backtest.
    • leverage: Leverage multiplier.
    • quantity: Fixed quantity per trade (ignored if dynamic_position_size=True).
    • transaction_fee_rate: Transaction fee rate (e.g., 0.001 for 0.1%).
    • dynamic_position_size: Whether to use risk-based position sizing.
    • risk_per_trade: Risk percentage per trade (used if dynamic_position_size=True).
    • max_slippage: Maximum slippage percentage.
    • start_date: Start date for filtering data.
    • end_date: End date for filtering data.
    • plot: Whether to generate plots.
  2. get_positions() Returns the current open positions.

  3. get_closed_positions() Returns the closed positions.

  4. print_closed_positions(n: int = None) Prints a summary of closed positions.


Outputs

Backtest Results

The run() method returns a dictionary with the following metrics:

  • Total Trades: Total number of trades executed.
  • Win Rate (%): Percentage of profitable trades.
  • Profit Factor: Ratio of gross profit to gross loss.
  • Max Drawdown (%): Maximum drawdown during the backtest.
  • Profit/Loss: Total profit or loss in dollars.
  • Initial Capital: Starting capital.
  • Final Capital: Ending capital.
  • Return (%): Total return percentage.

Plots

  • Price Action with Signals: Shows buy/sell signals on the price chart.
  • Equity Curve: Portfolio value over time.
  • Available Capital: Remaining capital over time.
  • Drawdown: Drawdown percentage over time.
  • Trade Performance Distribution: Pie chart of profitable vs. losing trades.

Example Output

Backtest Results

+-------------------+----------------+
| Metric            | Value          |
+-------------------+----------------+
| Total Trades      | 50             |
| Win Rate (%)      | 60.00          |
| Profit Factor     | 1.50           |
| Max Drawdown (%)  | -10.00         |
| Profit/Loss       | $1,500.00      |
| Initial Capital   | $10,000.00     |
| Final Capital     | $11,500.00     |
| Return (%)        | 15.00%         |
+-------------------+----------------+

Plots

  • Price Action with Signals: Shows buy/sell signals on the price chart.
  • Equity Curve: Portfolio value over time.
  • Drawdown: Drawdown percentage over time.

Contributing

Contributions are welcome! If you'd like to contribute, please fork the repository and submit a pull request.


License

This project is licensed under the MIT License. See the LICENSE file for details.


Contact

For questions or feedback, contact Shashwat Chaturvedi at chaturvedishashwat5@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

financial_backtesting-0.1.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

financial_backtesting-0.1.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file financial_backtesting-0.1.1.tar.gz.

File metadata

  • Download URL: financial_backtesting-0.1.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.18

File hashes

Hashes for financial_backtesting-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c7d98325024728493c9dfb4464fb22892382828395cb4b7933bfefb6bce21f6d
MD5 50bd267a96d9625b24f524f0d989ef18
BLAKE2b-256 a5f76cb4b26862abafb263acc738bb67ca3df83933bf6c557f303bff9b3938a0

See more details on using hashes here.

File details

Details for the file financial_backtesting-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for financial_backtesting-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa85ff6ac69297d9a7831bd7d78dba17cad8c282ec6368493c606facdee2ae4a
MD5 d4887b581db4fc9a2ce6a6029105a2ae
BLAKE2b-256 792c0b916c4fd118e4fbe3b19f1e81ae59d3ff8e81e764f7fc60aff58ec12205

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page