aboutsummaryrefslogtreecommitdiffstats
path: root/risk_insight/tranches_insight.py
diff options
context:
space:
mode:
Diffstat (limited to 'risk_insight/tranches_insight.py')
-rw-r--r--risk_insight/tranches_insight.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/risk_insight/tranches_insight.py b/risk_insight/tranches_insight.py
new file mode 100644
index 00000000..5ffb0383
--- /dev/null
+++ b/risk_insight/tranches_insight.py
@@ -0,0 +1,61 @@
+from flask import Flask, render_template, jsonify, request, g
+import os, csv, yaml
+import datetime
+import psycopg2
+import pdb
+
+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_risk_number():
+ index = request.args.get("i")
+ series = request.args.get("s", 0, int)
+ tenor = request.args.get("t")
+ greek = request.args.get("g")
+ data = []
+ db = get_db()
+ attach = get_attach_from_name(index, series)
+ sqlstr = "SELECT date, \"{0}\" from risk_numbers WHERE index=%s and series=%s" \
+ " and tenor=%s ORDER BY date".format(greek)
+ with db.cursor() as c:
+ c.execute(sqlstr, (index.upper(), series, tenor))
+ data = [["%s/%s/%s" % (date.year, date.month, date.day)] + val for date, val in c]
+ return jsonify(labels=["Date"] + ["{0}-{1} {2}".format(l, u, greek) for l, u in zip(attach[:-1], attach[1:])],
+ data=data)
+
+@app.route("/")
+def main():
+ with open(os.path.join(basedir, "code", "etc", "runs.yml")) as fh:
+ runs = yaml.load(fh)
+ series_list = sorted(list(set([int(name[2:]) for name in runs['name']])))
+ return render_template("index.html", series=series_list)
+
+if __name__ == "__main__":
+ app.run(debug=True)