Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying crypto DOGE fix from #220 #276

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion robin_stocks/robinhood/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,21 @@ def id_for_option(symbol, expirationDate, strike, optionType):
return(listOfOptions[0]['id'])


def round_price(price):
def round_price(price, forceWholeNumber=False):
"""Takes a price and rounds it to an appropriate decimal place that Robinhood will accept.

:param price: The input price to round.
:type price: float or int
:param forceWholeNumber: Force rounding to the nearest whole number
:type forceWholeNumber: bool
:returns: The rounded price as a float.

"""
price = float(price)

if(forceWholeNumber):
return round(price)

if price <= 1e-2:
returnPrice = round(price, 6)
elif price < 1e0:
Expand Down
12 changes: 8 additions & 4 deletions robin_stocks/robinhood/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ def order_sell_option_limit(positionEffect, creditOrDebit, price, symbol, quanti


@login_required
def order_buy_crypto_by_price(symbol, amountInDollars, timeInForce='gtc', jsonify=True):
def order_buy_crypto_by_price(symbol, amountInDollars, timeInForce='gtc', jsonify=True, forceWholeShares=False):
"""Submits a market order for a crypto by specifying the amount in dollars that you want to trade.
Good for share fractions up to 8 decimal places.

Expand All @@ -1213,12 +1213,14 @@ def order_buy_crypto_by_price(symbol, amountInDollars, timeInForce='gtc', jsonif
:type timeInForce: Optional[str]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:param forceWholeNumber: Force rounding to the nearest whole number of shares
:type forceWholeNumber: Optional[bool]
:returns: Dictionary that contains information regarding the buying of crypto, \
such as the order id, the state of order (queued, confired, filled, failed, canceled, etc.), \
the price, and the quantity.

"""
return order_crypto(symbol, "buy", amountInDollars, "price", None, timeInForce, jsonify)
return order_crypto(symbol, "buy", amountInDollars, "price", None, timeInForce, jsonify, forceWholeShares)


@login_required
Expand Down Expand Up @@ -1377,7 +1379,7 @@ def order_sell_crypto_limit_by_price(symbol, amountInDollars, limitPrice, timeIn


@login_required
def order_crypto(symbol, side, quantityOrPrice, amountIn="quantity", limitPrice=None, timeInForce="gtc", jsonify=True):
def order_crypto(symbol, side, quantityOrPrice, amountIn="quantity", limitPrice=None, timeInForce="gtc", jsonify=True, forceWholeShares=False):
"""Submits an order for a crypto.

:param symbol: The crypto ticker of the crypto to trade.
Expand All @@ -1395,6 +1397,8 @@ def order_crypto(symbol, side, quantityOrPrice, amountIn="quantity", limitPrice=
:type timeInForce: Optional[str]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:param forceWholeNumber: Force rounding to the nearest whole number of shares (if market order by price only)
:type forceWholeNumber: Optional[bool]
:returns: Dictionary that contains information regarding the selling of crypto, \
such as the order id, the state of order (queued, confired, filled, failed, canceled, etc.), \
the price, and the quantity.
Expand Down Expand Up @@ -1426,7 +1430,7 @@ def order_crypto(symbol, side, quantityOrPrice, amountIn="quantity", limitPrice=
if amountIn == "quantity":
quantity = quantityOrPrice
else:
quantity = round_price(quantityOrPrice/price)
quantity = round_price(quantityOrPrice/price, forceWholeShares)

payload = {
'account_id': load_crypto_profile(info="id"),
Expand Down