Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active October 2, 2017 18:28
Show Gist options
  • Save yhilpisch/069fead6d3ed08e93a34b05f07fcd5c2 to your computer and use it in GitHub Desktop.
Save yhilpisch/069fead6d3ed08e93a34b05f07fcd5c2 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, 03. & 04. June 2017

Resources

Slides

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

Python

Download and install Miniconda 2.7 from https://conda.io/miniconda.html

If you have either Miniconda or Anaconda already installed, there is not need to install anything new.

conda create -n epat python=2.7
(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

Books

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.
#
# Tick Data 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(zmq.SUBSCRIBE, '')
while True:
msg = socket.recv_string()
t = datetime.datetime.now()
print(str(t) + ' | ' + msg)
#
# Tick Data 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(zmq.SUBSCRIBE, '')
df = pd.DataFrame()
i = 0
while True:
i += 1
msg = socket.recv_string()
sym, value = msg.split()
t = datetime.datetime.now()
print(('%2d' % i) + ' | ' + str(t) + ' | ' + msg)
df = df.append(pd.DataFrame({sym: float(value)}, index=[t]))
if i % 5 == 0:
df['SMA1'] = df[sym].rolling(5).mean()
df['SMA2'] = df[sym].rolling(10).mean()
print(df.tail())
#
# Tick Data Server
# with ZeroMQ
#
import zmq
import random
import time
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 %.3f' % AAPL
print(msg)
socket.send_string(msg)
time.sleep(random.random() * 2)
#
# Tick Data 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(zmq.SUBSCRIBE, '')
df = pd.DataFrame()
i = 0
position = 0
while True:
i += 1
msg = socket.recv_string()
sym, value = msg.split()
t = datetime.datetime.now()
print(('%2d' % i) + ' | ' + str(t) + ' | ' + msg)
df = df.append(pd.DataFrame({sym: float(value)}, index=[t]))
if i >= 10 :
df['SMA1'] = df[sym].rolling(5).mean()
df['SMA2'] = df[sym].rolling(10).mean()
if position == 0:
if df['SMA1'].ix[-1] > df['SMA2'].ix[-1]:
print(50 * '=')
print('WE GO LONG THE MARKET.')
#
# code to connect to your broker
# to place a buy order
#
position = 1
print(df.ix[-1])
print('')
elif position == 1:
if df['SMA1'].ix[-1] < df['SMA2'].ix[-1]:
print(50 * '=')
print('WE EXIT THE MARKET.')
#
# code to connect to your broker
# to place a sell order
#
position = 0
print(df.ix[-1])
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment