Reputation: 491
I am using the Alpaca API.
import alpaca_trade_api as tradeapi
api = tradeapi.REST('key1', 'key2', 'https://paper-api.alpaca.markets', 'v2')
#retrieve open positions
pos = api.list_positions()
pos
Output:
[Position({ 'asset_class': 'us_equity',
'asset_id': 'b0b6dd9d-8b9b-48a9-ba46-b9d54906e415',
'avg_entry_price': '382.01',
'change_today': '0.0025959042399769',
'cost_basis': '382.01',
'current_price': '382.36',
'exchange': 'NASDAQ',
'lastday_price': '381.37',
'market_value': '382.36',
'qty': '1',
'side': 'long',
'symbol': 'AAPL',
'unrealized_intraday_pl': '0.35',
'unrealized_intraday_plpc': '0.0009162063820319',
'unrealized_pl': '0.35',
'unrealized_plpc': '0.0009162063820319'})]
This has been my attempt:
pos[0]['asset_id']
>>>TypeError: 'Position' object is not subscriptable
How can I isolate the content stored in the Position object without transforming it into a string?
type(Position)
is alpaca_trade_api.entity.Position
Upvotes: 1
Views: 772
Reputation: 5648
Try this:
getattr(pos, 'asset_id')
I don't have positions, but it works the same as Asset:
a = api.get_asset('NFLX')
print(getattr(a, 'symbol'))
Upvotes: 0
Reputation: 57105
So many questions of this sort have been around in the past few days, I wonder what sparked so much interest in Alpaca. The answer is pos[0].asset_id
: Alpaca objects have attributes, not keys.
Upvotes: 2