From fb023e0dea0a942fa4a3d3dbcb50783a58cba586 Mon Sep 17 00:00:00 2001 From: bspeice Date: Thu, 24 Nov 2016 15:10:36 -0500 Subject: [PATCH] Add code to get the options chain for a stock --- metrik/tasks/tradeking.py | 31 +++++++++++++++++++++++++++++-- test/tasks/test_tradeking.py | 11 ++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/metrik/tasks/tradeking.py b/metrik/tasks/tradeking.py index 244cf9a..d704813 100644 --- a/metrik/tasks/tradeking.py +++ b/metrik/tasks/tradeking.py @@ -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) + ) diff --git a/test/tasks/test_tradeking.py b/test/tasks/test_tradeking.py index 78a40fc..15641d9 100644 --- a/test/tasks/test_tradeking.py +++ b/test/tasks/test_tradeking.py @@ -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):