paperswithbacktest 0.2.0__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {paperswithbacktest-0.2.0/paperswithbacktest.egg-info → paperswithbacktest-0.2.2}/PKG-INFO +2 -2
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2/paperswithbacktest.egg-info}/PKG-INFO +2 -2
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/paperswithbacktest.egg-info/SOURCES.txt +7 -1
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/paperswithbacktest.egg-info/requires.txt +1 -1
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/__init__.py +1 -1
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/backtest_engine.py +21 -3
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/feeds.py +46 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/futures_run.py +9 -4
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/__init__.py +99 -14
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/futures.py +90 -14
- paperswithbacktest-0.2.2/pwb/datasets/nominal.py +234 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/docs/backtesting.md +20 -0
- paperswithbacktest-0.2.2/pwb/world_model/shock_lists.py +218 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/setup.cfg +2 -2
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_futures_loader.py +58 -0
- paperswithbacktest-0.2.2/tests/test_nominal_feed.py +100 -0
- paperswithbacktest-0.2.2/tests/test_nominal_price.py +257 -0
- paperswithbacktest-0.2.2/tests/test_world_model_shock_integration.py +39 -0
- paperswithbacktest-0.2.2/tests/test_world_model_shock_lists.py +138 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/LICENSE +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/NOTICE +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/README.md +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/paperswithbacktest.egg-info/dependency_links.txt +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/paperswithbacktest.egg-info/top_level.txt +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/__init__.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/base_strategy.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/checks.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/commission.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/indicators.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/lookahead.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/optimization_engine.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/option_costs.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/portfolio.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/strategies.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/backtesting/universe.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/french.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/futures_chain.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/index_currencies.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/option_cache.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/options.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/pit_universe.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/datasets/prepared_cache.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/docs/__init__.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/docs/__main__.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/docs/world_model.md +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/engine.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/execution/__init__.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/execution/broker_factory.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/execution/ccxt_connector.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/execution/ib_connector.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/execution/live_utils.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/execution/optimal_limit_order.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/performance/__init__.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/performance/metrics.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/performance/nav_metrics.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/performance/plots.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/performance/reports.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/performance/trade_stats.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/world_model/__init__.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/world_model/_pit.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/world_model/_source.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/world_model/propagate.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pwb/world_model/reader.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/pyproject.toml +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_api_cache.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_array_feed.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_bare_symbol_collisions.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_bond_rates.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_bounded_loader.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_compat_shim.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_currency_costs.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_docs_shipped.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_engine_parity.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_engine_runner.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_engine_selector.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_exposure_cap.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_forex_directions.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_french.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_futures_chain.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_futures_rank6.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_futures_run.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_harness_checks.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_holiday_bars.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_hub_snapshot.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_index_currencies.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_lookahead.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_option_cache.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_option_chain.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_option_chain_sources.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_option_costs.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_pit_exit.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_pit_hold.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_pit_hold_accounting.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_pit_hold_same_bar_mark.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_prepared_cache.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_quantile_signal.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_read_memo.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_resolve_reused_property.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_reused_tickers.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_same_bar_orders.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_sells_first.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_short_borrow.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_strategy_commission.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_universe_alias_costs.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_world_model.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_world_model_local.py +0 -0
- {paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/tests/test_world_model_shock.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: paperswithbacktest
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Papers With Backtest: datasets, backtesting harness and execution for quant traders (import pwb; formerly pwb-toolbox)
|
|
5
5
|
Home-page: https://paperswithbacktest.com
|
|
6
6
|
Author: Papers With Backtest
|
|
@@ -32,7 +32,7 @@ Requires-Dist: huggingface_hub
|
|
|
32
32
|
Requires-Dist: ib_insync
|
|
33
33
|
Requires-Dist: matplotlib
|
|
34
34
|
Requires-Dist: numpy
|
|
35
|
-
Requires-Dist: pandas
|
|
35
|
+
Requires-Dist: pandas<3,>=2.2
|
|
36
36
|
Requires-Dist: pyarrow
|
|
37
37
|
Requires-Dist: python-dateutil
|
|
38
38
|
Requires-Dist: requests
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: paperswithbacktest
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: Papers With Backtest: datasets, backtesting harness and execution for quant traders (import pwb; formerly pwb-toolbox)
|
|
5
5
|
Home-page: https://paperswithbacktest.com
|
|
6
6
|
Author: Papers With Backtest
|
|
@@ -32,7 +32,7 @@ Requires-Dist: huggingface_hub
|
|
|
32
32
|
Requires-Dist: ib_insync
|
|
33
33
|
Requires-Dist: matplotlib
|
|
34
34
|
Requires-Dist: numpy
|
|
35
|
-
Requires-Dist: pandas
|
|
35
|
+
Requires-Dist: pandas<3,>=2.2
|
|
36
36
|
Requires-Dist: pyarrow
|
|
37
37
|
Requires-Dist: python-dateutil
|
|
38
38
|
Requires-Dist: requests
|
{paperswithbacktest-0.2.0 → paperswithbacktest-0.2.2}/paperswithbacktest.egg-info/SOURCES.txt
RENAMED
|
@@ -29,6 +29,7 @@ pwb/datasets/french.py
|
|
|
29
29
|
pwb/datasets/futures.py
|
|
30
30
|
pwb/datasets/futures_chain.py
|
|
31
31
|
pwb/datasets/index_currencies.py
|
|
32
|
+
pwb/datasets/nominal.py
|
|
32
33
|
pwb/datasets/option_cache.py
|
|
33
34
|
pwb/datasets/options.py
|
|
34
35
|
pwb/datasets/pit_universe.py
|
|
@@ -54,6 +55,7 @@ pwb/world_model/_pit.py
|
|
|
54
55
|
pwb/world_model/_source.py
|
|
55
56
|
pwb/world_model/propagate.py
|
|
56
57
|
pwb/world_model/reader.py
|
|
58
|
+
pwb/world_model/shock_lists.py
|
|
57
59
|
tests/test_api_cache.py
|
|
58
60
|
tests/test_array_feed.py
|
|
59
61
|
tests/test_bare_symbol_collisions.py
|
|
@@ -77,6 +79,8 @@ tests/test_holiday_bars.py
|
|
|
77
79
|
tests/test_hub_snapshot.py
|
|
78
80
|
tests/test_index_currencies.py
|
|
79
81
|
tests/test_lookahead.py
|
|
82
|
+
tests/test_nominal_feed.py
|
|
83
|
+
tests/test_nominal_price.py
|
|
80
84
|
tests/test_option_cache.py
|
|
81
85
|
tests/test_option_chain.py
|
|
82
86
|
tests/test_option_chain_sources.py
|
|
@@ -97,4 +101,6 @@ tests/test_strategy_commission.py
|
|
|
97
101
|
tests/test_universe_alias_costs.py
|
|
98
102
|
tests/test_world_model.py
|
|
99
103
|
tests/test_world_model_local.py
|
|
100
|
-
tests/test_world_model_shock.py
|
|
104
|
+
tests/test_world_model_shock.py
|
|
105
|
+
tests/test_world_model_shock_integration.py
|
|
106
|
+
tests/test_world_model_shock_lists.py
|
|
@@ -8,4 +8,4 @@ this package, gives the very same modules and warns once (``DeprecationWarning``
|
|
|
8
8
|
# The release this code is: what the harness checks report as ``toolboxVersion``.
|
|
9
9
|
# Kept equal to ``version`` in setup.cfg (tests/test_harness_checks.py checks it):
|
|
10
10
|
# the installed metadata can belong to another copy than the one imported.
|
|
11
|
-
__version__ = "0.2.
|
|
11
|
+
__version__ = "0.2.2"
|
|
@@ -16,7 +16,7 @@ from ..engine import load as _load_engine
|
|
|
16
16
|
bt = _load_engine() # Tradewind by default, backtrader on request; see pwb.engine
|
|
17
17
|
from . import checks as _checks
|
|
18
18
|
from . import lookahead as _lookahead
|
|
19
|
-
from .feeds import ArrayPandasData
|
|
19
|
+
from .feeds import ArrayPandasData, NominalPandasData, require_feed_lines
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
def _apply_broker_kwargs(broker: bt.BrokerBase, kwargs: dict) -> None:
|
|
@@ -756,7 +756,7 @@ def _blank_out_bars(pivot_df: pd.DataFrame, membership: "_Membership") -> None:
|
|
|
756
756
|
if not names:
|
|
757
757
|
return
|
|
758
758
|
mask = np.column_stack([membership.out[s] for s in names])
|
|
759
|
-
for field in ("open", "high", "low", "close"):
|
|
759
|
+
for field in ("open", "high", "low", "close", "nominal_close"):
|
|
760
760
|
columns = [(s, field) for s in names]
|
|
761
761
|
if any(c not in pivot_df.columns for c in columns):
|
|
762
762
|
continue
|
|
@@ -1618,9 +1618,22 @@ def run_strategy(
|
|
|
1618
1618
|
engine=None,
|
|
1619
1619
|
futures=None,
|
|
1620
1620
|
futures_options=None,
|
|
1621
|
+
nominal=False,
|
|
1621
1622
|
):
|
|
1622
1623
|
"""Run a tactical asset allocation strategy with Backtrader (or Tradewind).
|
|
1623
1624
|
|
|
1625
|
+
``nominal`` -- give every feed a ``nominal`` line: ``data.nominal[0]`` is the
|
|
1626
|
+
price the stock traded at on the bar (``nominal_close`` of
|
|
1627
|
+
``get_pricing(..., nominal=True)``), where ``data.close`` is the close
|
|
1628
|
+
back-adjusted for later splits and dividends. A filter or a sort on a price
|
|
1629
|
+
*level* (``>= 5`` dollars, penny stocks, the share price) must read it: on
|
|
1630
|
+
the adjusted close, AAPL on 2020-08-28 is 121, for a share that traded at
|
|
1631
|
+
499. NaN for a name outside ``Stocks-Daily-Price`` (ETFs, indices...). Orders,
|
|
1632
|
+
fills and the NAV still use the adjusted prices: the option changes nothing
|
|
1633
|
+
a strategy does not read. On Tradewind it needs a release with feed lines
|
|
1634
|
+
(``pwb.backtesting.feeds.FEED_EXTRA_LINES``). ``False`` (default): the run
|
|
1635
|
+
of the releases before it.
|
|
1636
|
+
|
|
1624
1637
|
``futures`` -- futures roots (``["GC", "BZ"]``) the strategy trades as real
|
|
1625
1638
|
contracts of ``Futures-Daily-Price``, on Tradewind only. Each root is one feed
|
|
1626
1639
|
named after it, the ratio-adjusted series of the contract held (signals);
|
|
@@ -1835,6 +1848,9 @@ def run_strategy(
|
|
|
1835
1848
|
broker_kwargs = broker_kwargs or {}
|
|
1836
1849
|
# Load the data from https://paperswithbacktest.com/datasets -- once, and
|
|
1837
1850
|
# reused across attempts, because a re-run must not re-download anything.
|
|
1851
|
+
if nominal:
|
|
1852
|
+
require_feed_lines() # before the download, not at the first feed
|
|
1853
|
+
pricing_kwargs = {"nominal": True} if nominal else {}
|
|
1838
1854
|
pivot_df = pwb_ds.get_pricing(
|
|
1839
1855
|
symbol_list=symbols,
|
|
1840
1856
|
fields=["open", "high", "low", "close"],
|
|
@@ -1843,6 +1859,7 @@ def run_strategy(
|
|
|
1843
1859
|
# "hold" values a position carried through an exit at its real
|
|
1844
1860
|
# prices, so it needs the rows the membership cut would drop.
|
|
1845
1861
|
pit_cut=pit_exit != "hold",
|
|
1862
|
+
**pricing_kwargs,
|
|
1846
1863
|
)
|
|
1847
1864
|
# Which feed each column came from (``get_pricing``), popped so pandas
|
|
1848
1865
|
# does not carry it through every operation below.
|
|
@@ -1951,8 +1968,9 @@ def run_strategy(
|
|
|
1951
1968
|
# Keyed by feed name, because that is what ``addcommissioninfo(name=...)``
|
|
1952
1969
|
# matches on, and what the scheme reads the bar date from.
|
|
1953
1970
|
feeds = {}
|
|
1971
|
+
feed_cls = NominalPandasData if nominal else ArrayPandasData
|
|
1954
1972
|
for symbol in pivot_df.columns.levels[0]:
|
|
1955
|
-
data =
|
|
1973
|
+
data = feed_cls(dataname=pivot_df[symbol].copy())
|
|
1956
1974
|
cerebro.adddata(data, name=symbol)
|
|
1957
1975
|
feeds[symbol] = data
|
|
1958
1976
|
guard = {"liquidating": False}
|
|
@@ -31,6 +31,7 @@ identical to the cent.
|
|
|
31
31
|
from __future__ import annotations
|
|
32
32
|
|
|
33
33
|
import numpy as np
|
|
34
|
+
import pandas as pd
|
|
34
35
|
|
|
35
36
|
from ..engine import load as _load_engine
|
|
36
37
|
|
|
@@ -72,3 +73,48 @@ else:
|
|
|
72
73
|
getattr(lines, datafield)[0] = values[idx]
|
|
73
74
|
lines.datetime[0] = self._pwb_dtnum[idx]
|
|
74
75
|
return True
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
#: Whether the running engine's ``PandasData`` reads lines a subclass declares
|
|
79
|
+
#: (``lines = ("nominal",)``). backtrader always has; Tradewind from the release
|
|
80
|
+
#: that adds ``tradewind.feeds.EXTRA_LINES`` (paperswithbacktest/tradewind#67).
|
|
81
|
+
FEED_EXTRA_LINES = bt.__name__ == "backtrader" or bool(getattr(bt.feeds, "EXTRA_LINES", False))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def require_feed_lines() -> None:
|
|
85
|
+
"""Raise unless the running engine's feeds take the lines a subclass declares."""
|
|
86
|
+
if not FEED_EXTRA_LINES:
|
|
87
|
+
raise RuntimeError(
|
|
88
|
+
"data.nominal needs an engine whose feeds take extra lines: this Tradewind "
|
|
89
|
+
"does not (it needs the release of paperswithbacktest/tradewind#67). Run "
|
|
90
|
+
"with PWB_ENGINE=backtrader, or read nominal_close from "
|
|
91
|
+
"pwb.datasets.get_pricing(..., nominal=True) outside the feed."
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class NominalPandasData(ArrayPandasData):
|
|
96
|
+
"""``ArrayPandasData`` with one more line, ``nominal``: the price of the date.
|
|
97
|
+
|
|
98
|
+
It reads the ``nominal_close`` column of the frame (``get_pricing(...,
|
|
99
|
+
nominal=True)``, ``run_strategy(..., nominal=True)``), NaN when the frame
|
|
100
|
+
has none. ``data.close`` stays the adjusted close the strategy trades on;
|
|
101
|
+
``data.nominal[0]`` is what a filter on a price level (``>= 5`` dollars,
|
|
102
|
+
penny stocks, a sort on the share price) must compare -- the adjusted
|
|
103
|
+
close is a level the stock reached only after its later splits (see
|
|
104
|
+
:mod:`pwb.datasets.nominal`).
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
lines = ("nominal",)
|
|
108
|
+
params = (("nominal", "nominal_close"),)
|
|
109
|
+
|
|
110
|
+
def __init__(self):
|
|
111
|
+
require_feed_lines()
|
|
112
|
+
# The engines take the parameters (``dataname`` among them) before
|
|
113
|
+
# ``__init__`` runs, so the frame is completed on ``self.p``.
|
|
114
|
+
frame = self.p.dataname
|
|
115
|
+
column = self.p.nominal
|
|
116
|
+
if isinstance(frame, pd.DataFrame) and isinstance(column, str) and column not in frame.columns:
|
|
117
|
+
# One feed class for every name of a panel: an ETF or an index has
|
|
118
|
+
# no nominal price, and reads NaN rather than failing.
|
|
119
|
+
self.p.dataname = frame.assign(**{column: np.nan})
|
|
120
|
+
super().__init__()
|
|
@@ -58,6 +58,7 @@ DEFAULT_OPTIONS = {
|
|
|
58
58
|
"maintenance_margins": None,
|
|
59
59
|
"on_margin_call": "liquidate",
|
|
60
60
|
"end": None,
|
|
61
|
+
"dataset": None, # alternate source for daily and continuous tables
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
|
|
@@ -349,13 +350,16 @@ def _futures_daily(roots, start, options, held=None):
|
|
|
349
350
|
when that passes the deepest rank of the ``continuous`` table)."""
|
|
350
351
|
from ..datasets import futures as fds
|
|
351
352
|
|
|
352
|
-
|
|
353
|
+
window = {"start": start, "end": options["end"]}
|
|
354
|
+
if options["dataset"] is not None:
|
|
355
|
+
window["dataset"] = options["dataset"]
|
|
356
|
+
daily = fds.load_futures(roots, kind="daily", **window)
|
|
353
357
|
held = held or {}
|
|
354
358
|
if options["ranks"] is not None or held:
|
|
355
359
|
keep = set()
|
|
356
360
|
plain = [r for r in roots if r not in held]
|
|
357
361
|
if plain and options["ranks"] is not None:
|
|
358
|
-
keep |= fds.contract_symbols(plain, options["ranks"],
|
|
362
|
+
keep |= fds.contract_symbols(plain, options["ranks"], **window)
|
|
359
363
|
elif plain:
|
|
360
364
|
keep |= set(daily.loc[daily["root"].isin(plain), "symbol"].astype(str))
|
|
361
365
|
for root, ranks in held.items():
|
|
@@ -366,11 +370,12 @@ def _futures_daily(roots, start, options, held=None):
|
|
|
366
370
|
keep |= set(daily.loc[daily["root"] == root, "symbol"].astype(str))
|
|
367
371
|
continue
|
|
368
372
|
load = set(options["ranks"] or ()) | set(range(1, max(ranks) + 2))
|
|
369
|
-
keep |= fds.contract_symbols([root], sorted(load),
|
|
373
|
+
keep |= fds.contract_symbols([root], sorted(load), **window)
|
|
370
374
|
daily = daily[daily["symbol"].isin(keep)]
|
|
371
375
|
missing = sorted(set(roots) - set(daily["root"].unique()))
|
|
372
376
|
if missing:
|
|
373
|
-
|
|
377
|
+
dataset = options["dataset"] or fds.FUTURES_DAILY_DATASET
|
|
378
|
+
raise ValueError(f"{dataset} has no contract for roots {missing} in the window")
|
|
374
379
|
return daily, fds.engine_table(daily)
|
|
375
380
|
|
|
376
381
|
|
|
@@ -21,6 +21,14 @@ import requests
|
|
|
21
21
|
from tqdm import tqdm
|
|
22
22
|
|
|
23
23
|
from . import prepared_cache as _prepared
|
|
24
|
+
from .nominal import ( # noqa: F401 (re-exported)
|
|
25
|
+
DISTRIBUTION_ROWS,
|
|
26
|
+
NOMINAL_COLUMNS,
|
|
27
|
+
SPLITS_DATASET,
|
|
28
|
+
load_splits,
|
|
29
|
+
nominal_factor,
|
|
30
|
+
nominal_price,
|
|
31
|
+
)
|
|
24
32
|
from .index_currencies import ( # noqa: F401 (re-exported)
|
|
25
33
|
INDEX_CURRENCY,
|
|
26
34
|
IndexCurrencyWarning,
|
|
@@ -1115,6 +1123,8 @@ def _load_dataset_from_hf(dataset_name: str, split: str, hf_token: str, symbols=
|
|
|
1115
1123
|
return _read_parquet_files(local_files, desc=f"Load {dataset_name}", symbols=symbols,
|
|
1116
1124
|
columns=columns, window=_row_window(window))
|
|
1117
1125
|
|
|
1126
|
+
STOCKS_DAILY_DATASET = "Stocks-Daily-Price"
|
|
1127
|
+
|
|
1118
1128
|
DAILY_PRICE_DATASETS = [
|
|
1119
1129
|
"Bonds-Daily-Price",
|
|
1120
1130
|
"Commodities-Daily-Price",
|
|
@@ -1952,18 +1962,38 @@ def load_dataset(
|
|
|
1952
1962
|
start=None,
|
|
1953
1963
|
end=None,
|
|
1954
1964
|
columns=None,
|
|
1965
|
+
*,
|
|
1966
|
+
split="train",
|
|
1967
|
+
nominal=False,
|
|
1955
1968
|
):
|
|
1956
1969
|
"""Load a PWB dataset as a pandas DataFrame.
|
|
1957
1970
|
|
|
1958
1971
|
Parameters
|
|
1959
1972
|
----------
|
|
1960
1973
|
path : str
|
|
1961
|
-
Dataset name,
|
|
1974
|
+
Dataset name, optionally prefixed with ``paperswithbacktest/``.
|
|
1962
1975
|
symbols : str or list of str, optional
|
|
1963
1976
|
Keep only these symbols. The filter is pushed down to each shard's
|
|
1964
1977
|
read, so memory stays bounded to the matching rows. Universe names
|
|
1965
1978
|
(``"sp500"``...) are resolved as before.
|
|
1966
|
-
adjust
|
|
1979
|
+
adjust : bool
|
|
1980
|
+
Which prices of a daily price table come back under ``open``,
|
|
1981
|
+
``high``, ``low`` and ``close``. On ``Stocks-Daily-Price`` **neither
|
|
1982
|
+
setting gives the price the stock traded at**: the table's two price
|
|
1983
|
+
columns are both back-adjusted for every later split.
|
|
1984
|
+
|
|
1985
|
+
* ``True`` (default): ``adj_close``, adjusted for splits and
|
|
1986
|
+
dividends (a total-return series), under the name ``close``; the
|
|
1987
|
+
open, high and low are scaled by the same factor, and there is no
|
|
1988
|
+
``adj_close`` column.
|
|
1989
|
+
* ``False``: the table's own ``open``/``high``/``low``/``close``,
|
|
1990
|
+
adjusted for splits but not for dividends, **and** ``adj_close``
|
|
1991
|
+
next to them. It is not an unadjusted price: AAPL on 2020-08-28 is
|
|
1992
|
+
124.81, for a share that traded at 499.23. Strategies rely on
|
|
1993
|
+
getting both columns from one read, so ``adj_close`` stays.
|
|
1994
|
+
|
|
1995
|
+
For the price of the date, pass ``nominal=True``.
|
|
1996
|
+
extend, to_usd, rate_to_price : bool
|
|
1967
1997
|
Post-processing of the daily price datasets. ``rate_to_price``
|
|
1968
1998
|
turns the yields of ``Bonds-Daily-Price`` into zero-coupon prices,
|
|
1969
1999
|
the TIPS real yields (``US5YT``...) included; see
|
|
@@ -1983,6 +2013,16 @@ def load_dataset(
|
|
|
1983
2013
|
``date``/``datetime``). For the minute datasets the read never
|
|
1984
2014
|
materialises the other columns. An unknown column is an error.
|
|
1985
2015
|
|
|
2016
|
+
split : str, optional
|
|
2017
|
+
Named table to read. Most datasets use ``"train"``.
|
|
2018
|
+
nominal : bool, optional
|
|
2019
|
+
``Stocks-Daily-Price`` only: add ``nominal_open``, ``nominal_high``,
|
|
2020
|
+
``nominal_low`` and ``nominal_close``, the prices the stock traded at
|
|
2021
|
+
on the date (the split-adjusted prices times the
|
|
2022
|
+
``Stocks-Daily-Splits`` ratios dated after it; see
|
|
2023
|
+
:mod:`pwb.datasets.nominal`). What a filter or a sort on a price
|
|
2024
|
+
*level* must read. The other columns are those ``adjust`` gives.
|
|
2025
|
+
|
|
1986
2026
|
Without ``start``, ``end`` and ``columns`` the behaviour is unchanged. A
|
|
1987
2027
|
minute dataset loaded without ``start``/``end`` still loads, but warns
|
|
1988
2028
|
(``UnboundedMinuteLoadWarning``): it downloads every month, ~75 GB for
|
|
@@ -2003,7 +2043,8 @@ def load_dataset(
|
|
|
2003
2043
|
"which downloads only the chains and months asked for."
|
|
2004
2044
|
)
|
|
2005
2045
|
|
|
2006
|
-
|
|
2046
|
+
if isinstance(path, str):
|
|
2047
|
+
path = path.removeprefix("paperswithbacktest/")
|
|
2007
2048
|
pwb_api_key = _get_pwb_api_key()
|
|
2008
2049
|
|
|
2009
2050
|
if isinstance(columns, str):
|
|
@@ -2098,6 +2139,16 @@ def load_dataset(
|
|
|
2098
2139
|
if path == "Universe-Daily-Price":
|
|
2099
2140
|
df = _with_forex_inverse_names(df)
|
|
2100
2141
|
|
|
2142
|
+
if nominal:
|
|
2143
|
+
if path != STOCKS_DAILY_DATASET:
|
|
2144
|
+
raise ValueError(
|
|
2145
|
+
f"nominal=True: nominal prices exist for {STOCKS_DAILY_DATASET} only "
|
|
2146
|
+
f"(the split table is a table of stocks), not for {path!r}"
|
|
2147
|
+
)
|
|
2148
|
+
# From the split-adjusted columns, before ``adjust`` replaces them with
|
|
2149
|
+
# the dividend-adjusted ones.
|
|
2150
|
+
df = nominal_price(df, assume_split_adjusted=True)
|
|
2151
|
+
|
|
2101
2152
|
if path in DAILY_PRICE_DATASETS:
|
|
2102
2153
|
if adjust and "adj_close" in df.columns:
|
|
2103
2154
|
adj_factor = df["adj_close"] / df["close"]
|
|
@@ -2114,9 +2165,12 @@ def load_dataset(
|
|
|
2114
2165
|
},
|
|
2115
2166
|
inplace=True,
|
|
2116
2167
|
)
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2168
|
+
# adjust=False: the table's split-adjusted open/high/low/close, and
|
|
2169
|
+
# adj_close kept next to them. Until 0.2.1 this branch called
|
|
2170
|
+
# ``df.drop(columns=["adj_close"])`` without keeping the result, so
|
|
2171
|
+
# adj_close always stayed; 38 published strategies read both columns
|
|
2172
|
+
# from one ``adjust=False`` load (a dividend yield, a total return next
|
|
2173
|
+
# to a price), so the behaviour is kept and the dead line removed.
|
|
2120
2174
|
|
|
2121
2175
|
if path in DAILY_PRICE_DATASETS and (extend and path == "ETFs-Daily-Price"):
|
|
2122
2176
|
df = __extend_etfs(df)
|
|
@@ -2333,7 +2387,7 @@ def __extend_etfs(df_etfs):
|
|
|
2333
2387
|
)
|
|
2334
2388
|
|
|
2335
2389
|
|
|
2336
|
-
ALLOWED_FIELDS = {"open", "high", "low", "close", "volume"}
|
|
2390
|
+
ALLOWED_FIELDS = {"open", "high", "low", "close", "volume", *NOMINAL_COLUMNS.values()}
|
|
2337
2391
|
|
|
2338
2392
|
|
|
2339
2393
|
# Which repository a bare ticker resolves to when ``Universe-Daily-Price`` lists
|
|
@@ -2797,12 +2851,23 @@ def _pricing_shard_window(repo_id, names, extend, contested, reused_names, start
|
|
|
2797
2851
|
return None
|
|
2798
2852
|
|
|
2799
2853
|
|
|
2800
|
-
def
|
|
2854
|
+
def _pricing_load_kwargs(repo_id, extend, nominal) -> dict:
|
|
2855
|
+
"""The ``load_dataset`` arguments of one repository of a ``get_pricing`` read."""
|
|
2856
|
+
kwargs = {"extend": extend}
|
|
2857
|
+
if nominal and repo_id == STOCKS_DAILY_DATASET:
|
|
2858
|
+
# Passed only when asked for, so a loader replaced in a test keeps
|
|
2859
|
+
# the signature it has always had.
|
|
2860
|
+
kwargs["nominal"] = True
|
|
2861
|
+
return kwargs
|
|
2862
|
+
|
|
2863
|
+
|
|
2864
|
+
def _load_for_pricing(repo_id, names, extend, shard_window, nominal=False):
|
|
2865
|
+
kwargs = _pricing_load_kwargs(repo_id, extend, nominal)
|
|
2801
2866
|
if shard_window is None:
|
|
2802
|
-
return load_dataset(repo_id, names,
|
|
2867
|
+
return load_dataset(repo_id, names, **kwargs)
|
|
2803
2868
|
token = _SHARD_WINDOW.set(shard_window)
|
|
2804
2869
|
try:
|
|
2805
|
-
return load_dataset(repo_id, names,
|
|
2870
|
+
return load_dataset(repo_id, names, **kwargs)
|
|
2806
2871
|
finally:
|
|
2807
2872
|
_SHARD_WINDOW.reset(token)
|
|
2808
2873
|
|
|
@@ -2816,6 +2881,7 @@ def get_pricing(
|
|
|
2816
2881
|
extend=False,
|
|
2817
2882
|
keep_single_level=False, # backward-compat flag
|
|
2818
2883
|
pit_cut=True,
|
|
2884
|
+
nominal=False,
|
|
2819
2885
|
):
|
|
2820
2886
|
"""
|
|
2821
2887
|
Fetch OHLC pricing for the requested symbols.
|
|
@@ -2826,8 +2892,11 @@ def get_pricing(
|
|
|
2826
2892
|
One ticker or a list of tickers. Supports suffixed form "SYM.CAT"
|
|
2827
2893
|
where CAT in {CRYPTO, STOCK, CMDTY, IDX, ETF, FX, BND}.
|
|
2828
2894
|
fields : list[str] | None
|
|
2829
|
-
Any subset of ["open", "high", "low", "close"]
|
|
2830
|
-
|
|
2895
|
+
Any subset of ["open", "high", "low", "close", "volume"], plus
|
|
2896
|
+
``nominal_open``/``nominal_high``/``nominal_low``/``nominal_close``
|
|
2897
|
+
(which imply ``nominal=True``). Defaults to ["close"] for backward
|
|
2898
|
+
compatibility. ``close`` is the dividend- and split-adjusted close
|
|
2899
|
+
(``load_dataset``'s ``adjust=True``), not the price of the date.
|
|
2831
2900
|
start_date, end_date : str (YYYY-MM-DD)
|
|
2832
2901
|
Slice the date index (inclusive).
|
|
2833
2902
|
extend : bool
|
|
@@ -2842,6 +2911,13 @@ def get_pricing(
|
|
|
2842
2911
|
every price row of every name that was ever a member: what
|
|
2843
2912
|
``run_strategy`` needs to value a position held through an exit at its
|
|
2844
2913
|
real prices, membership being enforced on the orders instead.
|
|
2914
|
+
nominal : bool
|
|
2915
|
+
Add ``nominal_close`` to ``fields`` (unless a nominal field is already
|
|
2916
|
+
asked for): the price a stock traded at on the date, for a filter or a
|
|
2917
|
+
sort on a price level (see :mod:`pwb.datasets.nominal`). It exists for
|
|
2918
|
+
the names read from ``Stocks-Daily-Price`` only; for the others
|
|
2919
|
+
(ETFs, indices, crypto...) the column is absent from the panel.
|
|
2920
|
+
``False`` (default): the panel is the one of the releases before it.
|
|
2845
2921
|
|
|
2846
2922
|
Returns
|
|
2847
2923
|
-------
|
|
@@ -2866,6 +2942,10 @@ def get_pricing(
|
|
|
2866
2942
|
bad = [f for f in fields if f not in ALLOWED_FIELDS]
|
|
2867
2943
|
if bad:
|
|
2868
2944
|
raise ValueError(f"Invalid field(s): {bad}. Allowed: {sorted(ALLOWED_FIELDS)}")
|
|
2945
|
+
nominal_fields = set(NOMINAL_COLUMNS.values())
|
|
2946
|
+
if nominal and not nominal_fields.intersection(fields):
|
|
2947
|
+
fields = fields + ["nominal_close"]
|
|
2948
|
+
nominal = bool(nominal_fields.intersection(fields))
|
|
2869
2949
|
|
|
2870
2950
|
# --------------------------------------------------------------- download
|
|
2871
2951
|
# Direct routing for suffixed symbols (skip Universe lookup)
|
|
@@ -2966,7 +3046,7 @@ def get_pricing(
|
|
|
2966
3046
|
shard_window = _pricing_shard_window(
|
|
2967
3047
|
repo_id, names, ext_flag, contested, reused_names, start_date, end_date
|
|
2968
3048
|
)
|
|
2969
|
-
df_part = _load_for_pricing(repo_id, names, ext_flag, shard_window)
|
|
3049
|
+
df_part = _load_for_pricing(repo_id, names, ext_flag, shard_window, nominal)
|
|
2970
3050
|
if shard_window is not None:
|
|
2971
3051
|
pruned.append((repo_id, names, ext_flag))
|
|
2972
3052
|
if not df_part.empty:
|
|
@@ -2975,7 +3055,7 @@ def get_pricing(
|
|
|
2975
3055
|
# Nothing in the window anywhere: read everything, as before, so the
|
|
2976
3056
|
# panel comes out the way it always did for an empty window.
|
|
2977
3057
|
for repo_id, names, ext_flag in pruned:
|
|
2978
|
-
df_part = load_dataset(repo_id, names,
|
|
3058
|
+
df_part = load_dataset(repo_id, names, **_pricing_load_kwargs(repo_id, ext_flag, nominal))
|
|
2979
3059
|
if not df_part.empty:
|
|
2980
3060
|
loaded[repo_id] = df_part
|
|
2981
3061
|
loaded = _arbitrate_collisions(loaded, contested, start_date, end_date)
|
|
@@ -3011,6 +3091,11 @@ def get_pricing(
|
|
|
3011
3091
|
df = df.loc[start_date:end_date]
|
|
3012
3092
|
|
|
3013
3093
|
# ------------------------------------------------------------- reshape
|
|
3094
|
+
if nominal:
|
|
3095
|
+
# No stock in the panel: no nominal column was loaded at all.
|
|
3096
|
+
for field in nominal_fields.intersection(fields):
|
|
3097
|
+
if field not in df.columns:
|
|
3098
|
+
df[field] = np.nan
|
|
3014
3099
|
prices = df.pivot_table(values=fields, index=df.index, columns="symbol")
|
|
3015
3100
|
prices = prices.swaplevel(axis=1).sort_index(axis=1)
|
|
3016
3101
|
|