Finishing touches

I tried using the INTRADAY information but it does not go back far enough in time to be useful. So I wrapped up my options script by adding command line arguments. Now you can use it for your own stock symbol and create your own bins. For my next post I will move on to something else.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  7 07:11:46 2019

@author: Tom

Obtain the last 100 days of stock market data on the ticker symbol SPY
and compute the change in price from close to close. Then eliminate the
days that are not options expiration days and build a 7 bin histogram
"""
import argparse
import pandas as pd               # Series objects
import numpy as np                # Numerical recipes
import matplotlib.pyplot as plt   # To display plots
import seaborn as sns             # Sweet plotting package
import alphavantage as av         # My personal alphavantage library

from scipy.stats import norm # Just me drawing a superimposed normal curve

parser = argparse.ArgumentParser()
parser.add_argument( "symbol" )
args = parser.parse_args()

# A little warning here, this is just to prove that indeed the data is not 
# normal. I do not expect it to behave as a normal distribution over the period
# in question. I only go back 100 days because that is what the compact size
# gets you but also anything before that is unlikely to mean much.

# Get your own API KEY from the website

full = av.getSeries( args.symbol, 'GETYOUROWN' )

# Convert the json data into a pandas dataframe
s = av.getDailyClosingPrices( full.json() )

# Compute the change in price from previous close to this close.
day_to_day = pd.Series( [s[n] - s[n-1] for n in range(1,len(s) )], s.index[1:len(s)] )

Blog post code block formatting

As part of learning how to code super slick sweet python and maintain a blog about it I had to come up with a way to make the display of the code blocks look professional. My first code block was ugly and I had to go through and insert <br> after every line. This was unacceptable. After much searching around the web I came across the obvious answer. There exists a package in Python called Pygments. Pygments comes with a script entitled “pygmentize.” This script can be used to convert your Python source into succulent looking html code viewable from any browser.

pygmentize -l python -f html -o filename.html filename.py

Pretty simple except for one thing. While it creates tags for all of the different types of text you might find in a Python script it does not create styles for those. So I made the styles myself. By opening the preferences in my Spyder editor and seeing the colored syntax highlighting I was able to make a one for one style for each syntax tag I found in the html file. So when you look at the code blocks you are in essence seeing exactly what I see in my editor.