Files
manifoldbt/examples/00_template.py
T
2026-09-02 16:30:51 +00:00

77 lines
2.4 KiB
Python

"""Strategy template — copy this file and modify.
Demonstrates:
- the minimal shape of a backtest: strategy, config, store, run
Data: shared store — real market data from `data/` (see examples/README.md)
Usage:
python examples/00_template.py
"""
from time import perf_counter
import manifoldbt as mbt
from manifoldbt.indicators import close
from manifoldbt.helpers import time_range, Slippage, Interval
from _bootstrap import open_store, plots_available
# -- Indicators ---------------------------------------------------------------
# All 104 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(
universe={"binance": ["BTC-USDT:perp"]},
time_range_start=start,
time_range_end=end,
bar_interval=Interval.hours(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__":
store = open_store()
t0 = perf_counter()
result = mbt.run(strategy, config, store)
print(result.summary())
print(f"\nElapsed: {perf_counter() - t0:.2f}s")
if plots_available():
mbt.plot.tearsheet(result)