Skip to content

Instantly share code, notes, and snippets.

@yhilpisch
Last active January 20, 2017 21:07
Show Gist options
  • Save yhilpisch/dfaefc5f4f3ee53cdc034915f81e1a51 to your computer and use it in GitHub Desktop.
Save yhilpisch/dfaefc5f4f3ee53cdc034915f81e1a51 to your computer and use it in GitHub Desktop.
Introductory & Technical Bootcamp @ For Python Quants London 2016
WIFI
====
Ft1ch#2016#!
Quant Platform
==============
http://fpqlon.pqp.io
(follow registration link)
Miniconda
=========
http://conda.pydata.org/miniconda.html
http://conda.pydata.org/docs/using/envs.html
Environment
===========
conda create -n introbc python=3.5
source activate introbc (Linux/Mac)
activate introbc (Windows)
conda install ipython
conda install numpy
conda install matplotlib
conda install pandas-datareader
conda install seaborn
ipython
[1]: print('python')
import pandas as pd
ws = 'http://0.0.0.0:5555/?symbol=%s&start=%s&end=%s&format=%s'
def get_data(symbol, start, end):
req = ws % (symbol, start, end, 'json')
df = pd.read_json(req)
return df
if __name__ == '__main__':
AAPL = get_data('AAPL', start='2016-1-1', end='2016-6-30')
print(AAPL.tail())
#
# Bits and Pieces for
# Mean-Variance Portfolio Example
#
# The Python Quants GmbH
#
import numpy as np
import pandas as pd
from pandas_datareader import data as web
import matplotlib.pyplot as plt
symbols = ['AAPL', 'MSFT', 'YHOO']
data = pd.DataFrame()
for sym in symbols:
data[sym] = web.DataReader(sym, data_source='yahoo')['Adj Close']
rets = np.log(data / data.shift(1))
def pr(weights):
return np.dot(weights, rets.mean() * 252)
def pv(weights):
return np.sqrt(np.dot(weights.T, np.dot(rets.cov() * 252, weights)))
if __name__ == '__main__':
prets = []
pvols = []
for _ in range(2000):
weights = np.random.random(len(symbols))
weights /= weights.sum()
prets.append(pr(weights))
pvols.append(pv(weights))
print(prets[:10])
print(pvols[:10])
plt.figure(figsize=(10, 6))
plt.plot(pvols, prets, 'r.')
plt.grid(True)
plt.show()
Class Sex Age Survived Freq
1 1st Male Child No 0
2 2nd Male Child No 0
3 3rd Male Child No 35
4 Crew Male Child No 0
5 1st Female Child No 0
6 2nd Female Child No 0
7 3rd Female Child No 17
8 Crew Female Child No 0
9 1st Male Adult No 118
10 2nd Male Adult No 154
11 3rd Male Adult No 387
12 Crew Male Adult No 670
13 1st Female Adult No 4
14 2nd Female Adult No 13
15 3rd Female Adult No 89
16 Crew Female Adult No 3
17 1st Male Child Yes 5
18 2nd Male Child Yes 11
19 3rd Male Child Yes 13
20 Crew Male Child Yes 0
21 1st Female Child Yes 1
22 2nd Female Child Yes 13
23 3rd Female Child Yes 14
24 Crew Female Child Yes 0
25 1st Male Adult Yes 57
26 2nd Male Adult Yes 14
27 3rd Male Adult Yes 75
28 Crew Male Adult Yes 192
29 1st Female Adult Yes 140
30 2nd Female Adult Yes 80
31 3rd Female Adult Yes 76
32 Crew Female Adult Yes 20
#
# Flask Web Service for
# Financial Data with Python & pandas
#
# The Python Quants GmbH
#
from flask import Flask, request
from pandas_datareader import data as web
app = Flask(__name__)
@app.route('/')
def home():
symbol = request.args['symbol']
start = request.args['start']
end = request.args['end']
try:
out = request.args['format']
except:
out = 'html'
data = web.DataReader(symbol, start=start, end=end, data_source='yahoo')
if out == 'csv':
return data.to_csv()
elif out == 'json':
return data.to_json()
else:
return data.to_html()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5555, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment