jmfernandes / robin_stocks

This is a library to use with Robinhood Financial App. It currently supports trading crypto-currencies, options, and stocks. In addition, it can be used to get real time ticker information, assess the performance of your portfolio, and can also get tax documents, total dividends paid, and more. More info at
http://www.robin-stocks.com
MIT License
1.69k stars 459 forks source link

order_buy_crypto_limit_by_price() fails for DOGE with "Order quantity has invalid increment." #262

Open stevecolby opened 3 years ago

stevecolby commented 3 years ago

Tried to place a limit order on DOGE but am unable to. I run the following command:

purchase = rs.orders.order_buy_crypto_limit_by_price("DOGE", float(5), float(0.058501))

And the response received is "Order quantity has invalid increment."

justinpowers commented 3 years ago

You'll want to read this: #220

stevecolby commented 3 years ago

It also happen with BTC if you try to buy a fractional share that's too small. Since Bitcoin's now in the 5 figures, buying, say, $10 worth sometimes leads to the same error because the fractional share is such a small fraction. I've also received the same error "Order quantity has invalid increment" because it's too small (opposite of DOGE I guess).

aaroncouch commented 3 years ago

You can do this:

>>> from decimal import Decimal
>>> import robin_stocks.robinhood as rh
...
>>> info = rh.get_crypto_info("DOGE")
>>> quantity_precision = abs(Decimal(info["min_order_quantity_increment"].rstrip("0")).as_tuple().exponent)
>>> usd_amount = 10
>>> curr_price = 0.510713
>>> purchase_quantity = round(usd_amount / curr_price, quantity_precision)
>>> rh.order_buy_crypto_by_quantity(symbol="DOGE", quantity=purchase_quantity)
emwitchcraft commented 3 years ago

If you call get_crypto_positions () and look through the results, you'll see nested in the 'currency' key's value a dict with an 'increment' key. That value is the minimum quantity/coin increment for buy and sell orders through the api (oddly enough it's different on the app i.e. you can buy and sell fractional doge through it).

A couple share the same value, but otherwise they all have different minimum increments. I think the rounding method in the robin_stocks module worked in almost all cases simply by rounding off enough decimal places that the quantity ordered was always dividable by the minimum increment, except in the case of doge which has a minimum increment of 1.

I've been scootin my way around this for doge by doing essentially what aaroncouch is suggesting, namely converting usd to a quantity based off the current price and then rounding by the minimum increment.