Reputation: 13
request_close = StockLatestTradeRequest(symbol_or_symbols = "TSLA", start = datetime.now(), timeframe = TimeFrame.Minute, limit = 1)
price = [{x['p']} for x in data_client.get_stock_latest_trade(request_close)["TSLA"]][0].pop()
print(price)
I'm trying to use a different method in the alpaca API as seen above, but it doesn't work because the output is different. How am I supposed to format it?
I tried doing this to comprehend the list:
price = [{x['p']} for x in data_client.get_stock_latest_trade(request_close)["TSLA"]][0].pop()
print(price)
But the code doesn't work for the specific output that the get_stock_latest_trade
has.
Upvotes: 0
Views: 105
Reputation: 51
Lets look at the return types of the two functions you reference:
get_stock_bars()
has a return type of Union[BarSet, RawData]
. And if we look further, a BarSet is Dict[str, List[Bar]]
So your code works because you access the value associated with the "TSLA" key in the dictionary. That value you accessed is a list, so you are able to access the first item of that list.
get_stock_latest_trade()
has a return type of Union[Dict[str, Trade], RawData]
. If we look further, we can see that a Trade is just a dictionary of values. All you need to do is use the 'p' key to access the price value.
For your code to work:
request_close = StockLatestTradeRequest(symbol_or_symbols="TSLA")
price = data_client.get_stock_latest_trade(request_close)["TSLA"]['p']
print(price)
Another note: StockLatestTradeRequest only has two parameters:
symbol_or_symbols
(which is required)feed
(which is optional)So I am not sure why you had there was a start, timeframe, and limit parameters when you wrote the code.
References: https://github.com/alpacahq/alpaca-py/
Upvotes: 0