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

robinhood/orders: document market_hours, correspond with extendedHours #454

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
81 changes: 45 additions & 36 deletions robin_stocks/robinhood/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def order_buy_market(symbol, quantity, account_number=None, timeInForce='gtc', e
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand All @@ -337,8 +337,8 @@ def order_buy_fractional_by_quantity(symbol, quantity, account_number=None, time
:type account_number: Optional[str]
:param timeInForce: Changes how long the order will be in effect for. 'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand All @@ -363,10 +363,12 @@ def order_buy_fractional_by_price(symbol, amountInDollars, account_number=None,
:type account_number: Optional[str]
:param timeInForce: Changes how long the order will be in effect for. 'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:param market_hours: Valid values are 'regular_hours', 'extended_hours' and 'all_day_hours'.
:type market_hours: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
such as the order id, the state of order (queued, confired, filled, failed, canceled, etc.), \
the price, and the quantity.
Expand Down Expand Up @@ -398,8 +400,8 @@ def order_buy_limit(symbol, quantity, limitPrice, account_number=None, timeInFor
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand All @@ -425,8 +427,8 @@ def order_buy_stop_loss(symbol, quantity, stopPrice, account_number=None, timeIn
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand Down Expand Up @@ -454,8 +456,8 @@ def order_buy_stop_limit(symbol, quantity, limitPrice, stopPrice, account_number
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand All @@ -481,8 +483,8 @@ def order_buy_trailing_stop(symbol, quantity, trailAmount, trailType='percentage
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the selling of stocks, \
Expand All @@ -509,8 +511,8 @@ def order_sell_market(symbol, quantity, account_number=None, timeInForce='gtc',
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the selling of stocks, \
Expand All @@ -535,10 +537,12 @@ def order_sell_fractional_by_quantity(symbol, quantity, account_number=None, tim
:type account_number: Optional[str]
:param timeInForce: Changes how long the order will be in effect for. 'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:param market_hours: Valid values are 'regular_hours', 'extended_hours' and 'all_day_hours'.
:type market_hours: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
such as the order id, the state of order (queued, confired, filled, failed, canceled, etc.), \
the price, and the quantity.
Expand All @@ -561,8 +565,8 @@ def order_sell_fractional_by_price(symbol, amountInDollars, account_number=None,
:type account_number: Optional[str]
:param timeInForce: Changes how long the order will be in effect for. 'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand Down Expand Up @@ -595,8 +599,8 @@ def order_sell_limit(symbol, quantity, limitPrice, account_number=None, timeInFo
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the selling of stocks, \
Expand All @@ -622,8 +626,8 @@ def order_sell_stop_loss(symbol, quantity, stopPrice, account_number=None, timeI
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the selling of stocks, \
Expand Down Expand Up @@ -651,8 +655,8 @@ def order_sell_stop_limit(symbol, quantity, limitPrice, stopPrice, account_numbe
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the selling of stocks, \
Expand All @@ -678,8 +682,8 @@ def order_sell_trailing_stop(symbol, quantity, trailAmount, trailType='percentag
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the selling of stocks, \
Expand Down Expand Up @@ -712,8 +716,8 @@ def order_trailing_stop(symbol, quantity, side, trailAmount, trailType='percenta
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:returns: Dictionary that contains information regarding the purchase of stocks, \
Expand Down Expand Up @@ -793,10 +797,12 @@ def order(symbol, quantity, side, limitPrice=None, stopPrice=None, account_numbe
:param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \
'gfd' = good for the day.
:type timeInForce: str
:param extendedHours: Premium users only. Allows trading during extended hours. Should be true or false.
:type extendedHours: Optional[str]
:param extendedHours: Premium users only. Allows trading during extended hours.
:type extendedHours: Optional[bool]
:param jsonify: If set to False, function will return the request object which contains status code and headers.
:type jsonify: Optional[str]
:param market_hours: Valid values are 'regular_hours', 'extended_hours' and 'all_day_hours'.
:type market_hours: Optional[str]
:returns: Dictionary that contains information regarding the purchase or selling of stocks, \
such as the order id, the state of order (queued, confired, filled, failed, canceled, etc.), \
the price, and the quantity.
Expand All @@ -816,6 +822,9 @@ def order(symbol, quantity, side, limitPrice=None, stopPrice=None, account_numbe
else:
priceType = "bid_price"

if extendedHours and market_hours == 'regular_hours':
market_hours = 'extended_hours'

if limitPrice and stopPrice:
price = round_price(limitPrice)
stopPrice = round_price(stopPrice)
Expand Down Expand Up @@ -845,23 +854,23 @@ def order(symbol, quantity, side, limitPrice=None, stopPrice=None, account_numbe
'time_in_force': timeInForce,
'trigger': trigger,
'side': side,
'market_hours': market_hours, # choices are ['regular_hours', 'all_day_hours']
'market_hours': market_hours,
'extended_hours': extendedHours,
'order_form_version': 4
}
# adjust market orders
if orderType == 'market':
del payload['stop_price']
del payload['extended_hours']
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':
elif market_hours in ('extended_hours', 'all_day_hours'):
payload['type'] = 'limit'
payload['quantity']=int(payload['quantity']) # round to integer instead of fractional

Expand Down