Finding out bought and sold price of a share

I’m trying to get into Alpaca and facing some learning difficulties. I’m trying to develop a basic strategy, and for it I need to know at what price I bought and at what price I sold a share of Apple. I’m not sure as to which of these methods even exist or not

order = api_paper.get_latest_trade(‘AAPL’)
asset = api_paper.get_asset(‘AAPL’)
sell_price = order.filled_avg_price
purchase_price = asset.last_trade_price

Here’s what I’m working with right now, the methods I am trying to use apparently do not exist. What is the proper method to see at what price a share was bought/sold? And is there any place to find documentation? Thanks a lot

@Lord_Ingram Alpaca overview documentation for the Trading APIs can be found here. These are all the methods/endpoints to submit orders and fetch account information. Specific details on each API endpoint are documented here.

To get market data (eg last trade price) there is a completely separate set of APIs called (intuitively) the Market Data APIs. Overview documentation is here. Specific details on each API endpoint are documented here. Note that one needs to subscribe to a data plan to get full market data. The free plan is generally just data from a single exchange (IEX) and represents only about 2% of full market data.

That said, I primarily use Python and just use the built in ‘self documenting’ features. If one starts entering a method (even entering a period) a list of all available methods will display. Like this

After selecting a method, begin typing and a list of all the parameters will be displayed. Like this for the list_orders method

@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.

1 Like

Dan,

Is there a way to pull net gain/loss from sales, that would correctly match my monthly statement?

Thanks!