# Api.get\_position Error

**URL:** <https://forum.alpaca.markets/t/api-get-position-error/3589>\
**Category:** Alpaca Trading\
**Created:** [December 10, 2020, 7:58pm UTC](https://forum.alpaca.markets/t/api-get-position-error/3589 "2020-12-10T19:58:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jz\_trader](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jz\_trader](https://forum.alpaca.markets/u/jz_trader)\
**Post date:** [December 10, 2020, 7:58pm UTC](https://forum.alpaca.markets/t/api-get-position-error/3589/1 "2020-12-10T19:58:09Z")

</div>

Hello,

I am using api.get\_position(symbol) to check if I have a position for a given stock. But if the position is 0, it returns an error. Is there a way to return position of 0?

Thanks,  
Jack

---

<div class="post-metadata">

**Author:** ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)\
**Post date:** [December 11, 2020, 4:47pm UTC](https://forum.alpaca.markets/t/api-get-position-error/3589/2 "2020-12-11T16:47:23Z")

</div>

You can use a `try except` statement to check for an error and then set to 0. Maybe something like this

```
try:
  pos_qty = float(api.get_position('IBM').qty)
except Exception as exception:
  if exception. __str__ () == 'position does not exist':
    pos_qty = 0

```

However, I personally would load all the positions into a dataframe and then query the dataframe rather than using the API to check each individual position. Having the positions in a dataframe also makes the data easily available all in one place. Something like this

```
# First get all the positions into a dataframe indexed by symbol
import pandas as pd

my_positions = api.list_positions()
my_positions_df = pd.DataFrame([position._raw for position in my_positions])
my_positions_df.set_index('symbol', inplace=True)

# Then check if a particular stock is in the index
if my_stock in my_positions_df.index:
  qty = float(my_positions_df.at[my_stock, 'qty'])
else:
  qty = 0

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Glenn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/glenn/32/4226_2.png) [@Glenn](https://forum.alpaca.markets/u/Glenn)\
**Post date:** [May 13, 2022, 10:18pm UTC](https://forum.alpaca.markets/t/api-get-position-error/3589/3 "2022-05-13T22:18:46Z")

</div>

Very helpful!! Thank you for posting this
