@@ -736,9 +736,8 @@ def _get_option_data(self, month, year, expiry, name):
736
736
" found" .format (table_loc , ntables ))
737
737
738
738
option_data = _parse_options_data (tables [table_loc ])
739
- option_data = self ._process_data (option_data )
740
739
option_data ['Type' ] = name [:- 1 ]
741
- option_data . set_index ([ 'Strike' , 'Expiry' , 'Type' , 'Symbol' ], inplace = True )
740
+ option_data = self . _process_data ( option_data , name [: - 1 ] )
742
741
743
742
if month == CUR_MONTH and year == CUR_YEAR :
744
743
setattr (self , name , option_data )
@@ -859,8 +858,7 @@ def get_near_stock_price(self, above_below=2, call=True, put=False,
859
858
month = None , year = None , expiry = None ):
860
859
"""
861
860
***Experimental***
862
- Cuts the data frame opt_df that is passed in to only take
863
- options that are near the current stock price.
861
+ Returns a data frame of options that are near the current stock price.
864
862
865
863
Parameters
866
864
----------
@@ -889,34 +887,38 @@ def get_near_stock_price(self, above_below=2, call=True, put=False,
889
887
Note: Format of returned data frame is dependent on Yahoo and may change.
890
888
891
889
"""
892
- year , month , expiry = self ._try_parse_dates (year , month , expiry )
893
890
894
891
to_ret = Series ({'calls' : call , 'puts' : put })
895
892
to_ret = to_ret [to_ret ].index
896
893
897
894
data = {}
898
895
899
896
for nam in to_ret :
900
- if month :
901
- m1 = _two_char_month (month )
902
- name = nam + m1 + str (year )[2 :]
897
+ df = self ._get_option_data (month , year , expiry , nam )
898
+ data [nam ] = self .chop_data (df , above_below , self .underlying_price )
899
+
900
+ return concat ([data [nam ] for nam in to_ret ]).sortlevel ()
901
+
902
+ def chop_data (self , df , above_below = 2 , underlying_price = None ):
903
+ """Returns a data frame only options that are near the current stock price."""
903
904
905
+ if not underlying_price :
904
906
try :
905
- df = getattr ( self , name )
907
+ underlying_price = self . underlying_price
906
908
except AttributeError :
907
- meth_name = 'get_{0}_data' .format (nam [:- 1 ])
908
- df = getattr (self , meth_name )(expiry = expiry )
909
+ underlying_price = np .nan
909
910
910
- if self . underlying_price :
911
- start_index = np .where (df .index .get_level_values ('Strike' )
912
- > self . underlying_price )[0 ][0 ]
911
+ if underlying_price is not np . nan :
912
+ start_index = np .where (df .index .get_level_values ('Strike' )
913
+ > underlying_price )[0 ][0 ]
913
914
914
- get_range = slice (start_index - above_below ,
915
+ get_range = slice (start_index - above_below ,
915
916
start_index + above_below + 1 )
916
- chop = df [get_range ].dropna (how = 'all' )
917
- data [nam ] = chop
917
+ df = df [get_range ].dropna (how = 'all' )
918
+
919
+ return df
920
+
918
921
919
- return concat ([data [nam ] for nam in to_ret ]).sortlevel ()
920
922
921
923
@staticmethod
922
924
def _try_parse_dates (year , month , expiry ):
@@ -1048,7 +1050,7 @@ def get_forward_data(self, months, call=True, put=False, near=False,
1048
1050
frame = self .get_near_stock_price (call = call , put = put ,
1049
1051
above_below = above_below ,
1050
1052
month = m2 , year = y2 )
1051
- frame = self ._process_data (frame )
1053
+ frame = self ._process_data (frame , name [: - 1 ] )
1052
1054
1053
1055
all_data .append (frame )
1054
1056
@@ -1178,7 +1180,7 @@ def _parse_url(self, url):
1178
1180
return root
1179
1181
1180
1182
1181
- def _process_data (self , frame ):
1183
+ def _process_data (self , frame , type ):
1182
1184
"""
1183
1185
Adds columns for Expiry, IsNonstandard (ie: deliverable is not 100 shares)
1184
1186
and Tag (the tag indicating what is actually deliverable, None if standard).
@@ -1195,5 +1197,7 @@ def _process_data(self, frame):
1195
1197
frame ['Underlying_Price' ] = self .underlying_price
1196
1198
frame ["Quote_Time" ] = self .quote_time
1197
1199
frame .rename (columns = {'Open Int' : 'Open_Int' }, inplace = True )
1200
+ frame ['Type' ] = type
1201
+ frame .set_index (['Strike' , 'Expiry' , 'Type' , 'Symbol' ], inplace = True )
1198
1202
1199
1203
return frame
0 commit comments