@@ -947,6 +947,7 @@ def get_last_price_from_databento(
947947 api_key : str ,
948948 asset : Asset ,
949949 venue : Optional [str ] = None ,
950+ reference_date : Optional [datetime ] = None ,
950951 ** kwargs
951952) -> Optional [Union [float , Decimal ]]:
952953 """
@@ -978,12 +979,14 @@ def get_last_price_from_databento(
978979
979980 # For continuous futures, resolve to the current active contract
980981 if asset .asset_type == Asset .AssetType .CONT_FUTURE :
981- # Use Asset class method to resolve continuous futures to actual contract (returns string)
982- resolved_symbol = asset .resolve_continuous_futures_contract (year_digits = 1 )
982+ # Resolve based on reference date when backtesting so we match the contract in use
983+ resolved_symbol = _format_futures_symbol_for_databento (
984+ asset ,
985+ reference_date = reference_date ,
986+ )
983987 if resolved_symbol is None :
984988 logger .error (f"Could not resolve continuous futures contract for { asset .symbol } " )
985989 return None
986- # Generate the correct DataBento symbol format (should be single result)
987990 symbols_to_try = _generate_databento_symbol_alternatives (asset .symbol , resolved_symbol )
988991 logger .info (f"Resolved continuous future { asset .symbol } to specific contract: { resolved_symbol } " )
989992 logger .info (f"DataBento symbol format for last price: { symbols_to_try [0 ]} " )
@@ -1000,12 +1003,17 @@ def get_last_price_from_databento(
10001003 if hasattr (range_result , 'end' ) and range_result .end :
10011004 if hasattr (range_result .end , 'tz_localize' ):
10021005 # Already a pandas Timestamp
1003- available_end = range_result .end if range_result .end .tz else range_result .end .tz_localize ('UTC' )
1006+ if range_result .end .tz is not None :
1007+ available_end = range_result .end .tz_convert ('UTC' )
1008+ else :
1009+ available_end = range_result .end .tz_localize ('UTC' )
10041010 else :
10051011 # Convert to pandas Timestamp
1006- available_end = pd .to_datetime (range_result .end ).tz_localize ('UTC' )
1012+ ts = pd .to_datetime (range_result .end )
1013+ available_end = ts if ts .tz is not None else ts .tz_localize ('UTC' )
10071014 elif isinstance (range_result , dict ) and 'end' in range_result :
1008- available_end = pd .to_datetime (range_result ['end' ]).tz_localize ('UTC' )
1015+ ts = pd .to_datetime (range_result ['end' ])
1016+ available_end = ts if ts .tz is not None else ts .tz_localize ('UTC' )
10091017 else :
10101018 logger .warning (f"Could not parse dataset range for { dataset } : { range_result } " )
10111019 # Fallback: use a recent date that's likely to have data
@@ -1047,10 +1055,10 @@ def get_last_price_from_databento(
10471055 df = pd .DataFrame (data )
10481056
10491057 if not df .empty :
1050- # Get the last available price (close price of most recent bar)
10511058 if 'close' in df .columns :
1052- price = df ['close' ].iloc [- 1 ]
1053- if pd .notna (price ):
1059+ closes = df ['close' ].dropna ()
1060+ if not closes .empty :
1061+ price = closes .iloc [- 1 ]
10541062 logger .info (f"✓ SUCCESS: Got last price for { symbol_to_use } : { price } " )
10551063 return float (price )
10561064
0 commit comments