lib-pybroker 2.0.2__tar.gz → 2.0.3__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.
Files changed (53) hide show
  1. {lib_pybroker-2.0.2/src/lib_pybroker.egg-info → lib_pybroker-2.0.3}/PKG-INFO +1 -1
  2. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3/src/lib_pybroker.egg-info}/PKG-INFO +1 -1
  3. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/__init__.py +1 -1
  4. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/eval.py +97 -10
  5. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/ext/data.py +7 -1
  6. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/interval.py +10 -13
  7. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/scope.py +10 -9
  8. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/strategy.py +9 -1
  9. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_data.py +53 -13
  10. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_eval.py +1 -0
  11. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/LICENSE +0 -0
  12. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/MANIFEST.in +0 -0
  13. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/README.md +0 -0
  14. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/pyproject.toml +0 -0
  15. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/setup.cfg +0 -0
  16. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/lib_pybroker.egg-info/SOURCES.txt +0 -0
  17. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/lib_pybroker.egg-info/dependency_links.txt +0 -0
  18. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/lib_pybroker.egg-info/requires.txt +0 -0
  19. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/lib_pybroker.egg-info/top_level.txt +0 -0
  20. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/cache.py +0 -0
  21. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/common.py +0 -0
  22. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/config.py +0 -0
  23. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/context.py +0 -0
  24. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/data.py +0 -0
  25. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/ext/__init__.py +0 -0
  26. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/indicator.py +0 -0
  27. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/log.py +0 -0
  28. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/model.py +0 -0
  29. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/optimize.py +0 -0
  30. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/parallel.py +0 -0
  31. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/portfolio.py +0 -0
  32. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/py.typed +0 -0
  33. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/slippage.py +0 -0
  34. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/src/pybroker/vect.py +0 -0
  35. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_cache.py +0 -0
  36. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_common.py +0 -0
  37. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_context.py +0 -0
  38. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_exit_on_last_bar_perf.py +0 -0
  39. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_hyperparam.py +0 -0
  40. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_indicator.py +0 -0
  41. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_interval.py +0 -0
  42. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_log.py +0 -0
  43. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_model.py +0 -0
  44. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_model_lags.py +0 -0
  45. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_model_per_bar.py +0 -0
  46. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_model_timeseries.py +0 -0
  47. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_optimize.py +0 -0
  48. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_parallel.py +0 -0
  49. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_portfolio.py +0 -0
  50. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_scope.py +0 -0
  51. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_slippage.py +0 -0
  52. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_strategy.py +0 -0
  53. {lib_pybroker-2.0.2 → lib_pybroker-2.0.3}/tests/test_vect.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lib-pybroker
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: Algorithmic trading with machine learning
5
5
  Home-page: http://www.pybroker.com
6
6
  Author: Edward West
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lib-pybroker
3
- Version: 2.0.2
3
+ Version: 2.0.3
4
4
  Summary: Algorithmic trading with machine learning
5
5
  Home-page: http://www.pybroker.com
6
6
  Author: Edward West
@@ -112,4 +112,4 @@ from pybroker.vect import (
112
112
  sumv as sumv,
113
113
  )
114
114
 
115
- __version__ = "2.0.2"
115
+ __version__ = "2.0.3"
@@ -234,19 +234,106 @@ def _bca_boot_conf_sharpe(
234
234
  n = len(x)
235
235
  if not n:
236
236
  return BootConfIntervals(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
237
- idx = np.zeros(n, dtype=np.int64)
238
- x_buff = np.zeros(n)
239
- boot = np.zeros(n_boot)
237
+ # randint masks each 32-bit draw before rejecting indices outside n.
238
+ # Compute that mask once rather than setting up the range for every draw.
239
+ mask = np.uint64(n - 1)
240
+ mask |= mask >> 1
241
+ mask |= mask >> 2
242
+ mask |= mask >> 4
243
+ mask |= mask >> 8
244
+ mask |= mask >> 16
245
+ mask |= mask >> 32
246
+
247
+ def fill_indices(indices):
248
+ if n == 1:
249
+ indices[0] = 0
250
+ elif n > 0x100000000:
251
+ for j in range(n):
252
+ indices[j] = np.uint64(np.random.randint(0, n))
253
+ else:
254
+ j = 0
255
+ while j < n:
256
+ # Bound each batch by the remaining samples so no draws
257
+ # beyond this replicate's random sequence are consumed.
258
+ draws = np.random.randint(0, 0x100000000, n - j)
259
+ for draw in draws:
260
+ index = np.uint64(draw) & mask
261
+ # Rejected indices are overwritten at the same position.
262
+ indices[j] = index
263
+ j += int(index < n)
264
+
265
+ indices = np.empty(n, dtype=np.uint64)
266
+ x_buff = np.empty(n)
267
+ boot = np.empty(n_boot)
240
268
  theta_hat = sharpe_ratio(x, None)
241
269
  z0_count = 0
242
- for i in range(n_boot):
243
- _fill_bootstrap_row(idx, n)
244
- for j in range(n):
245
- x_buff[j] = x[idx[j]]
246
- param = sharpe_ratio(x_buff, None)
247
- boot[i] = param
270
+ total = 0.0
271
+ fill_indices(indices)
272
+ for j in range(n):
273
+ value = x[indices[j]]
274
+ x_buff[j] = value
275
+ total += value
276
+ mean = total / n
277
+ for i in range(1, n_boot):
278
+ # Finish the previous row's variance before overwriting each value
279
+ # with the next draw. Both reductions retain their original order.
280
+ fill_indices(indices)
281
+ total = 0.0
282
+ sum_sq = 0.0
283
+ j = 0
284
+ # Batch gathers to reduce loop overhead without regrouping the sum.
285
+ # Unsigned indices avoid negative-index adjustment for these draws.
286
+ while j + 3 < n:
287
+ idx0 = indices[j]
288
+ idx1 = indices[j + 1]
289
+ idx2 = indices[j + 2]
290
+ idx3 = indices[j + 3]
291
+ value0 = x[idx0]
292
+ value1 = x[idx1]
293
+ value2 = x[idx2]
294
+ value3 = x[idx3]
295
+ diff0 = x_buff[j] - mean
296
+ diff1 = x_buff[j + 1] - mean
297
+ diff2 = x_buff[j + 2] - mean
298
+ diff3 = x_buff[j + 3] - mean
299
+ sum_sq += diff0 * diff0
300
+ sum_sq += diff1 * diff1
301
+ sum_sq += diff2 * diff2
302
+ sum_sq += diff3 * diff3
303
+ x_buff[j] = value0
304
+ x_buff[j + 1] = value1
305
+ x_buff[j + 2] = value2
306
+ x_buff[j + 3] = value3
307
+ total += value0
308
+ total += value1
309
+ total += value2
310
+ total += value3
311
+ j += 4
312
+ for j in range(j, n):
313
+ value = x[indices[j]]
314
+ diff = x_buff[j] - mean
315
+ sum_sq += diff * diff
316
+ x_buff[j] = value
317
+ total += value
318
+ std = (sum_sq / n) ** 0.5
319
+ param = 0.0
320
+ if std != 0:
321
+ param = mean / std
322
+ boot[i - 1] = param
248
323
  if param < theta_hat:
249
324
  z0_count += 1
325
+ mean = total / n
326
+ sum_sq = 0.0
327
+ for j in range(n):
328
+ diff = x_buff[j] - mean
329
+ sum_sq += diff * diff
330
+ std = (sum_sq / n) ** 0.5
331
+ param = 0.0
332
+ if std != 0:
333
+ param = mean / std
334
+ boot[n_boot - 1] = param
335
+ if param < theta_hat:
336
+ z0_count += 1
250
337
  jackknife = _jackknife_sharpe(x)
251
338
  return _bca_intervals_from_boot(boot, z0_count, jackknife, n)
252
339
 
@@ -832,7 +919,7 @@ def ulcer_index(
832
919
  else:
833
920
  dd[i] = (values[i] - peak) / peak * 100
834
921
  return np.sqrt(np.mean(np.square(dd)))
835
- if n <= period:
922
+ if n < period:
836
923
  return 0
837
924
  start = period - 1
838
925
  dd = np.zeros(n - start)
@@ -64,8 +64,14 @@ def _fetch_akshare_symbol(
64
64
  "python -m pip install 'akshare>=1.17.50'"
65
65
  ) from exc
66
66
 
67
+ # akshare 1.20.46 renamed stock_zh_a_hist to stock_zh_a_hist_em (same
68
+ # signature and columns) and dropped the old name, while the supported
69
+ # floor (1.17.50) only has the old one.
70
+ em_hist = getattr(akshare, "stock_zh_a_hist_em", None)
71
+ if em_hist is None:
72
+ em_hist = akshare.stock_zh_a_hist
67
73
  try:
68
- return akshare.stock_zh_a_hist(
74
+ return em_hist(
69
75
  symbol=simple_symbol,
70
76
  start_date=start_date_str,
71
77
  end_date=end_date_str,
@@ -1033,19 +1033,16 @@ def _compute_completed(
1033
1033
  still forming. Interior bins are unaffected: they close at their final base
1034
1034
  bar, matching ``ctx.close[-1]`` being the current bar's close.
1035
1035
  """
1036
- completed = (
1037
- np.searchsorted(ends, np.arange(n, dtype=np.int64), side="right") - 1
1038
- )
1039
- if len(ends) == 0:
1040
- return completed
1041
- last_bin = len(ends) - 1
1042
- if interval_n > 0:
1043
- last_bin_size = ends[last_bin] - starts[last_bin] + 1
1044
- if last_bin_size >= interval_n:
1045
- return completed
1046
- mask = completed == last_bin
1047
- completed = completed.copy()
1048
- completed[mask] = last_bin - 1
1036
+ # Bins are contiguous, so the map is a running count of closed bin ends.
1037
+ # A still-forming trailing bin does not close at its final bar.
1038
+ closed = ends
1039
+ if len(ends) and (
1040
+ interval_n <= 0 or ends[-1] - starts[-1] + 1 < interval_n
1041
+ ):
1042
+ closed = ends[:-1]
1043
+ completed = np.bincount(closed, minlength=n)
1044
+ np.cumsum(completed, out=completed)
1045
+ completed -= 1
1049
1046
  return completed
1050
1047
 
1051
1048
 
@@ -654,13 +654,10 @@ def _build_sliced_sym_arrays(
654
654
  """Builds per-symbol column arrays for ``indices`` row selection."""
655
655
  if len(indices) == 0:
656
656
  return {}
657
- if len(indices) > 0 and indices[-1] - indices[0] + 1 == len(indices):
657
+ if indices[-1] - indices[0] + 1 == len(indices):
658
658
  start = int(indices[0])
659
659
  end = int(indices[-1]) + 1
660
- return {
661
- col: np.array(arr[start:end], copy=True)
662
- for col, arr in sym_data.items()
663
- }
660
+ return {col: arr[start:end].copy() for col, arr in sym_data.items()}
664
661
  float_cols: list[str] = []
665
662
  other_cols: list[str] = []
666
663
  for col, arr in sym_data.items():
@@ -885,6 +882,9 @@ def slice_symbol_array_store_by_dates(
885
882
  if len(target) == 0:
886
883
  return SymbolArrayStore(frozenset(), {})
887
884
  target_sorted = len(target) <= 1 or bool(np.all(target[:-1] <= target[1:]))
885
+ # A backing is lex-sorted by (symbol, date) when built, so only plain
886
+ # stores need a per-symbol sortedness scan.
887
+ backed = store.backing is not None
888
888
  sym_arrays: dict[str, dict[str, NDArray]] = {}
889
889
  for sym in store.symbols:
890
890
  sym_data = store.sym_arrays[sym]
@@ -892,10 +892,11 @@ def slice_symbol_array_store_by_dates(
892
892
  if dates is None or len(dates) == 0:
893
893
  continue
894
894
  dates_arr = np.ascontiguousarray(dates, dtype="datetime64[ns]")
895
- dates_sorted = len(dates_arr) <= 1 or bool(
896
- np.all(dates_arr[:-1] <= dates_arr[1:])
897
- )
898
- if target_sorted and dates_sorted:
895
+ if target_sorted and (
896
+ backed
897
+ or len(dates_arr) <= 1
898
+ or (dates_arr[:-1] <= dates_arr[1:]).all()
899
+ ):
899
900
  indices = _sorted_dates_indices_njit(dates_arr, target)
900
901
  else:
901
902
  mask = _dates_in_target_mask(dates_arr, target)
@@ -149,7 +149,15 @@ def _date_to_active_syms(
149
149
  test_dates_set = frozenset(test_dates)
150
150
  if not sym_exec_dates:
151
151
  return {}, True
152
- aligned = all(dates == test_dates_set for dates in sym_exec_dates.values())
152
+ n = len(test_dates_set)
153
+ test_arr = np.sort(np.fromiter(test_dates_set, "datetime64[ns]", n))
154
+ aligned = all(
155
+ len(dates) == n
156
+ and np.array_equal(
157
+ np.sort(np.fromiter(dates, "datetime64[ns]", n)), test_arr
158
+ )
159
+ for dates in sym_exec_dates.values()
160
+ )
153
161
  if aligned:
154
162
  return {}, True
155
163
  date_to_syms: dict[np.datetime64, list[str]] = defaultdict(list)
@@ -29,6 +29,14 @@ from pybroker.ext.data import YQuery
29
29
  from unittest import mock
30
30
  from yahooquery import Ticker
31
31
 
32
+ # akshare 1.20.46 renamed stock_zh_a_hist to stock_zh_a_hist_em; patch
33
+ # whichever name the installed version exposes.
34
+ _AK_EM_HIST = (
35
+ "stock_zh_a_hist_em"
36
+ if hasattr(akshare, "stock_zh_a_hist_em")
37
+ else "stock_zh_a_hist"
38
+ )
39
+
32
40
  API_KEY = "api_key"
33
41
  API_SECRET = "api_secret"
34
42
  API_VERSION = "v2"
@@ -752,7 +760,7 @@ class TestAKShare:
752
760
  "symbol": symbols,
753
761
  }
754
762
  )
755
- with mock.patch("akshare.stock_zh_a_hist", return_value=expected_df):
763
+ with mock.patch.object(akshare, _AK_EM_HIST, return_value=expected_df):
756
764
  df = ak.query(symbols, START_DATE, END_DATE, timeframe)
757
765
  assert set(df.columns) == {
758
766
  "date",
@@ -785,8 +793,9 @@ class TestAKShare:
785
793
  @pytest.mark.usefixtures("setup_ds_cache")
786
794
  def test_query_when_empty_result(self, columns):
787
795
  ak = AKShare()
788
- with mock.patch(
789
- "akshare.stock_zh_a_hist",
796
+ with mock.patch.object(
797
+ akshare,
798
+ _AK_EM_HIST,
790
799
  return_value=pd.DataFrame(columns=columns),
791
800
  ):
792
801
  df = ak.query(["A"], START_DATE, END_DATE)
@@ -822,12 +831,13 @@ class TestAKShare:
822
831
  }
823
832
  )
824
833
  with (
825
- mock.patch(
826
- "akshare.stock_zh_a_hist",
834
+ mock.patch.object(
835
+ akshare,
836
+ _AK_EM_HIST,
827
837
  side_effect=ConnectionError("failed"),
828
838
  ),
829
- mock.patch(
830
- "akshare.stock_zh_a_hist_tx", return_value=expected_df
839
+ mock.patch.object(
840
+ akshare, "stock_zh_a_hist_tx", return_value=expected_df
831
841
  ) as mock_tx,
832
842
  ):
833
843
  df = ak.query(symbols, START_DATE, END_DATE, "1d")
@@ -875,7 +885,7 @@ class TestAKShare:
875
885
  with (
876
886
  mock.patch.object(
877
887
  akshare,
878
- "stock_zh_a_hist",
888
+ _AK_EM_HIST,
879
889
  side_effect=ConnectionError("failed"),
880
890
  ),
881
891
  mock.patch.object(
@@ -903,7 +913,7 @@ class TestAKShare:
903
913
  produced a clean empty backtest with no diagnostic, where YQuery
904
914
  raises for the identical condition."""
905
915
  ak = AKShare()
906
- with mock.patch("akshare.stock_zh_a_hist") as fetch:
916
+ with mock.patch.object(akshare, _AK_EM_HIST) as fetch:
907
917
  with pytest.raises(ValueError, match="Unsupported timeframe"):
908
918
  ak.query(["A"], START_DATE, END_DATE, "2d")
909
919
  assert not fetch.called
@@ -915,6 +925,36 @@ class TestAKShare:
915
925
  with pytest.raises(ImportError, match="akshare>=1.17.50"):
916
926
  ak.query(["A"], START_DATE, END_DATE)
917
927
 
928
+ @pytest.mark.parametrize(
929
+ "em_name", ["stock_zh_a_hist_em", "stock_zh_a_hist"]
930
+ )
931
+ @pytest.mark.usefixtures("setup_ds_cache")
932
+ def test_query_uses_available_em_endpoint_name(self, em_name):
933
+ """akshare 1.20.46 renamed stock_zh_a_hist to stock_zh_a_hist_em and
934
+ dropped the old name. Both must work, whatever version is installed
935
+ here, since the supported floor predates the rename."""
936
+ em_df = pd.DataFrame(
937
+ {
938
+ "日期": [END_DATE],
939
+ "开盘": [1],
940
+ "收盘": [2],
941
+ "最高": [3],
942
+ "最低": [4],
943
+ "成交量": [5],
944
+ }
945
+ )
946
+ fetch = mock.Mock(return_value=em_df)
947
+ stub = mock.Mock(spec=[em_name, "stock_zh_a_hist_tx"])
948
+ setattr(stub, em_name, fetch)
949
+ ak = AKShare()
950
+ with mock.patch.dict(sys.modules, {"akshare": stub}):
951
+ df = ak.query(["000001"], START_DATE, END_DATE)
952
+ fetch.assert_called_once()
953
+ stub.stock_zh_a_hist_tx.assert_not_called()
954
+ assert len(df) == 1
955
+ assert df["close"].iloc[0] == 2
956
+ assert df["volume"].iloc[0] == 5
957
+
918
958
 
919
959
  class TestYQuery:
920
960
  @pytest.mark.usefixtures("setup_ds_cache")
@@ -1026,7 +1066,7 @@ class TestExtDataEdgeCases:
1026
1066
  columns=["日期", "开盘", "收盘", "最高", "最低", "成交量"]
1027
1067
  )
1028
1068
  ak = AKShare()
1029
- with mock.patch("akshare.stock_zh_a_hist", return_value=empty_em):
1069
+ with mock.patch.object(akshare, _AK_EM_HIST, return_value=empty_em):
1030
1070
  df = ak.query(["A"], START_DATE, END_DATE, "1d")
1031
1071
  assert df.empty
1032
1072
  assert {"date", "symbol", "open", "high", "low", "close"} <= set(
@@ -1069,9 +1109,9 @@ class TestExtDataEdgeCases:
1069
1109
  return em_frame.copy()
1070
1110
 
1071
1111
  ak = AKShare()
1072
- with mock.patch("akshare.stock_zh_a_hist", side_effect=em_fetch):
1073
- with mock.patch(
1074
- "akshare.stock_zh_a_hist_tx", return_value=tx_frame.copy()
1112
+ with mock.patch.object(akshare, _AK_EM_HIST, side_effect=em_fetch):
1113
+ with mock.patch.object(
1114
+ akshare, "stock_zh_a_hist_tx", return_value=tx_frame.copy()
1075
1115
  ):
1076
1116
  df = ak.query(["000001", "000002"], START_DATE, END_DATE)
1077
1117
  assert len(df) == 2
@@ -401,6 +401,7 @@ def test_relative_entropy(values, expected_entropy):
401
401
  # An explicit period keeps the trailing-window variant.
402
402
  ([100, 101, 102, 100, 99, 103, 103, 102], 2, 0.909259),
403
403
  ([100, 101, 102, 100, 99, 103, 103, 102], 1, 0),
404
+ ([100, 90, 95], 3, 5),
404
405
  ([0, 0, 0, 0, 0], 2, 0),
405
406
  ([1, 1, 1, 1, 1], 2, 0),
406
407
  ([100], 14, 0),
File without changes
File without changes
File without changes
File without changes