ivolatility-backtesting 2.156__tar.gz → 2.157__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 (29) hide show
  1. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/PKG-INFO +1 -1
  2. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting/ivolatility_backtesting.py +19 -8
  3. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting.egg-info/PKG-INFO +1 -1
  4. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting.egg-info/SOURCES.txt +2 -1
  5. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/pyproject.toml +1 -1
  6. ivolatility_backtesting-2.157/tests/test_2157_sl_symbol.py +94 -0
  7. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/README.md +0 -0
  8. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting/__init__.py +0 -0
  9. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting.egg-info/dependency_links.txt +0 -0
  10. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting.egg-info/requires.txt +0 -0
  11. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/ivolatility_backtesting.egg-info/top_level.txt +0 -0
  12. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/setup.cfg +0 -0
  13. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2142_fixes.py +0 -0
  14. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2144_duckdb_dedup.py +0 -0
  15. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2146_vix_vro.py +0 -0
  16. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2147_cents_multiplier.py +0 -0
  17. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2148_loader_memory.py +0 -0
  18. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2149_client_errors.py +0 -0
  19. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2150_prompt_patterns.py +0 -0
  20. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2152_cache_lock_and_rate_limit.py +0 -0
  21. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2153_cache_lock_kept.py +0 -0
  22. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2154_put_atm_legs.py +0 -0
  23. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2155_bar_prefetch.py +0 -0
  24. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2155_intraday_exits.py +0 -0
  25. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2155_ivx_stock_holes.py +0 -0
  26. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2155_key_redaction.py +0 -0
  27. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2155_long_expiry.py +0 -0
  28. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2155_opt_stop_merge.py +0 -0
  29. {ivolatility_backtesting-2.156 → ivolatility_backtesting-2.157}/tests/test_2156_phantom_quotes.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ivolatility_backtesting
3
- Version: 2.156
3
+ Version: 2.157
4
4
  Summary: A universal backtesting framework for financial strategies using the IVolatility API.
5
5
  Author-email: IVolatility <support@ivolatility.com>
6
6
  Project-URL: Homepage, https://ivolatility.com
@@ -9713,8 +9713,17 @@ class StopLossManager:
9713
9713
  # Debug level (0=off, 1=basic, 2=detailed)
9714
9714
  # Debug level (0=off, 1=basic, 2=detailed)
9715
9715
  self.debuginfo = debuginfo
9716
-
9717
- def add_position(self, position_id, entry_price, entry_date, stop_type='fixed_pct',
9716
+
9717
+ def _position_symbol(self, pos, kwargs=None):
9718
+ """Underlying of the intraday bar request: the position's, else the check's, else the config's.
9719
+ None means no bars are read and the EOD verdict stands; a ticker is never guessed."""
9720
+ sym = pos.get('symbol') or (kwargs or {}).get('symbol') or self.config.get('symbol')
9721
+ if not sym and not getattr(self, '_warned_no_symbol', False):
9722
+ self._warned_no_symbol = True
9723
+ print("⚠️ [INTRADAY SL] position has no symbol: intraday bars skipped, the EOD verdict stands")
9724
+ return sym
9725
+
9726
+ def add_position(self, position_id, entry_price, entry_date, stop_type='fixed_pct',
9718
9727
  stop_value=0.05, atr=None, trailing_distance=None, use_pnl_pct=False,
9719
9728
  is_short_bias=False, **kwargs):
9720
9729
  """
@@ -10707,7 +10716,7 @@ class StopLossManager:
10707
10716
  baseline_from_close = (pos.get('sl_baseline_reset')
10708
10717
  and pos.get('_sl_baseline_eod_date') is not None
10709
10718
  and pos.get('_sl_baseline_eod_date') == current_date)
10710
- if (intraday_mode in ('disabled', 'eod_only') and not _is_cc_sl) or baseline_from_close:
10719
+ if (intraday_mode in ('disabled', 'eod_only') and not _is_cc_sl) or baseline_from_close or not symbol:
10711
10720
  if debug:
10712
10721
  print(f" [P&L Stop Intraday] DISABLED, using EOD. P&L={eod_pnl_pct:.2f}%")
10713
10722
  return eod_triggered, {
@@ -11067,7 +11076,7 @@ class StopLossManager:
11067
11076
  )
11068
11077
  if (intraday_mode in ('auto', 'enabled') or _is_cc_baseline) and current_date is not None:
11069
11078
  try:
11070
- symbol = pos.get('symbol', kwargs.get('symbol', 'SPY'))
11079
+ symbol = self._position_symbol(pos, kwargs)
11071
11080
  strategy_type = pos.get('strategy_type', '')
11072
11081
  expiration = pos.get('expiration')
11073
11082
 
@@ -11260,7 +11269,7 @@ class StopLossManager:
11260
11269
 
11261
11270
  if triggered or _intraday_mode_of(self.config) in ('auto', 'enabled'):
11262
11271
  try:
11263
- symbol = pos.get('symbol', 'SPY')
11272
+ symbol = self._position_symbol(pos, extended_kwargs)
11264
11273
  intraday_triggered, intraday_info = self.check_pl_stop_intraday(
11265
11274
  pos, current_price, level, current_date, symbol,
11266
11275
  eod_triggered=triggered, pnl_base=extended_kwargs.get('pnl_base'),
@@ -11309,7 +11318,7 @@ class StopLossManager:
11309
11318
  )
11310
11319
  if _is_cc_pos:
11311
11320
  try:
11312
- _cc_symbol = pos.get('symbol', extended_kwargs.get('symbol', 'SPY'))
11321
+ _cc_symbol = self._position_symbol(pos, extended_kwargs)
11313
11322
  _cc_loss_threshold = level # e.g., -10.0 or -40.0
11314
11323
  _cc_intraday_triggered, _cc_intraday_info = self.check_pl_stop_intraday(
11315
11324
  pos, extended_kwargs.get('pnl_pct', 0), _cc_loss_threshold, current_date, _cc_symbol
@@ -11343,7 +11352,7 @@ class StopLossManager:
11343
11352
  intraday_mode = intraday_settings.get('intraday_mode', 'disabled')
11344
11353
  if level is not None and intraday_mode in ('auto', 'enabled'):
11345
11354
  try:
11346
- symbol = pos.get('symbol', extended_kwargs.get('symbol', 'SPY'))
11355
+ symbol = self._position_symbol(pos, extended_kwargs)
11347
11356
  intraday_triggered, intraday_info = self.check_pl_stop_intraday(
11348
11357
  pos, extended_kwargs.get('pnl_pct', 0), level, current_date, symbol,
11349
11358
  eod_triggered=triggered,
@@ -12358,7 +12367,7 @@ class StopLossManager:
12358
12367
  if should_check_intraday:
12359
12368
  try:
12360
12369
  current_date = kwargs.get('current_date')
12361
- symbol = pos.get('symbol', 'SPY')
12370
+ symbol = self._position_symbol(pos, kwargs)
12362
12371
  loss_threshold_pct = -pl_threshold * 100 # e.g., -12.0
12363
12372
 
12364
12373
  _, options_intraday_info = self.check_pl_stop_intraday(
@@ -13415,6 +13424,8 @@ class PositionManager:
13415
13424
  for _field in _spread_fields:
13416
13425
  if _field in kwargs:
13417
13426
  _sl_kwargs[_field] = kwargs[_field]
13427
+ # symbol is a named argument of open_position, so the loop above never sees it
13428
+ _sl_kwargs.setdefault('symbol', symbol)
13418
13429
  self.sl_manager.add_position(**_sl_kwargs)
13419
13430
 
13420
13431
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ivolatility_backtesting
3
- Version: 2.156
3
+ Version: 2.157
4
4
  Summary: A universal backtesting framework for financial strategies using the IVolatility API.
5
5
  Author-email: IVolatility <support@ivolatility.com>
6
6
  Project-URL: Homepage, https://ivolatility.com
@@ -24,4 +24,5 @@ tests/test_2155_ivx_stock_holes.py
24
24
  tests/test_2155_key_redaction.py
25
25
  tests/test_2155_long_expiry.py
26
26
  tests/test_2155_opt_stop_merge.py
27
- tests/test_2156_phantom_quotes.py
27
+ tests/test_2156_phantom_quotes.py
28
+ tests/test_2157_sl_symbol.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ivolatility_backtesting"
7
- version = "2.156"
7
+ version = "2.157"
8
8
  description = "A universal backtesting framework for financial strategies using the IVolatility API."
9
9
  readme = "README.md"
10
10
  authors = [
@@ -0,0 +1,94 @@
1
+ # Intraday stop checks ask for the bars of the position's own underlying, never a guessed ticker.
2
+ # Run: python3 tests/test_2157_sl_symbol.py (or pytest). No network. IVB_LIB_DIR overrides the library folder.
3
+ import datetime as dt
4
+ import os
5
+ import sys
6
+ import tempfile
7
+ import traceback
8
+
9
+ import matplotlib
10
+ matplotlib.use('Agg')
11
+
12
+ os.environ.setdefault('MPLCONFIGDIR', matplotlib.get_configdir())
13
+ os.environ['HOME'] = tempfile.mkdtemp(prefix='ivb_home_')
14
+
15
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16
+ sys.path.insert(0, os.environ.get('IVB_LIB_DIR') or os.path.join(_REPO, 'ivolatility_backtesting'))
17
+ import ivolatility_backtesting as lib # noqa: E402
18
+
19
+ ENTRY = dt.date(2024, 3, 21)
20
+ DAY = dt.date(2024, 3, 22)
21
+ EXP = dt.date(2024, 5, 3)
22
+ INTRADAY = {'intraday_mode': 'auto', 'minute_interval': 'MINUTE_5'}
23
+
24
+
25
+ def _config(stop):
26
+ sl = {'enabled': True, 'type': stop, 'intraday_settings': dict(INTRADAY)}
27
+ if stop == 'combined':
28
+ sl['combined_settings'] = {'pl_loss': 0.30, 'directional': 0.10, 'logic': 'or'}
29
+ else:
30
+ sl['value'] = 0.30
31
+ return {'symbol': 'TSLA', 'debuginfo': 0, 'strategy_type': 'STRADDLE', 'stop_loss_enabled': True,
32
+ 'stop_loss_config': sl, 'intraday_settings': dict(INTRADAY)}
33
+
34
+
35
+ def _short_straddle(stop):
36
+ pm = lib.PositionManager(_config(stop), debug=False)
37
+ pm.open_position(position_id='S1', symbol='TSLA', entry_date=ENTRY, entry_price=0.0, quantity=400,
38
+ strategy_type='STRADDLE', contracts=4, total_cost=-8360.0, is_short_bias=True,
39
+ entry_max_risk=8360.0, strike=175.0, expiration=EXP, underlying_entry_price=172.8,
40
+ call_entry_bid=10.9, call_entry_ask=12.75, put_entry_bid=10.0, put_entry_ask=13.1)
41
+ return pm
42
+
43
+
44
+ def _capture_symbol(mgr):
45
+ seen = []
46
+
47
+ def fake(pos, eod_pnl_pct, loss_threshold_pct, current_date, symbol, **kw):
48
+ seen.append(symbol)
49
+ return kw.get('eod_triggered', False), {'stop_triggered_by': 'pl_loss_eod',
50
+ 'options_stop_trigger_time': 'EOD'}
51
+ mgr.check_pl_stop_intraday = fake
52
+ return seen
53
+
54
+
55
+ def test_open_position_hands_symbol_to_stop_manager():
56
+ pm = _short_straddle('combined')
57
+ assert pm.sl_manager.positions['S1'].get('symbol') == 'TSLA', pm.sl_manager.positions['S1'].get('symbol')
58
+
59
+
60
+ def _check(pm, **extra):
61
+ return pm.sl_manager.check_stop('S1', current_price=-19.0, current_date=DAY, position_type='STRADDLE',
62
+ pnl_pct=-19.0, current_pnl=-1600.0, total_cost=-8360.0,
63
+ underlying_price=171.0, underlying_entry_price=172.8,
64
+ underlying_change_pct=-1.0, underlying_high=173.0, underlying_low=170.0,
65
+ **extra)
66
+
67
+
68
+ def test_combined_stop_reads_bars_of_the_position_underlying():
69
+ pm = _short_straddle('combined')
70
+ seen = _capture_symbol(pm.sl_manager)
71
+ _check(pm)
72
+ assert seen and set(seen) == {'TSLA'}, seen
73
+
74
+
75
+ def test_no_symbol_anywhere_reads_no_bars():
76
+ mgr = lib.StopLossManager(config={'stop_loss_config': _config('combined')['stop_loss_config']}, debuginfo=0)
77
+ assert mgr._position_symbol({}, {}) is None
78
+ triggered, info = mgr.check_pl_stop_intraday({'strategy_type': 'STRADDLE'}, -19.0, -30.0, DAY, None,
79
+ eod_triggered=False)
80
+ assert triggered is False and info.get('intraday_data_available') is False, info
81
+
82
+
83
+ if __name__ == '__main__':
84
+ failed = 0
85
+ for name, fn in list(globals().items()):
86
+ if name.startswith('test_') and callable(fn):
87
+ try:
88
+ fn()
89
+ print(f'PASS {name}')
90
+ except Exception:
91
+ failed += 1
92
+ print(f'FAIL {name}')
93
+ traceback.print_exc()
94
+ sys.exit(1 if failed else 0)