Skip to content

[Bug]: BarSet.__contains__ returns False even when data exists #641

Description

@tdebasis

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

The BarSet class returned by StockHistoricalDataClient.get_stock_bars() has a broken contains implementation. Using symbol in bars always returns False even when the symbol's data exists in bars.data.

Actual Behavior

  • 'SPY' in bars → False
  • 'SPY' in bars.data → True
  • bars['SPY'] → Works correctly (returns list of bars)

Environment

  • alpaca-py version: 0.33.1, reconfirmed in 0.43.2
  • Python version: 3.11

Suggested Fix

Add a contains method to BarSet that delegates to self.data:

def contains(self, key):
return key in self.data

Expected Behavior

When I call get_stock_bars() and check if a symbol exists using 'SPY' in bars, it should return True if bar data for that symbol was returned by the API.

SDK Version I encountered this issue in

Bug found in 0.33.1
Reconfirmed that bug exists in 0.43.2

Steps To Reproduce

Reproduction Steps

  from alpaca.data import StockHistoricalDataClient
  from alpaca.data.requests import StockBarsRequest
  from alpaca.data.timeframe import TimeFrame
  from datetime import datetime
  import pytz

  ET = pytz.timezone('America/New_York')

  client = StockHistoricalDataClient('<API_KEY>', '<API_SECRET>')

  now = datetime.now(ET)
  market_open = now.replace(hour=9, minute=30, second=0, microsecond=0)

  req = StockBarsRequest(
      symbol_or_symbols='SPY',
      timeframe=TimeFrame.Minute,
      start=market_open
  )
  bars = client.get_stock_bars(req)

  # BUG: These produce inconsistent results
  print(f"'SPY' in bars: {'SPY' in bars}")           # False (WRONG!)
  print(f"'SPY' in bars.data: {'SPY' in bars.data}") # True  (correct)
  print(f"len(bars['SPY']): {len(bars['SPY'])}")     # Works, returns 80+ bars

  Expected Behavior

  'SPY' in bars should return True when bars['SPY'] contains data.

Filled out the Steps to Reproduce section?

  • I have entered valid steps to reproduce my issue or have attached a minimally reproducible case in code that shows my issue happening; and understand that without this my issue will be flagged as invalid and closed after 30 days.

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions