1
0
mirror of https://github.com/bspeice/metrik synced 2024-11-04 22:48:11 -05:00

Add code to get the options chain for a stock

This commit is contained in:
bspeice 2016-11-24 15:10:36 -05:00
parent 3ba12bf224
commit fb023e0dea
2 changed files with 39 additions and 3 deletions

View File

@ -5,12 +5,11 @@ from datetime import timedelta
from dateutil.parser import parse
import logging
from metrik.tasks.base import MongoCreateTask, MongoRateLimit
from metrik.tasks.base import MongoCreateTask, MongoRateLimit, MongoNoBackCreateTask
from metrik.conf import get_config
class TradekingApi(object):
format_json = '.json'
format_xml = '.xml'
root_url = 'https://api.tradeking.com/v1/'
@ -85,3 +84,31 @@ class Tradeking1mTimesales(MongoCreateTask):
logging.error('Unable to acquire lock for Tradeking ticker {}'
.format(symbol))
return {}
class TradekingOptionsQuotes(MongoNoBackCreateTask):
symbol = Parameter()
def get_collection_name(self):
return 'tradeking_options'
@staticmethod
def retrieve_chain_syms(api, symbol):
results = api.api_request('market/options/search', {
'symbol': symbol,
'query': 'unique:strikeprice'
}).json()['response']['quotes']['quote']
return [r['symbol'] for r in results]
@staticmethod
def retrieve_data(symbol):
tradeking = TradekingApi()
# We request a first rate limit lock to get the options chain
ratelimit = MongoRateLimit()
chain_acquire = ratelimit.acquire_lock(
service='tradeking',
limit=60,
interval=timedelta(minutes=1)
)

View File

@ -6,7 +6,7 @@ import pytest
from six.moves import map
from pytz import utc
from metrik.tasks.tradeking import Tradeking1mTimesales
from metrik.tasks.tradeking import Tradeking1mTimesales, TradekingOptionsQuotes, TradekingApi
from metrik.trading_days import TradingDay
from metrik.targets.mongo import MongoTarget
from test.mongo_test import MongoTest
@ -40,6 +40,15 @@ def test_returns_verifiable(ticker):
assert_allclose(tradeking_ohlc, yahoo_ohlc, rtol=1e-3)
@pytest.mark.parametrize('ticker', [
'AAPL', 'GOOG', 'SPY', 'REGN', 'SWHC', 'BAC', 'NVCR', 'ARGT'
])
def test_chain_returns(ticker):
api = TradekingApi()
chain = TradekingOptionsQuotes.retrieve_chain_syms(api, ticker)
assert len(chain) > 20
class TradekingTest(MongoTest):
def test_record_is_saveable(self):