A modular and extensible backtesting framework for Options Trading in Python.
Supports advanced strategies like Long Straddle, Long Strangle, Bull Call Spread, Bear Put Spread, and custom user-defined strategies β complete with PnL analytics, ML-based performance prediction, and risk metrics.
- β Fetches NSE/BSE data automatically
- β Implements 5 popular option strategies
- β Simulates positional, intraday, and swing modes
- β Calculates key performance metrics: Cumulative PnL, Max Drawdown, Sharpe & Sortino Ratios, Win Rate
- β Machine Learningβbased direction prediction
- β Beautiful tabular summaries and graphs
from strategies.bull_call_spread import BullCallSpread
from utils.data_fetcher import get_index_data
from utils.metrics import backtest_strategy
data = get_index_data("^NSEI", "2024-01-01", "2024-12-31")
strategy = BullCallSpread(buy_call_premium=120, sell_call_premium=80)
results, metrics = backtest_strategy(
strategy, data, lot_size=50, premium=100,
stop_loss_pct=0.1, target_profit_pct=0.2,
trading_type="positional"
)π Performance Metrics
ββββββββββββββββββββββ€βββββββββββββ
β Metric β Value β
ββββββββββββββββββββββͺβββββββββββββ‘
β Final PnL (βΉ) β 12,500.00 β
β Max Drawdown (βΉ) β 3,200.00 β
β Sharpe Ratio β 1.42 β
β Sortino Ratio β 1.85 β
β Win Rate (%) β 61.23 β
ββββββββββββββββββββββ§βββββββββββββ
from utils.ml_evaluator import evaluate_with_ml
from tabulate import tabulate
ml_results = evaluate_with_ml(results)
print(tabulate(ml_results, headers="keys", tablefmt="fancy_grid"))Predicts the next-day direction of profit/loss using models like:
- Random Forest
- SVM
- Logistic Regression
- Neural Network (MLP)
- KNN
- Decision Tree
- Naive Bayes
- Gradient Boosting
git clone https://github.com/<your-username>/options-backtesting-framework.git
cd options-backtesting-framework
pip install -r requirements.txtpandas
numpy
matplotlib
scikit-learn
tabulate
nsepython
bse
MIT License Β© Rohit Baskaran
π€ Rohit Baskaran
πΌ CSE Student | Interested in Trading, Stock Market & AI
π§ brohitbaskaran@gmail.com
π GitHub: clashrohit
- Add visualization dashboards using Plotly
- Integrate live NSE option chain API
- Add Paper-Trading mode
- Connect with Zerodha Kite or Angel One SmartAPI
- Deploy via Streamlit or Flask for web interface