aboutsummaryrefslogtreecommitdiffstats
path: root/tranches_insight/tranches_insight.py
blob: 1c6b299113eb569b70f478d0a6eeab0c6c87a5b4 (plain)
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
from flask import Flask, render_template, jsonify, request, g
import os, csv, yaml
import datetime
import psycopg2

app = Flask(__name__)

basedir = "/home/share/CorpCDOs"

def get_attach_from_name(index_type, series):
    if index_type.lower() == "ig":
        if series == 9:
            attach = [0, 3, 7, 10, 15, 30, 100]
        else:
            attach = [0, 3, 7, 15, 100]
    elif index_type.lower() == "hy":
        attach = [0, 15, 25, 35, 100]
    return attach

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = psycopg2.connect(database="serenitasdb",
                                            user="serenitas_user",
                                            password="Serenitas1",
                                            host="debian")
    return db

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

@app.route("/_data")
def get_corr():
    index = request.args.get("i")
    series = request.args.get("s", 0, int)
    tenor = request.args.get("t")
    data = []
    db = get_db()
    attach = get_attach_from_name(index, series)
    with db.cursor() as c:
        c.execute("SELECT date, skew from risk_numbers WHERE index=%s and series=%s" \
                  "and tenor=%s ORDER BY date", (index.upper(), series, tenor))
        data = [["%s/%s/%s" % (date.year, date.month, date.day)] + skew[:-1] for date, skew in c]
    return jsonify(labels=["Date"] + ["{0} Corr".format(a) for a in attach[1:-1]], data=data)

@app.route("/")
def main():
    with open(os.path.join(basedir, "code", "etc", "runs.yml")) as fh:
        runs = yaml.load(fh)
    index_list = [[name[:2], name[2:], tenor] for name, tenor in zip(runs['name'], runs['tenor'])]
    return render_template("index.html", index_list=index_list)

if __name__ == "__main__":
    app.run(debug=True)