Simple, no frills backtesting
Project description
Simple Backtesting in Python
A simple backtesting library in Python with no frills and all the customization. Just easy backtesting.
Note that this project is in active development. For feature suggestions, feel free to head on over to the Issues tab.
Compatible with Python 3.9+.
Dependencies
- Numpy (1.26.0, <2.0.0)
- Pandas
- tqdm
Usage guide
Pretend we're looking to implement a simple strategy where if the closing price falls twice, we buy. If the price rises two candles in a row, we sell.
1. Implement your strategy
We do this by creating a class that inherits from the SBStrat subclass. All we need to do is implement the on_next method which only takes one argument: our latest data.
from simple_backtester import SBStrat
class SimpleStrat(SBStrat):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.bullish = False
self.bearish = False
self.last_price = None
def on_next(self, new_row: pd.Series):
# Fetch the latest close price
new_close = new_row[self.close_col]
# If last_price hasn't been set, let's do that first!
if not self.last_price:
self.last_price = new_close
return
# Compare our newest close price with our last close price
if self.last_price >= new_close:
if self.bullish:
self.sell() # Spot order
self.bullish = False
else:
self.bullish = True
self.bearish = False
else:
if self.bearish:
self.buy() # Spot order
self.bearish = False
else:
self.bearish = True
self.bullish = False
self.last_price = new_close
What happens whenever buy or sell is called, an Order object is made. Orders can have one of the following statuses:
SUBMITTEDOPENEXECUTED_LOSS(I.E. our position was closed at stop loss)EXECUTED_PROFIT(I.E. our position was closed at take profit)EXPIRED(I.E. order expired before it could be opened)
Note that at the moment, only market orders have been implemented. In future versions limit orders will be added
We can set prices for our market order if we so wish:
self.buy(
buy_stop = 1000.5 # Open buy order when we hit 1000.5
)
or for sell orders:
self.sell(
sell_stop = 1000.5 # Open sell order when we hit 1000.5
)
We can also set stop losses and take profits as needed:
# Set stop loss at 900, and take profit at 1100
self.buy(
buy_stop = 1000.5,
take_profit = 1100,
stop_loss = 900
)
Or even set an expiration datetime for our buy and sell orders:
# Set stop loss at 900, and take profit at 1100
self.sell(
sell_stop = 900,
take_profit = 800,
stop_loss = 1150,
expiry_time = "2024-01-01" # Can be str or pd.Timestamp
)
If we want to do something every time an order's status changes (such as logging), we can implement the on_order_change abstract method:
import logging
from simple_backtester import SBStrat, Order, ORDER_STATUS
class SimpleStrat(SBStrat):
def __init__(self, **kwargs):
...
def on_next(self, new_row: pd.Series):
...
def on_order_change(self, order: Order):
if order.status == ORDER_STATUS.OPEN:
logging.info(f"Order opened! {order.order_type}")
if order.status == sbs.ORDER_STATUS.EXECUTED_LOSS or order.status == sbs.ORDER_STATUS.EXECUTED_PROFIT:
logging.info(f"Order closed! Current portfolio value: {self.portfolio}")
if order.status == sbs.ORDER_STATUS.EXPIRED:
logging.info("Order expired!")
And then, all we need to do is to create an instance of our strategy class:
test_strat = SimpleStrat(balance = 15000) # Start with 15000 units. Defaults to 1000
2. Add your data
For now, the only way we can add our candle data data is by passing in a pandas DataFrame. I'll be adding alternative data sources in the near future.
Tick data is not supported at this time.
import pandas as pd
ohlc_data = pd.read_csv("example_candle_data.csv")
test_strat.add_data(ohlc_data)
If our columns have different names or our date column has a specific format, we can pass those in:
test_strat.add_data(
ohlc_data,
open_col = "OPEN", # Defaults to "open"
high_col = "HIGH", # Defaults to "high"
low_col = "LOW", # Defaults to "low"
close_col = "CLOSE", # Defaults to "close"
date_col = "DATETIME", # Defaults to "date"
date_fmt = "%d-%m-%Y %H:%M:%S" # Infers by default
)
3. Profit!
And now, all that's left is running our test!
test_strat.start()
We can specify our tests to be run from or to a certain date:
test_strat.start(
start_date = "01-01-2023",
end_date = "01-01-2024"
)
And we can fetch our win-rate like so:
test_strat.win_rate
And if we want to start over and rerun our tests, all we need to do is call the reset() function:
test_strat.reset()
It's that simple! :)
To-do List:
- Limit orders
- Add risk/reward ratio
- Adding support for Python3.8 and Python3.7
- Writing up proper documentation
- Adding more tests/cleaning up tests
- Adding indicators using
talib - Adding commission fees
- Adding Yfinance support
- Adding support for tick data
Questions? Concerns? Email me at ssbakh07 (at) gmail.com
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 simple_backtester-0.1.0.tar.gz.
File metadata
- Download URL: simple_backtester-0.1.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.3 Linux/6.8.0-47-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0987b99837bc4d7951c72f560a5ff471df2e2ecd71d534adb39c36ab7b6dde89
|
|
| MD5 |
a70d3f6c156fb93fdc83a5596f8171e6
|
|
| BLAKE2b-256 |
4a29f8e0c218e75545c64a92f522082b74db0bfc4a6ec49cb67de28bc4c8c9ce
|
File details
Details for the file simple_backtester-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simple_backtester-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.3 Linux/6.8.0-47-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ed63fe53bee768d9d5f7f777a25b42c72f8504df36d02b0e7ff392c990b5863
|
|
| MD5 |
92dad9a5af5a27e56d91efa8b75410be
|
|
| BLAKE2b-256 |
fb9762a02b8d65374faa52515d4c566eb47ffcb0e5236dda5b63879e6ede7542
|