Last active
July 22, 2019 04:10
-
-
Save yhilpisch/385e73625bf69aba3cad8d01bedb1ddf to your computer and use it in GitHub Desktop.
Files and Resources for Quant Insights Bootcamp (DAY 2)
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 | |
def generate_matrix(a, lags): | |
m = np.zeros((lags + 1, len(a) - lags)) | |
for i in range(lags + 1): | |
if i == lags: | |
m[i] = a[i:] | |
else: | |
m[i] = a[i:i - lags] | |
return m |
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 json | |
import pandas as pd | |
import oandapy as opy | |
oc = json.load(open('oc_pythonquant.json', 'r')) | |
oanda = opy.API(access_token=oc['api_key']) | |
class MyTrader(opy.Streamer): | |
def __init__(self, instrument, t1, t2, *args, **kwargs): | |
opy.Streamer.__init__(self, *args, **kwargs) | |
self.instrument = instrument | |
self.t1 = t1 | |
self.t2 = t2 | |
self.df = pd.DataFrame() | |
self.ticks = 0 | |
self.position = 0 | |
self.units = 100 | |
def go_long(self, units): | |
order = oanda.create_order(oc['account_id'], instrument=self.instrument, | |
side='buy', type='market', units=units) | |
print(25 * '=') | |
print(order) | |
def go_short(self, units): | |
order = oanda.create_order(oc['account_id'], instrument=self.instrument, | |
side='sell', type='market', units=units) | |
print(25 * '=') | |
print(order) | |
def on_success(self, data): | |
self.ticks += 1 | |
print(self.ticks, end=', ') | |
self.df = self.df.append(pd.DataFrame(data['tick'], | |
index=[pd.Timestamp(data['tick']['time'])])) | |
self.dfr = self.df.resample('5S').last().ffill() | |
if len(self.dfr) >= self.t2: | |
self.dfr['t1'] = self.dfr['ask'].rolling(self.t1).mean() | |
self.dfr['t2'] = self.dfr['ask'].rolling(self.t2).mean() | |
if self.position == 0: | |
if self.dfr.t1.ix[-1] > self.dfr.t2.ix[-1]: | |
self.go_long(int(self.units / 2)) | |
self.position = 1 | |
else: | |
self.go_short(int(self.units / 2)) | |
self.position = -1 | |
elif self.position == 1: | |
if self.dfr.t1.ix[-1] < self.dfr.t2.ix[-1]: | |
self.go_short(self.units) | |
self.position = -1 | |
elif self.position == -1: | |
if self.dfr.t1.ix[-1] > self.dfr.t2.ix[-1]: | |
self.go_long(self.units) | |
self.position = 1 | |
if self.ticks == 500: | |
if self.position == 1: | |
self.go_short(int(self.units / 2)) | |
print('Closing out long position.') | |
elif self.position == -1: | |
self.go_long(int(self.units / 2)) | |
print('Closing out short position.') | |
def on_error(self): | |
self.disconnect() |
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 | |
======================================================== | |
JUPYTER NOTEBOOK SERVER IN THE CLOUD | |
------------------------------------ | |
https://gist.github.com/yhilpisch/4b9ca48323d1f5c33002805542af8f54 | |
PLOTLY | |
------ | |
http://plot.ly | |
--> sign up for free account | |
--> go to Settings --> API Keys | |
ML TALK @ ODSC | |
-------------- | |
http://tpq.io/p/ml_finance.html#/ | |
TETHERING IPHONE | |
---------------- | |
F1tchQIW | |
OANDA | |
----- | |
http://oanda.com | |
--> register for demo account | |
install oandapy.py as follows: | |
pip install git+https://github.com/oanda/oandapy.git |
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 numpy as np | |
import pandas as pd | |
import seaborn as sns; sns.set() | |
from pandas_datareader import data as web | |
from sklearn import linear_model | |
class ScikitBacktester(object): | |
def __init__(self, symbol, start, end, lags): | |
self.symbol = symbol | |
self.start = start | |
self.end = end | |
self.lags = lags | |
self.get_data() | |
self.lm = linear_model.LinearRegression() | |
def get_data(self): | |
d = web.DataReader(self.symbol, start=self.start, end=self.end, data_source='yahoo')['Adj Close'] | |
d = pd.DataFrame(d) | |
d.columns = ['price'] | |
d['returns'] = np.log(d / d.shift(1)) | |
self.data = d.dropna() | |
def generate_matrix(self): | |
m = np.zeros((self.lags + 1, len(self.data) - self.lags)) | |
for i in range(self.lags + 1): | |
if i == self.lags: | |
m[i] = self.data.returns.values[i:] | |
else: | |
m[i] = self.data.returns.values[i:i - self.lags] | |
self.matrix = m | |
def fit_model(self): | |
self.generate_matrix() | |
self.lm.fit(self.matrix[:self.lags].T, self.matrix[self.lags]) | |
def predict_moves(self): | |
self.fit_model() | |
pred = self.lm.predict(self.matrix[:self.lags].T) | |
return pred | |
def run_strategy(self): | |
self.data['prediction'] = np.nan | |
self.data['prediction'].ix[self.lags:] = self.predict_moves() | |
self.data['position'] = np.sign(self.data['prediction']) | |
self.data['strategy'] = self.data['position'] * self.data['returns'] | |
title = '%s | lags = %d' % (self.symbol, self.lags) | |
self.data[['returns', 'strategy']].ix[self.lags:].cumsum().apply(np.exp).plot(title=title) |
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 numpy as np | |
import pandas as pd | |
import seaborn as sns; sns.set() | |
from pandas_datareader import data as web | |
from sklearn import linear_model, svm | |
class ScikitBacktester(object): | |
def __init__(self, symbol, lags, model): | |
self.symbol = symbol | |
self.lags = lags | |
self.model = model | |
self.get_data() | |
if self.model == 'logistic': | |
self.lm = linear_model.LogisticRegression(C=1e6) | |
elif self.model == 'svm': | |
# !!needs to be checked!! | |
self.lm = svm.SVC(kernel='linear') | |
elif self.model == 'regression': | |
self.lm = linear_model.LinearRegression() | |
else: | |
raise ValueError('Model not known.') | |
def get_data(self): | |
d = web.DataReader(self.symbol, data_source='yahoo')['Adj Close'] | |
d = pd.DataFrame(d) | |
d.columns = ['price'] | |
d['returns'] = np.log(d / d.shift(1)) | |
self.data = d.dropna() | |
def select_data(self, start, end): | |
d = self.data[(self.data.index >= start) & (self.data.index <= end)].copy() | |
return d | |
def generate_matrix(self, start, end): | |
d = self.select_data(start, end) | |
m = np.zeros((self.lags + 1, len(d) - self.lags)) | |
for i in range(self.lags + 1): | |
if i == self.lags: | |
m[i] = d.returns.values[i:] | |
else: | |
m[i] = d.returns.values[i:i - self.lags] | |
self.matrix = m | |
def fit_model(self, start, end): | |
self.generate_matrix(start, end) | |
self.lm.fit(self.matrix[:self.lags].T, np.sign(self.matrix[self.lags])) | |
def predict_moves(self, start, end): | |
self.generate_matrix(start, end) | |
pred = self.lm.predict(self.matrix[:self.lags].T) | |
return pred | |
def run_strategy(self, start_tr, end_tr, start_te, end_te, lags=None): | |
if lags is not None: | |
self.lags = lags | |
self.fit_model(start_tr, end_tr) | |
pred = self.predict_moves(start_te, end_te) | |
d = self.select_data(start_te, end_te) | |
d['prediction'] = np.nan | |
d['prediction'].ix[self.lags:] = pred | |
d['position'] = np.sign(d['prediction']) | |
d['strategy'] = d['position'] * d['returns'] | |
title = '%s | %s | lags = %d' % (self.symbol, self.model, self.lags) | |
d[['returns', 'strategy']].ix[self.lags:].cumsum().apply(np.exp).plot(title=title) |
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 json | |
import datetime | |
import plotly.plotly as ply | |
from plotly.graph_objs import * | |
# ZeroMQ | |
context = zmq.Context() | |
socket = context.socket(zmq.SUB) | |
socket.connect('tcp://0.0.0.0:6666') | |
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL') | |
# Plotly | |
pc = json.load(open('creds/plotly_creds.json', 'r')) | |
ply.sign_in(pc['username'], pc['api_key']) | |
stream0 = Stream(maxpoints=100, token=pc['stream_ids'][0]) | |
trace0 = Scatter(x=[], y=[], stream=stream0, name='AAPL', mode='lines+markers') | |
dat0 = Data([trace0]) | |
layout = Layout(title='Streaming Plot') | |
fig = Figure(data=dat0, layout=layout) | |
ply.plot(fig, filename='qi_bootcamp', auto_open=True) | |
s0 = ply.Stream(pc['stream_ids'][0]) | |
s0.open() | |
while True: | |
data = socket.recv_string() | |
sym, value = data.split() | |
x = str(datetime.datetime.now())[11:-4] | |
y = float(value) | |
print(x + ' | ' + data) | |
s0.write({'x': x, 'y': y}) |
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 json | |
import datetime | |
import pandas as pd | |
import plotly.plotly as ply | |
from plotly.graph_objs import * | |
# ZeroMQ | |
context = zmq.Context() | |
socket = context.socket(zmq.SUB) | |
socket.connect('tcp://0.0.0.0:6666') | |
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL') | |
# Plotly | |
pc = json.load(open('creds/plotly_creds.json', 'r')) | |
ply.sign_in(pc['username'], pc['api_key']) | |
stream0 = Stream(maxpoints=100, token=pc['stream_ids'][0]) | |
stream1 = Stream(maxpoints=100, token=pc['stream_ids'][1]) | |
stream2 = Stream(maxpoints=100, token=pc['stream_ids'][2]) | |
trace0 = Scatter(x=[], y=[], stream=stream0, name='AAPL', mode='lines+markers') | |
trace1 = Scatter(x=[], y=[], stream=stream1, name='trend 1', mode='lines') | |
trace2 = Scatter(x=[], y=[], stream=stream2, name='trend 2', mode='lines') | |
dat0 = Data([trace0, trace1, trace2]) | |
layout = Layout(title='Streaming Plot') | |
fig = Figure(data=dat0, layout=layout) | |
ply.plot(fig, filename='qi_bootcamp', auto_open=True) | |
s0 = ply.Stream(pc['stream_ids'][0]) | |
s1 = ply.Stream(pc['stream_ids'][1]) | |
s2 = ply.Stream(pc['stream_ids'][2]) | |
s0.open() | |
s1.open() | |
s2.open() | |
df = pd.DataFrame() | |
while True: | |
data = socket.recv_string() | |
sym, value = data.split() | |
x = str(datetime.datetime.now())[11:-4] | |
y = float(value) | |
df = df.append(pd.DataFrame({sym: y}, index=[x])) | |
print(x + ' | ' + data) | |
s0.write({'x': x, 'y': y}) | |
if len(df) >= 10: | |
df['t1'] = df[sym].rolling(5).mean() | |
s1.write({'x': x, 'y': df.t1.ix[-1]}) | |
df['t2'] = df[sym].rolling(10).mean() | |
s2.write({'x': x, 'y': df.t2.ix[-1]}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment