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

Commit e7c5fec

Browse files
committed
fix. make the "option_chain" and "historical_option_data" examples work again. issue #99.
1 parent 7afef05 commit e7c5fec

File tree

5 files changed

+52
-44
lines changed

5 files changed

+52
-44
lines changed

examples/historical_option_data.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from fast_arrow import (
66
Client,
77
Stock,
8+
StockMarketdata,
89
Option,
910
OptionChain,
1011
OptionMarketdata
@@ -24,18 +25,19 @@
2425
#
2526
client = Client(auth_data)
2627

28+
2729
#
2830
# get TLT options
2931
#
3032
symbol = "TLT"
31-
stock = Stock.fetch(client, symbol)
32-
33-
stock_id = stock["id"]
33+
md = StockMarketdata.quote_by_symbol(client, symbol)
34+
stock_id = md["instrument"].split("/")[-2]
3435
oc = OptionChain.fetch(client, stock_id, symbol)
3536
oc_id = oc["id"]
3637
next_2_eds = oc['expiration_dates'][0:1]
3738
ops = Option.in_chain(client, oc_id, expiration_dates=next_2_eds)
3839

40+
3941
#
4042
# get TLT in the middle of the current TLT trading range
4143
#
@@ -46,6 +48,7 @@
4648
higher_end = middle + diff
4749
urls_subset = urls[lower_end:higher_end]
4850

51+
4952
#
5053
# get historical data for TLT options
5154
#

examples/option_chain.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
from fast_arrow import (
44
Client,
5-
Stock,
5+
StockMarketdata,
66
OptionChain,
77
Option,
88
)
@@ -26,13 +26,13 @@
2626
# fetch the stock info for TLT
2727
#
2828
symbol = "TLT"
29-
stock = Stock.fetch(client, symbol)
29+
md = StockMarketdata.quote_by_symbol(client, symbol)
3030

3131

3232
#
3333
# get the TLT option chain info
3434
#
35-
stock_id = stock["id"]
35+
stock_id = md["instrument"].split("/")[-2]
3636
option_chain = OptionChain.fetch(client, stock_id, symbol)
3737
option_chain_id = option_chain["id"]
3838
expiration_dates = option_chain['expiration_dates']

fast_arrow/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def __init__(self, message, errors=None):
1616
self.errors = errors
1717

1818

19+
class ApiDoesNotSupportError(Exception):
20+
def __init__(self, message, errors=None):
21+
super().__init__(message)
22+
self.errors = errors
23+
24+
1925
class TradeExecutionError(Exception):
2026
def __init__(self, message, errors=None):
2127
super().__init__(message)

fast_arrow/resources/stock.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
from fast_arrow.version import VERSION
2+
from fast_arrow.exceptions import ApiDoesNotSupportError
13
from fast_arrow.resources.stock_marketdata import StockMarketdata
24

5+
import deprecation
6+
37

48
class Stock(object):
59

610
@classmethod
11+
@deprecation.deprecated(
12+
deprecated_in="1.0.0",
13+
removed_in="1.1",
14+
current_version=VERSION,
15+
details="Use 'StockMarketdata.quote_by_symbol'")
716
def fetch(cls, client, symbol):
8-
"""
9-
fetch data for stock
10-
"""
11-
assert(type(symbol) is str)
12-
13-
url = ("https://api.robinhood.com/instruments/?symbol={0}".
14-
format(symbol))
15-
data = client.get(url)
16-
return data["results"][0]
17+
message = '''
18+
Robinhood API seems to not support this endpoint.
19+
Use fast_arrow.StockMarketdata.quote_by_symbol
20+
'''
21+
raise ApiDoesNotSupportError(message)
1722

1823
@classmethod
1924
def mergein_marketdata_list(cls, client, stocks):
@@ -38,17 +43,14 @@ def mergein_marketdata_list(cls, client, stocks):
3843
return results
3944

4045
@classmethod
46+
@deprecation.deprecated(
47+
deprecated_in="1.0.0",
48+
removed_in="1.1",
49+
current_version=VERSION,
50+
details="Use 'StockMarketdata.quote_by_symbols'")
4151
def all(cls, client, symbols):
42-
""""
43-
fetch data for multiple stocks
44-
"""
45-
params = {"symbol": ",".join(symbols)}
46-
request_url = "https://api.robinhood.com/instruments/"
47-
48-
data = client.get(request_url, params=params)
49-
results = data["results"]
50-
51-
while data["next"]:
52-
data = client.get(data["next"])
53-
results.extend(data["results"])
54-
return results
52+
message = '''
53+
Robinhood API seems to not support this endpoint.
54+
Use fast_arrow.StockMarketdata.quote_by_symbols
55+
'''
56+
raise ApiDoesNotSupportError(message)

tests/resources/test_stock.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
from fast_arrow import Stock
2-
from tests.test_util import gen_vcr, gen_client
2+
from fast_arrow.exceptions import ApiDoesNotSupportError
3+
from tests.test_util import gen_client
34

5+
import unittest
46

5-
class TestStock(object):
67

7-
def test_fetch_fields(self):
8+
class TestStock(unittest.TestCase):
9+
10+
def test_fetch(self):
811
client = gen_client()
912
symbol = "TLT"
10-
with gen_vcr().use_cassette('stock_fetch.yaml'):
11-
stock = Stock.fetch(client, symbol)
12-
13-
expected_fields = [
14-
'margin_initial_ratio', 'rhs_tradability', 'id',
15-
'market', 'simple_name', 'min_tick_size', 'maintenance_ratio',
16-
'tradability', 'state', 'type', 'tradeable', 'fundamentals',
17-
'quote', 'symbol', 'day_trade_ratio', 'name',
18-
'tradable_chain_id', 'splits', 'url', 'country',
19-
'bloomberg_unique', 'list_date']
13+
with self.assertRaises(ApiDoesNotSupportError):
14+
Stock.fetch(client, symbol)
2015

21-
actual_fields = list(stock.keys())
22-
23-
assert(set(expected_fields) == set(actual_fields))
16+
def test_all(self):
17+
client = gen_client()
18+
symbols = ["TLT", "USO"]
19+
with self.assertRaises(ApiDoesNotSupportError):
20+
Stock.all(client, symbols)

0 commit comments

Comments
 (0)