A simple backtesting engine for trading strategies
Project description
ncBacktester
A simple backtesting engine for trading strategies, built for learning purposes.
Overview
ncBacktester is a lightweight Python package for backtesting trading strategies based on hold signals and OHLCV data. Unlike complex backtesting libraries, ncBacktester focuses on simplicity and educational value.
Features
- Simple Strategy Execution: Execute trades based on hold signal changes (0→1 for buy, 1→0 for sell)
- Performance Metrics: Calculate Sharpe Ratio, Sortino Ratio, Annualized Returns, Alpha, Beta, and Max Drawdown
- Stop Loss Support: Fixed and trailing stop loss functionality
- Static Visualization: Simple static plots of equity curves, trades, and drawdowns
- Easy to Use: Clean API similar to backtesting.py but simpler
Installation
pip install ncBacktester
Quick Start
from ncBacktester import Backtest
import pandas as pd
# Your data should have OHLCV columns + Hold_Signal column
data = pd.DataFrame({
'Open': [...],
'High': [...],
'Low': [...],
'Close': [...],
'Volume': [...],
'Hold_Signal': [0, 0, 1, 1, 0, ...] # 1 = hold, 0 = don't hold
})
# Create and run backtest
bt = Backtest(
data=data,
initial_capital=10000,
stop_loss_pct=0.05, # 5% stop loss
commission=0.001 # 0.1% commission
)
results = bt.run()
# View results
print(results['metrics'])
# Plot results
bt.plot()
Requirements
- Python >= 3.8
- pandas >= 1.3.0
- numpy >= 1.20.0
- matplotlib >= 3.3.0
Project Structure
ncBacktester/
├── ncBacktester/
│ ├── __init__.py
│ ├── backtest.py # Main Backtest class
│ ├── strategy_executor.py # Strategy execution (Coder A)
│ ├── metrics.py # Performance metrics (Coder B)
│ ├── stop_loss.py # Stop loss logic (Coder C)
│ └── plotter.py # Plotting (Coder C)
├── tests/
│ ├── test_strategy_executor.py # Tests for Coder A
│ ├── test_metrics.py # Tests for Coder B
│ ├── test_stop_loss.py # Tests for Coder C (Part 1)
│ ├── test_plotter.py # Tests for Coder C (Part 2)
│ └── test_integration.py # Integration tests
├── setup.py
├── pyproject.toml
└── README.md
How It Works
-
Signal Processing: The engine detects when
Hold_Signalchanges:0 → 1: Buy signal (go long)1 → 0: Sell signal (close position)
-
Trade Execution:
- On buy: Uses available capital to buy as many shares as possible
- On sell: Sells entire position
- Prices executed at Close price of the signal bar
-
Stop Loss:
- Fixed stop loss: Exits if price drops X% below entry
- Trailing stop loss: Exits if price drops X% below highest price since entry
-
Metrics Calculation: Calculates various performance metrics from the equity curve and trades.
Testing
# Install development dependencies
pip install -e ".[dev]"
# Run all tests
pytest
# Run tests for specific component
pytest tests/test_strategy_executor.py -v # Coder A
pytest tests/test_metrics.py -v # Coder B
pytest tests/test_stop_loss.py -v # Coder C
Development
This project is organized for collaborative development:
- Coder A: Strategy execution and trade management (
strategy_executor.py) - Coder B: Performance metrics calculation (
metrics.py) - Coder C: Stop loss logic (
stop_loss.py) and plotting (plotter.py)
Each component has detailed docstrings explaining what needs to be implemented.
Publishing to PyPI
- Update version in
setup.pyandncBacktester/__init__.py - Build the package:
python setup.py sdist bdist_wheel
- Upload to PyPI:
twine upload dist/*
License
MIT License
Contributing
This is a learning project. Contributions welcome!
Acknowledgments
Inspired by backtesting.py but simplified for educational purposes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ncbacktester-0.1.0.tar.gz.
File metadata
- Download URL: ncbacktester-0.1.0.tar.gz
- Upload date:
- Size: 23.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b51950ba584d1f918e22ca6a240adbdb9595e1380fba2e08cad0c078239ad25
|
|
| MD5 |
afc649e253662ea899f6f4ad37091ce4
|
|
| BLAKE2b-256 |
bc1bd59c40da8eddd6959e74a75d05e988be6dc5e4bb81df6c164c8b913bc4b4
|
File details
Details for the file ncbacktester-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ncbacktester-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13273ae8bb037964f1ebcf139b0d3ef560d7e0eed37034e98839850fed8ed5c9
|
|
| MD5 |
474093f4dc7f0691f332c74917408bbc
|
|
| BLAKE2b-256 |
44ca8a394f9a04f8d059a83ad415f5a2dd548777a76904645b8c3d54ddd2bfde
|