Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active September 18, 2022 18:06
Show Gist options
  • Save yhilpisch/ff4722776b0c10ab44e12d8716045795 to your computer and use it in GitHub Desktop.
Save yhilpisch/ff4722776b0c10ab44e12d8716045795 to your computer and use it in GitHub Desktop.

Executive Program in Algorithmic Trading (QuantInsti)

Python Sessions by Dr. Yves J. Hilpisch | The Python Quants GmbH

Online, 26. & 27. August 2017

Resources

Slides

You find the introduction slides under http://hilpisch.com/epat.pdf

Python

We are using Python 3.6. Download and install Miniconda 3.6 from https://conda.io/miniconda.html

If you have either Miniconda or Anaconda already installed, there is no need to install anything new. You should then execute the following lines on the shell/command prompt:

conda create -n epat python=3.6
(source) activate epat
conda install numpy pandas=0.19 scikit-learn matplotlib
conda install pandas-datareader pytables
conda install ipython jupyter
jupyter notebook

Management of environments: https://conda.io/docs/using/envs.html

Cloud

Use this link to get a 10 USD bonus on DigitalOcean when signing up for a new account.

Books

Good book about everything import in Python data analysis: Python Data Science Handbook, O'Reilly

Good book covering object-oriented programming in Python: Fluent Python, O'Reilly

Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#
# Simple Tick Client with ZeroMQ
#
import zmq
import datetime
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://127.0.0.1:5555')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
while True:
msg = socket.recv_string()
t = datetime.datetime.now()
print('%s | ' % str(t), msg)
#
# Simple Tick Collector with ZeroMQ
#
import zmq
import datetime
import pandas as pd
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://127.0.0.1:5555')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
data = pd.DataFrame()
ticks = 0
while ticks < 100:
ticks += 1
msg = socket.recv_string()
t = datetime.datetime.now()
print('%s | ' % str(t), msg)
sym, value = msg.split()
data = data.append(pd.DataFrame({sym: float(value)}, index=[t]))
if ticks % 10 == 0:
h5 = pd.HDFStore('data.h5', 'w')
h5['data'] = data
h5.close()
#
# Simple Tick Data Server with ZeroMQ
#
import zmq
import time
import random
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:5555')
AAPL = 100.
while True:
AAPL += random.gauss(0, 1) * 0.5
msg = 'AAPL %.4f' % AAPL
socket.send_string(msg)
print(msg)
time.sleep(random.random() * 2)
#
# Simple Tick Trader with ZeroMQ
#
import zmq
import datetime
import pandas as pd
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://127.0.0.1:5555')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
data = pd.DataFrame()
ticks = 0
position = 0
while ticks < 200:
ticks += 1
msg = socket.recv_string()
t = datetime.datetime.now()
sym, value = msg.split()
data = data.append(pd.DataFrame({sym: float(value)}, index=[t]))
resam = data.resample('2s', label='right').last()
resam['SMA1'] = resam['AAPL'].rolling(5).mean()
resam['SMA2'] = resam['AAPL'].rolling(10).mean()
resam.dropna(inplace=True)
if len(resam) > 2:
print('%3d | %s | ' % (ticks, str(t)), msg, '| %.3f | %.3f' %
(resam['SMA1'].iloc[-2], resam['SMA2'].iloc[-2]))
if position == 0 and resam['SMA1'].iloc[-2] > resam['SMA2'].iloc[-2]:
print('going long the market')
position = 1
#
# place trading code here (for buy order)
#
elif position == 1 and resam['SMA1'].iloc[-2] < resam['SMA2'].iloc[-2]:
print('going market neutral')
position = 0
#
# place trading code here (for sell order)
#
#if ticks % 10 == 0:
# h5 = pd.HDFStore('data.h5', 'w')
# h5['data'] = data
# h5.close()
#
# Simple Tick Trader LS with ZeroMQ
#
import zmq
import datetime
import pandas as pd
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://127.0.0.1:5555')
socket.setsockopt_string(zmq.SUBSCRIBE, 'AAPL')
data = pd.DataFrame()
ticks = 0
position = 0
while ticks < 200:
ticks += 1
msg = socket.recv_string()
t = datetime.datetime.now()
sym, value = msg.split()
data = data.append(pd.DataFrame({sym: float(value)}, index=[t]))
resam = data.resample('2s', label='right').last()
resam['SMA1'] = resam['AAPL'].rolling(5).mean()
resam['SMA2'] = resam['AAPL'].rolling(10).mean()
resam.dropna(inplace=True)
if len(resam) > 2:
print('%3d | %s | ' % (ticks, str(t)), msg, '| %.3f | %.3f' %
(resam['SMA1'].iloc[-2], resam['SMA2'].iloc[-2]))
if position in [0, -1] and resam['SMA1'].iloc[-2] > resam['SMA2'].iloc[-2]:
print('going long the market')
position = 1
#
# place trading code here (for buy order)
#
elif position in [0, 1] and resam['SMA1'].iloc[-2] < resam['SMA2'].iloc[-2]:
print('going short the market')
position = -1
#
# place trading code here (for sell order)
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment