lumibot 4.1.2__py3-none-any.whl → 4.1.3__py3-none-any.whl

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.

Potentially problematic release.


This version of lumibot might be problematic. Click here for more details.

@@ -1264,9 +1264,24 @@ class _Strategy:
1264
1264
  if show_indicators is None:
1265
1265
  show_indicators = SHOW_INDICATORS
1266
1266
 
1267
- # Auto-select datasource from environment variable if None
1268
- if datasource_class is None:
1269
- from lumibot.credentials import BACKTESTING_DATA_SOURCE
1267
+ from lumibot.credentials import BACKTESTING_DATA_SOURCE as _DEFAULT_BACKTESTING_DATA_SOURCE
1268
+
1269
+ # Determine whether an environment override exists. When BACKTESTING_DATA_SOURCE
1270
+ # is set (and not blank/\"none\"), it should take precedence even if a
1271
+ # datasource_class argument was provided.
1272
+ env_override_raw = os.environ.get("BACKTESTING_DATA_SOURCE")
1273
+ env_override_name = None
1274
+
1275
+ if env_override_raw is not None:
1276
+ trimmed = env_override_raw.strip()
1277
+ if trimmed and trimmed.lower() != "none":
1278
+ env_override_name = trimmed.lower()
1279
+ elif datasource_class is None:
1280
+ # No override provided and no class in code – fall back to the default
1281
+ # configured in credentials (ThetaData unless the project overrides it).
1282
+ env_override_name = _DEFAULT_BACKTESTING_DATA_SOURCE.lower()
1283
+
1284
+ if env_override_name is not None:
1270
1285
  from lumibot.backtesting import (
1271
1286
  PolygonDataBacktesting,
1272
1287
  ThetaDataBacktesting,
@@ -1285,18 +1300,24 @@ class _Strategy:
1285
1300
  "databento": DataBentoDataBacktesting,
1286
1301
  }
1287
1302
 
1288
- datasource_name = BACKTESTING_DATA_SOURCE.lower()
1289
- if datasource_name not in datasource_map:
1303
+ if env_override_name not in datasource_map:
1304
+ label = env_override_raw or _DEFAULT_BACKTESTING_DATA_SOURCE
1290
1305
  raise ValueError(
1291
- f"Unknown BACKTESTING_DATA_SOURCE: '{BACKTESTING_DATA_SOURCE}'. "
1306
+ f"Unknown BACKTESTING_DATA_SOURCE: '{label}'. "
1292
1307
  f"Valid options: {list(datasource_map.keys())}"
1293
1308
  )
1294
1309
 
1295
- datasource_class = datasource_map[datasource_name]
1310
+ datasource_class = datasource_map[env_override_name]
1311
+ label = env_override_raw or _DEFAULT_BACKTESTING_DATA_SOURCE
1296
1312
  get_logger(__name__).info(colored(
1297
- f"Auto-selected backtesting data source from BACKTESTING_DATA_SOURCE env var: {BACKTESTING_DATA_SOURCE}",
1313
+ f"Using BACKTESTING_DATA_SOURCE setting for backtest data: {label}",
1298
1314
  "green"
1299
1315
  ))
1316
+ elif datasource_class is None:
1317
+ raise ValueError(
1318
+ "No backtesting data source provided. Set BACKTESTING_DATA_SOURCE in the environment "
1319
+ "or pass datasource_class when calling backtest()."
1320
+ )
1300
1321
 
1301
1322
  # Make sure polygon_api_key is set if using PolygonDataBacktesting
1302
1323
  polygon_api_key = polygon_api_key if polygon_api_key is not None else POLYGON_API_KEY
@@ -593,13 +593,29 @@ def _filter_front_month_rows_pandas(
593
593
  if df.empty or "symbol" not in df.columns or schedule is None:
594
594
  return df
595
595
 
596
+ index_tz = getattr(df.index, "tz", None)
597
+
598
+ def _align(ts: datetime | pd.Timestamp | None) -> pd.Timestamp | None:
599
+ if ts is None:
600
+ return None
601
+ ts_pd = pd.Timestamp(ts)
602
+ if index_tz is None:
603
+ return ts_pd.tz_localize(None) if ts_pd.tz is not None else ts_pd
604
+ if ts_pd.tz is None:
605
+ ts_pd = ts_pd.tz_localize(index_tz)
606
+ else:
607
+ ts_pd = ts_pd.tz_convert(index_tz)
608
+ return ts_pd
609
+
596
610
  mask = pd.Series(False, index=df.index)
597
611
  for symbol, start_dt, end_dt in schedule:
598
612
  cond = df["symbol"] == symbol
599
- if start_dt is not None:
600
- cond &= df.index >= start_dt
601
- if end_dt is not None:
602
- cond &= df.index < end_dt
613
+ start_aligned = _align(start_dt)
614
+ end_aligned = _align(end_dt)
615
+ if start_aligned is not None:
616
+ cond &= df.index >= start_aligned
617
+ if end_aligned is not None:
618
+ cond &= df.index < end_aligned
603
619
  mask |= cond
604
620
 
605
621
  filtered = df.loc[mask]
@@ -783,15 +799,22 @@ def get_price_data_from_databento(
783
799
  start_naive = start.replace(tzinfo=None) if start.tzinfo is not None else start
784
800
  end_naive = end.replace(tzinfo=None) if end.tzinfo is not None else end
785
801
 
786
- if asset.asset_type == Asset.AssetType.CONT_FUTURE:
802
+ roll_asset = asset
803
+ if asset.asset_type == Asset.AssetType.FUTURE and not asset.expiration:
804
+ roll_asset = Asset(asset.symbol, Asset.AssetType.CONT_FUTURE)
805
+
806
+ if roll_asset.asset_type == Asset.AssetType.CONT_FUTURE:
787
807
  schedule_start = start
788
- symbols = databento_roll.resolve_symbols_for_range(asset, schedule_start, end)
789
- front_symbol = databento_roll.resolve_symbol_for_datetime(asset, reference_date or start)
808
+ symbols = databento_roll.resolve_symbols_for_range(roll_asset, schedule_start, end)
809
+ front_symbol = databento_roll.resolve_symbol_for_datetime(roll_asset, reference_date or start)
790
810
  if front_symbol not in symbols:
791
811
  symbols.insert(0, front_symbol)
792
812
  else:
793
813
  schedule_start = start
794
- front_symbol = _format_futures_symbol_for_databento(asset)
814
+ front_symbol = _format_futures_symbol_for_databento(
815
+ asset,
816
+ reference_date=reference_date or start,
817
+ )
795
818
  symbols = [front_symbol]
796
819
 
797
820
  # Ensure multiplier is populated using the first contract.
@@ -904,7 +927,7 @@ def get_price_data_from_databento(
904
927
  return definition
905
928
 
906
929
  schedule = databento_roll.build_roll_schedule(
907
- asset,
930
+ roll_asset,
908
931
  schedule_start,
909
932
  end,
910
933
  definition_provider=get_definition,
@@ -929,10 +929,14 @@ def get_price_data_from_databento_polars(
929
929
  start_naive = start.replace(tzinfo=None) if start.tzinfo is not None else start
930
930
  end_naive = end.replace(tzinfo=None) if end.tzinfo is not None else end
931
931
 
932
- if asset.asset_type == Asset.AssetType.CONT_FUTURE:
932
+ roll_asset = asset
933
+ if asset.asset_type == Asset.AssetType.FUTURE and not asset.expiration:
934
+ roll_asset = Asset(asset.symbol, Asset.AssetType.CONT_FUTURE)
935
+
936
+ if roll_asset.asset_type == Asset.AssetType.CONT_FUTURE:
933
937
  schedule_start = start
934
- symbols_to_fetch = databento_roll.resolve_symbols_for_range(asset, schedule_start, end)
935
- front_symbol = databento_roll.resolve_symbol_for_datetime(asset, reference_date or start)
938
+ symbols_to_fetch = databento_roll.resolve_symbols_for_range(roll_asset, schedule_start, end)
939
+ front_symbol = databento_roll.resolve_symbol_for_datetime(roll_asset, reference_date or start)
936
940
  if front_symbol not in symbols_to_fetch:
937
941
  symbols_to_fetch.insert(0, front_symbol)
938
942
  logger.info(
@@ -941,7 +945,10 @@ def get_price_data_from_databento_polars(
941
945
  )
942
946
  else:
943
947
  schedule_start = start
944
- front_symbol = _format_futures_symbol_for_databento(asset)
948
+ front_symbol = _format_futures_symbol_for_databento(
949
+ asset,
950
+ reference_date=reference_date or start,
951
+ )
945
952
  symbols_to_fetch = [front_symbol]
946
953
 
947
954
  # Fetch and cache futures multiplier from DataBento if needed (after symbol resolution)
@@ -1092,7 +1099,7 @@ def get_price_data_from_databento_polars(
1092
1099
  return definition
1093
1100
 
1094
1101
  schedule = databento_roll.build_roll_schedule(
1095
- asset,
1102
+ roll_asset,
1096
1103
  schedule_start,
1097
1104
  end,
1098
1105
  definition_provider=get_definition,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lumibot
3
- Version: 4.1.2
3
+ Version: 4.1.3
4
4
  Summary: Backtesting and Trading Library, Made by Lumiwealth
5
5
  Home-page: https://github.com/Lumiwealth/lumibot
6
6
  Author: Robert Grzesik
@@ -159,7 +159,7 @@ lumibot/example_strategies/__pycache__/test_broker_functions.cpython-312-pytest-
159
159
  lumibot/resources/ThetaTerminal.jar,sha256=K6GeeFcN8-gvyL2x5iq5pzD79KfPJvMK8iiezi3TmNQ,11834389
160
160
  lumibot/resources/conf.yaml,sha256=rjB9-10JP7saZ_edjX5bQDGfuc3amOQTUUUr-UiMpNA,597
161
161
  lumibot/strategies/__init__.py,sha256=jEZ95K5hG0f595EXYKWwL2_UsnWWk5Pug361PK2My2E,79
162
- lumibot/strategies/_strategy.py,sha256=QCrrZQzTZ0GjsNyNNxHNI3WaNZ98yvxT-E0dbOBETMg,109685
162
+ lumibot/strategies/_strategy.py,sha256=KI4nhQX4qrr0Dp4G0uN-njn03y3xoJDM762O-ru41Vg,110745
163
163
  lumibot/strategies/session_manager.py,sha256=Nze6UYNSPlCsf-tyHvtFqUeL44WSNHjwsKrIepvsyCY,12956
164
164
  lumibot/strategies/strategy.py,sha256=i-jAt2a1DXSiKT1gMQ5vXkbLb29x9vAFZl9zehbfJ1s,169301
165
165
  lumibot/strategies/strategy_executor.py,sha256=IrHwDOu5s3gG65dz7FL-0kllWy7COci7JFyB4iiPUrg,70801
@@ -172,8 +172,8 @@ lumibot/tools/alpaca_helpers.py,sha256=nhBS-sv28lZfIQ85szC9El8VHLrCw5a5KbsGOOEjm
172
172
  lumibot/tools/bitunix_helpers.py,sha256=-UzrN3w_Y-Ckvhl7ZBoAcx7sgb6tH0KcpVph1Ovm3gw,25780
173
173
  lumibot/tools/black_scholes.py,sha256=TBjJuDTudvqsbwqSb7-zb4gXsJBCStQFaym8xvePAjw,25428
174
174
  lumibot/tools/ccxt_data_store.py,sha256=VXLSs0sWcwjRPZzbuEeVPS-3V6D10YnYMfIyoTPTG0U,21225
175
- lumibot/tools/databento_helper.py,sha256=cbeMTrJOTu2E_Z9s021V_RGa4xrQ1LYI03SRi29pnQA,42976
176
- lumibot/tools/databento_helper_polars.py,sha256=yQN4nIWg-WNXKoW8s1x8GUoq3rtmf6-NWNPnjFSZz2Q,53153
175
+ lumibot/tools/databento_helper.py,sha256=XBCAxc_GJ_oOTnHIsc5XSsRGmwymTqosLUbaKpIwaBw,43807
176
+ lumibot/tools/databento_helper_polars.py,sha256=4Nx4rUC-zY7zF25J__HRf5PXyZQGSGuX7zfkllvuWIo,53418
177
177
  lumibot/tools/databento_roll.py,sha256=48HAw3h6OngCK4UTl9ifpjo-ki8qmB6OoJUrHp0gRmE,6767
178
178
  lumibot/tools/debugers.py,sha256=ga6npFsS9cpKtTXaygh9t2_txCElg3bfzfeqDBvSL8k,485
179
179
  lumibot/tools/decorators.py,sha256=gokLv6s37C1cnbnFSVOUc4RaVJ5aMTU2C344Vvi3ycs,2275
@@ -225,7 +225,7 @@ lumibot/trading_builtins/safe_list.py,sha256=IIjZOHSiZYK25A4WBts0oJaZNOJDsjZL65M
225
225
  lumibot/trading_builtins/__pycache__/__init__.cpython-312.pyc,sha256=ksqDHG5HzxBeh3sDNn2NjEhYtj3dI6TvuQoe03VAItg,345
226
226
  lumibot/trading_builtins/__pycache__/custom_stream.cpython-312.pyc,sha256=w9EEoPd4LTBKmS8x6x-umicO1GwzaHlZnAv7MC2A78o,7397
227
227
  lumibot/trading_builtins/__pycache__/safe_list.cpython-312.pyc,sha256=2MQnqSCnMHHVu_gMK-3xBVSdHFyhxGR7_UrNdOvb4So,4875
228
- lumibot-4.1.2.dist-info/licenses/LICENSE,sha256=fYhGIyxjyNXACgpNQS3xxpxDOaVOWRVxZMCRbsDv8k0,35130
228
+ lumibot-4.1.3.dist-info/licenses/LICENSE,sha256=fYhGIyxjyNXACgpNQS3xxpxDOaVOWRVxZMCRbsDv8k0,35130
229
229
  tests/__init__.py,sha256=3-VoT-nAuqMfwufd4ceN6fXaHl_zCfDCSXJOTp1ywYQ,393
230
230
  tests/conftest.py,sha256=UBw_2fx7r6TZPKus2b1Qxrzmd4bg8EEBnX1vCHUuSVA,3311
231
231
  tests/fixtures.py,sha256=wOHQsh1SGHnXe_PGi6kDWI30CS_Righi7Ig7vwSEKT4,9082
@@ -244,7 +244,7 @@ tests/test_backtesting_broker.py,sha256=rxZGH5cgiWLmNGdI3k9fti3Fp9IOSohq8xD2E3L2
244
244
  tests/test_backtesting_broker_await_close.py,sha256=WbehY7E4Qet3_Mo7lpfgjmhtI9pnJPIt9mkFI15Dzho,7545
245
245
  tests/test_backtesting_broker_time_advance.py,sha256=FCv0nKG8BQlEjNft7kmQYm9M2CsLIZ0b7mWCllOHQxc,6378
246
246
  tests/test_backtesting_crypto_cash_unit.py,sha256=4EO9jVajdZNV0M7zSyp4gpR_msZFoM4x5tb-6g-mHO8,11399
247
- tests/test_backtesting_data_source_env.py,sha256=Y36G2f0z_BsYkNA7zxKXwlbyQxKDXYbLDV_E4qjEem0,10671
247
+ tests/test_backtesting_data_source_env.py,sha256=MQaKNSbRzl5yXqFkMbBkt3C63stQtJJ8RE4jypVEv1U,12114
248
248
  tests/test_backtesting_flow_control.py,sha256=pBqW-fa-HnZq0apUBltalGMM-vNJ_2A5W2SoJzMK8Mg,7208
249
249
  tests/test_backtesting_multileg_unit.py,sha256=h1DPfVuYXXx-uq6KtUjr6_nasZuXPm_5gFat1XxCKIo,6456
250
250
  tests/test_backtesting_quiet_logs_complete.py,sha256=x-GfOiqkiUu8pYKCzB0UUacn13Nx_cPRth7_jmPY2Y8,14155
@@ -270,7 +270,7 @@ tests/test_databento_auto_expiry_integration.py,sha256=uPAyYTMTzEqppUUZx99G8pvnv
270
270
  tests/test_databento_backtesting.py,sha256=LqcuHgjivmHIcEcCjZNSPDdD7epCwdaKtj6Q6Q4KTF0,20671
271
271
  tests/test_databento_backtesting_polars.py,sha256=V9Z7-WG2TNKpHQVF5vGJSIdgUsuhJwlopZV66FREWA0,8097
272
272
  tests/test_databento_data.py,sha256=HakjDileGpicQc_OXeX7l8ncIDn3FtxP8ymKA6TQp8o,18860
273
- tests/test_databento_helper.py,sha256=C2FRkD3341FFHc09bObEYYN7eqUsO9aewyaLX9hCdvU,43465
273
+ tests/test_databento_helper.py,sha256=Oeojl4xTZi37JewuX0n-7ML8EQcLTbl5Jc4ugtQrYb8,43611
274
274
  tests/test_databento_live.py,sha256=tbg2C9cyW45OYY7dKYlPMNZpgsN_sjs4yk1GR23OL6o,15648
275
275
  tests/test_databento_timezone_fixes.py,sha256=NsfND7yTdKH2ddiYYhO6kU3m41V7se7C4_zTvqKOGv0,11562
276
276
  tests/test_drift_rebalancer.py,sha256=AUuEd3WIunfx3gwVdLVtq8jOHlz65UeqpO4adY1xfcs,105289
@@ -337,13 +337,13 @@ tests/backtest/test_buy_hold_quiet_logs_full_run.py,sha256=LDiR8wsEwIASPnO_bUMid
337
337
  tests/backtest/test_crypto_cash_regressions.py,sha256=-f0wjb-9nXpggS30N4zomYl098Qu-tfvfWwhlkoxPMM,6077
338
338
  tests/backtest/test_daily_data_timestamp_comparison.py,sha256=DeMLH0hwl0zEybJF0YTyBgfO8p96DGLahiBCwVQKjA4,35910
339
339
  tests/backtest/test_databento.py,sha256=L7UIN0IjxdVzE65ujBvsoKyQhY4KHTctIOeOeWvVNBY,7485
340
- tests/backtest/test_databento_comprehensive_trading.py,sha256=M7M2YHVkYRi5xBXifUESCnX5BFdtUDXy-HAhgazBAC4,23615
341
- tests/backtest/test_databento_parity.py,sha256=bG10kIJlkLnXfPaEYMVhaf8D5V5Gf-qoyB9PW-H8JzM,2553
340
+ tests/backtest/test_databento_comprehensive_trading.py,sha256=ZqZm6yOc9qAPSpjPLRIE2EZTr3jQPCtlmeKSLN9vOQw,21990
341
+ tests/backtest/test_databento_parity.py,sha256=YGF3L7dv4CMfJISj1KrObBWXneK3xwjp7Iw4mVtR32k,3481
342
342
  tests/backtest/test_debug_avg_fill_price.py,sha256=W4pgvCkAwmqlK82yzlw7u-s8VXe-dIQjPu-BVQkwJvg,3520
343
343
  tests/backtest/test_dividends.py,sha256=tDEnK4IcqHPHYPXAfUlApEBOf-iMROSl3aCap0BX1nI,9970
344
344
  tests/backtest/test_example_strategies.py,sha256=mSRRONtKdd7x0DUiuqCZD3YXLiPS8kAGn5wT1f_gDlk,15482
345
345
  tests/backtest/test_failing_backtest.py,sha256=jBkm_3Yq-TrzezAQM7XEAn3424lzG6Mu5agnTJQCo6E,5460
346
- tests/backtest/test_futures_edge_cases.py,sha256=87j1WQ3ywE1FkusZGlW9IXFlqFhf776EXHd-yQmySZY,15422
346
+ tests/backtest/test_futures_edge_cases.py,sha256=If97ybqmaGw3lxK8hMSNFGaRM3ovDphNdyCqLhGmFrE,18314
347
347
  tests/backtest/test_futures_single_trade.py,sha256=seGe-mOkoFnCY58HT-iaWktPo76-WG0xgxPkOiL6BOg,10838
348
348
  tests/backtest/test_futures_ultra_simple.py,sha256=UWGhuhwALCpez-yJxmbke5pfW6vHLDsHbsezwezU934,6875
349
349
  tests/backtest/test_index_data_verification.py,sha256=oCM_xRMylyDkUS0uDXCyj4KVKs_GG24lcqCvQq1Exvw,13737
@@ -356,7 +356,7 @@ tests/backtest/test_thetadata.py,sha256=xWYfC9C4EhbMDb29qyZWHO3sSWaLIPzzvcMbHCt5
356
356
  tests/backtest/test_thetadata_comprehensive.py,sha256=-gN3xLJcJtlB-k4vlaK82DCZDGDmr0LNZZDzn-aN3l4,26120
357
357
  tests/backtest/test_thetadata_vs_polygon.py,sha256=dZqsrOx3u3cz-1onIO6o5BDRjI1ey7U9vIkZupfXoig,22831
358
358
  tests/backtest/test_yahoo.py,sha256=uHb9aK3uHYEvA7MI_y1dbKm7mjHrErlxU7TJOgVdzs8,1966
359
- lumibot-4.1.2.dist-info/METADATA,sha256=45LvQ6tGhCYieVoXNPDspLGbhfj7JccZ1xZFzTilsHs,11721
360
- lumibot-4.1.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
361
- lumibot-4.1.2.dist-info/top_level.txt,sha256=otUnUjDFVASauEDiTiAzNgMyqQ1B6jjS3QqqP-WSx38,14
362
- lumibot-4.1.2.dist-info/RECORD,,
359
+ lumibot-4.1.3.dist-info/METADATA,sha256=bqCXhktv5-RaCGiktkddUfpmjDUfBKhzS5josA6A1Sk,11721
360
+ lumibot-4.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
361
+ lumibot-4.1.3.dist-info/top_level.txt,sha256=otUnUjDFVASauEDiTiAzNgMyqQ1B6jjS3QqqP-WSx38,14
362
+ lumibot-4.1.3.dist-info/RECORD,,
@@ -215,6 +215,14 @@ class TestDatabentoComprehensiveTrading:
215
215
 
216
216
  print(f"\n Instruments traded: {list(trades_by_instrument.keys())}")
217
217
 
218
+ snapshots_by_symbol = {}
219
+ for snap in strat.snapshots:
220
+ symbol = snap.get("current_asset")
221
+ if symbol:
222
+ snapshots_by_symbol.setdefault(symbol, []).append(snap)
223
+
224
+ fee_amount = float(fee.flat_fee)
225
+
218
226
  # Analyze each instrument's trades
219
227
  for symbol, trades in trades_by_instrument.items():
220
228
  print(f"\n" + "-"*80)
@@ -249,16 +257,45 @@ class TestDatabentoComprehensiveTrading:
249
257
  assert actual_asset.multiplier == expected_multiplier, \
250
258
  f"{symbol} asset.multiplier should be {expected_multiplier}, got {actual_asset.multiplier}"
251
259
 
252
- # For now, just verify cash is reasonable (not testing exact margin since
253
- # we may have P&L from previous trades affecting cash)
254
- print(f"\nCASH STATE:")
255
- print(f" Cash after entry: ${entry['cash_after']:,.2f}")
256
- print(f" (Note: Cash includes P&L from previous trades)")
257
-
258
- # Verify portfolio value is reasonable (shouldn't be massively negative)
259
- portfolio_after = entry['portfolio_after']
260
- assert portfolio_after > 50000, \
261
- f"{symbol} portfolio value seems wrong after entry: ${portfolio_after:,.2f}"
260
+ symbol_snapshots = snapshots_by_symbol.get(symbol, [])
261
+ entry_snapshot = next((s for s in symbol_snapshots if s.get("phase") == "BUY"), None)
262
+ sell_snapshot = next((s for s in symbol_snapshots if s.get("phase") == "SELL"), None)
263
+ hold_snapshots = [s for s in symbol_snapshots if s.get("phase") == "HOLD"]
264
+
265
+ assert entry_snapshot is not None, f"No entry snapshot recorded for {symbol}"
266
+ assert sell_snapshot is not None, f"No sell snapshot recorded for {symbol}"
267
+
268
+ cash_before_entry = float(entry_snapshot["cash"])
269
+ entry_cash_after = float(entry["cash_after"])
270
+ margin_deposit = cash_before_entry - entry_cash_after - fee_amount
271
+ expected_margin_total = expected_margin * float(entry["quantity"])
272
+
273
+ print(f"\nCASH / MARGIN STATE:")
274
+ print(f" Cash before entry: ${cash_before_entry:,.2f}")
275
+ print(f" Cash after entry: ${entry_cash_after:,.2f}")
276
+ print(f" Margin captured: ${margin_deposit:,.2f} (expected ${expected_margin_total:,.2f})")
277
+ assert pytest.approx(margin_deposit, abs=0.01) == expected_margin_total, (
278
+ f"{symbol} margin mismatch: expected ${expected_margin_total:,.2f}, "
279
+ f"got ${margin_deposit:,.2f}"
280
+ )
281
+
282
+ # Verify mark-to-market during hold period is exact
283
+ for snap in hold_snapshots:
284
+ price = snap.get("price")
285
+ if price is None:
286
+ continue
287
+ unrealized = (price - entry["price"]) * float(entry["quantity"]) * expected_multiplier
288
+ expected_portfolio = entry_cash_after + margin_deposit + unrealized
289
+ assert pytest.approx(expected_portfolio, abs=0.01) == float(snap["portfolio"]), (
290
+ f"{symbol} mark-to-market mismatch at {snap['datetime']}: "
291
+ f"expected ${expected_portfolio:,.2f}, got ${snap['portfolio']:,.2f}"
292
+ )
293
+
294
+ # Snapshot immediately before exit should have identical cash to post-entry state
295
+ assert pytest.approx(float(sell_snapshot["cash"]), abs=0.01) == entry_cash_after, (
296
+ f"{symbol} cash prior to exit changed unexpectedly: "
297
+ f"{sell_snapshot['cash']} vs {entry_cash_after}"
298
+ )
262
299
 
263
300
  if len(exits) > 0 and len(entries) > 0:
264
301
  entry = entries[0]
@@ -283,79 +320,21 @@ class TestDatabentoComprehensiveTrading:
283
320
  print(f" Price change: ${price_change:.2f}")
284
321
  print(f" Expected P&L: ${expected_pnl:.2f} (change × qty × {expected_multiplier})")
285
322
 
286
- # Verify final portfolio reflects P&L
287
- # Note: We can't verify exact final cash without knowing all previous trades,
288
- # but we can verify the P&L calculation makes sense
289
- assert abs(expected_pnl) < 100000, \
290
- f"{symbol} P&L seems unrealistic: {expected_pnl}"
291
-
292
- # CRITICAL: Verify portfolio value changed by approximately expected P&L
293
- # (can't be exact due to fees and previous trades, but should be in ballpark)
294
- entry_portfolio = entry['portfolio_after']
295
- exit_portfolio = exit_trade['portfolio_after']
296
- portfolio_change = exit_portfolio - entry_portfolio
297
-
298
- # Portfolio change should be close to expected P&L (within margin for fees/rounding)
299
- pnl_diff = abs(portfolio_change - expected_pnl)
300
- print(f" Portfolio change: ${portfolio_change:.2f}")
301
- print(f" Difference from expected: ${pnl_diff:.2f}")
302
-
303
- # Allow generous tolerance for fees, rounding, and concurrent trades
304
- # For small P&L, allow larger percentage; for large P&L, allow smaller percentage
305
- tolerance = max(abs(expected_pnl) * 0.5, 500)
306
- # For this comprehensive test with multiple concurrent trades, just verify it's reasonable
307
- # (exact match is tested in simpler single-trade tests)
308
- if pnl_diff < tolerance:
309
- print(f" ✓ Portfolio change matches expected P&L within tolerance")
310
- else:
311
- print(f" ⚠ Portfolio change differs (may be due to concurrent trades)")
312
-
313
- # CRITICAL: Verify unrealized P&L during HOLD periods
314
- # This catches bugs in portfolio value calculation (multiplier applied to unrealized P&L)
315
- print(f"\n" + "-"*80)
316
- print("VERIFYING UNREALIZED P&L DURING HOLD PERIODS")
317
- print("-"*80)
318
-
319
- for symbol in trades_by_instrument.keys():
320
- # Find snapshots where we're holding this position
321
- holding_snapshots = [s for s in strat.snapshots if s['position_qty'] > 0 and s.get('current_asset') == symbol]
322
-
323
- if len(holding_snapshots) >= 2:
324
- # Check a couple of snapshots during the hold
325
- snap = holding_snapshots[len(holding_snapshots)//2] # middle of hold period
326
-
327
- # Get the entry trade for this position
328
- entries = [t for t in trades_by_instrument[symbol] if "buy" in str(t["side"]).lower()]
329
- if entries:
330
- entry = entries[0]
331
- entry_price = entry['price']
332
- quantity = entry['quantity']
333
- current_price = snap['price']
334
- expected_mult = CONTRACT_SPECS.get(symbol, {}).get("multiplier", 1)
335
- expected_margin = CONTRACT_SPECS.get(symbol, {}).get("margin", 1000)
336
-
337
- # Calculate expected portfolio value
338
- cash = snap['cash']
339
- margin_tied_up = quantity * expected_margin
340
- unrealized_pnl = (current_price - entry_price) * quantity * expected_mult
341
- expected_portfolio = cash + margin_tied_up + unrealized_pnl
342
- actual_portfolio = snap['portfolio']
343
-
344
- print(f"\n{symbol} during HOLD (snapshot {strat.snapshots.index(snap)}):")
345
- print(f" Entry: ${entry_price:.2f} × {quantity} contracts")
346
- print(f" Current: ${current_price:.2f}")
347
- print(f" Cash: ${cash:,.2f}")
348
- print(f" Margin: ${margin_tied_up:,.2f}")
349
- print(f" Unrealized P&L: ${unrealized_pnl:,.2f} = (${current_price:.2f} - ${entry_price:.2f}) × {quantity} × {expected_mult}")
350
- print(f" Expected portfolio: ${expected_portfolio:,.2f}")
351
- print(f" Actual portfolio: ${actual_portfolio:,.2f}")
352
- print(f" Difference: ${abs(actual_portfolio - expected_portfolio):,.2f}")
353
-
354
- # This tolerance should catch multiplier bugs (5x error would be huge)
355
- tolerance = max(abs(expected_portfolio) * 0.02, 100) # 2% or $100
356
- assert abs(actual_portfolio - expected_portfolio) < tolerance, \
357
- f"{symbol} portfolio value incorrect during hold: expected ${expected_portfolio:,.2f}, got ${actual_portfolio:,.2f}"
358
- print(f" ✓ Portfolio value matches expected (within ${tolerance:.2f})")
323
+ cash_before_entry = float(snapshots_by_symbol[symbol][0]["cash"])
324
+ expected_cash_after_exit = (
325
+ cash_before_entry
326
+ - fee_amount # entry fee
327
+ - fee_amount # exit fee
328
+ + expected_pnl
329
+ )
330
+ print(f"\nCASH RECONCILIATION:")
331
+ print(f" Expected cash after exit: ${expected_cash_after_exit:,.2f}")
332
+ actual_cash_after_exit = float(exit_trade["cash_after"])
333
+ print(f" Actual cash after exit: ${actual_cash_after_exit:,.2f}")
334
+ assert pytest.approx(expected_cash_after_exit, abs=0.01) == actual_cash_after_exit, (
335
+ f"{symbol} cash after exit mismatch: expected ${expected_cash_after_exit:,.2f}, "
336
+ f"got ${actual_cash_after_exit:,.2f}"
337
+ )
359
338
 
360
339
  print(f"\n" + "="*80)
361
340
  print("✓ ALL INSTRUMENTS VERIFIED")
@@ -1,9 +1,10 @@
1
1
  """Parity checks between DataBento pandas and polars backends."""
2
2
 
3
3
  import shutil
4
- from pathlib import Path
5
4
  from datetime import datetime
5
+ from pathlib import Path
6
6
 
7
+ import numpy as np
7
8
  import pandas as pd
8
9
  import pytest
9
10
  import pytz
@@ -56,10 +57,31 @@ def test_databento_price_parity():
56
57
  )
57
58
 
58
59
  # Prime caches
59
- pandas_bars = pandas_ds.get_historical_prices(asset, 500, timestep="minute").df
60
- polars_bars = polars_ds.get_historical_prices(asset, 500, timestep="minute").df
60
+ pandas_bars = pandas_ds.get_historical_prices(asset, 500, timestep="minute").df.sort_index()
61
+ polars_bars = polars_ds.get_historical_prices(asset, 500, timestep="minute").df.sort_index()
62
+
63
+ candidate_columns = ["open", "high", "low", "close", "volume", "vwap"]
64
+ common_columns = [col for col in candidate_columns if col in pandas_bars.columns and col in polars_bars.columns]
65
+ assert common_columns, "No shared OHLCV columns between pandas and polars DataFrames"
61
66
 
62
- pd.testing.assert_frame_equal(polars_bars, pandas_bars)
67
+ aligned_pandas = pandas_bars[common_columns].copy()
68
+ aligned_polars = polars_bars[common_columns].copy()
69
+
70
+ for col in common_columns:
71
+ dtype_left = aligned_pandas[col].dtype
72
+ dtype_right = aligned_polars[col].dtype
73
+ if dtype_left != dtype_right:
74
+ target_dtype = np.promote_types(dtype_left, dtype_right)
75
+ aligned_pandas[col] = aligned_pandas[col].astype(target_dtype)
76
+ aligned_polars[col] = aligned_polars[col].astype(target_dtype)
77
+
78
+ pd.testing.assert_frame_equal(
79
+ aligned_pandas,
80
+ aligned_polars,
81
+ check_exact=True,
82
+ check_index_type=True,
83
+ check_column_type=True,
84
+ )
63
85
 
64
86
  checkpoints = [
65
87
  (0, 0),
@@ -76,6 +98,6 @@ def test_databento_price_parity():
76
98
  polars_ds._datetime = current_dt
77
99
  pandas_price = pandas_ds.get_last_price(asset)
78
100
  polars_price = polars_ds.get_last_price(asset)
79
- assert pandas_price == pytest.approx(polars_price), (
101
+ assert pandas_price == polars_price, (
80
102
  f"Mismatch at {current_dt}: pandas={pandas_price}, polars={polars_price}"
81
103
  )
@@ -31,6 +31,8 @@ DATABENTO_API_KEY = DATABENTO_CONFIG.get("API_KEY")
31
31
  # Contract specs
32
32
  MES_MULTIPLIER = 5
33
33
  ES_MULTIPLIER = 50
34
+ MES_MARGIN = 1300
35
+ ES_MARGIN = 13000
34
36
 
35
37
 
36
38
  def _clear_polars_cache():
@@ -211,6 +213,8 @@ class TestFuturesEdgeCases:
211
213
 
212
214
  broker = BacktestingBroker(data_source=data_source)
213
215
  fee = TradingFee(flat_fee=0.50)
216
+ fee_amount = float(fee.flat_fee)
217
+ fee_amount = float(fee.flat_fee)
214
218
 
215
219
  strat = ShortSellingStrategy(
216
220
  broker=broker,
@@ -252,7 +256,7 @@ class TestFuturesEdgeCases:
252
256
  assert trade['multiplier'] == MES_MULTIPLIER, \
253
257
  f"MES multiplier should be {MES_MULTIPLIER}, got {trade['multiplier']}"
254
258
 
255
- # If we have both entry and exit, verify P&L
259
+ # If we have both entry and exit, verify P&L and cash bookkeeping exactly
256
260
  if len(strat.trades) >= 2:
257
261
  entry = strat.trades[0] # Sell to open
258
262
  exit_trade = strat.trades[1] # Buy to cover
@@ -261,10 +265,19 @@ class TestFuturesEdgeCases:
261
265
  print("P&L VERIFICATION (SHORT TRADE)")
262
266
  print("-"*80)
263
267
 
264
- # For short: P&L = (Entry - Exit) × Qty × Multiplier (inverted!)
268
+ entry_snapshot = strat.snapshots[0]
269
+ holding_snapshots = [s for s in strat.snapshots if s['position_qty'] < 0]
270
+ assert holding_snapshots, "No holding snapshots recorded for short position"
271
+ sell_snapshot = holding_snapshots[-1] # still short immediately before closing
272
+ final_snapshot = next((s for s in strat.snapshots if s['position_qty'] == 0 and s['iteration'] > sell_snapshot['iteration']), strat.snapshots[-1])
273
+
274
+ margin_deposit = float(entry_snapshot['cash']) - float(entry['cash_after']) - fee_amount
275
+ print(f" Margin captured: ${margin_deposit:.2f}")
276
+ assert pytest.approx(margin_deposit, abs=0.01) == 1300.0, f"Expected $1,300 margin, got ${margin_deposit:.2f}"
277
+
265
278
  entry_price = entry['price']
266
279
  exit_price = exit_trade['price']
267
- price_change = entry_price - exit_price # Inverted for short
280
+ price_change = entry_price - exit_price # inverted for short
268
281
  expected_pnl = price_change * MES_MULTIPLIER
269
282
 
270
283
  print(f" Entry (sell): ${entry_price:.2f}")
@@ -272,23 +285,34 @@ class TestFuturesEdgeCases:
272
285
  print(f" Price change (entry - exit): ${price_change:.2f}")
273
286
  print(f" Expected P&L: ${expected_pnl:.2f} (inverted for short)")
274
287
 
275
- # Verify final cash
276
- starting_cash = 100000
277
- total_fees = 1.00 # 2 × $0.50
278
- expected_final_cash = starting_cash + expected_pnl - total_fees
279
-
280
- # Get final snapshot cash
281
- final_cash = strat.snapshots[-1]['cash']
282
- cash_diff = abs(expected_final_cash - final_cash)
283
-
284
- print(f"\n Starting cash: ${starting_cash:,.2f}")
285
- print(f" Expected final cash: ${expected_final_cash:,.2f}")
286
- print(f" Actual final cash: ${final_cash:,.2f}")
287
- print(f" Difference: ${cash_diff:.2f}")
288
-
289
- # Allow tolerance for timing/fill differences
290
- assert cash_diff < 150, f"Cash difference too large: ${cash_diff:.2f}"
291
- print(f"\n✓ PASS: Short selling P&L is correct")
288
+ # Mark-to-market validation during the hold window
289
+ for snap in holding_snapshots[:-1]:
290
+ current_price = snap['price']
291
+ if current_price is None:
292
+ continue
293
+ unrealized = (entry_price - current_price) * MES_MULTIPLIER
294
+ expected_portfolio = float(entry['cash_after']) + margin_deposit + unrealized
295
+ assert pytest.approx(expected_portfolio, abs=0.01) == float(snap['portfolio']), (
296
+ f"Short unrealized P&L mismatch at {snap['datetime']}: "
297
+ f"expected ${expected_portfolio:,.2f}, got ${snap['portfolio']:,.2f}"
298
+ )
299
+
300
+ assert pytest.approx(float(sell_snapshot['cash']), abs=0.01) == float(entry['cash_after']), \
301
+ "Cash should not drift while holding the short position"
302
+
303
+ # Final cash should equal initial cash minus fees plus realized P&L
304
+ starting_cash = float(entry_snapshot['cash'])
305
+ expected_final_cash = starting_cash - 2 * fee_amount + expected_pnl
306
+ print(f"\n Expected final cash: ${expected_final_cash:,.2f}")
307
+ actual_final_cash = float(final_snapshot['cash'])
308
+ print(f" Actual final cash: ${actual_final_cash:,.2f}")
309
+ assert pytest.approx(expected_final_cash, abs=0.01) == actual_final_cash, \
310
+ f"Final cash mismatch: expected ${expected_final_cash:,.2f}, got ${actual_final_cash:,.2f}"
311
+
312
+ assert pytest.approx(expected_final_cash, abs=0.01) == float(exit_trade['cash_after']), \
313
+ "Cash reported in exit trade callback should match final ledger"
314
+
315
+ print(f"\n✓ PASS: Short selling P&L and cash bookkeeping are exact")
292
316
 
293
317
  print("\n" + "="*80)
294
318
  print("✓ SHORT SELLING TEST PASSED")
@@ -383,54 +407,63 @@ class TestFuturesEdgeCases:
383
407
  assert trade['multiplier'] == ES_MULTIPLIER, \
384
408
  f"ES multiplier should be {ES_MULTIPLIER}, got {trade['multiplier']}"
385
409
 
386
- # Calculate expected P&L for each instrument
410
+ fee_amount = float(fee.flat_fee)
411
+ mes_entry = mes_trades[0]
412
+ mes_exit = mes_trades[1]
413
+ es_entry = es_trades[0]
414
+ es_exit = es_trades[1]
415
+
416
+ mes_entry_snapshot = next(s for s in strat.snapshots if s['datetime'] == mes_entry['datetime'])
417
+ es_entry_snapshot = next(s for s in strat.snapshots if s['datetime'] == es_entry['datetime'])
418
+
419
+ mes_margin = float(mes_entry_snapshot['cash']) - float(mes_entry['cash_after']) - fee_amount
420
+ es_margin = float(es_entry_snapshot['cash']) - float(es_entry['cash_after']) - fee_amount
421
+
422
+ assert pytest.approx(mes_margin, abs=0.01) == MES_MARGIN, \
423
+ f"MES margin should be ${MES_MARGIN}, got ${mes_margin:.2f}"
424
+ assert pytest.approx(es_margin, abs=0.01) == ES_MARGIN, \
425
+ f"ES margin should be ${ES_MARGIN}, got ${es_margin:.2f}"
426
+
387
427
  print("\n" + "-"*80)
388
- print("P&L VERIFICATION")
428
+ print("MARK-TO-MARKET VERIFICATION")
389
429
  print("-"*80)
390
430
 
391
- # MES P&L
392
- mes_entry = mes_trades[0]
393
- mes_exit = mes_trades[1]
394
- mes_pnl = (mes_exit['price'] - mes_entry['price']) * MES_MULTIPLIER
431
+ for snap in strat.snapshots:
432
+ expected_portfolio = float(snap['cash'])
395
433
 
396
- print(f"\nMES:")
397
- print(f" Entry: ${mes_entry['price']:.2f}")
398
- print(f" Exit: ${mes_exit['price']:.2f}")
399
- print(f" P&L: ${mes_pnl:.2f}")
434
+ if snap['mes_qty'] != 0 and snap.get('mes_price') is not None:
435
+ expected_portfolio += abs(snap['mes_qty']) * MES_MARGIN
436
+ expected_portfolio += (snap['mes_price'] - mes_entry['price']) * snap['mes_qty'] * MES_MULTIPLIER
400
437
 
401
- # ES P&L
402
- es_entry = es_trades[0]
403
- es_exit = es_trades[1]
404
- es_pnl = (es_exit['price'] - es_entry['price']) * ES_MULTIPLIER
438
+ if snap['es_qty'] != 0 and snap.get('es_price') is not None:
439
+ expected_portfolio += abs(snap['es_qty']) * ES_MARGIN
440
+ expected_portfolio += (snap['es_price'] - es_entry['price']) * snap['es_qty'] * ES_MULTIPLIER
405
441
 
406
- print(f"\nES:")
407
- print(f" Entry: ${es_entry['price']:.2f}")
408
- print(f" Exit: ${es_exit['price']:.2f}")
409
- print(f" P&L: ${es_pnl:.2f}")
442
+ assert pytest.approx(expected_portfolio, abs=0.01) == float(snap['portfolio']), (
443
+ f"Portfolio mismatch at {snap['datetime']}: "
444
+ f"expected ${expected_portfolio:,.2f}, got ${snap['portfolio']:,.2f}"
445
+ )
410
446
 
411
- # Total P&L
447
+ mes_pnl = (mes_exit['price'] - mes_entry['price']) * MES_MULTIPLIER
448
+ es_pnl = (es_exit['price'] - es_entry['price']) * ES_MULTIPLIER
412
449
  total_pnl = mes_pnl + es_pnl
413
- total_fees = 4.00 # 4 trades × $0.50 each (assuming $0.50 per side)
414
-
415
- print(f"\nTotal:")
416
- print(f" Combined P&L: ${total_pnl:.2f}")
417
- print(f" Total fees: ${total_fees:.2f}")
418
- print(f" Net P&L: ${total_pnl - total_fees:.2f}")
419
-
420
- # Verify final cash
421
- starting_cash = 100000
422
- expected_final_cash = starting_cash + total_pnl - total_fees
423
- final_cash = strat.snapshots[-1]['cash']
424
- cash_diff = abs(expected_final_cash - final_cash)
425
-
426
- print(f"\n Starting cash: ${starting_cash:,.2f}")
427
- print(f" Expected final cash: ${expected_final_cash:,.2f}")
428
- print(f" Actual final cash: ${final_cash:,.2f}")
429
- print(f" Difference: ${cash_diff:.2f}")
430
-
431
- # Allow tolerance
432
- assert cash_diff < 200, f"Cash difference too large: ${cash_diff:.2f}"
433
- print(f"\n✓ PASS: Multiple simultaneous positions tracked correctly")
450
+ total_fees = 4 * fee_amount
451
+
452
+ starting_cash = float(strat.snapshots[0]['cash'])
453
+ expected_final_cash = starting_cash - total_fees + total_pnl
454
+ final_cash = float(strat.snapshots[-1]['cash'])
455
+
456
+ print(f"\nTotal realised P&L: ${total_pnl:.2f}")
457
+ print(f"Total fees: ${total_fees:.2f}")
458
+ print(f"Expected final cash: ${expected_final_cash:,.2f}")
459
+ print(f"Actual final cash: ${final_cash:,.2f}")
460
+
461
+ assert pytest.approx(expected_final_cash, abs=0.01) == final_cash, \
462
+ f"Final cash mismatch: expected ${expected_final_cash:,.2f}, got ${final_cash:,.2f}"
463
+ assert pytest.approx(expected_final_cash, abs=0.01) == float(es_exit['cash_after']), \
464
+ "Exit trade cash should match ledger final cash"
465
+
466
+ print(f"\n✓ PASS: Multiple simultaneous positions tracked with exact accounting")
434
467
 
435
468
  # Verify we had both positions at the same time (iteration 3-4)
436
469
  snapshot_with_both = None
@@ -98,7 +98,7 @@ class TestBacktestingDataSourceEnv:
98
98
  )
99
99
 
100
100
  # Verify the log message shows polygon was selected
101
- assert any("Auto-selected backtesting data source from BACKTESTING_DATA_SOURCE env var: polygon" in record.message
101
+ assert any("Using BACKTESTING_DATA_SOURCE setting for backtest data: polygon" in record.message
102
102
  for record in caplog.records)
103
103
 
104
104
  def test_auto_select_thetadata_case_insensitive(self, clean_environment, restore_theta_credentials, caplog):
@@ -130,7 +130,7 @@ class TestBacktestingDataSourceEnv:
130
130
  pass
131
131
 
132
132
  # Verify the log message shows thetadata was selected OR check for ThetaData error
133
- thetadata_selected = any("Auto-selected backtesting data source from BACKTESTING_DATA_SOURCE env var: THETADATA" in record.message
133
+ thetadata_selected = any("Using BACKTESTING_DATA_SOURCE setting for backtest data: THETADATA" in record.message
134
134
  for record in caplog.records)
135
135
  thetadata_attempted = any("Cannot connect to Theta Data" in record.message or "ThetaData" in record.message
136
136
  for record in caplog.records)
@@ -183,8 +183,11 @@ class TestBacktestingDataSourceEnv:
183
183
  show_indicators=False,
184
184
  )
185
185
 
186
- def test_explicit_datasource_overrides_env(self, clean_environment, restore_theta_credentials, caplog):
187
- """Test that explicit datasource_class overrides BACKTESTING_DATA_SOURCE env var."""
186
+ def test_env_override_wins_over_explicit_datasource(self, clean_environment, restore_theta_credentials, caplog):
187
+ """Test that BACKTESTING_DATA_SOURCE env var takes precedence over explicit datasource_class."""
188
+ import logging
189
+ caplog.set_level(logging.INFO, logger='lumibot.strategies._strategy')
190
+
188
191
  with patch.dict(os.environ, {'BACKTESTING_DATA_SOURCE': 'polygon'}):
189
192
  # Re-import credentials to pick up env change
190
193
  from importlib import reload
@@ -205,9 +208,36 @@ class TestBacktestingDataSourceEnv:
205
208
  show_progress_bar=False,
206
209
  )
207
210
 
208
- # Verify the auto-select message was NOT logged (explicit datasource was used)
209
- assert not any("Auto-selected backtesting data source" in record.message
210
- for record in caplog.records)
211
+ # Verify the env override message was logged (env var wins)
212
+ assert any("Using BACKTESTING_DATA_SOURCE setting for backtest data: polygon" in record.message
213
+ for record in caplog.records)
214
+
215
+ def test_explicit_datasource_used_when_env_none(self, clean_environment, restore_theta_credentials, caplog):
216
+ """Test that setting BACKTESTING_DATA_SOURCE to 'none' defers to the explicit datasource_class."""
217
+ import logging
218
+ caplog.set_level(logging.INFO, logger='lumibot.strategies._strategy')
219
+
220
+ with patch.dict(os.environ, {'BACKTESTING_DATA_SOURCE': 'none'}):
221
+ from importlib import reload
222
+ import lumibot.credentials
223
+ reload(lumibot.credentials)
224
+
225
+ backtesting_start = datetime(2023, 1, 1)
226
+ backtesting_end = datetime(2023, 1, 10) # Shorter backtest for speed
227
+
228
+ SimpleTestStrategy.run_backtest(
229
+ YahooDataBacktesting,
230
+ backtesting_start=backtesting_start,
231
+ backtesting_end=backtesting_end,
232
+ show_plot=False,
233
+ show_tearsheet=False,
234
+ show_indicators=False,
235
+ show_progress_bar=False,
236
+ )
237
+
238
+ # Confirm no override occurred
239
+ assert not any("Using BACKTESTING_DATA_SOURCE setting for backtest data" in record.message
240
+ for record in caplog.records)
211
241
 
212
242
  def test_default_thetadata_when_no_env_set(self, clean_environment, restore_theta_credentials, caplog):
213
243
  """Test that ThetaData is the default when BACKTESTING_DATA_SOURCE is not set."""
@@ -240,9 +270,13 @@ class TestBacktestingDataSourceEnv:
240
270
  # Expected to fail with test credentials - that's okay
241
271
  pass
242
272
 
243
- # Verify ThetaData was attempted (no auto-select message since it's the default)
244
- assert any("Cannot connect to Theta Data" in record.message or "ThetaData" in record.message
245
- for record in caplog.records), "ThetaData was not used as default"
273
+ # Verify ThetaData was attempted (look for override message or Theta-specific logs)
274
+ assert any(
275
+ "Using BACKTESTING_DATA_SOURCE setting for backtest data: ThetaData" in record.message
276
+ or "Cannot connect to Theta Data" in record.message
277
+ or "ThetaData" in record.message
278
+ for record in caplog.records
279
+ ), "ThetaData was not used as default"
246
280
 
247
281
 
248
282
  if __name__ == "__main__":
@@ -49,7 +49,12 @@ class TestDataBentoHelper(unittest.TestCase):
49
49
  self.assertRegex(result, r"ES[FGHJKMNQUVXZ]\d", "Should auto-resolve to contract format like ESZ5")
50
50
 
51
51
  # Test specific contract with expiration (March 2025 = H25)
52
- result = databento_helper._format_futures_symbol_for_databento(self.test_asset_future)
52
+ specific_future = Asset(
53
+ symbol="ES",
54
+ asset_type="future",
55
+ expiration=datetime(2025, 3, 15).date()
56
+ )
57
+ result = databento_helper._format_futures_symbol_for_databento(specific_future)
53
58
  self.assertEqual(result, "ESH25") # March 2025 = H25
54
59
 
55
60
  # Test another month (December 2024 = Z24)