Skip to content

Commit

Permalink
Merge pull request #403 from henryzhangpku/master
Browse files Browse the repository at this point in the history
Fix 'Your app version is missing important stock trading updates.'  + 24 hours stock market trading
  • Loading branch information
jmfernandes authored Jul 6, 2023
2 parents 9266c48 + eeca486 commit e5be821
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
2 changes: 2 additions & 0 deletions robin_stocks/robinhood/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def request_post(url, payload=None, timeout=16, json=False, jsonify_data=True):
'Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
else:
res = SESSION.post(url, data=payload, timeout=timeout)
if res.status_code in [500,400]:
raise Exception("Received "+ str(res.status_code))
data = res.json()
except Exception as message:
print("Error in request_post: {0}".format(message), file=get_output())
Expand Down
41 changes: 26 additions & 15 deletions robin_stocks/robinhood/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_all_crypto_orders(info=None):


@login_required
def get_all_open_stock_orders(info=None):
def get_all_open_stock_orders(info=None, account_number=None):
"""Returns a list of all the orders that are currently open.
:param info: Will filter the results to get a specific value.
Expand All @@ -62,7 +62,7 @@ def get_all_open_stock_orders(info=None):
a list of strings is returned where the strings are the value of the key that matches info.
"""
url = orders_url()
url = orders_url(account_number=account_number)
data = request_get(url, 'pagination')

data = [item for item in data if item['cancel'] is not None]
Expand Down Expand Up @@ -350,7 +350,7 @@ def order_buy_fractional_by_quantity(symbol, quantity, account_number=None, time


@login_required
def order_buy_fractional_by_price(symbol, amountInDollars, account_number=None, timeInForce='gfd', extendedHours=False, jsonify=True):
def order_buy_fractional_by_price(symbol, amountInDollars, account_number=None, timeInForce='gfd', extendedHours=False, jsonify=True, market_hours='regular_hours'):
"""Submits a market order to be executed immediately for fractional shares by specifying the amount in dollars that you want to trade.
Good for share fractions up to 6 decimal places. Robinhood does not currently support placing limit, stop, or stop loss orders
for fractional trades.
Expand Down Expand Up @@ -380,7 +380,7 @@ def order_buy_fractional_by_price(symbol, amountInDollars, account_number=None,
price = next(iter(get_latest_price(symbol, 'ask_price', extendedHours)), 0.00)
fractional_shares = 0 if (price == 0.00) else round_price(amountInDollars/float(price))

return order(symbol, fractional_shares, "buy", account_number, None, None, timeInForce, extendedHours, jsonify)
return order(symbol, fractional_shares, "buy", None, None, account_number, timeInForce, extendedHours, jsonify, market_hours)


@login_required
Expand Down Expand Up @@ -522,7 +522,7 @@ def order_sell_market(symbol, quantity, account_number=None, timeInForce='gtc',


@login_required
def order_sell_fractional_by_quantity(symbol, quantity, account_number=None, timeInForce='gfd', priceType='bid_price', extendedHours=False, jsonify=True):
def order_sell_fractional_by_quantity(symbol, quantity, account_number=None, timeInForce='gfd', priceType='bid_price', extendedHours=False, jsonify=True, market_hours='regular_hours'):
"""Submits a market order to be executed immediately for fractional shares by specifying the amount that you want to trade.
Good for share fractions up to 6 decimal places. Robinhood does not currently support placing limit, stop, or stop loss orders
for fractional trades.
Expand All @@ -544,7 +544,7 @@ def order_sell_fractional_by_quantity(symbol, quantity, account_number=None, tim
the price, and the quantity.
"""
return order(symbol, quantity, "sell", account_number, None, None, timeInForce, extendedHours, jsonify)
return order(symbol, quantity, "sell", None, None, account_number, timeInForce, extendedHours, jsonify, market_hours)


@login_required
Expand Down Expand Up @@ -775,7 +775,7 @@ def order_trailing_stop(symbol, quantity, side, trailAmount, trailType='percenta


@login_required
def order(symbol, quantity, side, limitPrice=None, stopPrice=None, account_number=None, timeInForce='gtc', extendedHours=False, jsonify=True):
def order(symbol, quantity, side, limitPrice=None, stopPrice=None, account_number=None, timeInForce='gtc', extendedHours=False, jsonify=True, market_hours='regular_hours'):
"""A generic order function.
:param symbol: The stock ticker of the stock to sell.
Expand Down Expand Up @@ -845,17 +845,28 @@ def order(symbol, quantity, side, limitPrice=None, stopPrice=None, account_numbe
'time_in_force': timeInForce,
'trigger': trigger,
'side': side,
'extended_hours': extendedHours
'market_hours': market_hours, # choices are ['regular_hours', 'all_day_hours']
'extended_hours': extendedHours,
'order_form_version': 4
}
# BEGIN PATCH FOR NEW ROBINHOOD BUY FORM (GuitarGuyChrisB 5/26/2023)
if side == "buy":
payload['order_form_version'] = "2"
payload['preset_percent_limit'] = "0.05"
# END PATCH FOR NEW ROBINHOOD BUY FORM (GuitarGuyChrisB 5/26/2023)

# adjust market orders
if orderType == 'market':
del payload['stop_price']
del payload['extended_hours']

if market_hours == 'regular_hours':
if side == "buy":
payload['preset_percent_limit'] = "0.05"
payload['type'] = 'limit'
# regular market sell
elif orderType == 'market' and side == 'sell':
del payload['price']
elif market_hours == 'all_day_hours':
payload['type'] = 'limit'
payload['quantity']=int(payload['quantity']) # round to integer instead of fractional

url = orders_url()


data = request_post(url, payload, jsonify_data=jsonify)

return(data)
Expand Down
2 changes: 1 addition & 1 deletion robin_stocks/robinhood/stocks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Contains information in regards to stocks."""
from functools import cache
from functools import lru_cache as cache

from robin_stocks.robinhood.helper import *
from robin_stocks.robinhood.urls import *
Expand Down
11 changes: 7 additions & 4 deletions robin_stocks/robinhood/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,11 @@ def option_cancel_url(id):
return('https://api.robinhood.com/options/orders/{0}/cancel/'.format(id))


def orders_url(orderID=None):
def orders_url(orderID=None, account_number=None):
url = 'https://api.robinhood.com/orders/'
if orderID:
return('https://api.robinhood.com/orders/{0}/'.format(orderID))
else:
return('https://api.robinhood.com/orders/')
url += '{0}/'.format(orderID)
if account_number:
url += ('?account_numbers='+account_number)

return url

0 comments on commit e5be821

Please sign in to comment.