-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StockReader object for grabbing data from websites.
- Loading branch information
0 parents
commit 5e1466b
Showing
8 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.ipynb_checkpoints | ||
*.png | ||
*/__pycache__/ | ||
*.egg-info |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Stefanie Molin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Stock Analysis | ||
Package for making elements of technical analysis of a stock easier. This package is meant to be a starting point for you to develop your own. As such, all the instructions for installing/setup will be assuming you will continue to develop on your end. | ||
|
||
## Setup | ||
### with pip | ||
``` | ||
# should install requirements.txt packages | ||
pip install -e stock_analysis # path to top level where setup.py is | ||
# if not, install them explicitly | ||
pip install -r requirements.txt | ||
``` | ||
|
||
### with setup.py | ||
``` | ||
python setup.py develop | ||
``` | ||
|
||
## Usage | ||
Coming soon, this is a WIP. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pandas==0.23.4 | ||
pandas-datareader==0.7.0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from distutils.core import setup | ||
|
||
setup( | ||
name='stock_analysis', | ||
version='0.0', | ||
description='Classes for technical analysis of stocks.', | ||
author='Stefanie Molin', | ||
author_email='24376333+stefmolin@users.noreply.github.com', | ||
license='MIT', | ||
url='https://github.com/stefmolin/stock-analysis', | ||
packages=['stock_analysis'], | ||
install_requires=[ | ||
'pandas>=0.23.4', | ||
'pandas-datareader==0.7.0' | ||
], | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""Classes for making technical stock analysis easier.""" | ||
|
||
from .stock_reader import StockReader |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"""Gathering select stock data.""" | ||
|
||
import datetime | ||
import re | ||
|
||
import pandas as pd | ||
import pandas_datareader.data as web | ||
|
||
from .utils import label_sanitizer | ||
|
||
class StockReader: | ||
"""Class for reading financial data from websites.""" | ||
|
||
def __init__(self, start, end=None): | ||
""" | ||
Create a StockReader object for reading across a given date range. | ||
Parameters: | ||
- start: The first date to include, as a datetime object or | ||
a string in the format 'YYYYMMDD'. | ||
- end: The last date to include, as a datetime object or | ||
a string in the format 'YYYYMMDD'. Defaults to today | ||
if not provided. | ||
Returns: | ||
A StockReader object. | ||
""" | ||
self.start, self.end = map( | ||
lambda x: x.strftime('%Y%m%d') if isinstance(x, datetime.date) \ | ||
else re.sub(r'\D', '', x), | ||
[start, end or datetime.date.today()] | ||
) | ||
|
||
@label_sanitizer | ||
def get_ticker_data(self, ticker): | ||
""" | ||
Get historical OHLC data from Investors Exchange (IEX) | ||
for given date range and ticker. | ||
Parameter: | ||
- ticker: The stock symbol to lookup as a string. | ||
Returns: | ||
A pandas dataframe with the stock data. | ||
""" | ||
return web.DataReader(ticker, 'iex', self.start, self.end) | ||
|
||
@label_sanitizer | ||
def get_bitcoin_data(self): | ||
""" | ||
Get bitcoin historical OHLC data from coinmarketcap.com for given date range. | ||
Returns: | ||
A pandas dataframe with the bitcoin data. | ||
""" | ||
return pd.read_html( | ||
'https://coinmarketcap.com/currencies/bitcoin/historical-data/?' | ||
'start={}&end={}'.format( | ||
self.start, self.end | ||
), | ||
parse_dates=True, | ||
index_col='Date' | ||
)[0] | ||
|
||
@label_sanitizer | ||
def get_sp500_data(self): | ||
""" | ||
Get historical OHLC data from Yahoo Finance for the S&P 500 Index | ||
for given date range. | ||
Returns: | ||
A pandas dataframe with the S&P 500 index data. | ||
""" | ||
return web.get_data_yahoo('^GSPC', self.start, self.end) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""Utility functions for stock analysis.""" | ||
|
||
from functools import wraps | ||
import re | ||
|
||
def _sanitize_label(label): | ||
""" | ||
Clean up a label by removing non-letter, non-space characters and | ||
putting in all lowercase with underscores replacing spaces. | ||
Parameters: | ||
- label: The text you want to fix. | ||
Returns: | ||
The sanitized label. | ||
""" | ||
return re.sub(r'[^\w\s]', '', label).lower().replace(' ', '_') | ||
|
||
def label_sanitizer(method, *args, **kwargs): | ||
""" | ||
Decorator around a method that returns a dataframe to | ||
clean up all labels in said dataframe (column names and index name) | ||
by removing non-letter, non-space characters and | ||
putting in all lowercase with underscores replacing spaces. | ||
Parameters: | ||
- method: The dataframe with labels you want to fix. | ||
- args: Additional positional arguments to pass to the wrapped method. | ||
- kwargs: Additional keyword arguments to pass to the wrapped method. | ||
Returns: | ||
A decorated method. | ||
""" | ||
# keep the docstring of the data method for help() | ||
@wraps(method) | ||
def method_wrapper(self, *args, **kwargs): | ||
df = method(self, *args, **kwargs) | ||
|
||
# fix the column names | ||
df.columns = [ | ||
_sanitize_label(col) for col in df.columns | ||
] | ||
|
||
# fix the index name | ||
df.index.rename( | ||
_sanitize_label(df.index.name), | ||
inplace=True | ||
) | ||
return df | ||
return method_wrapper |