• Stars
    star
    325
  • Rank 129,350 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple to use interfaces for basic technical analysis of stocks.

Stock Analysis

Package for making elements of technical analysis of a stock easier from the book Hands-On Data Analysis with Pandas. 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

# should install requirements.txt packages
$ pip3 install -e stock-analysis # path to top level where setup.py is

# if not, install them explicitly
$ pip3 install -r requirements.txt

Usage

This section will show some of the functionality of each class; however, it is by no means exhaustive.

Getting data

from stock_analysis import StockReader

reader = StockReader('2017-01-01', '2018-12-31')

# get bitcoin data in USD
bitcoin = reader.get_bitcoin_data('USD')

# get faang data
fb, aapl, amzn, nflx, goog = (
    reader.get_ticker_data(ticker)
    for ticker in ['META', 'AAPL', 'AMZN', 'NFLX', 'GOOG']
)

# get S&P 500 data
sp = reader.get_index_data('S&P 500')

Grouping data

from stock_analysis.utils import group_stocks, describe_group

faang = group_stocks(
    {
        'Facebook': fb,
        'Apple': aapl,
        'Amazon': amzn,
        'Netflix': nflx,
        'Google': goog
    }
)

# describe the group
describe_group(faang)

Building a portfolio

Groups assets by date and sums columns to build a portfolio.

from stock_analysis.utils import make_portfolio

faang_portfolio = make_portfolio(faang)

Visualizing data

Be sure to check out the other methods here for different plot types, reference lines, shaded regions, and more!

Single asset

Evolution over time:

import matplotlib.pyplot as plt
from stock_analysis import StockVisualizer

netflix_viz = StockVisualizer(nflx)

ax = netflix_viz.evolution_over_time(
    'close',
    figsize=(10, 4),
    legend=False,
    title='Netflix closing price over time'
)
netflix_viz.add_reference_line(
    ax,
    x=nflx.high.idxmax(),
    color='k',
    linestyle=':',
    label=f'highest value ({nflx.high.idxmax():%b %d})',
    alpha=0.5
)
ax.set_ylabel('price ($)')
plt.show()

line plot with reference line

After hours trades:

netflix_viz.after_hours_trades()
plt.show()

after hours trades plot

Differential in closing price versus another asset:

netflix_viz.fill_between_other(fb)
plt.show()

differential between NFLX and FB

Candlestick plots with resampling (uses mplfinance):

netflix_viz.candlestick(resample='2W', volume=True, xrotation=90, datetime_format='%Y-%b -')

resampled candlestick plot

Note: run help() on StockVisualizer for more visualizations

Asset groups

Correlation heatmap:

from stock_analysis import AssetGroupVisualizer

faang_viz = AssetGroupVisualizer(faang)
faang_viz.heatmap(True)

correlation heatmap

Note: run help() on AssetGroupVisualizer for more visualizations. This object has many of the visualizations of the StockVisualizer class.

Analyzing data

Below are a few of the metrics you can calculate.

Single asset

from stock_analysis import StockAnalyzer

nflx_analyzer = StockAnalyzer(nflx)
nflx_analyzer.annualized_volatility()

Asset group

Methods of the StockAnalyzer class can be accessed by name with the AssetGroupAnalyzer class's analyze() method.

from stock_analysis import AssetGroupAnalyzer

faang_analyzer = AssetGroupAnalyzer(faang)
faang_analyzer.analyze('annualized_volatility')

faang_analyzer.analyze('beta', index=sp)

Modeling

from stock_analysis import StockModeler

Time series decomposition

decomposition = StockModeler.decompose(nflx, 20)
fig = decomposition.plot()
plt.show()

time series decomposition

ARIMA

Build the model:

arima_model = StockModeler.arima(nflx, ar=10, i=1, ma=5)

Check the residuals:

StockModeler.plot_residuals(arima_model)
plt.show()

ARIMA residuals

Plot the predictions:

arima_ax = StockModeler.arima_predictions(
    nflx, arima_model,
    start='2019-01-01', end='2019-01-07',
    title='ARIMA'
)
plt.show()

ARIMA predictions

Linear regression

Build the model:

X, Y, lm = StockModeler.regression(nflx)

Check the residuals:

StockModeler.plot_residuals(lm)
plt.show()

linear regression residuals

Plot the predictions:

linear_reg = StockModeler.regression_predictions(
    nflx, lm,
    start='2019-01-01', end='2019-01-07',
    title='Linear Regression'
)
plt.show()

linear regression predictions


About the Author

Stefanie Molin (@stefmolin) is a software engineer and data scientist at Bloomberg in New York City, where she tackles tough problems in information security, particularly those revolving around data wrangling/visualization, building tools for gathering data, and knowledge sharing. She is also the author of Hands-On Data Analysis with Pandas, which is currently in its second edition and has been translated into Korean. She holds a bachelorโ€™s of science degree in operations research from Columbia University's Fu Foundation School of Engineering and Applied Science, as well as a masterโ€™s degree in computer science, with a specialization in machine learning, from Georgia Tech. In her free time, she enjoys traveling the world, inventing new recipes, and learning new languages spoken among both people and computers.

More Repositories

1

Hands-On-Data-Analysis-with-Pandas-2nd-edition

Materials for following along with Hands-On Data Analysis with Pandas โ€“ Second Edition
Jupyter Notebook
570
star
2

Hands-On-Data-Analysis-with-Pandas

Materials for following along with Hands-On Data Analysis with Pandas.
Jupyter Notebook
410
star
3

pandas-workshop

An introductory workshop on pandas with notebooks and exercises for following along. Slides contain all solutions.
Jupyter Notebook
340
star
4

python-data-viz-workshop

A workshop on data visualization in Python with notebooks and exercises for following along. Slides contain all solutions.
Jupyter Notebook
239
star
5

data-morph

Morph an input dataset of 2D points into select shapes, while preserving the summary statistics to a given number of decimal points through simulated annealing. It is intended to be used as a teaching tool to illustrate the importance of data visualization.
Python
55
star
6

login-attempt-simulator

Simulation of regular login activity on a site and random activity from a hacker using a brute-force password guessing attack.
Python
14
star
7

ml-utils

Machine learning utility functions and classes.
Python
12
star
8

exif-stripper

Pre-commit hook to ensure image EXIF data is removed.
Python
8
star
9

pre-commit-workshop

"(Pre-)Commit to Better Code" workshop
6
star
10

airline-market-share-analysis

Blog post for OpenDataScience.com showing how to create a pivot table and stacked bar visualization of airline market share.
Jupyter Notebook
6
star
11

SCOPE-Anomaly-Detection-Case-Study

Case study on rules-based and machine learning models for anomaly detection used in SCOPE alerts.
Jupyter Notebook
5
star
12

Metis

Metis Web App built using Flask to collect opinions from users on KPI evolutions to use in Machine Learning Anomaly Detection methods.
Python
3
star
13

Custom-Colormaps

Utility functions for working with colors in Python.
Python
2
star
14

binder-environments

Central location for binder environments, especially those used with my book, Hands-On Data Analysis with Pandas.
2
star
15

R-training-program

This is the custom R training program I developed to teach fellow analysts using company data. All sensitive data has been removed.
R
1
star
16

Bulls-and-Cows

Bulls and Cows command line game for guessing a number.
Java
1
star
17

DidYouFeelIt

DidYouFeelIt? App from the Udacity Android Basics: Networking course (prerequisite for Grow with Google Android Developer Scholarship phase 1)
Java
1
star
18

stefmolin

Smarty
1
star
19

data-morph-talk

Slides for my talk "Data Morph: A Cautionary Tale of Summary Statistics"
HTML
1
star
20

Yeshiva-DAV5400

Template repository for Yeshiva's DAV5400 with configuration for GitHub codespaces (VS Code and JupyterLab).
1
star
21

stefmolin.github.io

My personal website (stefaniemolin.com).
TypeScript
1
star
22

SCOPE-anomaly-detection-emails

SCOPE Anomaly Detection System -- Sends daily emails with curated alerts to people who can investigate further.
HTML
1
star
23

CourtCounter

CourtCounter app from the Udacity Android Basics: User Input course (prerequisite for Grow with Google Android Developer Scholarship phase 1)
Java
1
star