Enhancing Market Charts with Matplotlib Background Coloring - Hands-On Market Analysis with Python
Introduction
Lets color the background of a chart to enhance some aspect of a market. By using the axvline()” function of Matplotlibs Axis function, we can customize the color of every vertical line available. Obviously, as this is at the pixel level, we have to keep things simple to keep them legible. MORE: Blog or code: http://www.viralml.com/video-content.html?fm=yt&v=mRHnW-9DNno Signup for my newsletter and more: http://www.viralml.com Connect on Twitter: https://twitter.com/amunategui Check out my book on Amazon The Little Book of Fundamental Market Indicators https://amzn.to/2DERG3d Candlestick Charting Video: https://youtu.be/crWLWZtJr1o Transcript: Hello YouTube Friends This is another market analysis hands-on with Python video. I recently did a video on plotting candlesticks using Matplotlib. A candlestick can help reduce the data yet retain a lot of pertinent market information. In this video, Ill show you an another way of doing it by coloring the background of the chart. We will download some S&P 500 data, calculate the percentage change and use that value to paint the background blue or red - blue if it is a positive percentage change, red if it is a negative one. 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 get early access to my material. Subscribe to the channel - and give it a big thumbs up, pretty please! As usual, the link to the Jupiter notebook is included in the description of this video. CATEGORY:Finance HASCODE:ViralML-Hands-On-Market-Analysis-Enhancing-Charts-With-Background-Colors.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 = gspc_df[['Date', 'Adj Close']]
gspc_df.columns = ['Date', 'SP500_close']
gspc_df['SP500_pct_change'] = gspc_df['SP500_close'].pct_change()
gspc_df.head()
gspc_df.tail()
Simple Visualization using the Percentage Change¶
A great way of enhancing price action visuals and your understanding of what is going on is to color the background of the chart. By using the "axvline" of the Matplotlibe's Axis function, we can color every vertical line a custom color. Obviously, as this is at the pixel level, we have to keep things simple so that the colors are still legible.
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(gspc_df['Date'], gspc_df['SP500_close'], color='black', label='Raw S&P 500')
plt.grid()
ax.legend(loc='upper right', frameon=False)
plt.title('S&P 500 and the S&P 500 Percentage Change')
# Get second axis
ax2 = ax.twinx()
plt.plot(gspc_df['Date'],
gspc_df['SP500_pct_change'].rolling(window=200).mean().values, color='blue', label='SP500_pct_change')
plt.axhline(0)
ax2.legend(loc='lower right', frameon=False)
tmp = gspc_df.copy()
cut_off_date = '2006-11-16'
tmp = tmp[tmp['Date'] >= cut_off_date]
# smoot the percentage change with a rolling average
tmp['SP500_pct_change'] = tmp['SP500_pct_change'].rolling(window=50).mean().values
tmp['i'] = range(1,len(tmp)+1)
fig, ax = plt.subplots(figsize=(16, 8))
plt.plot(tmp['Date'], tmp['SP500_pct_change'], color='white', label='SP500_pct_change')
ax.legend(loc='upper left', frameon=False)
plt.grid()
plt.axhline(0)
# track whenever the percentage change is above or b
pos_1 = tmp[tmp['SP500_pct_change'] >= 0]['i']
neg_1 = tmp[tmp['SP500_pct_change'] < 0]['i']
for x in pos_1:
ax.axvline(tmp[tmp['i']==x]['Date'].values, color='blue',linewidth=1,alpha=0.05)
for x in neg_1:
ax.axvline(tmp[tmp['i']==x]['Date'].values, color='red',linewidth=1,alpha=0.05)
plt.title('S&P 500 and the S&P 500 Percentage Change')
# Get second axis
ax2 = ax.twinx()
plt.plot(tmp['Date'],
tmp['SP500_close'], color='green', label='SP500_close')
ax2.legend(loc='upper right', frameon=False)
Show Notes
(pardon typos and formatting -these are the notes I use to make the videos)
Lets color the background of a chart to enhance some aspect of a market. By using the axvline()” function of Matplotlibs Axis function, we can customize the color of every vertical line available. Obviously, as this is at the pixel level, we have to keep things simple to keep them legible. MORE: Blog or code: http://www.viralml.com/video-content.html?fm=yt&v=mRHnW-9DNno Signup for my newsletter and more: http://www.viralml.com Connect on Twitter: https://twitter.com/amunategui Check out my book on Amazon The Little Book of Fundamental Market Indicators https://amzn.to/2DERG3d Candlestick Charting Video: https://youtu.be/crWLWZtJr1o Transcript: Hello YouTube Friends This is another market analysis hands-on with Python video. I recently did a video on plotting candlesticks using Matplotlib. A candlestick can help reduce the data yet retain a lot of pertinent market information. In this video, Ill show you an another way of doing it by coloring the background of the chart. We will download some S&P 500 data, calculate the percentage change and use that value to paint the background blue or red - blue if it is a positive percentage change, red if it is a negative one. 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 get early access to my material. Subscribe to the channel - and give it a big thumbs up, pretty please! As usual, the link to the Jupiter notebook is included in the description of this video. CATEGORY:Finance HASCODE:ViralML-Hands-On-Market-Analysis-Enhancing-Charts-With-Background-Colors.html