alpacahq / alpaca-py

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

[Bug]: Fractional Shares not supported in order endpoints #499

Closed lastmeta closed 2 months ago

lastmeta commented 2 months ago

Is there an existing issue for this?

Current Behavior

following the colab tutorial (https://colab.research.google.com/github/alpacahq/alpaca-py/blob/master/examples/stocks-trading-basic.ipynb#scrollTo=MH33Rtt6SX7L)

I tried to run this cell:

# simple, market order, fractional qty
# Alpaca trading platform support fractional trading by default
# you can specify:
# fractional qty (e.g. 0.01 qty) in the order request (which is shown in this example)
# or notional value (e.g. 100 USD) (which is in the next example)
req = MarketOrderRequest(
    symbol = symbol,
    qty = 5.5,
    side = OrderSide.BUY,
    type = OrderType.MARKET,
    time_in_force = TimeInForce.DAY,
)
res = trade_client.submit_order(req)
res

and got this error:

HTTPError                                 Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/alpaca/common/rest.py](https://localhost:8080/#) in _one_request(self, method, url, opts, retry)
    195         try:
--> 196             response.raise_for_status()
    197         except HTTPError as http_error:

5 frames
HTTPError: 422 Client Error: Unprocessable Entity for url: https://paper-api.alpaca.markets/v2/orders

During handling of the above exception, another exception occurred:

APIError                                  Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/alpaca/common/rest.py](https://localhost:8080/#) in _one_request(self, method, url, opts, retry)
    203             error = response.text
    204 
--> 205             raise APIError(error, http_error)
    206 
    207         if response.text != "":

APIError: {"code":40010001,"message":"qty must be integer"}

how could qty be an integer? I thought it was because I had done it after hours, but I'm still within extended hours... so that's not it.

Furthermore, this is a different bug:

# simple, market order, notional
# Alpaca trading platform support fractional trading by default
# you can specify:
# fractional qty (e.g. 0.01 qty) in the order request (which is in the example above)
# or notional value (e.g. 100 USD) (which is in this example)
req = MarketOrderRequest(
    symbol = symbol,
    notional = 1.11,  # notional is specified in USD, here we specify $1.11
    side = OrderSide.BUY,
    type = OrderType.MARKET,
    time_in_force = TimeInForce.DAY,
)
res = trade_client.submit_order(req)
res

that example said

APIError: {"code":40010001,"message":"qty is required"}

So maybe the colab example is just out of date?

Expected Behavior

allow me to use a float for fractional order.

SDK Version I encountered this issue in

0.30.0

Steps To Reproduce

https://colab.research.google.com/github/alpacahq/alpaca-py/blob/master/examples/stocks-trading-basic.ipynb#scrollTo=2wS5WVRJSX7I

Filled out the Steps to Reproduce section?

Anything else?

No response

hiohiohio commented 2 months ago

@lastmeta thank you for the report. Please let me investigate and fix the issue.

hiohiohio commented 2 months ago

@lastmeta Could you please try to Reset Account of your paper account via the Dashboard of Alpaca Trading API on the web? (The button is in the Your portfolio block of Home screen after select Paper account on the web) To enable features recently added (i.e. fractional/options trading) for paper account, resetting the paper account is required. (fyi. live account is not required this process.)

lastmeta commented 2 months ago

that was it! thanks!