-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
100 lines (70 loc) · 2.23 KB
/
server.py
File metadata and controls
100 lines (70 loc) · 2.23 KB
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
###########
# IMPORTS #
###########
import os
import redis
import pandas as pd
from flask import Flask
from flask import request
from flask import jsonify
from flask import abort
from flask_environments import Environments
from flask_sslify import SSLify
from lib.efficient_frontier import efficient_frontier
###############
# ENVIRONMENT #
###############
app = Flask(__name__)
env = Environments(app)
env.from_yaml(os.path.join(os.path.dirname(__file__), 'config', 'config.yml'))
redis_url = os.getenv('REDIS_URL', app.config['REDIS_URL'])
redis_conn = redis.StrictRedis.from_url(redis_url)
###################
# UTILITY METHODS #
###################
def check_for_authorization():
auth_token = os.getenv('AUTH_TOKEN', app.config['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 covariance_matrix(tickers):
# Covariance matrix is a *DataFrame*
json = redis_conn.get('covariance_matrix')
df = pd.io.json.read_json(json)
tickers_set = set(tickers)
available_tickers_set = set(df.index.values)
tickers_to_eliminate = list(available_tickers_set - tickers_set)
return df.drop(tickers_to_eliminate, axis=0).drop(tickers_to_eliminate, axis=1)
def mean_returns(tickers):
# Mean returns is a *Series*
json = redis_conn.get('mean_returns')
df = pd.io.json.read_json(json, typ='series')
tickers_set = set(tickers)
available_tickers_set = set(df.index.values)
tickers_to_eliminate = list(available_tickers_set - tickers_set)
return df.drop(tickers_to_eliminate)
##########
# ROUTES #
##########
@app.route('/')
def root():
return 'Hello World!'
@app.route('/health')
def health():
return "OK", 200
@app.route('/calc', methods=["POST"])
def cla_calc_route():
check_for_authorization()
j = request.json
tickers = j['tickers']
app.logger.info("Received CLA calc request for tickers: %s" % tickers)
means = mean_returns(tickers)
covars = covariance_matrix(tickers)
return jsonify( efficient_frontier(tickers, means, covars) )
##########
# LOADER #
##########
if __name__ == '__main__':
app.run()