-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathtest_options.py
228 lines (170 loc) · 6.12 KB
/
test_options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from datetime import datetime
import os
import numpy as np
import pandas as pd
from pandas import testing as tm
import pytest
from pandas_datareader import data as web
@pytest.fixture
def aapl():
aapl = web.Options("aapl", "yahoo")
yield aapl
aapl.close()
@pytest.fixture
def month():
# AAPL has monthlies
today = datetime.today()
month = today.month + 1
if month > 12: # pragma: no cover
month = 1
return month
@pytest.fixture
def year():
# AAPL has monthlies
today = datetime.today()
year = today.year
month = today.month + 1
if month > 12: # pragma: no cover
year = year + 1
return year
@pytest.fixture
def expiry(month, year):
return datetime(year, month, 1)
@pytest.fixture
def json1(datapath):
dirpath = datapath("yahoo", "data")
json1 = "file://" + os.path.join(dirpath, "yahoo_options1.json")
return json1
@pytest.fixture
def json2(datapath):
# see gh-22: empty table
dirpath = datapath("yahoo", "data")
json2 = "file://" + os.path.join(dirpath, "yahoo_options2.json")
return json2
@pytest.fixture
def data1(aapl, json1):
return aapl._process_data(aapl._parse_url(json1))
class TestYahooOptions(object):
@classmethod
def setup_class(cls):
pytest.skip("Skip all Yahoo! tests.")
def assert_option_result(self, df):
"""
Validate returned option data has expected format.
"""
assert isinstance(df, pd.DataFrame)
assert len(df) > 1
exp_columns = pd.Index(
[
"Last",
"Bid",
"Ask",
"Chg",
"PctChg",
"Vol",
"Open_Int",
"IV",
"Root",
"IsNonstandard",
"Underlying",
"Underlying_Price",
"Quote_Time",
"Last_Trade_Date",
"JSON",
]
)
tm.assert_index_equal(df.columns, exp_columns)
assert df.index.names == ["Strike", "Expiry", "Type", "Symbol"]
dtypes = [
np.dtype(x)
for x in ["float64"] * 7
+ [
"float64",
"object",
"bool",
"object",
"float64",
"datetime64[ns]",
"datetime64[ns]",
"object",
]
]
tm.assert_series_equal(df.dtypes, pd.Series(dtypes, index=exp_columns))
def test_get_options_data(self, aapl, expiry):
# see gh-6105: regression test
with pytest.raises(ValueError):
aapl.get_options_data(month=3)
with pytest.raises(ValueError):
aapl.get_options_data(year=1992)
options = aapl.get_options_data(expiry=expiry)
self.assert_option_result(options)
def test_get_near_stock_price(self, aapl, expiry):
options = aapl.get_near_stock_price(call=True, put=True, expiry=expiry)
self.assert_option_result(options)
def test_options_is_not_none(self):
option = web.Options("aapl", "yahoo")
assert option is not None
def test_get_call_data(self, aapl, expiry):
calls = aapl.get_call_data(expiry=expiry)
self.assert_option_result(calls)
assert calls.index.levels[2][0] == "call"
def test_get_put_data(self, aapl, expiry):
puts = aapl.get_put_data(expiry=expiry)
self.assert_option_result(puts)
assert puts.index.levels[2][1] == "put"
def test_get_expiry_dates(self, aapl):
dates = aapl._get_expiry_dates()
assert len(dates) > 1
def test_get_all_data(self, aapl):
data = aapl.get_all_data(put=True)
assert len(data) > 1
self.assert_option_result(data)
def test_get_data_with_list(self, aapl):
data = aapl.get_call_data(expiry=aapl.expiry_dates)
assert len(data) > 1
self.assert_option_result(data)
def test_get_all_data_calls_only(self, aapl):
data = aapl.get_all_data(call=True, put=False)
assert len(data) > 1
self.assert_option_result(data)
def test_get_underlying_price(self, aapl):
# see gh-7
options_object = web.Options("^spxpm", "yahoo")
quote_price = options_object.underlying_price
assert isinstance(quote_price, float)
# Tests the weekend quote time format
price, quote_time = aapl.underlying_price, aapl.quote_time
assert isinstance(price, (int, float, complex))
assert isinstance(quote_time, (datetime, pd.Timestamp))
@pytest.mark.xfail(reason="Invalid URL scheme")
def test_chop(self, aapl, data1):
# gh-7625: regression test
aapl._chop_data(data1, above_below=2, underlying_price=np.nan)
chopped = aapl._chop_data(data1, above_below=2, underlying_price=100)
assert isinstance(chopped, pd.DataFrame)
assert len(chopped) > 1
chopped2 = aapl._chop_data(data1, above_below=2, underlying_price=None)
assert isinstance(chopped2, pd.DataFrame)
assert len(chopped2) > 1
@pytest.mark.xfail(reason="Invalid URL scheme")
def test_chop_out_of_strike_range(self, aapl, data1):
# gh-7625: regression test
aapl._chop_data(data1, above_below=2, underlying_price=np.nan)
chopped = aapl._chop_data(data1, above_below=2, underlying_price=100000)
assert isinstance(chopped, pd.DataFrame)
assert len(chopped) > 1
@pytest.mark.xfail(reason="Invalid URL scheme")
def test_sample_page_chg_float(self, data1):
# Tests that numeric columns with comma's are appropriately dealt with
assert data1["Chg"].dtype == "float64"
def test_month_year(self, aapl, month, year):
# see gh-168
data = aapl.get_call_data(month=month, year=year)
assert len(data) > 1
assert data.index.levels[0].dtype == "float64"
self.assert_option_result(data)
@pytest.mark.xfail(reason="Invalid URL scheme")
def test_empty_table(self, aapl, json2):
# see gh-22
empty = aapl._process_data(aapl._parse_url(json2))
assert len(empty) == 0