@Lord_Ingram There are several ways to get the price at which one bought, for example, a share of TSLA. One can either get ‘position’ information or one can get ‘order’ information. Getting position information is generally easiest. Like this
api.get_position(‘TSLA’)
This will return a number of fields of data about the position
Position({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘8ccae427-5dd0-45b3-b5fe-7ba5e422c766’,
‘asset_marginable’: False,
‘avg_entry_price’: ‘210.71’,
‘change_today’: ‘-0.0171600797238831’,
‘cost_basis’: ‘210.71’,
‘current_price’: ‘202.18’,
‘exchange’: ‘NASDAQ’,
‘lastday_price’: ‘205.71’,
‘market_value’: ‘202.18’,
‘qty’: ‘1’,
‘qty_available’: ‘1’,
‘side’: ‘long’,
‘symbol’: ‘TSLA’,
‘unrealized_intraday_pl’: ‘-3.53’,
‘unrealized_intraday_plpc’: ‘-0.0171600797238831’,
‘unrealized_pl’: ‘-8.53’,
‘unrealized_plpc’: ‘-0.040482179298562’})
The field avg_entry_price will be the average price at which it was bought.
While fetching the position this way is easy, the one drawback is it only works for current positions. After one closes (ie sells) a position it’s not available. One would need to look at the sell order to get the price it was sold at.
api.list_orders(status=‘all’, symbols=[‘TSLA’])
This will return a list of all TSLA orders. Each order has a lot of information
[Order({ ‘asset_class’: ‘us_equity’,
‘asset_id’: ‘8ccae427-5dd0-45b3-b5fe-7ba5e422c766’,
‘canceled_at’: None,
‘client_order_id’: ‘da6ad9ab-84e1-4fab-9809-22d7e2baa4f2’,
‘created_at’: ‘2023-02-28T00:19:29.320254Z’,
‘expired_at’: None,
‘extended_hours’: False,
‘failed_at’: None,
‘filled_at’: ‘2023-02-28T14:30:02.579564Z’,
‘filled_avg_price’: ‘210.71’,
‘filled_qty’: ‘1’,
‘hwm’: None,
‘id’: ‘aeb82d8a-9665-43d7-86d1-757b64c2bc2a’,
‘legs’: None,
‘limit_price’: None,
‘notional’: None,
‘order_class’: ‘’,
‘order_type’: ‘market’,
‘qty’: ‘1’,
‘replaced_at’: None,
‘replaced_by’: None,
‘replaces’: None,
‘side’: ‘buy’,
‘source’: None,
‘status’: ‘filled’,
‘stop_price’: None,
‘submitted_at’: ‘2023-02-28T09:00:02.586491Z’,
‘subtag’: None,
‘symbol’: ‘TSLA’,
‘time_in_force’: ‘gtc’,
‘trail_percent’: None,
‘trail_price’: None,
‘type’: ‘market’,
‘updated_at’: ‘2023-02-28T14:30:02.581822Z’}),
The filled_avg_price
is what you want. The added complexity of looking at orders is selecting the correct sell order (there could be many).
Typically, an algo doesn’t care about the sell, or close price of a position (that happened in the past). One is most often just concerned about the price paid for current holdings. In that case use the easier get_position
method.