My New Udemy Class: Fundamental Market Analysis with Python

Fundamental Market Analysis with Python

Fundamental Market Analysis with Python - Find Your Own Answers On What Is Going on in the Financial Markets

Hot off the Press!

The Little Book of Fundamental Market Indicators

My New Book: "The Little Book of Fundamental Analysis: Hands-On Market Analysis with Python" is Out!

Grow Your Web Brand, Visibility & Traffic Organically

The Little Book of Fundamental Market Indicators

5 Years of amunategui.github.Io and the Lessons I Learned from Growing My Online Community from the Ground Up

Simple Candlestick Charting in Python: Hands-On Market Analysis

Introduction

Short video on charting the financial markets with candlesticks in python and Matplotlib. Candlesticks aggregate time series data into 4 essential point - the open, high, low, and close. Lets roll up our sleeves, download some S&P 500 data and get plotting!

Code

Visualizing the S&P 500
In [1]:
from IPython.display import Image
Image(filename='double logos.png')
Out[1]:
In [2]:
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
import io, base64, os, json, re 
import pandas as pd
import numpy as np
import datetime
import warnings
warnings.filterwarnings('ignore')

Load S&P 500 Historical Data

In [3]:
gspc_df = pd.read_csv('^GSPC.csv')
gspc_df['Date'] = pd.to_datetime(gspc_df['Date'])
gspc_df.head()
Out[3]:
Date Open High Low Close Adj Close Volume
0 1950-01-03 16.66 16.66 16.66 16.66 16.66 1260000
1 1950-01-04 16.85 16.85 16.85 16.85 16.85 1890000
2 1950-01-05 16.93 16.93 16.93 16.93 16.93 2550000
3 1950-01-06 16.98 16.98 16.98 16.98 16.98 2010000
4 1950-01-09 17.08 17.08 17.08 17.08 17.08 2520000
In [4]:
gspc_df.tail()
Out[4]:
Date Open High Low Close Adj Close Volume
17426 2019-04-05 2884.159912 2893.239990 2882.989990 2892.739990 2892.739990 3146820000
17427 2019-04-08 2888.459961 2895.949951 2880.780029 2895.770020 2895.770020 3054030000
17428 2019-04-09 2886.580078 2886.879883 2873.330078 2878.199951 2878.199951 3007980000
17429 2019-04-10 2881.370117 2889.709961 2879.129883 2888.209961 2888.209961 3062380000
17430 2019-04-11 2891.919922 2893.419922 2881.989990 2888.320068 2888.320068 2938540000

Simple Visualization using the Adjusted Close

In [5]:
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(gspc_df['Date'], gspc_df['Adj Close'], c='black')
plt.grid()
plt.show()

Aggregating Time Series Data using Candlesticks

In [81]:
# install the mpl_finance library if you haven't already
# !pip3 install mpl_finance
In [17]:
# work off a copy of the dataset to preserve the original
ohlc_df = gspc_df.copy()

# aggregation frequency
time_grouper = 'Y' # try 'Y', '5Y', 'M', etc

ohlc_df = ohlc_df.set_index('Date')
tmp_open = ohlc_df.groupby(
    pd.Grouper(freq=time_grouper))['Adj Close'].nth([0])
tmp_close = ohlc_df.groupby(
    pd.Grouper(freq=time_grouper))['Adj Close'].nth([-1])
ohlc_df = ohlc_df.groupby(
    pd.Grouper(freq=time_grouper))['Adj Close'].agg(
    {'Low': np.min,'High':np.max}).reset_index()
ohlc_df['Open'] = tmp_open.values
ohlc_df['Close'] = tmp_close.values

ohlc_df = ohlc_df[['Date', 'Open', 'High', 'Low', 'Close']]
ohlc_df.tail()

    
Out[17]:
Date Open High Low Close
65 2015-12-31 2058.199951 2130.820068 1867.609985 2043.939941
66 2016-12-31 2012.660034 2271.719971 1829.079956 2238.830078
67 2017-12-31 2257.830078 2690.159912 2257.830078 2673.610107
68 2018-12-31 2695.810059 2930.750000 2351.100098 2506.850098
69 2019-12-31 2510.030029 2895.770020 2447.889893 2888.320068
In [18]:
import matplotlib as mpl
import matplotlib.dates as mdates
from mpl_finance import candlestick_ohlc
mpl.style.use('default')

# Converting dates column to float values
ohlc_df['Date'] = ohlc_df['Date'].map(mdates.date2num)
In [19]:
# Making plot
fig, ax = plt.subplots(figsize=(16, 8))

# Converts raw mdate numbers to dates
ax.xaxis_date()
plt.xlabel("Date")

# Making candlestick plot
candlestick_ohlc(ax, ohlc_df.values, width = 200, 
                 colorup = 'g', colordown = 'r', alpha = 0.8)

plt.ylabel("Price")
plt.title('S&P 500')
plt.grid()
plt.show()

Show Notes

(pardon typos and formatting -
these are the notes I use to make the videos)

Short video on charting the financial markets with candlesticks in python and Matplotlib. Candlesticks aggregate time series data into 4 essential point - the open, high, low, and close. Lets roll up our sleeves, download some S&P 500 data and get plotting! MORE: Blog or code: http://www.viralml.com/video-content.html?fm=yt&v=crWLWZtJr1o Signup for my newsletter and more: http://www.viralml.com Connect on Twitter: https://twitter.com/amunategui My books on Amazon: The Little Book of Fundamental Indicators: Hands-On Market Analysis with Python: Find Your Market Bearings with Python, Jupyter Notebooks, and Freely Available Data: https://amzn.to/2DERG3d Monetizing Machine Learning: Quickly Turn Python ML Ideas into Web Applications on the Serverless Cloud: https://amzn.to/2PV3GCV Grow Your Web Brand, Visibility & Traffic Organically: 5 Years of amunategui.github.Io and the Lessons I Learned from Growing My Online Community from the Ground Up: Fringe Tactics - Finding Motivation in Unusual Places: Alternative Ways of Coaxing Motivation Using Raw Inspiration, Fear, and In-Your-Face Logic https://amzn.to/2DYWQas Create Income Streams with Online Classes: Design Classes That Generate Long-Term Revenue: https://amzn.to/2VToEHK Defense Against The Dark Digital Attacks: How to Protect Your Identity and Workflow in 2019: https://amzn.to/2Jw1AYS Transcript Hello, Youtube friends, A short video on charting the financial markets with candlesticks in python and Matplotlib Well use data from the S&P 500 which is the economic barometer of the world. It is made up of 500 of the largest US companies listed on US exchanges and is weighed by the total market value of their outstanding shares It captures over 80% total equity market value and almost 30% of its revenue comes from outside the United States, its a bit like the electrocardiogram of the worlds economic health. Welcome to the ViralML Show, my name is Manuel Amunategui. I am the author of a few books including a new one that will be out at the end of the month The Little Book of Fundamental Indicators where I share my favorite hands-on market analysis fundamental indicators and data sets. Things like the S&P500, unemployment, real estate, CPI, VIX, etc. Please signup for my newsletter to advance access to my material. Connect on Twitter and subscribe to the channel. So, all the code in this Jupyter notebook and the link is in the description Lets download the data on Yahoo Finance which generously makes it available for free “^GSPC.csv” Lets start by loading the S&P 500 historical data. Make sure your notebook is in the same directory as your historical data. CATEGORY:Finance HASCODE:ViralML-Hands-On-Market-Analysis-Visualizing-SP500-With-Candlesticks.html