Extracting bidprice from Python Streaming

This is a pretty basic Python based question. I’m currently running the following to run a few tests on the streaming API and getting back results; however, I would like to get only the ‘bidprice’ that is returned. I’m having a really difficult time extracting only that key, value pair.

How can I change this code to just return the bidprice?

import alpaca_trade_api as tradeapi

APCA_API_KEY_ID = '{my_key_id}'
APCA_API_SECRET_KEY = '{my_secret_key}'
uri = 'https://paper-api.alpaca.markets/stream'

conn = tradeapi.StreamConn(APCA_API_KEY_ID, APCA_API_SECRET_KEY, uri)

@conn.on(r'.*')
async def on_minute(conn, channel, bar):
    test = bar
    print(test)

# blocks forever
conn.run(['Q.SPY'])

# if Data API streaming is enabled
#conn.run(['trade_updates', 'alpacadatav1/T.SPY'])

Current output is:

Quote({   'askexchange': 9,
    'askprice': 306.2,
    'asksize': 1,
    'bidexchange': 17,
    'bidprice': 306.24,
    'bidsize': 1,
    'conditions': [1],
    'symbol': 'SPY',
    'timestamp': 1591114429414462654})

The return is an Object with this class:

<class 'alpaca_trade_api.entity.Quote'>

I know it’s a noob question, lol.
Thanks!

Same problem, I don’t know how to extract any information from the objects that are returned.

you can use:
print (bar._raw[‘bidprice’])

1 Like