Skip to content

Commit

Permalink
Merge branch 'mchauhan3-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
anilshanbhag committed Jun 28, 2018
2 parents 919a610 + 8256855 commit 92132e4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
50 changes: 30 additions & 20 deletions Robinhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Transaction(Enum):
class Robinhood:
"""wrapper class for fetching/parsing Robinhood endpoints"""
endpoints = {
"login": "https://api.robinhood.com/api-token-auth/",
"login": "https://api.robinhood.com/oauth2/token/",
"logout": "https://api.robinhood.com/api-token-logout/",
"investment_profile": "https://api.robinhood.com/user/investment_profile/",
"accounts": "https://api.robinhood.com/accounts/",
Expand All @@ -52,6 +52,8 @@ class Robinhood:
"watchlists": "https://api.robinhood.com/watchlists/",
"news": "https://api.robinhood.com/midlands/news/",
"fundamentals": "https://api.robinhood.com/fundamentals/",
"options": "https://api.robinhood.com/options/",
"marketdata": "https://api.robinhood.com/marketdata/"
}

session = None
Expand Down Expand Up @@ -81,7 +83,8 @@ def __init__(self):
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"X-Robinhood-API-Version": "1.0.0",
"Connection": "keep-alive",
"User-Agent": "Robinhood/823 (iPhone; iOS 7.1.2; Scale/2.00)"
"User-Agent": "Robinhood/823 (iPhone; iOS 7.1.2; Scale/2.00)",
"Origin": "https://robinhood.com"
}
self.session.headers = self.headers

Expand Down Expand Up @@ -111,7 +114,11 @@ def login(
self.password = password
payload = {
'password': self.password,
'username': self.username
'username': self.username,
'scope': 'internal',
'grant_type': 'password',
'client_id': 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS',
'expires_in': 86400
}

if mfa_code:
Expand All @@ -125,14 +132,15 @@ def login(
res.raise_for_status()
data = res.json()
except requests.exceptions.HTTPError:

raise RH_exception.LoginFailed()

if 'mfa_required' in data.keys(): #pragma: no cover
raise RH_exception.TwoFactorRequired() #requires a second call to enable 2FA

if 'token' in data.keys():
self.auth_token = data['token']
self.headers['Authorization'] = 'Token ' + self.auth_token
if 'access_token' in data.keys():
self.auth_token = data['access_token']
self.headers['Authorization'] = 'Bearer ' + self.auth_token
return True

return False
Expand Down Expand Up @@ -666,18 +674,6 @@ def securities_owned(self):
"""
return self.session.get(self.endpoints['positions']+'?nonzero=true').json()

def options_owned(self):
"""
Returns list of options which are in user's portfolio
"""
data = self.session.get(self.endpoints['options_positions']+'?nonzero=true').json()
if 'results' in data:
results = data['results']
results = [result for result in results if int(float(result['quantity'])) != 0]
data['results'] = results

return data

##############################
#PLACE ORDER
##############################
Expand Down Expand Up @@ -877,11 +873,12 @@ def cancel_open_orders(self):
##############################
#WATCHLIST(S)
##############################

def get_watchlists(self):
return self.session.get(self.endpoints['watchlists']).json()

def get_watchlist_instruments(self, watchlist_name):
return self.session.get(self.endpoints['watchlists'] + watchlist_name + '/').json()
return self.session.get(self.endpoints['watchlists'] + watchlist_name + '/').json()

def add_instrument_to_watchlist(self, watchlist_name, stock):
pass
Expand All @@ -893,7 +890,7 @@ def reorder_watchlist(self):
pass

def create_watchlist(self, name):
payload = {
payload = {
'name': 'Technology'
}
res = self.session.post(
Expand All @@ -903,3 +900,16 @@ def create_watchlist(self, name):
res.raise_for_status()
data = res.json()
return data

##############################
# GET OPTIONS POSITIONS
##############################

def options_owned(self):
options = self.session.get(self.endpoints['options'] + "positions/?nonzero=true").json()
options = options['results']
return options

def get_option_info(self, instrument):
info = self.session.get(self.endpoints['marketdata'] + "options/?instruments=" + instrument).json()
return info['results']
40 changes: 23 additions & 17 deletions shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,33 @@ def do_l(self, arg):
p_l = total_equity - buy_price * quantity
table.append_row([symbol, price, quantity, total_equity, buy_price, p_l])


print "Stocks:"
print(table)

# Load Options
options_positions = self.trader.options_owned()

if len(options_positions['results']) > 0:
options_table = BeautifulTable()
options_table.column_headers = ["symbol", "quantity", "cost basis"]
# options_table.column_headers = ["symbol", "current price", "quantity", "total equity", "cost basis", "p/l"]

for position in options_positions['results']:
quantity = int(float(position['quantity']))
symbol = self.get_symbol(position['option'])
buy_price = float(position['average_price'])
# total_equity = float(price) * quantity
options_table.append_row([symbol, quantity, buy_price])

print "Options:"
print(options_table)
option_positions = self.trader.options_owned()
table = BeautifulTable()
table.column_headers = ["symbol", "price", "quantity", "equity", "cost basis", "p/l", "type", "expiry"]

for op in option_positions:
quantity = op['quantity']
if float(quantity) == 0:
continue
cost = op['average_price']
symbol = op['chain_symbol']
instrument = op['option']
option_data = self.trader.session.get(instrument).json()
expiration_date = option_data['expiration_date']
strike = float(option_data['strike_price'])
type = option_data['type']
info = self.trader.get_option_info(instrument)
last_price = float(info[0]['last_trade_price'])
total_equity = (100 * last_price) * float(quantity)
change = total_equity - (float(cost) * float(quantity))
table.append_row([symbol, last_price, quantity, total_equity, cost, change, type, expiration_date])

print "Options:"
print(table)

def do_w(self, arg):
'Show watchlist w \nAdd to watchlist w a <symbol> \nRemove from watchlist w r <symbol>'
Expand Down

0 comments on commit 92132e4

Please sign in to comment.