simple-backtest-kankane 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simple_backtest_kankane-0.3.1/LICENSE +21 -0
- simple_backtest_kankane-0.3.1/MANIFEST.in +8 -0
- simple_backtest_kankane-0.3.1/PKG-INFO +305 -0
- simple_backtest_kankane-0.3.1/README.md +271 -0
- simple_backtest_kankane-0.3.1/pyproject.toml +64 -0
- simple_backtest_kankane-0.3.1/setup.cfg +4 -0
- simple_backtest_kankane-0.3.1/simple_backtest/__init__.py +67 -0
- simple_backtest_kankane-0.3.1/simple_backtest/commission/__init__.py +13 -0
- simple_backtest_kankane-0.3.1/simple_backtest/commission/base.py +60 -0
- simple_backtest_kankane-0.3.1/simple_backtest/commission/flat.py +40 -0
- simple_backtest_kankane-0.3.1/simple_backtest/commission/percentage.py +40 -0
- simple_backtest_kankane-0.3.1/simple_backtest/commission/tiered.py +78 -0
- simple_backtest_kankane-0.3.1/simple_backtest/config/__init__.py +5 -0
- simple_backtest_kankane-0.3.1/simple_backtest/config/settings.py +320 -0
- simple_backtest_kankane-0.3.1/simple_backtest/core/__init__.py +7 -0
- simple_backtest_kankane-0.3.1/simple_backtest/core/backtest.py +381 -0
- simple_backtest_kankane-0.3.1/simple_backtest/core/portfolio.py +251 -0
- simple_backtest_kankane-0.3.1/simple_backtest/core/results.py +352 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/__init__.py +17 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/alphavantage_loader.py +85 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/base.py +91 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/ccxt_loader.py +115 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/csv_loader.py +85 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/polygon_loader.py +110 -0
- simple_backtest_kankane-0.3.1/simple_backtest/data/yfinance_loader.py +49 -0
- simple_backtest_kankane-0.3.1/simple_backtest/metrics/__init__.py +5 -0
- simple_backtest_kankane-0.3.1/simple_backtest/metrics/calculator.py +220 -0
- simple_backtest_kankane-0.3.1/simple_backtest/metrics/definitions.py +289 -0
- simple_backtest_kankane-0.3.1/simple_backtest/optimization/__init__.py +13 -0
- simple_backtest_kankane-0.3.1/simple_backtest/optimization/base.py +77 -0
- simple_backtest_kankane-0.3.1/simple_backtest/optimization/grid_search.py +107 -0
- simple_backtest_kankane-0.3.1/simple_backtest/optimization/random_search.py +120 -0
- simple_backtest_kankane-0.3.1/simple_backtest/optimization/walk_forward.py +178 -0
- simple_backtest_kankane-0.3.1/simple_backtest/strategy/__init__.py +8 -0
- simple_backtest_kankane-0.3.1/simple_backtest/strategy/base.py +241 -0
- simple_backtest_kankane-0.3.1/simple_backtest/strategy/buy_and_hold.py +48 -0
- simple_backtest_kankane-0.3.1/simple_backtest/strategy/dca.py +70 -0
- simple_backtest_kankane-0.3.1/simple_backtest/strategy/moving_average.py +59 -0
- simple_backtest_kankane-0.3.1/simple_backtest/utils/__init__.py +47 -0
- simple_backtest_kankane-0.3.1/simple_backtest/utils/caching.py +222 -0
- simple_backtest_kankane-0.3.1/simple_backtest/utils/commission.py +117 -0
- simple_backtest_kankane-0.3.1/simple_backtest/utils/execution.py +150 -0
- simple_backtest_kankane-0.3.1/simple_backtest/utils/logger.py +97 -0
- simple_backtest_kankane-0.3.1/simple_backtest/utils/validation.py +468 -0
- simple_backtest_kankane-0.3.1/simple_backtest/visualization/__init__.py +25 -0
- simple_backtest_kankane-0.3.1/simple_backtest/visualization/plotter.py +691 -0
- simple_backtest_kankane-0.3.1/simple_backtest_kankane.egg-info/PKG-INFO +305 -0
- simple_backtest_kankane-0.3.1/simple_backtest_kankane.egg-info/SOURCES.txt +49 -0
- simple_backtest_kankane-0.3.1/simple_backtest_kankane.egg-info/dependency_links.txt +1 -0
- simple_backtest_kankane-0.3.1/simple_backtest_kankane.egg-info/requires.txt +17 -0
- simple_backtest_kankane-0.3.1/simple_backtest_kankane.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Backtesting Framework Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simple-backtest-kankane
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Simple backtesting framework for trading strategies
|
|
5
|
+
Author-email: Raghav Kankane <raghavkankane07@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: backtesting,trading,finance,algorithmic-trading
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: pandas>=2.0.0
|
|
18
|
+
Requires-Dist: numpy>=1.24.0
|
|
19
|
+
Requires-Dist: pydantic>=2.0.0
|
|
20
|
+
Requires-Dist: pydantic-settings>=2.0.0
|
|
21
|
+
Requires-Dist: plotly>=5.14.0
|
|
22
|
+
Requires-Dist: joblib>=1.3.0
|
|
23
|
+
Requires-Dist: tqdm>=4.65.0
|
|
24
|
+
Requires-Dist: scipy>=1.11.0
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-cov>=4.1.0; extra == "dev"
|
|
28
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
29
|
+
Requires-Dist: yfinance>=0.2.0; extra == "dev"
|
|
30
|
+
Requires-Dist: jupyter>=1.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: matplotlib>=3.7.0; extra == "dev"
|
|
32
|
+
Requires-Dist: pre-commit>=3.5.0; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
<div align="center">
|
|
36
|
+
|
|
37
|
+
# Simple Backtest
|
|
38
|
+
|
|
39
|
+
**A high-performance, asset-agnostic backtesting framework for Python**
|
|
40
|
+
|
|
41
|
+
[](https://www.python.org/downloads/)
|
|
42
|
+
[](https://opensource.org/licenses/MIT)
|
|
43
|
+
[](https://github.com/astral-sh/ruff)
|
|
44
|
+
[](#)
|
|
45
|
+
|
|
46
|
+
[Features](#-features) • [Installation](#-installation) • [Quick Start](#-quick-start) • [Data Loaders](#-data-loaders) • [Documentation](#-documentation)
|
|
47
|
+
|
|
48
|
+
</div>
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 📖 About
|
|
53
|
+
|
|
54
|
+
Simple Backtest provides a clean framework for running strategy backtests with strong validation, robust metrics, and extensible architecture.
|
|
55
|
+
|
|
56
|
+
You can still bring your own pandas DataFrame, but the project now also includes **optional data source integrations** so users can load and normalize OHLCV data faster.
|
|
57
|
+
|
|
58
|
+
## ✨ Features
|
|
59
|
+
|
|
60
|
+
- **Backtesting Engine**: Fast, deterministic strategy execution
|
|
61
|
+
- **Validation First**: Actionable errors for data, config, and strategy inputs
|
|
62
|
+
- **Asset-Agnostic Design**: Works with stocks, forex, crypto, ETFs, and more
|
|
63
|
+
- **20+ Metrics**: Return, drawdown, Sharpe, Sortino, Calmar, alpha/beta, etc.
|
|
64
|
+
- **Optimization**: Grid search, random search, walk-forward
|
|
65
|
+
- **Optional Data Integrations**:
|
|
66
|
+
- `CSVLoader`
|
|
67
|
+
- `YFinanceLoader`
|
|
68
|
+
- `CCXTLoader`
|
|
69
|
+
- `AlphaVantageLoader`
|
|
70
|
+
- `PolygonLoader`
|
|
71
|
+
|
|
72
|
+
## 📦 Installation
|
|
73
|
+
|
|
74
|
+
### Core package
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pip install simple-backtest
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Optional loader dependencies
|
|
81
|
+
|
|
82
|
+
Install only what you use:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Yahoo Finance
|
|
86
|
+
pip install yfinance
|
|
87
|
+
|
|
88
|
+
# Crypto exchange data
|
|
89
|
+
pip install ccxt
|
|
90
|
+
|
|
91
|
+
# REST API loaders (Alpha Vantage, Polygon)
|
|
92
|
+
pip install requests
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Development setup
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
git clone <your-repository-url>
|
|
99
|
+
cd simple-backtest
|
|
100
|
+
pip install -e ".[dev]"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Requirements**: Python 3.10+
|
|
104
|
+
|
|
105
|
+
## 🚀 Quick Start
|
|
106
|
+
|
|
107
|
+
### Backtest from any OHLCV DataFrame
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from simple_backtest import Backtest, BacktestConfig, MovingAverageStrategy
|
|
111
|
+
|
|
112
|
+
# data must contain: Open, High, Low, Close (Volume optional unless configured)
|
|
113
|
+
strategy = MovingAverageStrategy(short_window=10, long_window=30, shares=10)
|
|
114
|
+
config = BacktestConfig.default(initial_capital=10000)
|
|
115
|
+
|
|
116
|
+
backtest = Backtest(data, config)
|
|
117
|
+
results = backtest.run([strategy])
|
|
118
|
+
|
|
119
|
+
print(results.get_strategy(strategy.get_name()).summary())
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Backtest using built-in CSV loader
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from simple_backtest import Backtest, BacktestConfig, CSVLoader, MovingAverageStrategy
|
|
126
|
+
|
|
127
|
+
loader = CSVLoader()
|
|
128
|
+
data = loader.load("data/aapl.csv", start="2020-01-01", end="2023-12-31")
|
|
129
|
+
|
|
130
|
+
backtest = Backtest(data, BacktestConfig.default(initial_capital=10000))
|
|
131
|
+
results = backtest.run([MovingAverageStrategy(short_window=10, long_window=30, shares=10)])
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## 🔌 Data Loaders
|
|
135
|
+
|
|
136
|
+
All loaders inherit from `DataLoader` and return a validated DataFrame with standardized columns:
|
|
137
|
+
|
|
138
|
+
`Open`, `High`, `Low`, `Close`, `Volume`
|
|
139
|
+
|
|
140
|
+
Validation is always run internally before the DataFrame is returned.
|
|
141
|
+
|
|
142
|
+
### `CSVLoader`
|
|
143
|
+
|
|
144
|
+
- Reads local CSV files
|
|
145
|
+
- Auto-detects date column (`Date`, `date`, `Datetime`, `datetime`, or datetime index)
|
|
146
|
+
- Normalizes common column variants (`open` → `Open`, etc.)
|
|
147
|
+
- Supports optional date filtering via `start` and `end`
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from simple_backtest import CSVLoader
|
|
151
|
+
|
|
152
|
+
data = CSVLoader().load("prices.csv", start="2021-01-01", end="2021-12-31")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### `YFinanceLoader`
|
|
156
|
+
|
|
157
|
+
- Uses `yfinance.download(...)`
|
|
158
|
+
- Handles yfinance MultiIndex column outputs
|
|
159
|
+
- Raises clear import/data errors
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from simple_backtest import YFinanceLoader
|
|
163
|
+
|
|
164
|
+
data = YFinanceLoader().load("AAPL", "2020-01-01", "2023-12-31")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### `CCXTLoader`
|
|
168
|
+
|
|
169
|
+
- Uses `ccxt` exchange clients
|
|
170
|
+
- Supports constructor args: `exchange_name`, optional `api_key`, `api_secret`
|
|
171
|
+
- Converts millisecond timestamps to `DatetimeIndex`
|
|
172
|
+
- Auto-paginates OHLCV fetches for larger ranges
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from simple_backtest import CCXTLoader
|
|
176
|
+
|
|
177
|
+
loader = CCXTLoader(exchange_name="binance")
|
|
178
|
+
data = loader.load("BTC/USDT", "2021-01-01", "2021-12-31", timeframe="1d")
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### `AlphaVantageLoader`
|
|
182
|
+
|
|
183
|
+
- Uses Alpha Vantage daily REST endpoint
|
|
184
|
+
- Constructor requires `api_key`
|
|
185
|
+
- Parses API JSON into standardized OHLCV DataFrame
|
|
186
|
+
- Applies `start` / `end` filtering post-load
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
from simple_backtest import AlphaVantageLoader
|
|
190
|
+
|
|
191
|
+
loader = AlphaVantageLoader(api_key="YOUR_KEY")
|
|
192
|
+
data = loader.load("AAPL", "2020-01-01", "2023-12-31")
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### `PolygonLoader`
|
|
196
|
+
|
|
197
|
+
- Uses Polygon aggregates REST endpoint
|
|
198
|
+
- Constructor requires `api_key`
|
|
199
|
+
- Handles `next_url` pagination
|
|
200
|
+
- Parses `o/h/l/c/v/t` fields into standardized OHLCV DataFrame
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
from simple_backtest import PolygonLoader
|
|
204
|
+
|
|
205
|
+
loader = PolygonLoader(api_key="YOUR_KEY")
|
|
206
|
+
data = loader.load("AAPL", "2020-01-01", "2023-12-31", timespan="day", multiplier=1)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Create your own loader
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
import pandas as pd
|
|
213
|
+
from simple_backtest import DataLoader
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
class MyCustomLoader(DataLoader):
|
|
217
|
+
def load(self, symbol, start, end) -> pd.DataFrame:
|
|
218
|
+
# fetch/construct your data
|
|
219
|
+
data = pd.DataFrame(...)
|
|
220
|
+
return self._finalize_dataframe(data)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## 📚 Documentation
|
|
224
|
+
|
|
225
|
+
### Built-in strategy helpers
|
|
226
|
+
|
|
227
|
+
When writing a custom strategy (subclass of `Strategy`), you can use:
|
|
228
|
+
|
|
229
|
+
- `self.has_position()`
|
|
230
|
+
- `self.get_position()`
|
|
231
|
+
- `self.get_cash()`
|
|
232
|
+
- `self.get_portfolio_value()`
|
|
233
|
+
- `self.buy(shares)`
|
|
234
|
+
- `self.sell(shares)`
|
|
235
|
+
- `self.sell_all()`
|
|
236
|
+
- `self.hold()`
|
|
237
|
+
- `self.buy_percent(percent)`
|
|
238
|
+
- `self.buy_cash(amount)`
|
|
239
|
+
|
|
240
|
+
### Config presets
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
from simple_backtest import BacktestConfig
|
|
244
|
+
|
|
245
|
+
config = BacktestConfig.default(initial_capital=10000)
|
|
246
|
+
config_zero_fees = BacktestConfig.zero_commission(initial_capital=10000)
|
|
247
|
+
config_hft = BacktestConfig.high_frequency(initial_capital=100000)
|
|
248
|
+
config_swing = BacktestConfig.swing_trading(initial_capital=10000)
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Optimizers
|
|
252
|
+
|
|
253
|
+
- `GridSearchOptimizer`
|
|
254
|
+
- `RandomSearchOptimizer`
|
|
255
|
+
- `WalkForwardOptimizer`
|
|
256
|
+
|
|
257
|
+
## 📓 Notebooks
|
|
258
|
+
|
|
259
|
+
Jupyter examples are available in the [notebooks](notebooks) folder:
|
|
260
|
+
|
|
261
|
+
- `01_basic_usage.ipynb`
|
|
262
|
+
- `02_candle_strategies.ipynb`
|
|
263
|
+
- `03_ta_strategies.ipynb`
|
|
264
|
+
- `04_ml_strategies.ipynb`
|
|
265
|
+
- `05_commission_usage.ipynb`
|
|
266
|
+
- `06_advanced_optimization.ipynb`
|
|
267
|
+
|
|
268
|
+
## 🛠️ Development
|
|
269
|
+
|
|
270
|
+
### Run tests
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
pytest
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Run linting
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
ruff check simple_backtest tests
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Format
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
ruff format simple_backtest tests
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## 🤝 Contributing
|
|
289
|
+
|
|
290
|
+
Contributions are welcome.
|
|
291
|
+
|
|
292
|
+
1. Fork repository
|
|
293
|
+
2. Create branch
|
|
294
|
+
3. Add tests for changes
|
|
295
|
+
4. Run `pytest` and `ruff check`
|
|
296
|
+
5. Open pull request
|
|
297
|
+
|
|
298
|
+
## 📄 License
|
|
299
|
+
|
|
300
|
+
MIT. See [LICENSE](LICENSE).
|
|
301
|
+
|
|
302
|
+
## 📬 Support
|
|
303
|
+
|
|
304
|
+
- Issues: Use your repository issue tracker
|
|
305
|
+
- Discussions: Use your repository discussions page
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# Simple Backtest
|
|
4
|
+
|
|
5
|
+
**A high-performance, asset-agnostic backtesting framework for Python**
|
|
6
|
+
|
|
7
|
+
[](https://www.python.org/downloads/)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
[](https://github.com/astral-sh/ruff)
|
|
10
|
+
[](#)
|
|
11
|
+
|
|
12
|
+
[Features](#-features) • [Installation](#-installation) • [Quick Start](#-quick-start) • [Data Loaders](#-data-loaders) • [Documentation](#-documentation)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 📖 About
|
|
19
|
+
|
|
20
|
+
Simple Backtest provides a clean framework for running strategy backtests with strong validation, robust metrics, and extensible architecture.
|
|
21
|
+
|
|
22
|
+
You can still bring your own pandas DataFrame, but the project now also includes **optional data source integrations** so users can load and normalize OHLCV data faster.
|
|
23
|
+
|
|
24
|
+
## ✨ Features
|
|
25
|
+
|
|
26
|
+
- **Backtesting Engine**: Fast, deterministic strategy execution
|
|
27
|
+
- **Validation First**: Actionable errors for data, config, and strategy inputs
|
|
28
|
+
- **Asset-Agnostic Design**: Works with stocks, forex, crypto, ETFs, and more
|
|
29
|
+
- **20+ Metrics**: Return, drawdown, Sharpe, Sortino, Calmar, alpha/beta, etc.
|
|
30
|
+
- **Optimization**: Grid search, random search, walk-forward
|
|
31
|
+
- **Optional Data Integrations**:
|
|
32
|
+
- `CSVLoader`
|
|
33
|
+
- `YFinanceLoader`
|
|
34
|
+
- `CCXTLoader`
|
|
35
|
+
- `AlphaVantageLoader`
|
|
36
|
+
- `PolygonLoader`
|
|
37
|
+
|
|
38
|
+
## 📦 Installation
|
|
39
|
+
|
|
40
|
+
### Core package
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install simple-backtest
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Optional loader dependencies
|
|
47
|
+
|
|
48
|
+
Install only what you use:
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
# Yahoo Finance
|
|
52
|
+
pip install yfinance
|
|
53
|
+
|
|
54
|
+
# Crypto exchange data
|
|
55
|
+
pip install ccxt
|
|
56
|
+
|
|
57
|
+
# REST API loaders (Alpha Vantage, Polygon)
|
|
58
|
+
pip install requests
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Development setup
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
git clone <your-repository-url>
|
|
65
|
+
cd simple-backtest
|
|
66
|
+
pip install -e ".[dev]"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Requirements**: Python 3.10+
|
|
70
|
+
|
|
71
|
+
## 🚀 Quick Start
|
|
72
|
+
|
|
73
|
+
### Backtest from any OHLCV DataFrame
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from simple_backtest import Backtest, BacktestConfig, MovingAverageStrategy
|
|
77
|
+
|
|
78
|
+
# data must contain: Open, High, Low, Close (Volume optional unless configured)
|
|
79
|
+
strategy = MovingAverageStrategy(short_window=10, long_window=30, shares=10)
|
|
80
|
+
config = BacktestConfig.default(initial_capital=10000)
|
|
81
|
+
|
|
82
|
+
backtest = Backtest(data, config)
|
|
83
|
+
results = backtest.run([strategy])
|
|
84
|
+
|
|
85
|
+
print(results.get_strategy(strategy.get_name()).summary())
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Backtest using built-in CSV loader
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from simple_backtest import Backtest, BacktestConfig, CSVLoader, MovingAverageStrategy
|
|
92
|
+
|
|
93
|
+
loader = CSVLoader()
|
|
94
|
+
data = loader.load("data/aapl.csv", start="2020-01-01", end="2023-12-31")
|
|
95
|
+
|
|
96
|
+
backtest = Backtest(data, BacktestConfig.default(initial_capital=10000))
|
|
97
|
+
results = backtest.run([MovingAverageStrategy(short_window=10, long_window=30, shares=10)])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## 🔌 Data Loaders
|
|
101
|
+
|
|
102
|
+
All loaders inherit from `DataLoader` and return a validated DataFrame with standardized columns:
|
|
103
|
+
|
|
104
|
+
`Open`, `High`, `Low`, `Close`, `Volume`
|
|
105
|
+
|
|
106
|
+
Validation is always run internally before the DataFrame is returned.
|
|
107
|
+
|
|
108
|
+
### `CSVLoader`
|
|
109
|
+
|
|
110
|
+
- Reads local CSV files
|
|
111
|
+
- Auto-detects date column (`Date`, `date`, `Datetime`, `datetime`, or datetime index)
|
|
112
|
+
- Normalizes common column variants (`open` → `Open`, etc.)
|
|
113
|
+
- Supports optional date filtering via `start` and `end`
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from simple_backtest import CSVLoader
|
|
117
|
+
|
|
118
|
+
data = CSVLoader().load("prices.csv", start="2021-01-01", end="2021-12-31")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `YFinanceLoader`
|
|
122
|
+
|
|
123
|
+
- Uses `yfinance.download(...)`
|
|
124
|
+
- Handles yfinance MultiIndex column outputs
|
|
125
|
+
- Raises clear import/data errors
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from simple_backtest import YFinanceLoader
|
|
129
|
+
|
|
130
|
+
data = YFinanceLoader().load("AAPL", "2020-01-01", "2023-12-31")
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### `CCXTLoader`
|
|
134
|
+
|
|
135
|
+
- Uses `ccxt` exchange clients
|
|
136
|
+
- Supports constructor args: `exchange_name`, optional `api_key`, `api_secret`
|
|
137
|
+
- Converts millisecond timestamps to `DatetimeIndex`
|
|
138
|
+
- Auto-paginates OHLCV fetches for larger ranges
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
from simple_backtest import CCXTLoader
|
|
142
|
+
|
|
143
|
+
loader = CCXTLoader(exchange_name="binance")
|
|
144
|
+
data = loader.load("BTC/USDT", "2021-01-01", "2021-12-31", timeframe="1d")
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### `AlphaVantageLoader`
|
|
148
|
+
|
|
149
|
+
- Uses Alpha Vantage daily REST endpoint
|
|
150
|
+
- Constructor requires `api_key`
|
|
151
|
+
- Parses API JSON into standardized OHLCV DataFrame
|
|
152
|
+
- Applies `start` / `end` filtering post-load
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from simple_backtest import AlphaVantageLoader
|
|
156
|
+
|
|
157
|
+
loader = AlphaVantageLoader(api_key="YOUR_KEY")
|
|
158
|
+
data = loader.load("AAPL", "2020-01-01", "2023-12-31")
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `PolygonLoader`
|
|
162
|
+
|
|
163
|
+
- Uses Polygon aggregates REST endpoint
|
|
164
|
+
- Constructor requires `api_key`
|
|
165
|
+
- Handles `next_url` pagination
|
|
166
|
+
- Parses `o/h/l/c/v/t` fields into standardized OHLCV DataFrame
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from simple_backtest import PolygonLoader
|
|
170
|
+
|
|
171
|
+
loader = PolygonLoader(api_key="YOUR_KEY")
|
|
172
|
+
data = loader.load("AAPL", "2020-01-01", "2023-12-31", timespan="day", multiplier=1)
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Create your own loader
|
|
176
|
+
|
|
177
|
+
```python
|
|
178
|
+
import pandas as pd
|
|
179
|
+
from simple_backtest import DataLoader
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
class MyCustomLoader(DataLoader):
|
|
183
|
+
def load(self, symbol, start, end) -> pd.DataFrame:
|
|
184
|
+
# fetch/construct your data
|
|
185
|
+
data = pd.DataFrame(...)
|
|
186
|
+
return self._finalize_dataframe(data)
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## 📚 Documentation
|
|
190
|
+
|
|
191
|
+
### Built-in strategy helpers
|
|
192
|
+
|
|
193
|
+
When writing a custom strategy (subclass of `Strategy`), you can use:
|
|
194
|
+
|
|
195
|
+
- `self.has_position()`
|
|
196
|
+
- `self.get_position()`
|
|
197
|
+
- `self.get_cash()`
|
|
198
|
+
- `self.get_portfolio_value()`
|
|
199
|
+
- `self.buy(shares)`
|
|
200
|
+
- `self.sell(shares)`
|
|
201
|
+
- `self.sell_all()`
|
|
202
|
+
- `self.hold()`
|
|
203
|
+
- `self.buy_percent(percent)`
|
|
204
|
+
- `self.buy_cash(amount)`
|
|
205
|
+
|
|
206
|
+
### Config presets
|
|
207
|
+
|
|
208
|
+
```python
|
|
209
|
+
from simple_backtest import BacktestConfig
|
|
210
|
+
|
|
211
|
+
config = BacktestConfig.default(initial_capital=10000)
|
|
212
|
+
config_zero_fees = BacktestConfig.zero_commission(initial_capital=10000)
|
|
213
|
+
config_hft = BacktestConfig.high_frequency(initial_capital=100000)
|
|
214
|
+
config_swing = BacktestConfig.swing_trading(initial_capital=10000)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Optimizers
|
|
218
|
+
|
|
219
|
+
- `GridSearchOptimizer`
|
|
220
|
+
- `RandomSearchOptimizer`
|
|
221
|
+
- `WalkForwardOptimizer`
|
|
222
|
+
|
|
223
|
+
## 📓 Notebooks
|
|
224
|
+
|
|
225
|
+
Jupyter examples are available in the [notebooks](notebooks) folder:
|
|
226
|
+
|
|
227
|
+
- `01_basic_usage.ipynb`
|
|
228
|
+
- `02_candle_strategies.ipynb`
|
|
229
|
+
- `03_ta_strategies.ipynb`
|
|
230
|
+
- `04_ml_strategies.ipynb`
|
|
231
|
+
- `05_commission_usage.ipynb`
|
|
232
|
+
- `06_advanced_optimization.ipynb`
|
|
233
|
+
|
|
234
|
+
## 🛠️ Development
|
|
235
|
+
|
|
236
|
+
### Run tests
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
pytest
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Run linting
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
ruff check simple_backtest tests
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Format
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
ruff format simple_backtest tests
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## 🤝 Contributing
|
|
255
|
+
|
|
256
|
+
Contributions are welcome.
|
|
257
|
+
|
|
258
|
+
1. Fork repository
|
|
259
|
+
2. Create branch
|
|
260
|
+
3. Add tests for changes
|
|
261
|
+
4. Run `pytest` and `ruff check`
|
|
262
|
+
5. Open pull request
|
|
263
|
+
|
|
264
|
+
## 📄 License
|
|
265
|
+
|
|
266
|
+
MIT. See [LICENSE](LICENSE).
|
|
267
|
+
|
|
268
|
+
## 📬 Support
|
|
269
|
+
|
|
270
|
+
- Issues: Use your repository issue tracker
|
|
271
|
+
- Discussions: Use your repository discussions page
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "simple-backtest-kankane"
|
|
3
|
+
version = "0.3.1"
|
|
4
|
+
description = "Simple backtesting framework for trading strategies"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = {text = "MIT"}
|
|
8
|
+
authors = [{name = "Raghav Kankane", email = "raghavkankane07@gmail.com"}]
|
|
9
|
+
keywords = ["backtesting", "trading", "finance", "algorithmic-trading"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 4 - Beta",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"License :: OSI Approved :: MIT License",
|
|
14
|
+
"Programming Language :: Python :: 3.10",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
]
|
|
18
|
+
dependencies = [
|
|
19
|
+
"pandas>=2.0.0",
|
|
20
|
+
"numpy>=1.24.0",
|
|
21
|
+
"pydantic>=2.0.0",
|
|
22
|
+
"pydantic-settings>=2.0.0",
|
|
23
|
+
"plotly>=5.14.0",
|
|
24
|
+
"joblib>=1.3.0",
|
|
25
|
+
"tqdm>=4.65.0",
|
|
26
|
+
"scipy>=1.11.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = [
|
|
31
|
+
"pytest>=7.4.0",
|
|
32
|
+
"pytest-cov>=4.1.0",
|
|
33
|
+
"ruff>=0.1.0",
|
|
34
|
+
"yfinance>=0.2.0",
|
|
35
|
+
"jupyter>=1.0.0",
|
|
36
|
+
"matplotlib>=3.7.0",
|
|
37
|
+
"pre-commit>=3.5.0",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[build-system]
|
|
41
|
+
requires = ["setuptools>=68.0.0"]
|
|
42
|
+
build-backend = "setuptools.build_meta"
|
|
43
|
+
|
|
44
|
+
[tool.setuptools.packages.find]
|
|
45
|
+
include = ["simple_backtest*"]
|
|
46
|
+
exclude = ["tests*"]
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
testpaths = ["tests"]
|
|
50
|
+
addopts = ["-ra", "--strict-markers"]
|
|
51
|
+
|
|
52
|
+
[tool.coverage.run]
|
|
53
|
+
source = ["simple_backtest"]
|
|
54
|
+
|
|
55
|
+
[tool.ruff]
|
|
56
|
+
line-length = 100
|
|
57
|
+
target-version = "py310"
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint]
|
|
60
|
+
select = ["E", "F", "I"]
|
|
61
|
+
ignore = ["E501"]
|
|
62
|
+
|
|
63
|
+
[tool.ruff.lint.per-file-ignores]
|
|
64
|
+
"__init__.py" = ["F401"]
|