Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active December 6, 2024 08:03
Show Gist options
  • Save yhilpisch/4ba8d5b3e17d8480e8a1d552c8b4567e to your computer and use it in GitHub Desktop.
Save yhilpisch/4ba8d5b3e17d8480e8a1d552c8b4567e 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, 08. & 09. December 2018

Python for Finance (2nd ed.)

Sign up under http://py4fi.pqp.io to access all the Jupyter Notebooks and codes and execute them on our Quant Platform.

Short Link

http://bit.ly/epat_dec_2018

Resources

Slides & Materials

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

You find the materials about OOP under http://hilpisch.com/py4fi_oop_epat.html

Python

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

The code that follows uses Python 3.6. For example, download and install Miniconda 3.6 from https://conda.io/miniconda.html if you do not have conda already installed.

In any case, for Linux/Mac you should execute the following lines on the shell to create a new environment with the needed packages:

conda create -n epat python=3.6
source activate epat
conda install numpy pandas matplotlib statsmodels
pip install plotly==2.4.0 cufflinks
conda install ipython jupyter
jupyter notebook

On Windows, execute the following lines on the Anaconda prompt:

conda create -n epat python=3.6
activate epat
conda install numpy pandas matplotlib statsmodels
pip install plotly==2.4.0 cufflinks
pip install win-unicode-console
set PYTHONIOENCODING=UTF-8
conda install ipython jupyter
jupyter notebook

Read more about the management of environments under https://conda.io/docs/using/envs.html

Docker

To install Docker see https://docs.docker.com/install/.

To run a Ubuntu-based Docker container, execute on the shell the following:

docker run -ti -p 9000:9000 -h epat -v /Users/yves/Temp/:/root/tmp ubuntu:latest /bin/bash

Make sure to adjust the folder to be mounted accordingly.

ZeroMQ

The major resource for the ZeroMQ distributed messaging package based on sockets is http://zeromq.org/

Cloud

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

Books & Resources

An overview of the Python Data Model is found under: Python Data Model

Good book about everything important 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.
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 Data Client
#
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(str(t.time()) + ' | ' + msg)
#
# Simple Tick Data Collector
#
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')
raw = pd.DataFrame()
while True:
msg = socket.recv_string()
tr = datetime.datetime.now()
print(str(tr.time()) + ' | ' + msg)
sym, price, ts = msg.split()
raw = raw.append(pd.DataFrame(
{'ts': pd.Timestamp(ts),
'sym': sym, 'price': float(price)},
index=[tr,]))
#
# Simple Tick Data Server
#
import zmq
import time
import random
import datetime
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
t = datetime.datetime.now()
msg = 'AAPL {:.3f} {}'.format(AAPL, t.time())
socket.send_string(msg)
print(msg)
time.sleep(random.random() * 2)
#
# Simple Tick Data Collector
#
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')
raw = pd.DataFrame()
SMA1 = 3
SMA2 = 6
min_length = SMA2 + 1
position = 0
while True:
msg = socket.recv_string()
tr = datetime.datetime.now()
print(str(tr.time()) + ' | ' + msg)
sym, price, ts = msg.split()
raw = raw.append(pd.DataFrame(
{'ts': pd.Timestamp(ts),
'sym': sym, 'price': float(price)},
index=[tr,]))
data = raw.resample('5s', label='right').last().ffill()
if len(data) > min_length:
min_length += 1
data['SMA1'] = data['price'].rolling(SMA1).mean()
data['SMA2'] = data['price'].rolling(SMA2).mean()
if data['SMA1'].iloc[-2] > data['SMA2'].iloc[-2]:
print('\n**** GOING/STAYING LONG ****')
elif data['SMA1'].iloc[-2] < data['SMA2'].iloc[-2]:
print('\n**** GOING/STAYING SHORT ****')
print(data[['SMA1', 'SMA2']].tail(), '\n\n')
gist -u 4ba8d5b3e17d8480e8a1d552c8b4567e *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment