Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #24 from westonplatter/historicals
Browse files Browse the repository at this point in the history
Historical Data - options
  • Loading branch information
westonplatter authored Aug 31, 2018
2 parents a5f73b8 + 9daf083 commit e198f90
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 19 deletions.
51 changes: 51 additions & 0 deletions examples/historical_option_data_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import configparser
from fast_arrow import (
Client,
Stock,
Option,
OptionChain,
OptionMarketdata
)

#
# get the authentication configs
#
config_file = "config.debug.ini"
config = configparser.ConfigParser()
config.read(config_file)
username = config['account']['username']
password = config['account']['password']

#
# instantiate and authetnicate client
#
client = Client(username=username, password=password)
client.authenticate()

#
# get TLT options
#
symbol = "TLT"
stock = Stock.fetch(client, symbol)

stock_id = stock["id"]
oc = OptionChain.fetch(client, stock_id, symbol)
oc_id = oc["id"]
next_2_eds = oc['expiration_dates'][0:1]
ops = Option.in_chain(client, oc_id, expiration_dates=next_2_eds)

#
# get TLT in the middle of the current TLT trading range
#
urls = [op["url"] for op in ops]
import math
middle = math.floor(len(urls)/2)
diff = math.floor(len(urls) * 0.7)
lower_end = middle - diff
higher_end = middle + diff
urls_subset = urls[lower_end:higher_end]

#
# get historical data for TLT options
#
hd = OptionMarketdata.historical_quotes_by_urls(client, urls_subset, span="year")
1 change: 1 addition & 0 deletions examples/option_chain_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#
ops = Option.in_chain(client, option_chain_id, expiration_dates=next_3_expiration_dates)


#
# merge in market data fro TLT option instruments
#
Expand Down
3 changes: 3 additions & 0 deletions fast_arrow/resources/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def fetch_by_ids(cls, client, ids):
results.extend(data["results"])
return results

@classmethod
def fetch_by_id(cls, bearer, _id):
return cls.fetch_by_ids(bearer, [_id])

# deprecate me
@classmethod
Expand Down
60 changes: 42 additions & 18 deletions fast_arrow/resources/option_marketdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,65 @@ def quote_by_instrument_id(cls, client, _id):
return cls.quotes_by_instrument_ids(client, [_id])[0]


@classmethod
def quote_by_instrument_url(cls, client, url):
return cls.quotes_by_instrument_urls(client, [url])[0]


@classmethod
def quotes_by_instrument_ids(cls, client, ids):
"""
create instrument urls, fetch, return results
"""
base_url = "https://api.robinhood.com/options/instruments/"
id_urls = ["{}{}/".format(base_url, _id) for _id in ids]
return cls.quotes_by_instrument_urls(client, id_urls)


@classmethod
def quote_by_instrument_url(cls, client, url):
return cls.quotes_by_instrument_urls(client, [url])[0]


@classmethod
def quotes_by_instrument_urls(cls, client, urls):
"""
fetch and return results
note - data requests are batched to limit urls to 50 per http request
"""
results = []

for _urls in chunked_list(urls, 25):
for _urls in chunked_list(urls, 50):
url = "https://api.robinhood.com/marketdata/options/"
params = {"instruments": ",".join(_urls)}
data = client.get(url, params=params)

if data and "results" in data:

partial_results = data["results"]

while ("next" in data and data["next"]):
data = get(data["next"], bearer=bearer)
partial_results.extend(data["results"])

results.extend(partial_results)
return results


@classmethod
def historical_quote_by_id(cls, client, _id, span="year"):
return cls.historical_quotes_by_ids(client, [_id], span)[0]


@classmethod
def historical_quotes_by_ids(cls, client, ids, span="year"):
base_url = "https://api.robinhood.com/options/instruments/"
urls = ["{}{}/".format(base_url, _id) for _id in ids]
return cls.historical_quotes_by_urls(client, urls)


@classmethod
def historical_quote_by_url(cls, client, url, span="year"):
return cls.historical_quotes_by_urls(client, [url], span)[0]


@classmethod
def historical_quotes_by_urls(cls, client, urls, span="year"):
possible_intervals = {
"day": "5minute",
"week": "10minute",
"year": "day",
"5year": "week" }
assert span in possible_intervals.keys()
interval = possible_intervals[span]
results = []
request_url = "https://api.robinhood.com/marketdata/options/historicals/"
for _urls in chunked_list(urls, 5):
params = { "span": span, "interval": interval, "instruments": ",".join(_urls) }
data = client.get(request_url, params=params)
if data and data["results"]:
results.extend(data["results"])
return results
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run_tests(self):
'vcrpy']

setup(name='fast_arrow',
version='0.2.0',
version='0.2.1',
description='API client for Robinhood',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit e198f90

Please sign in to comment.