diff options
| -rw-r--r-- | tranches_insight/static/main.js | 41 | ||||
| -rw-r--r-- | tranches_insight/templates/index.html | 46 | ||||
| -rw-r--r-- | tranches_insight/tranches_insight.py | 18 |
3 files changed, 79 insertions, 26 deletions
diff --git a/tranches_insight/static/main.js b/tranches_insight/static/main.js index 74bc3cdd..b7b2a659 100644 --- a/tranches_insight/static/main.js +++ b/tranches_insight/static/main.js @@ -1,21 +1,46 @@ -g2 = new Dygraph(document.getElementById("graphdiv2"), +g1 = new Dygraph(document.getElementById("graph1"), [], // path to CSV file { labels: [], title: "Correlation numbers" } // options ); +g2 = new Dygraph(document.getElementById("graph2"), + [], // path to CSV file + { labels: [], + title: "Risk numbers" } // options + ); -document.getElementById('dropdown'). - addEventListener('change', function(e) { - var index = this.value.split("."); +function update_graph(g) { + return function(e) { + var payload = {i: document.getElementById('index').value, + s: document.getElementById('series').value, + t: document.getElementById('tenor').value, + g: document.getElementById('greek').value}; + console.log(g); + if( g.maindiv_.id == "graph1"){ + payload['g'] = "skew"; + } $.getJSON("_data", - {i: index[0], s: index[1], t: index[2]}, + payload, function(data){ var newdata = data["data"].map(function(e){ f = e.slice(1); f.unshift(new Date(e[0])); return f}); - g2.updateOptions({'file': newdata, 'labels': data['labels']}); + g.updateOptions({'file': newdata, 'labels': data['labels']}); }); - }); + } +}; + + +document.getElementById('index'). + addEventListener('change', update_graph(g1)); +document.getElementById('series'). + addEventListener('change', update_graph(g1)); +document.getElementById('tenor'). + addEventListener('change', update_graph(g1)); +document.getElementById('greek'). + addEventListener('change', update_graph(g2)); + var event = new Event('change'); -document.getElementById('dropdown').dispatchEvent(event); +document.getElementById('index').dispatchEvent(event); +document.getElementById('greek').dispatchEvent(event); diff --git a/tranches_insight/templates/index.html b/tranches_insight/templates/index.html index d3cd66a3..c58e1d9a 100644 --- a/tranches_insight/templates/index.html +++ b/tranches_insight/templates/index.html @@ -5,18 +5,42 @@ <script type="text/javascript" src="{{ url_for('static', filename='jquery-2.1.1.min.js') }}"></script> </head> <body> -<div id="graphdiv2" - style="width:800px; height:500px;"> -</div> + <div id="graph1" + style="width:800px; height:500px;float:left;margin:auto"> + </div> + <div id="graph2" + style="width:800px; height:500px;float:left;padding-left:5em"> + </div> + <div style="margin-top:5em;display:inline-block"> + <select id="index"> + <option value="ig">IG</option> + <option value="hy">HY</option> + </select> + <select id="series"> + {% for s in series %} + <option value="{{s}}">{{s}}</option> + {% endfor %} + </select> + <select id="tenor"> + <option value="3yr">3yr</option> + <option value="5yr">5yr</option> + <option value="7yr">7yr</option> + <option value="10yr">10yr</option> + </select> + <select id="greek"> + <option value="Dealer Deltas">Dealer Delta</option> + <option value="Model Deltas">Model Delta</option> + <option value="Forward Deltas">Forward Delta</option> + <option value="gammas">Gamma</option> + <option value="thetas">Theta</option> + <option value="corr01">Corr01</option> + <option value="durations">Duration</option> + <option value="el">Expected Loss</option> + </select> + </div> + <div> - <select id="dropdown"> - {% for index in index_list %} - <option value="{{"{0}.{1}.{2}".format(*index)}}" - {% if loop.index==1 %}selected="selected"{% endif %}> - {{"{0}{1} {2}".format(*index)}} - </option> - {% endfor %} - </select> + </div> <script type="text/javascript" src="{{ url_for('static', filename='main.js') }}"></script> </body> diff --git a/tranches_insight/tranches_insight.py b/tranches_insight/tranches_insight.py index 1c6b2991..5ffb0383 100644 --- a/tranches_insight/tranches_insight.py +++ b/tranches_insight/tranches_insight.py @@ -2,6 +2,7 @@ from flask import Flask, render_template, jsonify, request, g import os, csv, yaml import datetime import psycopg2 +import pdb app = Flask(__name__) @@ -33,25 +34,28 @@ def close_connection(exception): db.close() @app.route("/_data") -def get_corr(): +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("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) + 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) - 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) + 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) |
