Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active November 3, 2016 01:27
Show Gist options
  • Save yhilpisch/9649a3d8515d43ff284ca10f6c258bc7 to your computer and use it in GitHub Desktop.
Save yhilpisch/9649a3d8515d43ff284ca10f6c258bc7 to your computer and use it in GitHub Desktop.
Files and Resources for Quant Insights Bootcamp (DAY 1)
Vectorized Backtesting
----------------------
Retrieve data for Apple (AAPL) from 2005 on until yesterday.
Calculate the SMA42 and SMA252 and store them as new columns.
Generate positiongs based on the relationship between the two SMAs.
Compare the performance of the strategy with the market performance.
QUANT INSIGHTS BOOTCAMP
=======================
WIFI
----
Ft1ch#2016#!
MINICONDA
---------
http://conda.pydata.org/miniconda.html
http://conda.pydata.org/docs/using/envs.html
IPYTHON MAGIC COMMANDS
----------------------
https://ipython.org/ipython-doc/3/interactive/magics.html
QUANT PLATFORM
--------------
http://training.pqp.io
--> follow registration link
[special ports used are 8000 and 5565]
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python for Algorithmic Trading"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Quant Insights Bootcamp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dr. Yves J. Hilpisch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[yves@tpq.io](mailto:yves@tpq.io) | [http://tpq.io](http://tpq.io) | [@dyjh](http://twitter.com/dyjh)\n",
"\n",
"The Python Quants GmbH"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## The Python Quants"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"http://hilpisch.com/tpq_logo.png\" width=\"60%\">\n",
"\n",
"http://tpq.io"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"http://hilpisch.com/pqp_overview.png\" width=\"90%\">\n",
"\n",
"http://pqp.io"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"https://camo.githubusercontent.com/ae62a985112cd05290e6f5392215ba664b14dc87/687474703a2f2f68696c70697363682e636f6d2f64785f646f635f30322e706e67\" width=\"90%\">\n",
"\n",
"http://dx-analytics.com"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"http://hilpisch.com/images/python_for_finance.png\" width=\"40%\">\n",
"\n",
"http://python-for-finance.com"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"http://hilpisch.com/images/derivatives_analytics_front.jpg\" width=\"40%\">\n",
"\n",
"http://derivatives-analytics-with-python.com"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"http://hilpisch.com/images/lvvd_cover.png\" width=\"40%\">\n",
"\n",
"http://lvvd.tpq.io"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"<img src=\"http://hilpisch.com/images/pff_certificate.png\">\n",
"\n",
"http://training.tpq.io/pythonforfinance.html"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"For the curious:\n",
"\n",
"* http://tpq.io (company Web site)\n",
"* http://pqp.io (Quant Platform)\n",
"* http://datapark.io (data science in the browser)\n",
"* http://fpq.io (For Python Quants conference)\n",
"* http://meetup.com/Python-for-Quant-Finance-London/ (1,600+ members)\n",
"* http://pff.tpq.io | http://dawp.tpq.io | http://lvvd.tpq.io\n",
"* http://hilpisch.com (all my talks and more)\n",
"* http://twitter.com/dyjh (events, talks, finance news)\n",
"* http://training.tpq.io (online training)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Agenda"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Menu of topic options for the bootcamp:\n",
"\n",
"* **Setting up the Local Environment**\n",
"* **pandas for Vectorized Backtesting**\n",
"* **Object-Oriented Programming for Event-based Backtesting**\n",
"* **Stock Market Prediction with Regression & Machine Learning**\n",
"* **Sockets, Real-Time Data and Stream Plotting**\n",
"* **Oanda for Automated, Algorithmic Trading**\n",
"* **Deployment in the Cloud**\n",
"* ** ... ** (= many more)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Major Question 1\n",
"\n",
"> Where do you stand today?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Major Question 2\n",
"\n",
"> Where do you want to go (until when)?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Major Question 3\n",
"\n",
"> How does this bootcamp fit in?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Major Question 4\n",
"\n",
"> When exactly would you consider the bootcamp to be a success?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>\n",
"\n",
"<a href=\"http://tpq.io\" target=\"_blank\">http://tpq.io</a> | <a href=\"http://twitter.com/dyjh\" target=\"_blank\">@dyjh</a> | <a href=\"mailto:team@tpq.io\">team@tpq.io</a>\n",
"\n",
"**Python Quant Platform** |\n",
"<a href=\"http://quant-platform.com\">http://quant-platform.com</a>\n",
"\n",
"**Python for Finance** |\n",
"<a href=\"http://python-for-finance.com\" target=\"_blank\">Python for Finance @ O'Reilly</a>\n",
"\n",
"**Derivatives Analytics with Python** |\n",
"<a href=\"http://derivatives-analytics-with-python.com\" target=\"_blank\">Derivatives Analytics @ Wiley Finance</a>\n",
"\n",
"**Listed Volatility and Variance Derivatives** |\n",
"<a href=\"http://lvvd.tpq.io\" target=\"_blank\">Listed VV Derivatives @ Wiley Finance</a>\n",
"\n",
"**Python Training** |\n",
"<a href=\"http://training.tpq.io\" target=\"_blank\">Python for Finance University Certificate</a>"
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python [conda env:py3]",
"language": "python",
"name": "conda-env-py3-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
from pandas_datareader import data as web
class StrategyOptimizer(object):
def __init__(self, symbol, start, end, t1, t2):
self.symbol = symbol
self.start = start
self.end = end
self.t1 = t1
self.t2 = t2
self.get_data()
def get_data(self):
d = web.DataReader(self.symbol, data_source='yahoo',
start=self.start, end=self.end)['Adj Close']
d = pd.DataFrame(d)
d.columns = [self.symbol]
d['returns'] = np.log(d / d.shift(1))
self.data = d
def plot_data(self):
plt.figure()
self.data[self.symbol].plot(title='%s' % self.symbol)
def plot_returns(self):
plt.figure()
self.data['returns'].hist(bins=35)
def run_strategy(self, plot=False):
self.data['t1'] = self.data[self.symbol].rolling(self.t1).mean()
self.data['t2'] = self.data[self.symbol].rolling(self.t2).mean()
self.data['position'] = np.where(self.data.t1 > self.data.t2, 1, -1)
self.data['strategy'] = self.data.position.shift(1) * self.data.returns
if plot is True:
self.data.dropna()[['returns', 'strategy']].cumsum().apply(np.exp).plot()
perf = self.data.dropna()[['returns', 'strategy']].cumsum().apply(np.exp).ix[-1]
return perf
def generate_heat_meap(self, range_t1, range_t2):
results = np.zeros((len(range_t1), len(range_t2)))
for i, t1 in enumerate(range_t1):
for j, t2 in enumerate(range_t2):
self.t1 = t1
self.t2 = t2
perf = self.run_strategy()
net_perf = perf.loc['strategy'] - perf.loc['returns']
results[i, j] = net_perf
heat_map = pd.DataFrame(results, index=range_t1, columns=range_t2)
return heat_map
#
# Python for Algorithmic Trading
# Quant Insights Bootcamp
#
# The Python Quants
#
import sys
import numpy as np
from pandas_datareader import data as web
sym = 'AAPL'
start = '2010-1-1'
t1 = 42
t2 = 252
if len(sys.argv) > 1:
start = sys.argv[1]
sym = sys.argv[2]
t1 = int(sys.argv[3])
t2 = int(sys.argv[4])
data = web.DataReader(sym, data_source='yahoo', start=start)
data['SMAshort'] = data['Adj Close'].rolling(t1).mean()
data['SMAlong'] = data['Adj Close'].rolling(t2).mean()
data['Position'] = np.where(data['SMAshort'] > data['SMAlong'], 1, -1)
data['Returns'] = np.log(data['Adj Close'] / data['Adj Close'].shift(1))
data['Strategy'] = data['Position'].shift(1) * data['Returns']
data.dropna(inplace=True)
data[['Adj Close', 'SMAshort', 'SMAlong']].plot(
title='%s | SMAs = %d & %d' % (sym, t1, t2))
data[['Returns', 'Strategy']].cumsum().apply(np.exp).plot(title=sym)
#
# Python for Algorithmic Trading
# Quant Insights Bootcamp
#
# The Python Quants
#
import zmq
import datetime
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://0.0.0.0:6666')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
while True:
data = socket.recv_string()
print(str(datetime.datetime.now())+ ' | ' + data)
#
# Python for Algorithmic Trading
# Quant Insights Bootcamp
#
# The Python Quants
#
import zmq
import pandas as pd
import datetime
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://0.0.0.0:6666')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
df = pd.DataFrame()
for i in range(20):
data = socket.recv_string()
sym, value = data.split()
timestamp = datetime.datetime.now()
df = df.append(pd.DataFrame({sym: value}, index=[timestamp]))
print(i, data)
print(df)
#
# Python for Algorithmic Trading
# Quant Insights Bootcamp
#
# The Python Quants
#
import zmq
import time
import random
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://0.0.0.0:6666')
AAPL = 100.
while True:
AAPL += random.gauss(0, 1)
out = 'AAPL %f' % AAPL
print(out)
socket.send_string(out)
time.sleep(random.random() * 3)
import zmq
import datetime
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://0.0.0.0:6666')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
while True:
data = socket.recv_string()
print(str(datetime.datetime.now())+ ' | ' + data)
#
# Python for Algorithmic Trading
# Quant Insights Bootcamp
#
# The Python Quants
#
import zmq
import numpy as np
import pandas as pd
import datetime
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://0.0.0.0:6666')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
df = pd.DataFrame()
for i in range(20):
data = socket.recv_string()
sym, value = data.split()
timestamp = datetime.datetime.now()
df = df.append(pd.DataFrame({sym: value}, index=[timestamp]))
if i >= 9:
df['t1'] = df[sym].rolling(5).mean()
df['t2'] = df[sym].rolling(10).mean()
df['position'] = np.where(df.t1 > df.t2, 1, -1)
# place trading logic here
print(i, df.ix[-1])
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment