Last active
November 3, 2016 01:27
-
-
Save yhilpisch/9649a3d8515d43ff284ca10f6c258bc7 to your computer and use it in GitHub Desktop.
Files and Resources for Quant Insights Bootcamp (DAY 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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