2026-03-17 16:13:34 +01:00
|
|
|
"""Strategy template — copy this file and modify.
|
|
|
|
|
|
2026-08-23 13:31:37 +00:00
|
|
|
Demonstrates:
|
|
|
|
|
- the minimal shape of a backtest: strategy, config, store, run
|
|
|
|
|
|
|
|
|
|
Data: shared store — real market data from `data/` (see examples/README.md)
|
|
|
|
|
|
2026-03-17 16:13:34 +01:00
|
|
|
Usage:
|
|
|
|
|
python examples/00_template.py
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
from time import perf_counter
|
|
|
|
|
import manifoldbt as mbt
|
|
|
|
|
from manifoldbt.indicators import close
|
|
|
|
|
from manifoldbt.helpers import time_range, Slippage, Interval
|
|
|
|
|
|
|
|
|
|
# -- Indicators ---------------------------------------------------------------
|
|
|
|
|
# All 45+ indicators available: rsi, ema, sma, bollinger, macd, atr, etc.
|
|
|
|
|
# See: from manifoldbt.indicators import <tab> for full list
|
|
|
|
|
|
|
|
|
|
zscore = close.zscore(60)
|
|
|
|
|
|
|
|
|
|
# -- Strategy -----------------------------------------------------------------
|
|
|
|
|
# mbt.when(condition, value_if_true, value_if_false)
|
|
|
|
|
# - Omit 3rd arg → hold current position
|
|
|
|
|
# - Nest mbt.when() for multiple conditions
|
|
|
|
|
#
|
|
|
|
|
# Examples:
|
|
|
|
|
# signal = mbt.when(rsi < 30, 0.5, mbt.when(rsi > 70, 0.0))
|
|
|
|
|
# signal = mbt.when(fast_ema > slow_ema, 1.0, -1.0)
|
|
|
|
|
|
|
|
|
|
signal = mbt.when(zscore < -1.0, 1.0, # oversold → long
|
|
|
|
|
mbt.when(zscore > 1.0, 0.0)) # overbought → exit, else hold
|
|
|
|
|
|
|
|
|
|
strategy = (
|
|
|
|
|
mbt.Strategy.create("my_strategy")
|
|
|
|
|
.signal("zscore", zscore)
|
|
|
|
|
.size(signal)
|
|
|
|
|
.describe("Z-score mean reversion")
|
|
|
|
|
# .stop_loss(pct=3.0)
|
|
|
|
|
# .take_profit(pct=5.0)
|
|
|
|
|
# .trailing_stop(pct=2.0)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# -- Config -------------------------------------------------------------------
|
|
|
|
|
start, end = time_range("2021-01-01", "2026-01-01")
|
|
|
|
|
|
|
|
|
|
config = mbt.BacktestConfig(
|
2026-04-01 01:18:05 +02:00
|
|
|
universe={"binance": ["BTC-USDT:perp"]},
|
2026-03-17 16:13:34 +01:00
|
|
|
time_range_start=start,
|
|
|
|
|
time_range_end=end,
|
|
|
|
|
bar_interval=Interval.minutes(1), # bar resolution
|
|
|
|
|
initial_capital=10_000,
|
|
|
|
|
execution=mbt.ExecutionConfig(
|
|
|
|
|
allow_short=False,
|
|
|
|
|
max_position_pct=1.0,
|
|
|
|
|
),
|
|
|
|
|
fees=mbt.FeeConfig.binance_perps(),
|
|
|
|
|
slippage=Slippage.fixed_bps(2),
|
|
|
|
|
warmup_bars=60,
|
|
|
|
|
output_resolution=Interval.hours(1), # Pro: sub-daily, Community: capped to daily
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# -- Run ----------------------------------------------------------------------
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
root = os.path.join(os.path.dirname(__file__), "..")
|
2026-04-01 01:18:05 +02:00
|
|
|
data_root = os.path.abspath(os.path.join(root, "data"))
|
2026-03-17 16:13:34 +01:00
|
|
|
store = mbt.DataStore(
|
2026-04-01 01:18:05 +02:00
|
|
|
data_root=data_root,
|
2026-03-17 16:13:34 +01:00
|
|
|
metadata_db=os.path.abspath(os.path.join(root, "metadata", "metadata.sqlite")),
|
2026-04-01 01:18:05 +02:00
|
|
|
arrow_dir=os.path.join(data_root, "mega"),
|
2026-03-17 16:13:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
t0 = perf_counter()
|
|
|
|
|
result = mbt.run(strategy, config, store)
|
|
|
|
|
print(result.summary())
|
|
|
|
|
print(f"\nElapsed: {perf_counter() - t0:.2f}s")
|
|
|
|
|
|
2026-07-19 02:07:07 +00:00
|
|
|
mbt.plot.tearsheet(result)
|