Chris Degiere

Software Engineering and Data Science

Introduction to Financial Market Data Visualization with Python

June 16, 2014 By Chris

View the iPython notebook version of this post. (Much nicer formatting)

In finance, market data is price and trade data for a given instrument like a
stock, currency pair, or futures contract. Often times this data is visualized
as a chart of historical data.

Python is an excellent language for working with financial data. Python syntax
is compact for data exploration using an interactive shell like iPython. There
is also strong support for downloading, manipulating, and visualizing financial
market data through popular open source libraries like Pandas and Matplotlib.

# download free end of day historical stock data
# from yahoo finance using pandas
import pandas.io.data as web
from datetime import datetime
   
end = datetime.now()
start = datetime(end.year - 5, end.month, end.day)
df = web.DataReader("SPY", 'yahoo', start, end)
    
print df.tail()


Open High Low Close Volume Adj Close
Date
2014-06-05 193.41 194.65 192.70 194.45 92103000 194.45
2014-06-06 194.87 195.43 194.78 195.38 78696000 195.38
2014-06-09 195.35 196.05 195.17 195.58 65119000 195.58
2014-06-10 195.34 195.64 194.92 195.60 57129000 195.60
2014-06-11 194.90 195.12 194.48 194.92 68711600 194.92

[5 rows x 6 columns]

The above is a Pandas DataFrame, a two-dimensional tabular, column-oriented data
structure with rich, high-performance time series functionality built on top of
NumPy’s array-computing features. A DataFrame provides many of the capabilities
of a spreadsheet and relational database with flexible handling of missing data
and integration with Matplotlib for visualization.

# summary statistics accross the whole DataFrame
df.describe()
Open High Low Close Volume Adj Close
count 1258.000000 1258.000000 1258.000000 1258.000000 1.258000e+03 1258.000000
mean 136.639539 137.375763 135.841924 136.679245 1.685675e+08 130.850079
std 25.695177 25.643999 25.745236 25.700838 7.963665e+07 28.457251
min 87.700000 88.490000 87.000000 87.960000 3.967750e+07 79.860000
25% 116.085000 117.275000 115.080000 116.295000 1.153016e+08 107.932500
50% 132.555000 133.220000 131.640000 132.500000 1.524652e+08 125.405000
75% 154.820000 155.540000 154.150000 154.907500 2.046574e+08 151.655000
max 195.350000 196.050000 195.170000 195.600000 7.178287e+08 195.600000

8 rows × 6 columns

Slicing a DataFrame’s column yields a Series that can be operated on alone as
seen below.

# Closing price for most recent 5 trading days
df[['Close']].tail()
Close
Date
2014-06-05 194.45
2014-06-06 195.38
2014-06-09 195.58
2014-06-10 195.60
2014-06-11 194.92

5 rows × 1 columns

# volume statistics
vol = df[['Volume']]
print "Min: %s Max: %s Average: %s" % (vol.min().values[0], vol.max().values[0], vol.mean().values[0])


Min: 39677500 Max: 717828700 Average: 168567548.649

A wrapper around Matplotlib produces preformatted two-dimensional charts.

# plot the historical closing prices and volume using matplotlib
plots = df[['Close', 'Volume']].plot(subplots=True, figsize=(10, 10))
plt.show()

python-finance-matplotlib-chart1

# chart a basic 50 period moving average of the closing price
import pandas as pd
df['ma50'] = pd.rolling_mean(df['Close'], 50)
df['ma200'] = pd.rolling_mean(df['Close'], 200)
plots = df[['Close', 'ma50', 'ma200']].plot(subplots=False, figsize=(10, 4))
plt.show()

python-finance-matplotlib-chart2

For more details on Pandas and matplotlib see:

  • pandas.pydata.org/pandas-docs/stable/10min.html
  • pandas.pydata.org/pandas-docs/stable/visualization.html

Filed Under: python Tagged With: finance, matplotlib, pandas, python, visualization

Interview with Striker Securities

March 29, 2014 By Chris

Trading SystemsWe were interviewed for the February issue of the Striker Report. Below is an excerpt from the interview.

Developer/CTA Interview

Chris Degiere, Founder of Trading Technicians

Program(s) Developed: Evolution Gold Trading System
Interview Date: February, 2014
Interviewed by John F. Gallwas, Founder of Striker Securities, Inc.

Chris Degiere is the founder and principal of Trading Technicians, a Silicon Valley company that develops and markets futures trading systems. Chris is a software engineer and consultant with more than 17 years of experience in the technology and financial industries and has been an active trader and system developer since 2007.

John Gallwas: Tell us a little about yourself and what led you to starting Trading Technicians?

Chris Degiere: I’ve been interested in the financial markets since the dot-com bubble in the late 90s. A lot of money was made and lost during that period and I had my own naive part in it. After the bust I set out to learn all I could about alternative investment strategies to hopefully beat the next downturn.

Mechanical trading strategies and money management techniques were a great fit for me with my software automation and data engineering background. Being able to code seemed like a good advantage and I started building my own trading systems for TradeStation and scripting a lot of the discovery and testing process. Automated trade execution also freed up the time I’d otherwise be glued to charts during market hours so I could keep working on research and development projects.

It took a while before I made significant progress. I launched Trading Technicians mainly so I could lower the barrier to entry to alternative investment technology like this for others. Trading system development is difficult and time consuming and there’s so much emotional stress involved with active trading that I saw an opportunity to offer a valuable service to other traders and investors.

John Gallwas: As an active trader yourself, why would you want to sell your trading systems to other traders?

Chris Degiere: One of my original goals was to develop trading systems that worked well with small accounts. My private trading is still modest and geared more towards account growth so it’s not an immediate source of income for me. By keeping profits in the account I can better utilize position-sizing techniques to grow the account faster. Leasing my trading systems helps cover some of my costs so I can focus on research and development and trade to the fullest potential.

I think the Striker platform is also a great place to build a public track record, and accountability is a terrific motivator. Already I’ve met a lot of traders, investors, and experts in the industry that I might not have otherwise met if I had stayed private. By offering a service to the public I get to talk with people and learn from their experience and find out how to focus on what matters most.

John Gallwas: What are the key points to your trading systems and do you periodically update?

Chris Degiere: The biggest breakthrough for me has been applying machine learning techniques and automation to the strategy development and testing process. The key point with this method is to avoid over fitting. What has worked for me has been concentrating on systems with simple trading rules and a small number of moving parts. I look for candidate models with a high number of trades spread over as much historical data as possible and over different market conditions so I can trust the performance statistics. Making sure there’s a good balance of long and short trades and a high enough average trade size to cover slippage and commission is also key. The most robust models will perform on unseen data, hold up to stress conditions, and produce realistic live results in my own live account before they’re released to the public.

Entry and exit signals for my trading systems are adjusted by volatility so they can adapt to changing market conditions on their own. I also look for stable ranges of inputs to the trading rules so they will have good longevity without changes. Market dynamics do change over time though so I keep a number of candidates ready to rotate in that are variations of the core model. This allows me to “evolve” the live trading system to adapt and offer the best chance for ongoing positive performance. Being able to react to change quickly and adjust without starting from scratch is also a key point for me.

…

Read the full interview »

Filed Under: Automated trading systems, Commodity futures trading, Trading Tagged With: finance, python

About

Hi, I'm Chris, an experienced technology leader, software engineer, and data scientist. I'm passionate about data strategy, the intersection of engineering and analytics, and innovation work in finance and technology.

Code
  • Chris Degiere @ LinkedIn
  • Chris Degiere @ GitHub
  • Chris Degiere @ Coderwall

Music
  • Chris Degiere @ Bandcamp
  • Chris Degiere @ SoundCloud
  • Chris Degiere @ Discogs
  • Chris Degiere @ Linktree

Blog

  • Introduction to Financial Market Data Visualization with Python
  • Interview with Striker Securities
  • Automated Trading Systems
  • Simulating High Latency Low Bandwidth Connections
Tweets by degiere

Copyright © 2023 Chris Degiere · Log in