Skip to content

Commit 5e88bcd

Browse files
committed
fix docs, add doctests and docs to pre-commit.
1 parent 8eae136 commit 5e88bcd

File tree

11 files changed

+62
-28
lines changed

11 files changed

+62
-28
lines changed

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,18 @@ repos:
3535
files: \.ipynb$
3636
language: system
3737

38+
- id: doctest
39+
name: run doctests
40+
files: ^docs/
41+
entry: just doctest
42+
language: system
43+
pass_filenames: false
44+
45+
- id: docs
46+
name: build docs
47+
files: ^docs/
48+
entry: just docs
49+
language: system
50+
pass_filenames: false
3851

3952

docs/open_docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import sys
33
import webbrowser
44

5-
open_flag = sys.argv[1] == "--open"
6-
output_path = sys.argv[2]
5+
open_flag = len(sys.argv) > 1 and sys.argv[1] == "--open"
6+
output_path = sys.argv[2] if len(sys.argv) > 2 else ""
77

88
if open_flag:
99
webbrowser.open(pathlib.Path(output_path, "index.html").resolve().as_uri())
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
21
========
32
Decision
43
========
54

6-
.. currentmodule:: backtest_lib
5+
.. currentmodule:: backtest_lib.engine.decision
76

87
.. autoclass:: Decision
9-
:members:
108

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=======
2+
Execute
3+
=======
4+
5+
.. currentmodule:: backtest_lib.engine.execute
6+
7+
.. autoclass:: PlanExecutor
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
======
2+
Engine
3+
======
4+
5+
.. toctree::
6+
:hidden:
7+
8+
decision/index
9+
plan/index
10+
execute/index
11+
12+
.. currentmodule:: backtest_lib.engine
13+
14+
.. autoclass:: Engine
15+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
====
2+
Plan
3+
====
4+
5+
.. currentmodule:: backtest_lib.engine.plan
6+
7+
.. autoclass:: PlanGenerator

docs/source/reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ This is an overview of the public API of backtest-lib.
1111
market/index
1212
strategy/index
1313
portfolio/index
14+
engine/index
1415
universe/index

docs/source/reference/strategy/index.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ Strategy
44

55
.. currentmodule:: backtest_lib
66

7-
.. toctree::
8-
:hidden:
9-
10-
decision
11-
127
.. autoclass:: Strategy
138
:members:
149

src/backtest_lib/backtest/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,11 @@ class Backtest:
101101
>>> market = btl.MarketView(spot_prices)
102102
>>> universe = market.securities
103103
>>> def hold_strategy(universe, current_portfolio, market, ctx):
104-
... return btl.Decision(current_portfolio)
105-
>>> bt = btl.Backtest(
106-
... hold_strategy, universe, market, uniform_portfolio(universe)
107-
... )
104+
... return btl.hold()
105+
>>> bt = btl.Backtest(hold_strategy, market, uniform_portfolio(universe))
108106
>>> results = bt.run()
109107
>>> results.annualized_return
110-
-0.00022650...
108+
-0.00553564...
111109
"""
112110

113111
strategy: Strategy
@@ -133,7 +131,7 @@ def __init__(
133131
backend="polars",
134132
):
135133
self.strategy = strategy
136-
self.universe = universe or tuple(market_view.securities)
134+
self.universe = universe or market_view.securities
137135
self.market_view = market_view
138136
if isinstance(initial_portfolio, CashPortfolio):
139137
initial_portfolio = initial_portfolio.materialize(

src/backtest_lib/market/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ class PastView[ValueT: (float, int), Index: Comparable](ABC):
100100

101101
@property
102102
@abstractmethod
103-
def periods(self) -> Sequence[Index]:
103+
def periods(self) -> tuple[Index, ...]:
104104
"""PLACEHOLDER PROPERTY"""
105105
...
106106

107107
@property
108108
@abstractmethod
109-
def securities(self) -> Sequence[str]:
109+
def securities(self) -> tuple[str, ...]:
110110
"""PLACEHOLDER PROPERTY"""
111111
...
112112

@@ -430,8 +430,8 @@ def __init__(
430430
periods = self._resolve_period_axis_spec(reference_view_for_axis_values)
431431
securities = self._resolve_security_axis_spec(reference_view_for_axis_values)
432432

433-
self._periods: Sequence[Index] = periods
434-
self._securities: Sequence[str] = securities
433+
self._periods: tuple[Index, ...] = periods
434+
self._securities: tuple[str, ...] = securities
435435
self._security_policy: SecurityAxisPolicy = security_policy
436436
self._period_policy: PeriodAxisPolicy = period_policy
437437
self._backend: str = backend
@@ -443,11 +443,11 @@ def prices(self) -> PastUniversePrices[Index]:
443443
return self._prices
444444

445445
@property
446-
def periods(self) -> Sequence[Index]:
446+
def periods(self) -> tuple[Index, ...]:
447447
return self._periods
448448

449449
@property
450-
def securities(self) -> Sequence[str]:
450+
def securities(self) -> tuple[str, ...]:
451451
return self._securities
452452

453453
@property
@@ -544,10 +544,10 @@ def _align(
544544
# TODO: Add a reindexing method to the PastView protocol in some way
545545
# return view.reindex(securities=new_sec, periods=new_per)
546546

547-
def _resolve_period_axis_spec(self, spec: str) -> Sequence[Index]:
547+
def _resolve_period_axis_spec(self, spec: str) -> tuple[Index, ...]:
548548
return self._resolve_axis_spec(spec).periods
549549

550-
def _resolve_security_axis_spec(self, spec: str) -> Sequence[str]:
550+
def _resolve_security_axis_spec(self, spec: str) -> tuple[str, ...]:
551551
return self._resolve_axis_spec(spec).securities
552552

553553
def _resolve_axis_spec(self, spec: str) -> PastView:

0 commit comments

Comments
 (0)