-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
250 lines (198 loc) · 8.2 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
###########
# IMPORTS #
###########
from __future__ import absolute_import
import os
import json
import redis
import bmemcached
import hashlib
from flask import Flask
from flask import request
from flask import Response
from flask import jsonify
from flask import abort
from flask_sslify import SSLify
import pandas as pd
from lib.efficient_frontier import efficient_frontier
###############
# ENVIRONMENT #
###############
app = Flask(__name__)
app.config['DEBUG'] = os.environ.get('DEBUG', False)
# http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xviii-deployment-on-the-heroku-cloud
if not app.config['DEBUG']:
import logging
stream_handler = logging.StreamHandler()
app.logger.addHandler(stream_handler)
app.logger.setLevel(logging.INFO)
if not app.config['DEBUG']:
app.logger.info("Forcing SSL")
sslify = SSLify(app)
if app.config['DEBUG']:
cache = bmemcached.Client(servers=['127.0.0.1:11211'])
else:
cache = bmemcached.Client(
servers=os.environ['MEMCACHEDCLOUD_SERVERS'].split(","),
username=os.environ['MEMCACHEDCLOUD_USERNAME'],
password=os.environ['MEMCACHEDCLOUD_PASSWORD']
)
redis_conn = redis.StrictRedis.from_url(os.environ['REDIS_URL'])
###################
# UTILITY METHODS #
###################
def check_for_authorization():
auth_token = os.environ['AUTH_TOKEN']
provided_token = request.headers.get('Authorization') or request.args.get('auth_token')
if (provided_token and provided_token == auth_token):
return True
else:
return abort(403)
def get_key_in_json(key, json):
if json is None:
return abort(400)
if key not in json:
return abort(422)
return json[key]
######################
# APP HELPER METHODS #
######################
def covariance_matrix(asset_ids):
# No need to memoize this unless jsonify is taking lots of time - data from redis
# Covariance matrix is a *DataFrame*
json = redis_conn.get('covariance_matrix')
df = pd.io.json.read_json(json)
asset_ids_set = set(asset_ids)
available_asset_ids_set = set(df.index.values)
asset_ids_to_eliminate = list(available_asset_ids_set - asset_ids_set)
return df.drop(asset_ids_to_eliminate, axis=0).drop(asset_ids_to_eliminate, axis=1)
def cholesky_decomposition(asset_ids):
# No need to memoize this unless jsonify is taking lots of time - data from redis
# Cholesky decomp matrix is a *DataFrame*
json = redis_conn.get('cholesky_decomposition')
df = pd.io.json.read_json(json)
asset_ids_set = set(asset_ids)
available_asset_ids_set = set(df.index.values)
asset_ids_to_eliminate = list(available_asset_ids_set - asset_ids_set)
return df.drop(asset_ids_to_eliminate, axis=0).drop(asset_ids_to_eliminate, axis=1)
def mean_returns(asset_ids, returns_source):
"""
Grabs mean return data, and returns a dataframe with only the relavant columns
:param asset_ids: array of asset ID's (e.g. INTL-STOCK)
:param returns_source: Which data we are interested in - one of: 'reverse_optimized_returns', 'mean_returns', 'five_year_returns'
"""
# No need to memoize this unless jsonify is taking lots of time - data from redis
# Mean returns is a *Series*
json = redis_conn.get(returns_source)
df = pd.io.json.read_json(json, typ='series')
if len(asset_ids) > 0:
asset_ids_set = set(asset_ids)
available_asset_ids_set = set(df.index.values)
asset_ids_to_eliminate = list(available_asset_ids_set - asset_ids_set)
return df.drop(asset_ids_to_eliminate)
else:
return df
def std_dev_returns(asset_ids):
# No need to memoize this unless jsonify is taking lots of time - data from redis
# Mean returns is a *Series*
json = redis_conn.get('std_dev_returns')
df = pd.io.json.read_json(json, typ='series')
if len(asset_ids) > 0:
asset_ids_set = set(asset_ids)
available_asset_ids_set = set(df.index.values)
asset_ids_to_eliminate = list(available_asset_ids_set - asset_ids_set)
return df.drop(asset_ids_to_eliminate)
else:
return df
def build_efficient_frontier_for(asset_ids, use_market_implied_returns=True):
md5Hash = hashlib.md5("-".join(asset_ids)).hexdigest()
cache_key = "efficient_frontier/" + md5Hash + "/" + ("implied" if use_market_implied_returns else "historical")
val = cache.get(cache_key)
if val is None:
app.logger.info("[Cache Miss] Building efficient frontier for: %s" % asset_ids)
if use_market_implied_returns:
asset_returns = mean_returns(asset_ids, returns_source="reverse_optimized_returns")
else:
asset_returns = mean_returns(asset_ids, returns_source="mean_returns")
covars = covariance_matrix(asset_ids)
historical_returns = mean_returns(asset_ids, returns_source="five_year_returns")
frontier = efficient_frontier(asset_ids, asset_returns, historical_returns, covars)
# FIXME: You are json dumping to store in memcache, marshalling to return
# to request, then flask-jsonifying back again. Better way? For later......
cache.set(cache_key, json.dumps(frontier))
else:
app.logger.info("[Cache Hit] Retreiving efficient frontier for: %s" % asset_ids)
# FIXME: You are json dumping to store in memcache, marshalling to return
# to request, then flask-jsonifying back again. Better way? For later......
frontier = json.loads(val)
# app.logger.info("Frontier: %r" % frontier)
return frontier
##########
# ROUTES #
##########
# Utility routes
@app.route('/')
def root():
return 'Hello World!'
@app.route('/health')
def health():
return "OK", 200
@app.route('/clear_cache', methods=["GET"])
def clear_cache():
check_for_authorization()
cache.flush_all()
return jsonify({"success": True, "message": "Cache cleared."})
# App routes
@app.route('/assets', methods=['GET'])
def assets_route():
check_for_authorization()
return jsonify( json.loads(redis_conn.get('asset_list')) )
@app.route('/etfs', methods=['GET'])
def etfs_route():
check_for_authorization()
return jsonify( json.loads(redis_conn.get('etf_list')) )
@app.route('/performance', methods=['GET'])
def performance_route():
check_for_authorization()
asset_ids = get_key_in_json('asset_ids', request.json)
asset_ids.sort()
df = pd.DataFrame()
df['mean'] = mean_returns(asset_ids, returns_source="reverse_optimized_returns")
df['historical_mean'] = mean_returns(asset_ids, returns_source="mean_returns")
df['five_year_mean'] = mean_returns(asset_ids, returns_source="five_year_returns")
df['std_dev'] = std_dev_returns(asset_ids)
return Response(df.transpose().to_json(), mimetype='application/json')
@app.route('/quotes', methods=['GET'])
def quotes_route():
check_for_authorization()
return jsonify( json.loads(redis_conn.get('quotes')) )
@app.route('/inflation', methods=['GET'])
def inflation_route():
check_for_authorization()
return jsonify( json.loads(redis_conn.get('inflation')) )
@app.route('/real_estate', methods=['GET'])
def real_estate_route():
check_for_authorization()
return jsonify( json.loads(redis_conn.get('real_estate')) )
@app.route('/cholesky', methods=['GET'])
def cholesky_route():
check_for_authorization()
asset_ids = get_key_in_json('asset_ids', request.json)
asset_ids.sort()
app.logger.info("Received cholesky request for: %s" % asset_ids)
cholesky_dataframe_as_array = cholesky_decomposition(asset_ids).values
as_flat_array = cholesky_dataframe_as_array.flatten().tolist() # Just do .tolist() if you want it as an array of arrays
return jsonify( { "cholesky_decomposition": as_flat_array } )
@app.route('/efficient_frontier', methods=["GET"])
def efficient_frontier_route():
check_for_authorization()
asset_ids = get_key_in_json('asset_ids', request.json)
asset_ids.sort()
app.logger.info("Received CLA Efficient Frontier request for: %s" % asset_ids)
return jsonify(build_efficient_frontier_for(asset_ids))
##########
# LOADER #
##########
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(port=port)