ivolatility-backtesting 2.147__tar.gz → 2.151__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 (18) hide show
  1. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/PKG-INFO +2 -2
  2. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting/__init__.py +2 -0
  3. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting/ivolatility_backtesting.py +204 -85
  4. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting.egg-info/PKG-INFO +2 -2
  5. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting.egg-info/SOURCES.txt +4 -1
  6. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting.egg-info/requires.txt +1 -1
  7. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/pyproject.toml +2 -2
  8. ivolatility_backtesting-2.151/tests/test_2148_loader_memory.py +223 -0
  9. ivolatility_backtesting-2.151/tests/test_2149_client_errors.py +131 -0
  10. ivolatility_backtesting-2.151/tests/test_2150_prompt_patterns.py +378 -0
  11. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/README.md +0 -0
  12. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting.egg-info/dependency_links.txt +0 -0
  13. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/ivolatility_backtesting.egg-info/top_level.txt +0 -0
  14. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/setup.cfg +0 -0
  15. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/tests/test_2142_fixes.py +0 -0
  16. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/tests/test_2144_duckdb_dedup.py +0 -0
  17. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/tests/test_2146_vix_vro.py +0 -0
  18. {ivolatility_backtesting-2.147 → ivolatility_backtesting-2.151}/tests/test_2147_cents_multiplier.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ivolatility_backtesting
3
- Version: 2.147
3
+ Version: 2.151
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
@@ -19,7 +19,7 @@ Requires-Dist: pandas<3.0,>=1.5.0
19
19
  Requires-Dist: numpy<3.0,>=1.21.0
20
20
  Requires-Dist: matplotlib>=3.5.0
21
21
  Requires-Dist: seaborn>=0.11.0
22
- Requires-Dist: ivolatility>=1.8.2
22
+ Requires-Dist: ivolatility>=1.9.6
23
23
  Requires-Dist: psutil>=7.1.0
24
24
 
25
25
  # IVolatility Backtesting Framework v1.32
@@ -28,6 +28,7 @@ from .ivolatility_backtesting import (
28
28
  FUTURES_HEDGE_MAP, _load_futures_for_hedge,
29
29
  set_project_root, get_project_root,
30
30
  get_position_multiplier, get_hedge_multiplier,
31
+ parse_earnings_calendar,
31
32
  )
32
33
 
33
34
  __all__ = [
@@ -60,4 +61,5 @@ __all__ = [
60
61
  'FUTURES_HEDGE_MAP', '_load_futures_for_hedge',
61
62
  'set_project_root', 'get_project_root',
62
63
  'get_position_multiplier', 'get_hedge_multiplier',
64
+ 'parse_earnings_calendar',
63
65
  ]
@@ -40,13 +40,24 @@ import os
40
40
  # Distinctive User-Agent on ALL REST traffic: lets the gateway/Graylog tell
41
41
  # framework calls apart from hand-written scripts, and workspace runs from
42
42
  # local pip installs (CODER_AGENT_URL is set inside Coder workspaces only).
43
+ def _ivb_client():
44
+ if os.getenv('IVOL_CLIENT'):
45
+ return os.getenv('IVOL_CLIENT')
46
+ if os.getenv('CODER_AGENT_URL'):
47
+ return 'workspace'
48
+ # venv created by the IVolAI plugin (ivolai-dist setup_env.sh); pods use /home/coder/.venv
49
+ if os.path.normpath(__import__('sys').prefix).endswith(os.path.join('ivolatility', '.venv')):
50
+ return 'plugin'
51
+ return 'local'
52
+
53
+
43
54
  def _ivb_user_agent():
44
55
  try:
45
56
  from importlib.metadata import version as _pkg_version
46
57
  _ver = _pkg_version('ivolatility_backtesting')
47
58
  except Exception:
48
59
  _ver = 'dev'
49
- _env = 'workspace' if os.getenv('CODER_AGENT_URL') else 'local'
60
+ _env = _ivb_client()
50
61
  try:
51
62
  import requests as _rq
52
63
  _rq_ver = _rq.__version__
@@ -518,9 +529,8 @@ from typing import Callable, Dict, List, Optional, Tuple, Union, Any
518
529
  # malloc_trim→250MB). malloc_trim(0) forces glibc to return free pages. Resolved
519
530
  # lazily once; a no-op on musl/macOS (returns None / raises → swallowed).
520
531
  _libc_malloc_trim = None
521
- def _release_freed_memory():
522
- """gc.collect() + glibc malloc_trim(0) — actually shrinks RSS, unlike gc alone."""
523
- gc.collect()
532
+ def _trim_heap():
533
+ """glibc malloc_trim(0) without a gc pass — cheap enough to call after every chunk."""
524
534
  global _libc_malloc_trim
525
535
  if _libc_malloc_trim is None:
526
536
  try:
@@ -534,6 +544,67 @@ def _release_freed_memory():
534
544
  except Exception:
535
545
  pass
536
546
 
547
+
548
+ def _release_freed_memory():
549
+ """gc.collect() + glibc malloc_trim(0) — actually shrinks RSS, unlike gc alone."""
550
+ gc.collect()
551
+ _trim_heap()
552
+
553
+
554
+ import threading
555
+
556
+ # Admission control by cgroup free memory; the first download always passes so a load never stalls.
557
+ def _env_int(name, default):
558
+ try:
559
+ return int(os.environ.get(name, default))
560
+ except ValueError:
561
+ return default
562
+
563
+
564
+ _API_MEM_EST_MB = _env_int('IVOL_API_MEM_EST_MB', 150)
565
+ _API_MEM_SAFETY_MB = _env_int('IVOL_API_MEM_SAFETY_MB', 300)
566
+ _api_mem_lock = threading.Lock()
567
+ _api_mem_inflight = 0
568
+
569
+
570
+ def _cgroup_free_mb():
571
+ """Container free memory in MB (limit minus usage excluding inactive file cache); None without a cgroup v2 limit."""
572
+ try:
573
+ with open('/sys/fs/cgroup/memory.max') as f:
574
+ raw = f.read().strip()
575
+ if raw == 'max':
576
+ return None
577
+ with open('/sys/fs/cgroup/memory.current') as f:
578
+ current = int(f.read())
579
+ inactive = 0
580
+ with open('/sys/fs/cgroup/memory.stat') as f:
581
+ for line in f:
582
+ if line.startswith('inactive_file '):
583
+ inactive = int(line.split()[1])
584
+ break
585
+ return (int(raw) - (current - inactive)) >> 20
586
+ except (OSError, ValueError):
587
+ return None
588
+
589
+
590
+ def _api_mem_acquire():
591
+ """Block until the container can fit one more in-flight download."""
592
+ global _api_mem_inflight
593
+ while True:
594
+ with _api_mem_lock:
595
+ free_mb = _cgroup_free_mb()
596
+ if (free_mb is None or _api_mem_inflight == 0
597
+ or free_mb - _api_mem_inflight * _API_MEM_EST_MB >= _API_MEM_EST_MB + _API_MEM_SAFETY_MB):
598
+ _api_mem_inflight += 1
599
+ return
600
+ time.sleep(0.5)
601
+
602
+
603
+ def _api_mem_release():
604
+ global _api_mem_inflight
605
+ with _api_mem_lock:
606
+ _api_mem_inflight = max(0, _api_mem_inflight - 1)
607
+
537
608
  # Trim once at import: if the lib is re-imported into a kernel that already ran
538
609
  # heavy work, hand back any glibc arenas before this session allocates.
539
610
  _release_freed_memory()
@@ -687,6 +758,12 @@ def _get_options_eod_table(config_like: Optional[Dict[str, Any]]) -> str:
687
758
  return 'options_eod_1545' if snap == '1545' else 'options_eod_close'
688
759
 
689
760
 
761
+ def _complete_option_dates_sql(table: str) -> str:
762
+ """Dates holding both calls and puts; a date with a single type is treated as not loaded."""
763
+ return (f"SELECT date FROM {table} WHERE symbol = ? "
764
+ f"GROUP BY date HAVING COUNT(DISTINCT UPPER(SUBSTR(CAST(type AS VARCHAR), 1, 1))) >= 2")
765
+
766
+
690
767
  def _get_legacy_1545_duckdb_path(config_like: Optional[Dict[str, Any]]) -> str:
691
768
  """Legacy pre-refactor 15:45 DuckDB path kept for one-time migration."""
692
769
  cache_dir = _resolve_path((config_like or {}).get('cache_dir', 'cache'))
@@ -1733,8 +1810,10 @@ def _create_sdk_like_session():
1733
1810
  from urllib3.util.retry import Retry
1734
1811
  from requests.adapters import HTTPAdapter
1735
1812
  s = _req.Session()
1736
- retry = Retry(total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504],
1737
- allowed_methods=["GET", "POST", "DELETE"])
1813
+ # SDK >= 1.9.6 pauses before the first retry; reuse it so thread sessions behave the same
1814
+ retry = getattr(_ivol_inner, '_retry_strategy', None) or Retry(
1815
+ total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504],
1816
+ allowed_methods=["GET", "POST", "DELETE"])
1738
1817
  adapter = HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=retry)
1739
1818
  s.mount("https://", adapter)
1740
1819
  s.mount("http://", adapter)
@@ -3667,6 +3746,11 @@ STRATEGIES = {
3667
3746
  },
3668
3747
  }
3669
3748
 
3749
+ STRATEGIES['DIRECTIONAL_LONG'] = {**STRATEGIES['LONG_STOCK'], 'name': 'Directional Long (stock)'}
3750
+ STRATEGIES['DIRECTIONAL_SHORT'] = {**STRATEGIES['SHORT_STOCK'], 'name': 'Directional Short (stock)'}
3751
+ STOCK_STRATEGY_TYPES = ('LONG_STOCK', 'SHORT_STOCK', 'DIRECTIONAL_LONG', 'DIRECTIONAL_SHORT')
3752
+ SHORT_STOCK_TYPES = ('SHORT_STOCK', 'DIRECTIONAL_SHORT')
3753
+
3670
3754
  # Backwards compatibility mapping (lowercase to UPPERCASE)
3671
3755
  _STRATEGY_NAME_MAP = {
3672
3756
  'iron_condor': 'IRON_CONDOR',
@@ -6079,7 +6163,7 @@ class StrategyRegistry:
6079
6163
  call_premium = position.get('call_premium', abs(credit))
6080
6164
  capital_at_risk = max(stock_value - call_premium, stock_value * 0.8)
6081
6165
  locked_capital = stock_value
6082
- elif position_type in ('LONG_STOCK', 'SHORT_STOCK'):
6166
+ elif position_type in STOCK_STRATEGY_TYPES:
6083
6167
  stock_value = underlying_price * contracts * um
6084
6168
  capital_at_risk = stock_value
6085
6169
  locked_capital = stock_value
@@ -6351,13 +6435,13 @@ class StrategyRegistry:
6351
6435
  # Universal CREDIT detection (works for ANY strategy)
6352
6436
  is_credit = cls.is_credit_strategy(strategy_type=strategy_type, position=position)
6353
6437
 
6354
- # ⚠️ CRITICAL: For NEUTRAL strategies (STRADDLE, STRANGLE) without 'short' prefix
6355
- # If ALL legs don't have 'short' prefix AND strategy is CREDIT → treat ALL as SHORT
6356
- if is_credit and not is_short_leg and category == 'NEUTRAL':
6438
+ # ⚠️ CRITICAL: NEUTRAL (STRADDLE, STRANGLE) and single-leg CREDIT (SHORT_PUT, SHORT_CALL)
6439
+ # legs carry no 'short' prefix. If ALL legs lack it AND position is CREDIT → treat ALL as SHORT
6440
+ if is_credit and not is_short_leg and category in ('NEUTRAL', 'CREDIT'):
6357
6441
  # Check if NO legs have 'short' prefix (simple STRADDLE/STRANGLE)
6358
6442
  has_short_prefix = any('short' in l['name'].lower() for l in legs)
6359
6443
  if not has_short_prefix:
6360
- # Simple STRADDLE/STRANGLE: all legs are SHORT for CREDIT
6444
+ # STRADDLE/STRANGLE, SHORT_PUT/SHORT_CALL: all legs are SHORT for CREDIT
6361
6445
  is_short_leg = True
6362
6446
 
6363
6447
  # ⚠️ FIX: For DEBIT strategies used as CREDIT (e.g. DCS SELL, calendar SELL)
@@ -6561,7 +6645,7 @@ class StrategyRegistry:
6561
6645
  # total_cost is negative (credit from selling call), so adding it reduces risk
6562
6646
  return (stock_price * um * contracts) + total_cost # subtract call premium
6563
6647
 
6564
- elif strategy_type in ('LONG_STOCK', 'SHORT_STOCK'):
6648
+ elif strategy_type in STOCK_STRATEGY_TYPES:
6565
6649
  stock_price = position_params.get('stock_price', position_params.get('underlying_entry_price', 0))
6566
6650
  return stock_price * um * contracts
6567
6651
 
@@ -6760,13 +6844,13 @@ class StrategyRegistry:
6760
6844
  # doubling the call bid while completely missing the stock value.
6761
6845
  # Fix: compute P&L directly using underlying_price (available here) + call leg.
6762
6846
  # ── LONG_STOCK / SHORT_STOCK — pure stock position, no option legs ──
6763
- if strategy_type in ('LONG_STOCK', 'SHORT_STOCK'):
6847
+ if strategy_type in STOCK_STRATEGY_TYPES:
6764
6848
  stock_exit_value = underlying_price * contracts * um
6765
- if strategy_type == 'SHORT_STOCK':
6849
+ if strategy_type in SHORT_STOCK_TYPES:
6766
6850
  pnl = entry_cost - stock_exit_value # short: profit when price drops
6767
6851
  else:
6768
6852
  pnl = stock_exit_value - entry_cost # long: profit when price rises
6769
- is_credit = (strategy_type == 'SHORT_STOCK')
6853
+ is_credit = (strategy_type in SHORT_STOCK_TYPES)
6770
6854
  close_cost = stock_exit_value
6771
6855
  # ── COVERED_CALL / COVERED_PUT — stock + option leg ──────────
6772
6856
  elif (strategy_type in ('COVERED_CALL', 'COVERED_CALL_ATM', 'COVERED_CALL_25D', 'COVERED_PUT', 'COVERED_PUT_ATM', 'COVERED_PUT_25D')
@@ -7415,8 +7499,7 @@ class APIHelper:
7415
7499
  if response.empty:
7416
7500
  return None
7417
7501
 
7418
- records = response.to_dict('records')
7419
- return {'data': records, 'status': 'success'}
7502
+ return {'data': response, 'status': 'success', '_is_df': True}
7420
7503
 
7421
7504
  if debug:
7422
7505
  print(f"[APIHelper] Unexpected type: {type(response)}")
@@ -7763,8 +7846,9 @@ def _api_call_direct(endpoint: str, max_retries: int = 3, **kwargs) -> Optional[
7763
7846
  return None
7764
7847
  df = data if isinstance(data, pd.DataFrame) else pd.DataFrame(data)
7765
7848
  return df if not df.empty else None
7766
- except RETRYABLE:
7767
- if attempt == max_retries - 1:
7849
+ except RETRYABLE as e:
7850
+ if _is_client_error(e) or attempt == max_retries - 1:
7851
+ _safe_print(f" ⚠️ {endpoint}: {e}")
7768
7852
  return None
7769
7853
  continue
7770
7854
  except Exception:
@@ -7772,6 +7856,12 @@ def _api_call_direct(endpoint: str, max_retries: int = 3, **kwargs) -> Optional[
7772
7856
  return None
7773
7857
 
7774
7858
 
7859
+ def _is_client_error(exc):
7860
+ """4xx other than 429: the same request cannot succeed on retry."""
7861
+ status = getattr(getattr(exc, 'response', None), 'status_code', None)
7862
+ return status is not None and 400 <= status < 500 and status != 429
7863
+
7864
+
7775
7865
  def api_call(endpoint, cache_config=None, debug=False, max_retries=3, skip_parquet_cache=False,
7776
7866
  _chunk_info=None, **kwargs):
7777
7867
  """
@@ -7868,6 +7958,8 @@ def api_call(endpoint, cache_config=None, debug=False, max_retries=3, skip_parqu
7868
7958
 
7869
7959
  except RETRYABLE_ERRORS as e:
7870
7960
  last_exception = e
7961
+ if _is_client_error(e):
7962
+ raise
7871
7963
  if debug_level >= 1:
7872
7964
  print(f"[RETRY] Network error on attempt {attempt + 1}/{max_retries}: {type(e).__name__}: {e}")
7873
7965
 
@@ -9693,7 +9785,7 @@ class StopLossManager:
9693
9785
 
9694
9786
  # --- Stock-based strategies (no option legs for intraday SL) ---
9695
9787
 
9696
- elif strategy_type in ('LONG_STOCK', 'SHORT_STOCK'):
9788
+ elif strategy_type in STOCK_STRATEGY_TYPES:
9697
9789
  pass
9698
9790
 
9699
9791
  elif strategy_type in ('COVERED_CALL', 'COVERED_CALL_ATM', 'COVERED_CALL_25D', 'COVERED_PUT', 'COVERED_PUT_ATM', 'COVERED_PUT_25D') or 'COVERED_CALL' in strategy_type.upper() or 'COVERED_PUT' in strategy_type.upper():
@@ -10393,10 +10485,9 @@ class StopLossManager:
10393
10485
  pnl_from_baseline = current_value - baseline_close_cost # change in close cost
10394
10486
  denom = abs(baseline_close_cost)
10395
10487
  else:
10396
- # Fallback: old behavior (no baseline close cost stored)
10397
- original_credit_fb = original_credit or entry_value
10398
- position_value_now = original_credit_fb + current_value
10399
- pnl_from_baseline = position_value_now - entry_value
10488
+ # Simple credit (straddle, single leg): entry_value is the 09:30 close cost
10489
+ # (or the credit before any reset); current_value is -close cost now
10490
+ pnl_from_baseline = abs(entry_value) + current_value
10400
10491
  denom = abs(entry_value)
10401
10492
  intraday_pnl_pct = (pnl_from_baseline / denom) * 100 if denom != 0 else 0
10402
10493
  else:
@@ -11264,6 +11355,18 @@ class StopLossManager:
11264
11355
 
11265
11356
  def _check_pl_loss_stop(self, pos, kwargs):
11266
11357
  """Stop-loss based on actual P&L"""
11358
+ # Stock positions: entry_price may be per share or a whole-position dollar value, so measure the
11359
+ # per-share move from underlying_entry_price (per share in both conventions)
11360
+ if pos.get('sl_baseline_reset') and pos.get('strategy_type') in STOCK_STRATEGY_TYPES:
11361
+ base_px = pos.get('underlying_entry_price')
11362
+ px = kwargs.get('underlying_price')
11363
+ if base_px and px:
11364
+ move_pct = (px - base_px) / base_px * 100
11365
+ is_short = pos.get('is_short_bias') or pos.get('strategy_type') in SHORT_STOCK_TYPES
11366
+ stock_pnl_pct = -move_pct if is_short else move_pct
11367
+ stop_level = -pos['stop_value'] * 100
11368
+ return stock_pnl_pct <= stop_level, stop_level, 'pl_loss'
11369
+
11267
11370
  pnl_pct = kwargs.get('pnl_pct')
11268
11371
 
11269
11372
  # [EARNINGS SL FIX] Force recalculation when baseline was reset
@@ -12627,7 +12730,7 @@ class PositionManager:
12627
12730
  call_intrinsic = max(0, underlying_price - call_strike)
12628
12731
  intrinsic_value = call_intrinsic * unit_multiplier * contracts # Only call obligation
12629
12732
 
12630
- elif strategy_type in ('LONG_STOCK', 'SHORT_STOCK'):
12733
+ elif strategy_type in STOCK_STRATEGY_TYPES:
12631
12734
  intrinsic_value = underlying_price * unit_multiplier * contracts
12632
12735
  if self.debug:
12633
12736
  print(f"[INTRINSIC SETTLEMENT] {position_id} - {strategy_type}:")
@@ -12766,7 +12869,16 @@ class PositionManager:
12766
12869
  # BACKWARD COMPATIBILITY: If 'type' passed but 'strategy_type' not, copy type → strategy_type
12767
12870
  if 'strategy_type' not in kwargs and 'type' in kwargs:
12768
12871
  kwargs['strategy_type'] = kwargs['type']
12769
-
12872
+
12873
+ # DIRECTIONAL_* pass shares in `quantity` with contracts=0; stock P&L paths use contracts × unit_multiplier
12874
+ if kwargs.get('strategy_type') in ('DIRECTIONAL_LONG', 'DIRECTIONAL_SHORT'):
12875
+ if not kwargs.get('contracts'):
12876
+ kwargs['contracts'] = quantity
12877
+ kwargs.setdefault('unit_multiplier', 1)
12878
+ if not kwargs.get('total_cost'):
12879
+ entry_px = kwargs.get('underlying_entry_price') or entry_price
12880
+ kwargs['total_cost'] = entry_px * kwargs['contracts'] * kwargs['unit_multiplier']
12881
+
12770
12882
  # AUTO-DETECT position_type if not provided
12771
12883
  if position_type is None:
12772
12884
  strategy_type = kwargs.get('strategy_type')
@@ -13425,7 +13537,7 @@ class PositionManager:
13425
13537
  _is_stock_position = (
13426
13538
  'COVERED_CALL' in strategy_type.upper()
13427
13539
  or 'COVERED_PUT' in strategy_type.upper()
13428
- or strategy_type in ('LONG_STOCK', 'SHORT_STOCK')
13540
+ or strategy_type in STOCK_STRATEGY_TYPES
13429
13541
  )
13430
13542
  if _is_stock_position:
13431
13543
  # entry_value/current_value are full position values
@@ -13440,10 +13552,16 @@ class PositionManager:
13440
13552
  is_credit = StrategyRegistry.is_credit_strategy(
13441
13553
  strategy_type=strategy_type, position=position
13442
13554
  )
13555
+ # entry_value may be the reset 09:30 baseline; the trade is booked against its cost
13556
+ _orig_cost = abs(position.get('total_cost') or entry_value)
13443
13557
  if is_credit:
13444
- intraday_pnl = entry_value + current_value
13558
+ intraday_pnl = _orig_cost + current_value
13559
+ _pnl_denom = position.get('entry_max_risk') or position.get('max_risk') or _orig_cost
13445
13560
  else:
13446
- intraday_pnl = current_value - entry_value
13561
+ intraday_pnl = current_value - _orig_cost
13562
+ _pnl_denom = _orig_cost
13563
+ if _pnl_denom:
13564
+ intraday_pnl_pct = (intraday_pnl / _pnl_denom) * 100
13447
13565
 
13448
13566
  if self.debug:
13449
13567
  print(f" [Stop Loss] Using intraday P&L: {intraday_pnl_pct:.2f}% (${intraday_pnl:.2f}) instead of EOD: {current_pnl_pct:.2f}% (${current_pnl:.2f})")
@@ -13760,11 +13878,11 @@ class PositionManager:
13760
13878
 
13761
13879
  # LONG_STOCK / SHORT_STOCK: no option legs → compute P&L directly from stock price
13762
13880
  strategy_type = position.get('strategy_type', self.config.get('strategy_type', 'STRADDLE'))
13763
- if leg_data is None and strategy_type in ('LONG_STOCK', 'SHORT_STOCK'):
13881
+ if leg_data is None and strategy_type in STOCK_STRATEGY_TYPES:
13764
13882
  entry_cost = abs(position.get('total_cost', 0))
13765
13883
  contracts = position.get('contracts', 1)
13766
13884
  stock_exit_value = stock_price * contracts * _pos_unit_multiplier(position)
13767
- if strategy_type == 'SHORT_STOCK':
13885
+ if strategy_type in SHORT_STOCK_TYPES:
13768
13886
  pnl = entry_cost - stock_exit_value
13769
13887
  else:
13770
13888
  pnl = stock_exit_value - entry_cost
@@ -14807,7 +14925,7 @@ class PositionManager:
14807
14925
  # ========================================
14808
14926
  # LONG STOCK (pure stock position)
14809
14927
  # ========================================
14810
- elif position_type in ('LONG_STOCK', 'SHORT_STOCK'):
14928
+ elif position_type in STOCK_STRATEGY_TYPES:
14811
14929
  contracts = position.get('contracts', 1)
14812
14930
  underlying_price = position.get('underlying_entry_price', 0)
14813
14931
  stock_value = underlying_price * contracts * _pos_unit_multiplier(position)
@@ -19219,9 +19337,7 @@ def _try_read_from_duckdb_storage(config, cache_config, symbol, extended_start,
19219
19337
  _rich_print(f" 📋 DB DTE: 0-{int(db_max_dte)}")
19220
19338
 
19221
19339
  # Get dates that have options data
19222
- options_dates_result = db.execute_read(f"""
19223
- SELECT DISTINCT date FROM {_opt_tbl} WHERE symbol = ?
19224
- """, [symbol]).fetchdf()
19340
+ options_dates_result = db.execute_read(_complete_option_dates_sql(_opt_tbl), [symbol]).fetchdf()
19225
19341
  options_dates = set(pd.to_datetime(options_dates_result['date']).dt.strftime('%Y-%m-%d'))
19226
19342
 
19227
19343
  _rich_print(f" 🔍 Need: {extended_start} → {end_date}, DTE 0-{required_max_dte}")
@@ -19377,9 +19493,7 @@ def _try_read_from_duckdb_storage(config, cache_config, symbol, extended_start,
19377
19493
  if trading_days:
19378
19494
  try:
19379
19495
  db.refresh_conn()
19380
- _post_df = db.execute_read(
19381
- f"SELECT DISTINCT date FROM {_opt_tbl} WHERE symbol = ?", [symbol]
19382
- ).fetchdf()
19496
+ _post_df = db.execute_read(_complete_option_dates_sql(_opt_tbl), [symbol]).fetchdf()
19383
19497
  _post_dates = set(pd.to_datetime(_post_df['date']).dt.strftime('%Y-%m-%d'))
19384
19498
  _req_min = pd.to_datetime(extended_start)
19385
19499
  _req_max = pd.to_datetime(end_date)
@@ -20004,9 +20118,7 @@ def _preload_duckdb_SSD_storage(config, cache_config):
20004
20118
  # Cold-path completeness: every backtest trading day must exist in
20005
20119
  # the options table; misses are loud + flag _failed_chunks
20006
20120
  try:
20007
- _cc_df = conn.execute(
20008
- f"SELECT DISTINCT date FROM {_opt_tbl} WHERE symbol = ?", [symbol]
20009
- ).fetchdf()
20121
+ _cc_df = conn.execute(_complete_option_dates_sql(_opt_tbl), [symbol]).fetchdf()
20010
20122
  _cc_have = set(pd.to_datetime(_cc_df['date']).dt.strftime('%Y-%m-%d'))
20011
20123
  _cc_need = {pd.Timestamp(d).strftime('%Y-%m-%d') for d in backtest_trading_days}
20012
20124
  _cc_missing = sorted(_cc_need - _cc_have)
@@ -20193,23 +20305,28 @@ def _api_call_logged(endpoint, cache_config, skip_parquet_cache=False, debuginfo
20193
20305
 
20194
20306
  # Make API call with debug level
20195
20307
  # debuginfo: 0=silent, 1=basic, 2=detailed (URLs), 3=verbose timing
20196
- response = api_call(
20197
- endpoint,
20198
- cache_config,
20199
- debug=debuginfo, # Pass integer directly
20200
- skip_parquet_cache=skip_parquet_cache,
20201
- _chunk_info=_chunk_info,
20202
- **normalized_params
20203
- )
20204
-
20308
+ _api_mem_acquire()
20309
+ try:
20310
+ response = api_call(
20311
+ endpoint,
20312
+ cache_config,
20313
+ debug=debuginfo, # Pass integer directly
20314
+ skip_parquet_cache=skip_parquet_cache,
20315
+ _chunk_info=_chunk_info,
20316
+ **normalized_params
20317
+ )
20318
+ finally:
20319
+ _api_mem_release()
20320
+
20205
20321
  # Parse response
20206
20322
  if response is None:
20207
20323
  return None
20208
-
20324
+
20209
20325
  if isinstance(response, pd.DataFrame):
20210
20326
  return response if not response.empty else None
20211
20327
  elif isinstance(response, dict) and 'data' in response:
20212
- df = pd.DataFrame(response['data'])
20328
+ data = response['data']
20329
+ df = data if isinstance(data, pd.DataFrame) else pd.DataFrame(data)
20213
20330
  return df if not df.empty else None
20214
20331
 
20215
20332
  return None
@@ -20308,16 +20425,14 @@ def _load_options_to_duckdb(config, cache_config, symbol, start_date, end_date):
20308
20425
  print(f" ⚡ PARALLEL mode: {total_requests} requests with {max_workers} workers")
20309
20426
 
20310
20427
  all_data = []
20428
+ # Legacy (pandas-indicator) mode still returns the sample if reading DuckDB back fails.
20429
+ _keep_sample = not config.get('use_duckdb_indicators', True)
20311
20430
  total_rows = 0
20312
20431
  api_debuginfo = debuginfo
20313
20432
  # Resolve snapshot-aware endpoint ONCE for both fetch and save
20314
20433
  _save_ep = _get_options_endpoints(_get_options_snapshot_mode(config))['filtered']
20315
20434
 
20316
- # MEMFIX(backpressure): воркеры качают быстрее, чем главный поток успевает
20317
- # писать чанк в DuckDB (запись ~3с), поэтому готовые, но ещё не обработанные
20318
- # DataFrame копились в памяти. Семафор держит очередь готовых результатов
20319
- # ограниченной: воркер не отдаёт результат, пока главный поток не разгрёб
20320
- # предыдущие. Это ограничивает пик числом воркеров, а не длиной периода.
20435
+ # Caps downloaded-but-not-yet-written chunks; each loop must release after writing a non-empty chunk.
20321
20436
  import threading as _mf_thr
20322
20437
  _mf_inflight = _mf_thr.Semaphore(max(2, max_workers))
20323
20438
 
@@ -20438,29 +20553,27 @@ def _load_options_to_duckdb(config, cache_config, symbol, start_date, end_date):
20438
20553
  _safe_print(f" ⏱️ [SLOW DUCKDB WRITE] {_db_elapsed:.1f}s for {len(df)} rows (chunk {completed})")
20439
20554
  rows_saved_total += rows_saved
20440
20555
  total_rows += len(df)
20441
-
20442
- if len(all_data) < 5:
20556
+
20557
+ # Sample is only a fallback for a failed DuckDB write.
20558
+ if rows_saved_total > 0 and not _keep_sample:
20559
+ all_data.clear()
20560
+ elif len(all_data) < 5:
20443
20561
  all_data.append(df)
20444
-
20562
+
20445
20563
  if df is not None and not getattr(df, 'empty', True):
20446
20564
  try:
20447
20565
  _mf_inflight.release()
20448
20566
  except Exception:
20449
20567
  pass
20450
- # MEMFIX: release the chunk once it is persisted — the Future keeps a
20451
- # reference to its result until the executor block exits, so without this
20452
- # the whole fetch accumulates in RAM regardless of DuckDB writes.
20568
+ # The Future holds its result until the executor exits; drop it once persisted.
20453
20569
  try:
20454
20570
  future._result = None
20455
20571
  except Exception:
20456
20572
  pass
20457
20573
  df = None
20574
+ _trim_heap()
20458
20575
  if completed % 25 == 0:
20459
- # MEMFIX(page-cache): страницы записанного .duckdb оседают в file-кэше
20460
- # cgroup и подтягивают memory.current к лимиту. Кэш вытесняемый, но
20461
- # держит счётчик у потолка и сокращает буфер до OOM. Сбрасываем его:
20462
- # сначала fdatasync (грязные страницы иначе не освободить), затем
20463
- # POSIX_FADV_DONTNEED на файл БД и его WAL.
20576
+ # Written .duckdb pages count toward the cgroup limit: fdatasync, then drop them from page cache.
20464
20577
  try:
20465
20578
  import os as _os4
20466
20579
  _dbp = None
@@ -20487,13 +20600,7 @@ def _load_options_to_duckdb(config, cache_config, symbol, start_date, end_date):
20487
20600
  pass
20488
20601
  except Exception:
20489
20602
  pass
20490
- import gc as _gc
20491
- _gc.collect()
20492
- try:
20493
- import ctypes as _ct
20494
- _ct.CDLL("libc.so.6").malloc_trim(0)
20495
- except Exception:
20496
- pass
20603
+ _release_freed_memory()
20497
20604
 
20498
20605
  _watchdog_completed[0] = completed
20499
20606
  if completed % 10 == 0 or completed == total_requests:
@@ -20534,11 +20641,16 @@ def _load_options_to_duckdb(config, cache_config, symbol, start_date, end_date):
20534
20641
  )
20535
20642
  rows_saved_total += rows_saved
20536
20643
  total_rows += len(df)
20537
-
20538
- # Keep small sample for return
20539
- if len(all_data) < 5:
20644
+
20645
+ # Sample is only a fallback for a failed DuckDB write.
20646
+ if rows_saved_total > 0 and not _keep_sample:
20647
+ all_data.clear()
20648
+ elif len(all_data) < 5:
20540
20649
  all_data.append(df)
20541
-
20650
+ _mf_inflight.release()
20651
+ df = None
20652
+ _trim_heap()
20653
+
20542
20654
  # Progress summary every 10 requests
20543
20655
  if (i + 1) % 10 == 0 or i == len(all_requests) - 1:
20544
20656
  print(f" 📦 Request {i + 1}/{total_requests}: {total_rows:,} rows so far")
@@ -21794,8 +21906,11 @@ def _load_futures_for_hedge(config, preloaded):
21794
21906
  if r.status_code == 200:
21795
21907
  data = r.json().get('data', [])
21796
21908
  all_rows.extend(data)
21797
- except Exception:
21798
- pass
21909
+ else:
21910
+ print(f" ⚠️ futures/eod/prices {fut_root} {current:%Y-%m-%d}..{chunk_end:%Y-%m-%d}: "
21911
+ f"HTTP {r.status_code} {r.text[:300]}")
21912
+ except Exception as e:
21913
+ print(f" ⚠️ futures/eod/prices {fut_root} {current:%Y-%m-%d}: {e}")
21799
21914
  current = chunk_end + _td(days=1)
21800
21915
 
21801
21916
  if all_rows:
@@ -23623,6 +23738,7 @@ def preload_data_universal(config, data_requests=None, debug=False):
23623
23738
  # and _try_read_from_duckdb_storage). Setting the old per-row lazy
23624
23739
  # patch here would be redundant and wasteful (extra intraday API calls
23625
23740
  # for data that is already correct in DuckDB).
23741
+ _activate_runtime_stock_shim(config, result)
23626
23742
  _timing.end_stage() # End "Loading Data" timing
23627
23743
  return result
23628
23744
  # Check if lock was the reason for failure
@@ -23661,6 +23777,7 @@ def preload_data_universal(config, data_requests=None, debug=False):
23661
23777
  return None
23662
23778
  if config.get('bar_exit') == '16:00':
23663
23779
  set_option_patch_context(OptionPatchContext(_sym, _fetch_opt_intraday_duckdb_ram))
23780
+ _activate_runtime_stock_shim(config, result)
23664
23781
  _timing.end_stage() # End "Loading Data" timing
23665
23782
  return result
23666
23783
  print(" ⚠️ DuckDB RAM reader: No cached Parquet data, falling back to API...")
@@ -24908,7 +25025,7 @@ def parse_earnings_calendar(earnings_df):
24908
25025
 
24909
25026
  Returns:
24910
25027
  list: [{'date': datetime.date, 'estimate': float, 'reported': float,
24911
- 'time_of_day': str}, ...]
25028
+ 'time_of_day': str, 'time_of_day_code': str}, ...]
24912
25029
  Sorted by date ascending.
24913
25030
 
24914
25031
  Usage in strategy:
@@ -24936,11 +25053,13 @@ def parse_earnings_calendar(earnings_df):
24936
25053
  else:
24937
25054
  date_obj = earning_date
24938
25055
 
25056
+ time_of_day = record.get('time_of_day_code', 'UNK')
24939
25057
  earnings_events.append({
24940
25058
  'date': date_obj,
24941
25059
  'estimate': record.get('estimate'),
24942
25060
  'reported': record.get('reported_earning'),
24943
- 'time_of_day': record.get('time_of_day_code', 'UNK')
25061
+ 'time_of_day': time_of_day,
25062
+ 'time_of_day_code': time_of_day,
24944
25063
  })
24945
25064
 
24946
25065
  return sorted(earnings_events, key=lambda x: x['date'])
@@ -31751,7 +31870,7 @@ class MarketDataManager:
31751
31870
  ep = self.ENDPOINT_OPTIONS_RAWIV
31752
31871
  return self._get(
31753
31872
  ep,
31754
- api_params=dict(symbol=symbol, startDate=start_date, endDate=end_date),
31873
+ api_params=dict(symbol=symbol, from_=start_date, to=end_date),
31755
31874
  symbol=symbol, start_date=start_date, end_date=end_date,
31756
31875
  cache_key=self._cache_key(ep, s=symbol, f=start_date, t=end_date),
31757
31876
  )
@@ -31845,7 +31964,7 @@ class MarketDataManager:
31845
31964
  ep = self.ENDPOINT_IVX
31846
31965
  return self._get(
31847
31966
  ep,
31848
- api_params=dict(symbol=symbol, startDate=start_date, endDate=end_date),
31967
+ api_params=dict(symbol=symbol, from_=start_date, to=end_date),
31849
31968
  symbol=symbol, start_date=start_date, end_date=end_date,
31850
31969
  cache_key=self._cache_key(ep, s=symbol, f=start_date, t=end_date),
31851
31970
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ivolatility_backtesting
3
- Version: 2.147
3
+ Version: 2.151
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
@@ -19,7 +19,7 @@ Requires-Dist: pandas<3.0,>=1.5.0
19
19
  Requires-Dist: numpy<3.0,>=1.21.0
20
20
  Requires-Dist: matplotlib>=3.5.0
21
21
  Requires-Dist: seaborn>=0.11.0
22
- Requires-Dist: ivolatility>=1.8.2
22
+ Requires-Dist: ivolatility>=1.9.6
23
23
  Requires-Dist: psutil>=7.1.0
24
24
 
25
25
  # IVolatility Backtesting Framework v1.32
@@ -11,4 +11,7 @@ ivolatility_backtesting.egg-info/top_level.txt
11
11
  tests/test_2142_fixes.py
12
12
  tests/test_2144_duckdb_dedup.py
13
13
  tests/test_2146_vix_vro.py
14
- tests/test_2147_cents_multiplier.py
14
+ tests/test_2147_cents_multiplier.py
15
+ tests/test_2148_loader_memory.py
16
+ tests/test_2149_client_errors.py
17
+ tests/test_2150_prompt_patterns.py
@@ -2,5 +2,5 @@ pandas<3.0,>=1.5.0
2
2
  numpy<3.0,>=1.21.0
3
3
  matplotlib>=3.5.0
4
4
  seaborn>=0.11.0
5
- ivolatility>=1.8.2
5
+ ivolatility>=1.9.6
6
6
  psutil>=7.1.0