Skip to content

Commit c6feb24

Browse files
committed
docs: declare continuous crypto calendars in runnable tutorials
1 parent fc3bc76 commit c6feb24

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

‎docs/tutorials/costs-and-funding.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ funding = pl.DataFrame({
105105
"asset": ["BTC-PERP", "BTC-PERP"],
106106
"rate": [0.001, -0.0005],
107107
})
108-
config = BacktestConfig(initial_cash=100_000, allow_leverage=True)
108+
config = BacktestConfig(initial_cash=100_000, allow_leverage=True, calendar="crypto")
109109
spec = ContractSpec("BTC-PERP", AssetClass.FUTURE, margin=4000)
110110
base = Engine(DataFeed(prices_df=prices), ExampleRoundTrip("BTC-PERP", 1),
111111
config, contract_specs={"BTC-PERP": spec}).run()
112112
funded = Engine(DataFeed(prices_df=prices), ExampleRoundTrip("BTC-PERP", 1),
113113
config, contract_specs={"BTC-PERP": spec}, funding_df=funding).run()
114114
print("ml4t-backtest " + version("ml4t-backtest"))
115+
assert funded.config.resolved_calendar == "crypto"
116+
assert funded.equity.periods_per_year == 365.0
115117
for row in funded.to_funding_dataframe().iter_rows(named=True):
116118
print(f"{row['timestamp']:%Y-%m-%d} rate={row['rate']:.4f} cash={row['cash_delta']:.2f}")
117119
print(f"trading P&L: ${base.trades[0].pnl:.2f}")

‎docs/tutorials/data.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ from ml4t.backtest import AssetClass, ContractSpec
142142

143143
prices = load_example_prices("crypto_perp")
144144
feed = DataFeed(prices_df=prices)
145-
config = BacktestConfig(initial_cash=100000, allow_leverage=True)
145+
config = BacktestConfig(initial_cash=100000, allow_leverage=True, calendar="crypto")
146146
spec = ContractSpec(symbol="BTC-PERP", asset_class=AssetClass.FUTURE, margin=4000)
147147
engine = Engine(feed, ExampleRoundTrip("BTC-PERP", 1), config, contract_specs={"BTC-PERP": spec})
148148
result = engine.run()
149149
assert len(result.fills) == 2 and len(result.trades) == 1
150150
print("ml4t-backtest " + version("ml4t-backtest"))
151+
assert result.config.resolved_calendar == "crypto"
152+
assert result.equity.periods_per_year == 365.0
151153
print(f"crypto_perp: fills={len(result.fills)}, trades={len(result.trades)}")
152154
print(f"final equity: ${result.metrics['final_value']:.2f}")
153155
```

‎docs/tutorials/diagnostic-handoff.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ from ml4t.diagnostic.integration import portfolio_analysis_from_result
2525
prices = load_example_prices("crypto_perp").filter(pl.col("asset") == "BTC-PERP")
2626
result = Engine(
2727
DataFeed(prices_df=prices), ExampleRoundTrip("BTC-PERP", 1),
28-
BacktestConfig(initial_cash=100_000, allow_leverage=True),
28+
BacktestConfig(initial_cash=100_000, allow_leverage=True, calendar="crypto"),
2929
contract_specs={"BTC-PERP": ContractSpec("BTC-PERP", AssetClass.FUTURE, margin=4000)},
3030
).run()
31+
assert result.config.resolved_calendar == "crypto"
32+
assert result.equity.periods_per_year == 365.0
3133
analysis = portfolio_analysis_from_result(result, calendar="crypto")
3234
stats = analysis.compute_summary_stats()
3335
print("ml4t-backtest " + version("ml4t-backtest"))

‎docs/tutorials/results-and-analysis.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ funding = pl.DataFrame({
4040
})
4141
result = Engine(
4242
DataFeed(prices_df=prices, signals_df=signals), ExampleRoundTrip("BTC-PERP", 1),
43-
BacktestConfig(initial_cash=100_000, allow_leverage=True, timezone=zone),
43+
BacktestConfig(initial_cash=100_000, allow_leverage=True, timezone=zone, calendar="crypto"),
4444
contract_specs={"BTC-PERP": ContractSpec("BTC-PERP", AssetClass.FUTURE, margin=4000)},
4545
funding_df=funding,
4646
).run()
47+
assert result.config.resolved_calendar == "crypto"
48+
assert result.equity.periods_per_year == 365.0
4749
equity = result.to_equity_dataframe()
4850
fills = result.to_fills_dataframe()
4951
trades = result.to_trades_dataframe()

0 commit comments

Comments
 (0)