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

patch options functions #235

Merged
merged 2 commits into from
Feb 4, 2021
Merged
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
1 change: 1 addition & 0 deletions newsfragments/235.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the options functions.
57 changes: 28 additions & 29 deletions pyrh/robinhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import dateutil
import requests
from yarl import URL

from pyrh import urls
from pyrh.exceptions import InvalidTickerSymbol
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyrh/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down