mcdallas / wallstreet

Real time stock and option data.
MIT License
1.38k stars 203 forks source link

Is it possible to use Yahoo instead of Google? #6

Closed pkedrosky closed 7 years ago

pkedrosky commented 7 years ago

Google Finance is somewhat spotty in terms of option data, and I'd like to use Yahoo instead. It seems from your code that Yahoo is supported, but not sure whether it works on current site, or what the correct flag is to change sources.

mcdallas commented 7 years ago

Yes there is a source parameter that you can use. i.e g = Stock('GOOG', source='yahoo'). You have to install directly from the repo though, I haven't updated the package on pypi.

pkedrosky commented 7 years ago

Thanks. Tried, and having an issue. The following code produces a looping error, and never locks down an applicable option date:

gc = Call("goog", d=1, m=4, y=2017, strike=s.price, source='yahoo')

Here is looping message: No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead No options listed for given date, using 09-03-2017 instead

Any ideas? When using Google as a source, it finds options on nearest date to that specified. The trouble is that the Google option data is limited, on many stocks, to a year out.

mcdallas commented 7 years ago

Hmm this is definitely a bug but I don't seem to get it when I run your code. It seems like something that would happen if an expiration date in the json data is not an actual expiration date. Do you still get this error?

mcdallas commented 7 years ago

349b580c20fb3be12944a0c951b02e4303dc9d0b should be a fix to that.

pkedrosky commented 7 years ago

Thanks. No longer getting infinite loop, but now it is throwing error on valid expiration dates. See below. Error suggests this isn't valid date, which it is.

import numpy as np
from wallstreet import Stock, Call, Put
st = "gpro"
s = Stock(st, source='yahoo')
gc = Call(st, d=23, m=3, y=2017, strike=s.price, source='yahoo')
gp = Put(st, d=23, m=3, y=2017, strike=s.price, source='yahoo')'

Output:

No options listed for given date, using 23-03-2017 instead
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-ee5d9b57177e> in <module>()
      3 st = "gpro"
      4 s = Stock(st, source='yahoo')
----> 5 gc = Call(st, d=23, m=3, y=2017, strike=s.price, source='yahoo')
      6 gp = Put(st, d=23, m=3, y=2017, strike=s.price, source='yahoo')
      7 # Call = Put + (Stock – Strike) + PV(Interest on Strike) – PV(Dividends)

/Users/pk/anaconda/lib/python3.5/site-packages/wallstreet-0.1.5-py3.5.egg/wallstreet/wallstreet.py in __init__(self, quote, d, m, y, strike, strict, source)
    222         quote = quote.upper()
    223         kw = {'d': d, 'm': m, 'y': y, 'strict': strict, 'source': source}
--> 224         super().__init__(quote, **kw)
    225 
    226         if self.__class__.Option_type == 'Call':

/Users/pk/anaconda/lib/python3.5/site-packages/wallstreet-0.1.5-py3.5.egg/wallstreet/wallstreet.py in __init__(self, quote, d, m, y, strict, source)
    155                 print('No options listed for given date, using %s instead' % closest_date.strftime(DATE_FORMAT))
    156                 self._has_run = True
--> 157                 self.__init__(quote, closest_date.day, closest_date.month, closest_date.year, source=source)
    158             else:
    159                 raise ValueError('Possible expiration dates for this stock are:', self.expirations) from None

/Users/pk/anaconda/lib/python3.5/site-packages/wallstreet-0.1.5-py3.5.egg/wallstreet/wallstreet.py in __init__(self, quote, d, m, y, strike, strict, source)
    222         quote = quote.upper()
    223         kw = {'d': d, 'm': m, 'y': y, 'strict': strict, 'source': source}
--> 224         super().__init__(quote, **kw)
    225 
    226         if self.__class__.Option_type == 'Call':

/Users/pk/anaconda/lib/python3.5/site-packages/wallstreet-0.1.5-py3.5.egg/wallstreet/wallstreet.py in __init__(self, quote, d, m, y, strict, source)
    157                 self.__init__(quote, closest_date.day, closest_date.month, closest_date.year, source=source)
    158             else:
--> 159                 raise ValueError('Possible expiration dates for this stock are:', self.expirations) from None
    160 
    161     def _yahoo(self, quote, d, m, y):

ValueError: ('Possible expiration dates for this stock are:', ['16-03-2017', '23-03-2017', '30-03-2017', '06-04-2017', '12-04-2017', '20-04-2017', '27-04-2017', '20-07-2017', '19-10-2017', '18-01-2018', '17-01-2019'])
mcdallas commented 7 years ago

Ok this has to be a timezone issue since for the same options I get: ['17-03-2017', '24-03-2017', '31-03-2017', '07-04-2017', '13-04-2017', '21-04-2017', '28-04-2017', '21-07-2017', '20-10-2017', '19-01-2018', '18-01-2019']

Let me look into that

mcdallas commented 7 years ago

@pkedrosky I think I found the problem. Should be ok after 0d5ea95e5793b260045d2114a7e08f5accd2c9a0 . Could you try again ?

pkedrosky commented 7 years ago

Yup, that did it. Thanks for the fix.