From 11f7a5e6cb33f9cef895c89a629bfb26d87d89d5 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 25 Apr 2020 19:18:11 -0700 Subject: [PATCH 1/2] patch options functions --- pyrh/robinhood.py | 57 +++++++++++++++++++++++------------------------ pyrh/urls.py | 2 +- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/pyrh/robinhood.py b/pyrh/robinhood.py index be2f3db6..f3efdafe 100644 --- a/pyrh/robinhood.py +++ b/pyrh/robinhood.py @@ -5,6 +5,7 @@ import dateutil import requests +from yarl import URL from pyrh import urls from pyrh.exceptions import InvalidTickerSymbol @@ -520,22 +521,19 @@ def get_options(self, stock, expiration_dates, option_type): # return market_data def options_owned(self): - options = self.get_url(urls.options_base() + "positions/?nonzero=true") + options = self.get_url(urls.OPTIONS_BASE.join(URL("positions/?nonzero=true"))) options = options["results"] return options - def get_option_marketdata(self, instrument): - info = self.get_url( - urls.build_market_data() + "options/?instruments=" + instrument - ) - return info["results"][0] + def get_option_marketdata(self, option_id): + info = self.get_url(urls.MARKET_DATA_BASE.join(URL(f"options/{option_id}/"))) + return info def get_option_chainid(self, symbol): - stock_info = self.get_url(self.endpoints["instruments"] + "?symbol=" + symbol) - stock_id = stock_info["results"][0]["id"] - params = {} - params["equity_instrument_ids"] = stock_id - chains = self.get_url(urls.options_base() + "chains/", params=params) + stock_info = self.get_url(urls.INSTRUMENTS_BASE.with_query(symbol=symbol)) + instrument_id = stock_info["results"][0]["id"] + url = urls.OPTIONS_BASE.join(URL("chains/")) + chains = self.get_url(url.with_query(equity_instrument_ids=instrument_id)) chains = chains["results"] chain_id = None @@ -545,24 +543,25 @@ def get_option_chainid(self, symbol): return chain_id - def get_option_quote(self, arg_dict): - chain_id = self.get_option_chainid(arg_dict.pop("symbol", None)) - arg_dict["chain_id"] = chain_id - option_info = self.get_url( - self.endpoints.options_base() + "instruments/", params=arg_dict - ) - option_info = option_info["results"] - exp_price_list = [] - - for op in option_info: - mrkt_data = self.get_option_marketdata(op["url"]) - op_price = mrkt_data["adjusted_mark_price"] - exp = op["expiration_date"] - exp_price_list.append((exp, op_price)) - - exp_price_list.sort() - - return exp_price_list + def get_option_quote(self, symbol, strike, expiry, otype, state="active"): + url = urls.OPTIONS_BASE.join(URL("instruments/")) + params = { + "chain_symbol": symbol, + "strike_price": strike, + "expiration_dates": expiry, + "type": otype, + "state": state, + } + # symbol, strike, expiry, otype should uniquely define an option + results = self.get_url(url.with_query(**params)).get("results") + if not results: + return + else: + option_id = results[0]["id"] + result = self.get_option_marketdata(option_id) + params["ask"] = "{} x {}".format(result["ask_size"], result["ask_price"]) + params["bid"] = "{} x {}".format(result["bid_size"], result["bid_price"]) + return params ########################################################################### # GET FUNDAMENTALS diff --git a/pyrh/urls.py b/pyrh/urls.py index a611d4ac..e6dc2877 100755 --- a/pyrh/urls.py +++ b/pyrh/urls.py @@ -21,7 +21,7 @@ INSTRUMENTS_BASE = API_BASE / "instruments/" MARGIN_UPGRADES = API_BASE / "margin/upgrades/" # not implemented MARKETS = API_BASE / "markets/" # not implemented -MARKET_DATA_BASE = API_BASE / "marketdata/options/" +MARKET_DATA_BASE = API_BASE / "marketdata/" NEWS_BASE = API_BASE / "midlands/news/" NOTIFICATIONS = API_BASE / "notifications/" # not implemented ORDERS_BASE = API_BASE / "orders/" From a3734aaa6c81dde985fed7950d778d63b5daa4db Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 25 Apr 2020 19:21:38 -0700 Subject: [PATCH 2/2] add news fragment --- newsfragments/235.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/235.bugfix diff --git a/newsfragments/235.bugfix b/newsfragments/235.bugfix new file mode 100644 index 00000000..fab7c19a --- /dev/null +++ b/newsfragments/235.bugfix @@ -0,0 +1 @@ +Fix the options functions.