Created
October 15, 2014 08:20
-
-
Save steven-cutting/5bcd7b6f7be6226b303e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Oct 9 21:41:08 2014 | |
@author: Steven Cutting | |
@author email: steven.e.cutting@gmail.com | |
@project site: https://github.com/steven-cutting/Misc.git | |
Notes on formatting: | |
**Triple quote uses** | |
+ docstrings use doule quotes | |
+ commented out code use single quotes. | |
""" | |
# TODO (Steven): need to update/do doc strings | |
# TODO (Steven): need to add the ability to define date points | |
# TODO (Steven) Continue switch to using dictionaries to hold 'options' | |
# --dictionaries of list/arrays. | |
import numpy as np | |
import pandas as pd | |
# --------------------------------------------------------------------- | |
# Options classes and subclasses | |
class Options(object): | |
def __init__(self, argsList): | |
if any(ea_tokens['default']) in argsList: | |
pass | |
api = 'http://finance.yahoo.com/d/quotes' | |
tickers = np.array([('S&P 500', '^GSPC'), | |
('NASDAQ', '^IXIC'), | |
('Russell 2000', '^RUT'), | |
('Nikkei 225', '^N225'), | |
('Crude Oil', 'CLX14.NYM'), | |
('10-Yr Bond', '^TNX'), | |
('EUR/USD', 'EURUSD=X'), | |
('USD/JPY', 'USDJPY=X') | |
]) | |
data_codes = np.array([('Name', 'n'), | |
('LTrade', 'l1'), | |
('DaysValueChg', 'w4') | |
]) | |
temporal_option = 'rt' | |
_arrays = {'keys': ['yahoof', 'tickers', 'data_codes', 'temporal_option'], | |
'yahoof': api, | |
'tickers': tickers, | |
'data_codes': data_codes, | |
'temporal_option': temporal_option | |
} | |
# --------------------------------------------------------------------- | |
# Getting the data and providing it in a more usable format | |
class URL(object): | |
"""Creats an url object to be used to make requests from Yahoo Finances API. | |
$ ex = yahoo_url(mkt.index) | |
$ print ex._url() | |
""" | |
def __init__(self, options_dict): | |
if options_dict in ['df', 'default']: | |
options_dict = Options._arrays | |
self.keys = options_dict['keys'] | |
if 'yahoof' is self.keys[0]: # non agnostic term -----------!!! | |
self._baseUrl = options_dict[self.keys[0]] | |
self._urlPrtOne = '.csv?s=' | |
self._tickerSet = options_dict[self.keys[1]] | |
self._utlPrtTwo = '&f=' | |
self._optionSet = options_dict[self.keys[2]] | |
def _compile_string_list_from_options(self, options, delimiter): | |
"""Pulls the tickers from a numpy array and/or list of tickers and | |
places them in a single string delmited by plus signs ('+') for the | |
url builder to use. | |
**This version is for tickers stored in the secound column of a numpy | |
array. | |
""" | |
option_string = '' | |
if type(options) == np.ndarray: | |
for i in range(len(options) - 1): | |
option_string += options[i][1] + delimiter | |
option_string += options[-1][1] | |
elif type(options) == list: | |
for i in range(len(options) - 1): | |
option_string += options[i] + delimiter | |
option_string += options[-1] | |
else: | |
# TODO (Steven): change to an actual error thing | |
print "blerg!" | |
return option_string | |
# TODO (Steven): figure out the historical api shit | |
def _build_yahoof_url(self): # non agnostic term -----------!!! | |
url_tickers = self._compile_string_list_from_options(self._tickerSet, | |
'+') | |
url_options = self._compile_string_list_from_options(self._optionSet, | |
'') | |
url = (self._baseUrl + self._urlPrtOne + url_tickers + | |
self._utlPrtTwo + url_options) | |
return url | |
def url_(self): | |
# TODO (Steven): Make it not specific to one api | |
""" | |
builds the url | |
calls the correct url builder based on api selected | |
and passes relevant info to the url builder | |
""" | |
if self.keys[0] is 'yahoof': # non agnostic term -----------!!! | |
url = self._build_yahoof_url() # non agnostic term -----------!!! | |
return url | |
class DataTable(object): | |
def __init__(self, options_dict): | |
if options_dict in ['df', 'default', '']: | |
options_dict = Options._arrays | |
self.keys = options_dict['keys'] | |
if 'yahoof' is self.keys[0]: | |
self._ticker_set = options_dict[self.keys[1]] | |
self._option_set = options_dict[self.keys[2]] | |
self._temporal_option = options_dict[self.keys[3]] | |
self._url = URL(options_dict).url_() | |
def _headers(self): | |
headers = [] | |
for i in range(len(self._option_set)): | |
headers.append((self._option_set[i][0]) + ':') | |
return headers | |
def print_table(self): | |
if self._temporal_option is 'rt': | |
table = pd.read_csv(self._url, | |
names=(self._headers()), header=None, | |
index_col=0) | |
return table | |
# --------------------------------------------------------------------- | |
# Testing | |
# need to move to a dedicated doc at somepoint | |
def test(): | |
"""Using this to test code, remove before final""" | |
'''df = DefaultOptions()''' | |
#ex = URL('df') | |
#print ex.url_() | |
#print '\n' | |
terst = DataTable('df') | |
print terst.print_table() | |
test() | |
# --------------------------------------------------------------------- | |
# Yahoo Finance data options cheatsheet (short list) | |
''' | |
http://finance.yahoo.com/d/quotes | |
http://ichart.finance.yahoo.com/table | |
c6: Change (Realtime) | |
k2: Change Percent (Realtime) | |
c8: After Hours Change (Realtime) | |
k1: Last Trade (Realtime) With Time | |
l: Last Trade (With Time) | |
l1: Last Trade (Price Only) | |
w4: Day’s Value Change (Realtime) | |
m2: Day’s Range (Realtime) | |
**Symbol Info** | |
v: More Info | |
j1: Market Capitalization | |
j3: Market Cap (Realtime) | |
f6: Float Shares | |
n: Name | |
n4: Notes | |
s: Symbol | |
s1: Shares Owned | |
x: Stock Exchange | |
j2: Shares Outstanding | |
**Averages:** | |
m5: Change From 200 Day Moving Average | |
m6: Percent Change From 200 Day Moving Average | |
m7: Change From 50 Day Moving Average | |
m8: Percent Change From 50 Day Moving Average | |
m3: 50 Day Moving Average | |
m4: 200 Day Moving Average | |
''' | |
# --------------------------------------------------------------------- | |
# easyAnalysis Token dictionaries | |
"""ea = easyAnalysis""" | |
ea_tokens = {'default': ['df', 'defaul', ''], | |
} | |
# --------------------------------------------------------------------- | |
# mktdotpy Token dictionaries | |
dataCodeDict = {' Change (Realtime)': 'c6', | |
' Change Percent (Realtime)': 'k2', | |
' After Hours Change (Realtime)': 'c8', | |
' Last Trade (Realtime) With Time': 'k1', | |
' Last Trade (With Time)': 'l', | |
' Last Trade (Price Only)': 'l1', | |
' Day’s Value Change (Realtime)': 'w4', | |
' Day’s Range (Realtime)': 'm2', | |
' More Info': 'v', | |
' Market Capitalization': 'j1', | |
' Market Cap (Realtime)': 'j3', | |
' Float Shares': 'f6', | |
' Name': 'n', | |
' Notes': 'n4', | |
' Symbol': 's', | |
' Shares Owned': 's1', | |
' Stock Exchange': 'x', | |
' Shares Outstanding': 'j2', | |
' Change From 200 Day Moving Average': 'm5', | |
' Percent Change From 200 Day Moving Average': 'm6', | |
' Change From 50 Day Moving Average': 'm7', | |
' Percent Change From 50 Day Moving Average': 'm8', | |
' 50 Day Moving Average': 'm3', | |
' 200 Day Moving Average': 'm4', | |
} | |
tickerDict = {'S&P 500': '^GSPC', | |
'^GSPC': '^GSPC', | |
'NASDAQ': '^IXIC', | |
'^IXIC': '^IXIC', | |
'Russell 2000': '^RUT', | |
'^RUT': '^RUT', | |
'Nikkei 225': '^N225', | |
'^N225': '^N225', | |
'Crude Oil': 'CLX14.NYM', | |
'CLX14.NYM': 'CLX14.NYM', | |
'10-Yr Bond': '^TNX', | |
'^TNX': '^TNX', | |
'EUR/USD': 'EURUSD=X', | |
'EURUSD=X': 'ERUUSD=X', | |
'USD/JPY': 'USDJPY=X', | |
'USDJPY=X': 'USDJPY=X' | |
} | |
tickerDict_extended = {'S&P 500': '^GSPC', | |
'S&P500': '^GSPC', | |
's&p 500': '^GSPC', | |
's&p500': '^GSPC', | |
'^GSPC': '^GSPC', | |
'^Gspc': '^GSPC', | |
'^gspc': '^GSPC', | |
'NASDAQ': '^IXIC', | |
'nasdaq': '^IXIC', | |
'Nasdaq': '^IXIC', | |
'^IXIC': '^IXIC', | |
'^ixic': '^IXIC', | |
'^Ixic': '^IXIC', | |
'NASDAQ Composite': '^IXIC', | |
'nasdaq Composite': '^IXIC', | |
'Nasdaq Composite': '^IXIC', | |
'NASDAQ Composite': '^IXIC', | |
'nasdaq composite': '^IXIC', | |
'Nasdaq composite': '^IXIC', | |
'Russell 2000': '^RUT', | |
'Russell2000': '^RUT', | |
'RUSSELL 2000': '^RUT', | |
'RUSSELL2000': '^RUT', | |
'russell 2000': '^RUT', | |
'russell2000': '^RUT', | |
'^RUT': '^RUT', | |
'^Rut': '^RUT', | |
'^rut': '^RUT', | |
'Nikkei 225': '^N225', | |
'Nikkei225': '^N225', | |
'Nikkei 225': '^N225', | |
'NIKKEI225': '^N225', | |
'NIKKEI 225': '^N225', | |
'nikkei225': '^N225', | |
'nikkei 225': '^N225', | |
'^N225': '^N225', | |
'^n225': '^N225', | |
'Crude Oil': 'CLX14.NYM', | |
'crude oil': 'CLX14.NYM', | |
'CRUDE OIL': 'CLX14.NYM', | |
'Crude Oil Nov 14': 'CLX14.NYM', | |
'CrudeOil': 'CLX14.NYM', | |
'crudeoil': 'CLX14.NYM', | |
'CRUDEOIL': 'CLX14.NYM', | |
'CLX14.NYM': 'CLX14.NYM', | |
'Clx14.Nym': 'CLX14.NYM', | |
'clx14.nym': 'CLX14.NYM', | |
'10-Yr Bond': '^TNX', | |
'10-yr bond': '^TNX', | |
'Bond 10yr': '^TNX', | |
'bond 10yr': '^TNX', | |
'Bond 10yr': '^TNX', | |
'CBOE Interest Rat': '^TNX', | |
'CBOE Interest Rate': '^TNX', | |
'cboe interest rate': '^TNX', | |
'^TNX': '^TNX', | |
'^Tnx': '^TNX', | |
'^tnx': '^TNX', | |
'EUR/USD': 'EURUSD=X', | |
'Eur/Usd': 'EURUSD=X', | |
'eur/usd': 'EURUSD=X', | |
'EUR to USD': 'EURUSD=X', | |
'Eur to Usd': 'EURUSD=X', | |
'eur to usd': 'EURUSD=X', | |
'Euro/USD': 'EURUSD=X', | |
'Euro/usd': 'EURUSD=X', | |
'euro/USD': 'EURUSD=X', | |
'euro/usd': 'EURUSD=X', | |
'Euro to USD': 'EURUSD=X', | |
'Euro to usd': 'EURUSD=X', | |
'euro to USD': 'EURUSD=X', | |
'euro to usd': 'EURUSD=X', | |
'EURUSD=X': 'EURUSD=X', | |
'EurUsd=x': 'EURUSD=X', | |
'eurusd=x': 'EURUSD=X', | |
'USD/JPY': 'USDJPY=X', | |
'usd/jpy': 'USDJPY=X', | |
'USD/YEN': 'USDJPY=X', | |
'USD/Yen': 'USDJPY=X', | |
'usd/yen': 'USDJPY=X', | |
'USD to JPY': 'USDJPY=X', | |
'usd to jpy': 'USDJPY=X', | |
'USD to YEN': 'USDJPY=X', | |
'USD to Yen': 'USDJPY=X', | |
'usd to yen': 'USDJPY=X', | |
'USDJPY=X': 'USDJPY=X', | |
'UsdJpy=x': 'USDJPY=X', | |
'usdjpy=x': 'USDJPY=X', | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment