A simple backtesting engine for trading strategies
Project description
ncBacktester
ncBacktester is a small, opinionated Python package for backtesting simple long-only strategies driven by a Hold_Signal column on OHLCV data. It is designed for clarity and education rather than exhaustive feature coverage.
Quick summary: feed time-series OHLCV data with a Hold_Signal (1 = hold, 0 = not hold), instantiate Backtest, call .run() and examine metrics and plots.
Install (development)
pip install ncBacktester
Minimal Quick Start
import pandas as pd
from ncBacktester.backtest import Backtest
# data must include: Open, High, Low, Close, Volume, Hold_Signal
data = pd.read_csv('your_data.csv', parse_dates=['Date'], index_col='Date')
bt = Backtest(data=data, initial_capital=10000.0, stop_loss_pct=0.05, commission=0.001)
results = bt.run()
print(results['metrics']) # key performance metrics
results['equity_curve'].plot() # equity curve
bt.plot(save_path='backtest.png') # static plots
Required data format
- Columns:
Open,High,Low,Close,Volume,Hold_Signal. Hold_Signalshould be 0 or 1; changes 0→1 trigger buys, 1→0 trigger sells.
Main components
Backtest(ncBacktester/ncBacktester/backtest.py): orchestrates the workflow — validate data, initialize components, execute strategy, compute metrics, and prepare plots.StrategyExecutor(ncBacktester/ncBacktester/strategy_executor.py): implements trade sizing and execution logic (usesCloseprices and accounts forcommission).MetricsCalculator(ncBacktester/ncBacktester/metrics.py): computes performance statistics (Sharpe, Sortino, CAGR, drawdowns, etc.) from trades and the equity curve.StopLossManager(ncBacktester/ncBacktester/stop_loss.py): supports fixed and trailing stop loss behaviour during the backtest.Plotter(ncBacktester/ncBacktester/plotter.py): generates static plots showing price, trades, equity curve and drawdowns.
Behavior & assumptions
- Buys and sells are executed at the bar
Closewhere theHold_Signalchanges. - On a buy the framework purchases as many shares as possible with available capital; on sell it liquidates the full position.
- Stop loss (fixed or trailing) is optional and configured via
Backtest(stop_loss_pct=..., trailing_stop_pct=...).
Advanced usage
- If you want to reuse
StrategyExecutororStopLossManagerdirectly, import them from their modules and instantiate with the same parameters used byBacktest.
Contributing & license
- This project is intended for learning; contributions and issues are welcome.
- Licensed under the MIT License.
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.1.tar.gz.
File metadata
- Download URL: ncbacktester-0.1.1.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30af352f147f0813ce232a6272cf324167855d6fe0ff3865a63240ae05206f0c
|
|
| MD5 |
c521efe3136a00ee0d79f7885ee8fede
|
|
| BLAKE2b-256 |
d41389d85ad89c78a16d8ad1f9a03e4b9cef763d19aefd31721f749a52b79e08
|
File details
Details for the file ncbacktester-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ncbacktester-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.5 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 |
e24073063ffad7b1f5ec394d6899d96eea9ccd0bb9f69b8525ce26dff66ba166
|
|
| MD5 |
7dc523d51b02918f23bf9a4d60ed0345
|
|
| BLAKE2b-256 |
b373a1d84aa6ad4095fb2136d53c5adfc4ca60c1d91945a6f460be6c18c9a596
|