alpacahq / alpaca-py

The Official Python SDK for Alpaca API
https://alpaca.markets/sdks/python/getting_started.html
Apache License 2.0
537 stars 134 forks source link

UUID from TradingClient.get_all_assets does not match UUID from TradingClient.get_all_positions() [Bug]: #490

Open jose-main-sequence opened 1 month ago

jose-main-sequence commented 1 month ago

Is there an existing issue for this?

Current Behavior

When i request TradingClient.get_all_assets() Bitcoin to USD with symbol BTC/USD has a uid 276e2673764b4ab6a611caf665ca6340, however when i query my position in th paper account I get BTCUSD with UUID of 64bbff5159d64b3c935113ad85e3c752. Also symbol is BTCUSD rather than BTC/USD

Expected Behavior

The expected behavior should be consistent UUID and symbols from

SDK Version I encountered this issue in

alpaca-py==0.28.0

Steps To Reproduce

trading_client = TradingClient(api_key, secret_key)
search_params = GetAssetsRequest(asset_class=asset_class, status="active")
assets = trading_client.get_all_assets(search_params)
target_asset=[a for a in assets if a.symbol=="BTC/USD"][0]

#get positions
positions = trading_client.get_all_positions()
assert len([p for p in positions if p.asset_id.hex==target_asset.id.hex])>0

Filled out the Steps to Reproduce section?

Anything else?

No response

hiohiohio commented 1 month ago

@jose-main-sequence Thank you for the feedback.

The difference of assets in list assets and list positions is expected behaviour and by design.

Basically order assets and positions assets are different.

Do you have any issue/concern of this difference?

jose-main-sequence commented 1 month ago

Hi, thank you for the clarification. My next question is, how can someone map an order to a position? In the case of equities, orders and positions do match. I believe this originates from the design where all US equities settle in USD. However, in the case of cryptocurrencies, how can a user match an order with a position? Is there any endpoint that provides this mapping? For example, can one obtain the UUID of BTCUSD (position asset) before trading on it? It will be good to add then the "settlement asset" UUID to the endpoint so when i get BTC/ETH asset the response also shows me that this will settle as ETHUSD (position valuation)

bests

On Mon, 22 Jul 2024 at 08:25, hiohiohio @.***> wrote:

@jose-main-sequence https://github.com/jose-main-sequence Thank you for the feedback.

The difference of assets in list assets and list positions is expected behaviour and by design.

Basically order assets and positions assets are different.

  • orders are what you are trading it can be ETH/USD, BTC/USD, ETH/BTC,
  • positions are basically what you hold, you could be holding ETH, BTC , these are denoted by ETHUSD, BTCUSD

Do you have any issue/concern of this difference?

— Reply to this email directly, view it on GitHub https://github.com/alpacahq/alpaca-py/issues/490#issuecomment-2242182024, or unsubscribe https://github.com/notifications/unsubscribe-auth/BJ6FXM3BSW6EXTKUGWL3NFLZNSQXJAVCNFSM6AAAAABLCCC2GCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBSGE4DEMBSGQ . You are receiving this because you were mentioned.Message ID: @.***>

hiohiohio commented 1 month ago

how can someone map an order to a position? In the case of equities, orders and positions do match. I believe this originates from the design where all US equities settle in USD. However, in the case of cryptocurrencies, how can a user match an order with a position?

I think it depends on purpose of the mapping though. As a one way for the crypto, you could divide the asset symbol into two parts by slash (/). i.e. <quote>/<base>, ETH/BTC => quote: ETH, base: BTC Then you can then get two position assets by putting USD as suffix into quote/base. a. <quote>USD => ETHUSD b. <base>USD => BTCUSD

By the way, asset_id of equity could be changed by corporate actions in time as well as symbol. Therefore, asset_id/symbol of orders vs positions cannot be matching 100% for equity as well.

Is there any endpoint that provides this mapping? For example, can one obtain the UUID of BTCUSD (position asset) before trading on it?

Currently we do not provide the API endpoint. And also I cannot remember/find the way to get UUID of BTCUSD other than positions api.

It will be good to add then the "settlement asset" UUID to the endpoint so when i get BTC/ETH asset the response also shows me that this will settle as ETHUSD (position valuation)

Could you please elaborate on the reason/purpose of necessity of asset UUID of positions API?

jose-main-sequence commented 1 month ago

I understand. The main reason is to have proper mapping between our database assets and Alpaca assets. A symbol alone is not sufficient; for example, there is an asset in Alpaca with the ticker USD. If there is also a USD asset in our database, this will create problems. Therefore, when we collect positions from Alpaca, we want to map them to the positions in our database. That's why having this mapping is important for us. Additionally, we need a unique identifier for each asset that we hold or may hold. Thank you for the advice regarding corporate actions, we will instead then build our mappings differently.

On Tue, 23 Jul 2024 at 08:31, hiohiohio @.***> wrote:

how can someone map an order to a position? In the case of equities, orders and positions do match. I believe this originates from the design where all US equities settle in USD. However, in the case of cryptocurrencies, how can a user match an order with a position?

I think it depends on purpose of the mapping though. As a one way for the crypto, you could divide the asset symbol into two parts by slash (/). i.e. /, ETH/BTC => quote: ETH, base: BTC Then you can then get two position assets by putting USD as suffix into quote/base. a. USD => ETHUSD b. USD => BTCUSD

By the way, asset_id of equity could be changed by corporate actions in time as well as symbol. Therefore, asset_id/symbol of orders vs positions cannot be matching 100% for equity as well.

Is there any endpoint that provides this mapping? For example, can one obtain the UUID of BTCUSD (position asset) before trading on it?

Currently we do not provide the API endpoint. And also I cannot remember/find the way to get UUID of BTCUSD other than positions api.

It will be good to add then the "settlement asset" UUID to the endpoint so when i get BTC/ETH asset the response also shows me that this will settle as ETHUSD (position valuation)

Could you please elaborate on the reason/purpose of necessity of asset UUID of positions API?

— Reply to this email directly, view it on GitHub https://github.com/alpacahq/alpaca-py/issues/490#issuecomment-2244356437, or unsubscribe https://github.com/notifications/unsubscribe-auth/BJ6FXM7IHWBKOYSDLESEQFLZNX2ENAVCNFSM6AAAAABLCCC2GCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENBUGM2TMNBTG4 . You are receiving this because you were mentioned.Message ID: @.***>

hiohiohio commented 1 month ago

@jose-main-sequence Thank you for letting us know the details. Understood the reality of data mapping and data management. Please feel free to contact us if there are things we can help.