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! 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
Code
from IPython.display import Image
Image(filename='double logos.png')
%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¶
gspc_df = pd.read_csv('^GSPC.csv')
gspc_df['Date'] = pd.to_datetime(gspc_df['Date'])
gspc_df.head()
gspc_df.tail()
Simple Visualization using the Adjusted Close¶
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¶
# install the mpl_finance library if you haven't already
# !pip3 install mpl_finance
# 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()
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)
# 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