Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active October 3, 2017 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yhilpisch/64aeedbeb334c89aa05529a79881ceeb to your computer and use it in GitHub Desktop.
Save yhilpisch/64aeedbeb334c89aa05529a79881ceeb to your computer and use it in GitHub Desktop.
Gist with additional files from For Python Quants Bootcamp, May 2017, New York City
3 + 4
3 * 4
3 / 4
type(3)
type(4)
3 ** 4
sqrt(3)
3 ** 0.5
import math
math.sqrt(3)
print("Python.")
a = 3
b = 0.75
c = 'Python.'
d = "He said:'I am late.'"
d
d = "He said:"I am late.""
d = 'He said:"I am late."'
d
a
print(a)
a
b
a * b
a ** b
d
2 * d
d + d
d + d * 2
d / d
d / 2
d
d[0]
d[1]
len(d)
d[20]
d[19]
d[-1]
d[-2]
d[-20]
d[-21]
d[2]
d[:2]
d[:2] + d[2:]
d[2:]
d[2:7]
d[2:7:2]
d[::2]
d[::-1]
range(10)
type(range(10))
for i in range(10):
print('For Python Quants')
for i in range(10):
print(i)
for i in range(10):
print(i ** 2)
%magic
%lsmagic
%hist
%hist?
%history?
len?
for i in range(10):
print(d[i])
for c in d:
print(c)
for _ in d:
print(_)
c
for _ in d:
print(_, end='')
for _ in d:
print(_, end='|')
for x in range(10):
print(x)
for x in range(10):
print(x ** 2)
l = [x for x in range(10)]
l
l = [x ** 2 for x in range(10)]
l
type(l)
l2 = [x ** 2 for x in range(10) if x > 2]
l2
l2 = [x ** 2 for x in range(10) if (x > 2) and (x < 8)]
l2
l[0]
l[:5]
l[5:]
l[::-1]
l = [x ** 2 for x in range(10)]
10 % 2
11 % 2
l = [x ** 2 for x in range(10) if x % 2 == 0]
l
l = [x for x in range(20) if x % 2 == 0]
l
for x in range(20):
for y in range(10, 50):
if x % 2 == 0:
# then do something
pass
def f(x):
return x ** 2
f
f(10)
f(10.5)
l = [f(x) for x in range(20) if x % 2 == 0]
l
l3 = [5, 'fpq', a, l]
l3
l3.append('this is new')
l3
l3.append(f)
l3
l3[-1](5)
l.append('new')
l
l3
l
l3
def is_prime(I):
for i in range(2, I):
if I % i == 0:
return False
return True
is_prime(8)
is_prime(10)
is_prime(11)
is_prime(13)
l = [is_prime(x) for x in range(2, 101)]
l
l = [is_prime(x) for x in range(2, 20)]
l
class MyClass(object):
pass
class my_class(object):
pass
int(Ture)
int(True)
int(False)
while True:
print('hi')
while 2:
print('hi')
2 == 2
True == 2
True == 1
def is_prime_2(I):
for i in range(2, I ** 0.5):
if I % i == 0:
return False
return True
is_prime_2(10)
def is_prime_2(I):
for i in range(2, int(I ** 0.5)):
if I % i == 0:
return False
return True
int(2.3)
int(2.7)
def is_prime_2(I):
for i in range(2, int(I ** 0.5) + 1):
if I % i == 0:
return False
return True
is_prime_2(10)
is_prime_2(11)
%ed
p1 = int(1e8 + 1)
p2 = int(1e8 + 3)
p1
p2
is_prime(p1)
is_prime(p2)
p2 = 2** 17 − 1
p2 = 2 ** 17 - 1
p2
p2 = 2 ** 31 - 1
p2
%time is_prime(p1)
%time is_prime(p2)
p2 = 2 ** 17 - 1
%time is_prime(p2)
%time is_prime_2(p2)
%time is_prime_2(int(2**31 - 1))
def is_prime_3(I):
if I % 2 == 0:
return False
for i in range(3, int(I ** 0.5) + 1, 2):
if I % i == 0:
return False
return True
%time is_prime_3(int(2**31 - 1))
%ed is_prime_3
%ed -p
from math import sqrt
sqrt(4)
ls
cd ..
ls
cd bc
!mkdir bc
cd bc/
%hist -f bc_day_1_section_02
Display the source blob
Display the rendered blob
Raw
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
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
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
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
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"http://hilpisch.com/tpq_logo.png\" width=\"350px\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# For Python Quants Bootcamp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Day 4**\n",
"\n",
"Yves Hilpisch\n",
"\n",
"The Python Quants GmbH"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" conda install scikit-learn\n",
" pip install tensorflow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple Classification"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"i = np.array([0, 1, 1.5, 2, 2.5, 3, 4, 5.5, 6, 7])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"o = np.array([0, 0, 0, 0, 1, 1, 0, 1, 1, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### OLS Regression"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"reg = np.polyfit(i, o, deg=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"reg"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pred = np.polyval(reg, i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pred"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pylab import plt\n",
"plt.style.use('seaborn')\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plt.plot(i, o, 'ro')\n",
"plt.plot(i, pred, 'm')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logistic Regression"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from sklearn import linear_model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"log_reg = linear_model.LogisticRegression()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"iT = i.reshape(1, -1).T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"log_reg.fit(iT, o)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pred = log_reg.predict(iT)\n",
"pred"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"prob = log_reg.predict_proba(iT)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plt.figure(figsize=(10, 6))\n",
"plt.plot(i, o, 'ro', label='data')\n",
"plt.plot(i, pred, 'b', label='prediction')\n",
"plt.plot(i, prob, 'm--', label='probability')\n",
"plt.legend(loc=0);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deep Learning"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"tf.logging.set_verbosity(tf.logging.ERROR)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fc = [tf.contrib.layers.real_valued_column('i', dimension=1)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = tf.contrib.learn.DNNClassifier(hidden_units=[50, 50],\n",
" feature_columns=fc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def get_data():\n",
" fc = ({'i': tf.constant(i)})\n",
" la = tf.constant(o, shape=(len(o), 1))\n",
" return fc, la"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"get_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.fit(input_fn=get_data, steps=50)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pred = list(model.predict(input_fn=get_data))\n",
"pred"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.evaluate(input_fn=get_data, steps=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plt.figure(figsize=(10, 6))\n",
"plt.plot(i, o, 'ro', label='data')\n",
"plt.plot(i, pred, 'b', label='prediction')\n",
"plt.legend(loc=0);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Stock Market Prediction"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Preparing Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pandas_datareader import data as web"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data = pd.DataFrame(web.DataReader('^GSPC', data_source='yahoo')['Adj Close'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data.columns = ['prices']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data['returns'] = np.log(data / data.shift(1))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lags = 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cols = []\n",
"for lag in range(1, lags+1):\n",
" col = 'ret_%s' % lag\n",
" data[col] = data['returns'].shift(lag)\n",
" cols.append(col)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data.dropna(inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logistic Regression"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lm = linear_model.LogisticRegression(C=1e6)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cols"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lm.fit(data[cols], np.sign(data['returns']))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data['log_pred'] = lm.predict(data[cols])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data.tail()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data['log_strategy'] = data['returns'] * data['log_pred']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data[['returns', 'log_strategy']].cumsum().apply(np.exp).plot(figsize=(10, 6))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## TensorFlow"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fc = tf.contrib.layers.real_valued_column('returns', dimension=lags)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"mean = data['returns'].mean()\n",
"mean"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"std = data['returns'].std()\n",
"std"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"fcb = [tf.contrib.layers.bucketized_column(fc,\n",
" boundaries=[mean-std, mean, mean+std])]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = tf.contrib.learn.DNNClassifier(hidden_units=[100, 100],\n",
" feature_columns=fcb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"(data['returns'] > 0).astype(int).values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def get_data():\n",
" fc = {'returns': tf.constant(data[cols].values)}\n",
" la = tf.constant((data['returns'] > 0).astype(int).values,\n",
" shape=[len(data), 1])\n",
" return fc, la"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"get_data()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.fit(input_fn=get_data, steps=100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model.evaluate(input_fn=get_data, steps=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"list(model.predict(input_fn=get_data))[:10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data['dnn_pred'] = np.where(np.array(list(model.predict(input_fn=get_data))) > 0,\n",
" 1, -1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data['dnn_strategy'] = data['dnn_pred'] * data['returns']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data[['returns', 'log_strategy', 'dnn_strategy']].cumsum(\n",
" ).apply(np.exp).plot(figsize=(10, 6));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotly"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import plotly"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"plotly.tools.set_credentials_file(username='yves', api_key='lr1c37zw81',\n",
" stream_ids=['xyz', 'abc'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Oanda"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### First Steps "
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"!pip install v20"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"# if oandapyV20\n",
"# import oandapyV20 as v20"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"!pip install pyyaml"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from tpqoa import tpqoa"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"from pylab import plt\n",
"plt.style.use('seaborn')\n",
"import cufflinks\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"oanda = tpqoa('../code/pyalgo.cfg')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# \"older\" version needed here\n",
"# http://hilpisch.com/v20.zip\n",
"# download to current working folder and unzip there\n",
"import v20"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v20/__init__.py v20/order.py v20/response.py\r\n",
"v20/account.py v20/position.py v20/spec_properties.py\r\n",
"v20/base_entity.py v20/pricing.py v20/trade.py\r\n",
"v20/errors.py v20/primitives.py v20/transaction.py\r\n",
"v20/instrument.py v20/request.py v20/user.py\r\n",
"\r\n",
"v20/__pycache__:\r\n",
"__init__.cpython-36.pyc primitives.cpython-36.pyc\r\n",
"account.cpython-36.pyc request.cpython-36.pyc\r\n",
"base_entity.cpython-36.pyc response.cpython-36.pyc\r\n",
"errors.cpython-36.pyc spec_properties.cpython-36.pyc\r\n",
"instrument.cpython-36.pyc trade.cpython-36.pyc\r\n",
"order.cpython-36.pyc transaction.cpython-36.pyc\r\n",
"position.cpython-36.pyc user.cpython-36.pyc\r\n",
"pricing.cpython-36.pyc\r\n"
]
}
],
"source": [
"ls v20/*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# v20?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# !cat tpqoa.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"data = oanda.get_history('SPX500_USD', '2017-5-9', '2017-5-10', 'S5', 'A')"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"oanda.get_history?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data['c'].plot(figsize=(10, 6))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"oanda.get_instruments()[:5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"oanda.stream_data('EUR_USD', stop=10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# oanda.on_success??"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Streamer(tpqoa):\n",
" def __init__(self, conf_file):\n",
" tpqoa.__init__(self, conf_file)\n",
" self.data = pd.DataFrame()\n",
" self.instrument = 'USD_CAD'\n",
" self.units = 1000000\n",
" self.position = 0\n",
" def on_success(self, time, bid, ask):\n",
" print('BID %s | ASK %s' % (bid, ask))\n",
" self.data = self.data.append(\n",
" pd.DataFrame({'bid': bid, 'ask': ask},\n",
" index=[pd.Timestamp(time[:-7])]))\n",
" self.data['mid'] = (self.data['ask'] +\n",
" self.data['bid']) / 2\n",
" self.data['SMA1'] = self.data['mid'].rolling(5).mean()\n",
" self.data['SMA2'] = self.data['mid'].rolling(10).mean()\n",
" if len(self.data) >= 10:\n",
" if self.data['SMA1'].ix[-1] > self.data['SMA2'].ix[-1]:\n",
" if self.position == 0:\n",
" self.create_order(self.instrument, self.units)\n",
" self.position = 1\n",
" if self.data['SMA1'].ix[-1] < self.data['SMA2'].ix[-1]:\n",
" if self.position == 1:\n",
" self.create_order(self.instrument, -1 * self.units)\n",
" self.position = 0\n",
" # self.data.index = pd.DatetimeIndex(self.data.index)\n",
" print(75 * '=')\n",
" print(self.data.tail())\n",
" print(2 * '\\n')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"s = Streamer('../code/pyalgo.cfg')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.data.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s.stream_data('USD_CAD')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" s.create_order(instrument='DE30_EUR',\n",
" units=-100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"http://hilpisch.com/tpq_logo.png\" width=\"350px\">"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
#
# 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_string(zmq.SUBSCRIBE, '')
while True:
msg = socket.recv_string()
t = datetime.datetime.now()
print(str(t) + ' | ' + msg)
#
# Tick Data Client
# with ZeroMQ
#
import zmq
import datetime
import plotly.plotly as ply
import plotly.tools as tls
from plotly.graph_objs import *
stream_ids = tls.get_credentials_file()['stream_ids']
# socket
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://127.0.0.1:5555')
socket.setsockopt_string(zmq.SUBSCRIBE, '')
# plotting
s = Stream(maxpoints=100, token=stream_ids[0])
t = Scatter(x=[], y=[], name='tick data', mode='lines+markers', stream=s)
d = Data([t])
l = Layout(title='Bootcamp Tick Data')
f = Figure(data=d, layout=l)
ply.plot(f, filename='fpq_bootcamp', auto_open=True)
st = ply.Stream(stream_ids[0])
st.open()
while True:
msg = socket.recv_string()
t = datetime.datetime.now()
sym, value = msg.split()
print(str(t) + ' | ' + msg)
st.write({'x': t, 'y': float(value)})
#
# 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')
AMZN = 100.
while True:
AMZN += random.gauss(0, 1) * 0.5
msg = 'AMZN %s' % AMZN
socket.send_string(msg)
print(msg)
time.sleep(random.random() * 2)
#
# tpqoa is a wrapper class for the
# Oanda v20 API (RESTful & streaming)
# (c) Dr. Yves J. Hilpisch
# The Python Quants GmbH
#
import v20
import pandas as pd
import datetime as dt
import configparser
class tpqoa(object):
''' tpqoa is a Python wrapper class for the Oanda v20 API. '''
def __init__(self, conf_file):
''' Init function expecting a configuration file with
the following content:
[oanda_v20]
account_id = XYZ-ABC-...
access_token = ZYXCAB...
Parameters
==========
conf_file: string
path to and filename of the configuration file, e.g. '/home/me/oanda.cfg'
'''
self.config = configparser.ConfigParser()
self.config.read(conf_file)
self.access_token = self.config['oanda_v20']['access_token']
self.account_id = self.config['oanda_v20']['account_id']
self.ctx = v20.Context(
hostname='api-fxpractice.oanda.com',
port=443,
ssl=True,
application='sample_code',
token=self.access_token,
datetime_format='RFC3339')
self.ctx_stream = v20.Context(
hostname='stream-fxpractice.oanda.com',
port=443,
ssl=True,
application='sample_code',
token=self.access_token,
datetime_format='RFC3339'
)
self.suffix = '.000000000Z'
def get_instruments(self):
''' Retrieves and returns all instruments for the given account. '''
resp = self.ctx.account.instruments(self.account_id)
instruments = resp.get('instruments')
instruments = [ins.dict() for ins in instruments]
instruments = [(ins['displayName'], ins['name'])
for ins in instruments]
return instruments
def transform_datetime(self, dt):
''' Transforms Python datetime object to string. '''
if isinstance(dt, str):
dt = pd.Timestamp(dt).to_pydatetime()
return dt.isoformat('T') + self.suffix
def get_history(self, instrument, start, end,
granularity, price):
''' Retrieves historical data for instrument.
Parameters
==========
instrument: string
valid instrument name
start, end: datetime, str
Python datetime or string objects for start and end
granularity: string
a string like 'S5', 'M1' or 'D'
price: string
one of 'A' (ask) or 'B' (bid)
Returns
=======
data: pd.DataFrame
pandas DataFrame object with data
'''
start = self.transform_datetime(start)
end = self.transform_datetime(end)
raw = self.ctx.instrument.candles(
instrument=instrument,
fromTime=start, toTime=end,
granularity=granularity, price=price)
raw = raw.get('candles')
raw = [cs.dict() for cs in raw]
for cs in raw:
cs.update(cs['ask'])
del cs['ask']
if len(raw) == 0:
return 'No data available.'
data = pd.DataFrame(raw)
data['time'] = pd.to_datetime(data['time'])
data = data.set_index('time')
data.index = pd.DatetimeIndex(data.index)
for col in list('ohlc'):
data[col] = data[col].astype(float)
return data
def create_order(self, instrument, units):
''' Places order with Oanda.
Parameters
==========
instrument: string
valid instrument name
units: int
number of units of instrument to be bought (positive int, eg 'units=50')
or to be sold (negative int, eg 'units=-100')
'''
request = self.ctx.order.market(
self.account_id,
instrument=instrument,
units=units,
)
order = request.get('orderFillTransaction')
print('\n\n', order.dict(), '\n')
def stream_data(self, instrument, stop=None):
''' Starts a real-time data stream.
Parameters
==========
instrument: string
valid instrument name
'''
self.stream_instrument = instrument
self.ticks = 0
response = self.ctx_stream.pricing.stream(
self.account_id, snapshot=True,
instruments=instrument)
for msg_type, msg in response.parts():
# print(msg_type, msg)
if msg_type == 'pricing.Price':
self.ticks +=1
self.on_success(msg.time,
float(msg.bids[0].price),
float(msg.asks[0].price))
if stop is not None:
if self.ticks >= stop:
break
def on_success(self, time, bid, ask):
''' Method called when new data is retrieved. '''
print(time, bid, ask)
def get_account_summary(self, detailed=False):
''' Returns summary data for Oanda account.'''
if detailed is True:
response = self.ctx.account.get(self.account_id)
else:
response = self.ctx.account.summary(self.account_id)
raw = response.get('account')
return raw.dict()
def get_transactions(self, tid=0):
''' Retrieves and returns transactions data. '''
response = self.ctx.transaction.since(self.account_id, id=tid)
transactions = response.get('transactions')
transactions = [t.dict() for t in transactions]
return transactions
def print_transactions(self, tid=0):
''' Prints basic transactions data. '''
transactions = self.get_transactions(tid)
for trans in transactions:
templ = '%5s | %s | %9s | %12s'
print(templ % (trans['id'],
trans['time'],
trans['instrument'],
trans['units']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment