Skip to content

Commit 12a0683

Browse files
committed
Re-export free decision functions from package root, update README
1 parent b0beb00 commit 12a0683

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ This library provides a lightweight framework for backtesting trading strategies
99
A strategy must conform to the following structure:
1010

1111
```python
12-
class Decision:
13-
target: WeightedPortfolio
14-
notes: UniverseMapping[Any] | None = None
15-
1612
class Strategy(Protocol):
1713
def __call__(
1814
self,
@@ -23,39 +19,38 @@ class Strategy(Protocol):
2319
) -> Decision: ...
2420
```
2521

26-
_In short, a function is called a strategy as long as it takes the universe, the current holdings, a market view, and additional context as parameters, and returns a Decision (which wraps the target portfolio)._
22+
_In short, a function is called a strategy as long as it takes the universe, the current holdings, a market view, and additional context as parameters, and returns a Decision, which can be constructed using the functions provided in the `decision` module, re-exported at the library root for convenience._
2723

2824
As inputs, the strategy receives the available universe, current holdings, a view of the market, and a context object for state.
2925

30-
For outputs, the strategy emits a Decision specifying the new target portfolio for each decision point in the _decision schedule_
26+
For outputs, the strategy emits a Decision for each decision point in the _decision schedule_
3127

3228
An toy example strategy can be written as follows, where we allocate our holdings uniformly across our universe:
3329

3430
```python
35-
import backtest_lib as btl
36-
from backtest_lib.portfolio import uniform_portfolio
31+
from backtest_lib import target_weights
3732

3833
def equal_weight_strategy(
3934
universe,
4035
current_portfolio,
4136
market,
4237
ctx,
4338
):
44-
return btl.Decision(uniform_portfolio(universe))
39+
return target_weights({sec: 1/len(universe) for sec in universe})
4540
```
4641

4742
Or alternatively, another trivial strategy where we do nothing after creating our initial portfolio:
4843

4944
```python
50-
import backtest_lib as btl
45+
from backtest_lib import hold
5146

5247
def buy_and_hold_strategy(
5348
universe,
5449
current_portfolio,
5550
market,
5651
ctx,
5752
):
58-
return btl.Decision(current_portfolio)
53+
return hold()
5954
```
6055

6156
## Market

src/backtest_lib/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
from backtest_lib.backtest import Backtest
2+
from backtest_lib.engine.decision import (
3+
combine,
4+
hold,
5+
reallocate,
6+
target_holdings,
7+
target_weights,
8+
trade,
9+
)
210
from backtest_lib.market import MarketView
311
from backtest_lib.portfolio import (
412
FractionalQuantityPortfolio,
@@ -16,4 +24,10 @@
1624
"QuantityPortfolio",
1725
"FractionalQuantityPortfolio",
1826
"Strategy",
27+
"hold",
28+
"reallocate",
29+
"target_holdings",
30+
"target_weights",
31+
"trade",
32+
"combine",
1933
]

0 commit comments

Comments
 (0)