Robust and rigorous backtesting framework for cryptocurrencies.
Project description
coin-test
Coin-test is a backtesting library designed for cryptocurrency trading. It supports trading strategies across multiple currencies and advanced configurations of tests, including cron-based scheduled execution of strategies, synthetic data generation, slippage modeling, and trading fees.
Quick Start
Coin-test runs on Python 3.10 or higher. Install the package via pip:
pip3 install coin-test
To run a backtest, import the coin-test library. Then define your data source, strategy, and test settings to run the analysis.
import datetime as dt
import os
import pandas as pd
from coin_test.backtest import Portfolio, Strategy, MarketTradeRequest
from coin_test.data import CustomDataset
from coin_test.util import AssetPair, Ticker, Money, Side
Then, import data from a CSV or another source to load into the backtest.
dataset_file = "data/ETHUSDT-1h-monthly/BTCUSDT-1h-2017-08.csv"
header = ["Open Time", "Open", "High", "Low", "Close", "Volume", "Close Time",
"Quote asset volume", "Number of trades", "Taker buy base asset volume",
"Taker buy quote asset volume", "Ignore"
]
df = pd.read_csv(dataset_file, names=header)
df = df.drop(columns=["Close Time", "Quote asset volume", "Number of trades",
"Taker buy base asset volume",
"Taker buy quote asset volume", "Ignore"])
df["Open Time"] //= 1000 # To seconds
df = df.sort_values(by=["Open Time"])
# define dataset metadata
usdt = Ticker("USDT")
btc = Ticker("BTC")
asset_pair = AssetPair(btc, usdt)
freq = "H"
dataset = CustomDataset(df, freq, asset_pair)
Strategies are stored in classes as shown below. Each strategy
should have a schedule, which is a cron string representing
when this strategy is run, a lookback, which is how much
data is accessed in the strategy, and a __call__
method
which returns a list of TradeRequest objects, which represent
trades the strategy wants to make.
class MACD(Strategy):
def __init__(self, asset_pair) -> None:
"""Initialize a MACD object."""
super().__init__(
name="MACD",
asset_pairs=[asset_pair],
schedule="0 9 * * *",
lookback=dt.timedelta(days=26),
)
self.perc = 0.2
def __call__(self, time, portfolio, lookback_data):
"""Execute test strategy."""
asset_pair = self.asset_pairs[0]
exp1 = lookback_data[asset_pair]["Close"].ewm(span=12 * 24, adjust=False).mean()
exp2 = lookback_data[asset_pair]["Close"].ewm(span=26 * 24, adjust=False).mean()
macd = exp1 - exp2
exp3 = macd.ewm(span=9 * 24, adjust=False).mean()
if macd.iloc[-1] > exp3.iloc[-1]:
return [MarketTradeRequest(
asset_pair,
Side.BUY,
notional=portfolio.available_assets(usdt).qty * self.perc,
)]
elif macd.iloc[-1] < exp3.iloc[-1]:
return [MarketTradeRequest(
asset_pair,
Side.SELL,
qty=portfolio.available_assets(btc).qty * self.perc,
)]
return []
This package supports multiple strategies, train-test splits for historical data, synthetic data, and further customization. To run the backtest, define the datasets
from coin_test.backtest import ConstantSlippage, ConstantTransactionFeeCalculator
sc = ConstantSlippage(50)
tc = ConstantTransactionFeeCalculator(50)
datasets = [dataset]
strategies = [MACD]
results = coin_test.run(datasets, strategies, sc, tc,
portfolio, pd.Timedelta(days=90),
n_parallel=8)
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
File details
Details for the file coin_test-0.1.0.tar.gz
.
File metadata
- Download URL: coin_test-0.1.0.tar.gz
- Upload date:
- Size: 22.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/5.15.93-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b8811e49d43ad0f53c04a8f3c6b63bc0c60fa7f48197da08ca24c063479e1e6 |
|
MD5 | 5a107cce2308fb9f3723fbeefc27cb10 |
|
BLAKE2b-256 | 0087244bfa811303a6d2329c3f64760840d1d7e2476c976b2755598ffb766e88 |
Provenance
File details
Details for the file coin_test-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: coin_test-0.1.0-py3-none-any.whl
- Upload date:
- Size: 27.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/5.15.93-1-MANJARO
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2478b9e9434dc28382a6276d83cf7bcf928e76c2970e44f4c275e9d4f6cdbf08 |
|
MD5 | 7c87ab54ef13f43cb142f300f52734bc |
|
BLAKE2b-256 | 22da50d6740738e138718c27ec1ed9c8f256880dde01df72e916b920b2676e46 |