/
www.py
172 lines (130 loc) · 4.71 KB
/
www.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
import json
import os
from datetime import timedelta
from collections import namedtuple
import tornado.ioloop
import tornado.web
import asyncmongo
from rates import getZeroData
from calcs import interpolate, makeForward, makeProb, makeFutureCurve
Cache = namedtuple('Cache', ['rates', 'updated'])
cache = Cache(rates=None, updated=None)
def mapzip(f, a):
ret = []
for x in a:
y = f(x)
if y is not None:
ret.append((x, y))
return ret
class RatesHandler(tornado.web.RequestHandler):
def get(self):
self.write(json.dumps(cache.rates))
class ForwardRateHandler(tornado.web.RequestHandler):
def get(self, months):
months = int(months)
lastMonth = cache.rates[-1][0]
if months >= lastMonth:
return self.write("")
irates = interpolate(cache.rates)
forward = makeForward(months, irates)
x = range(1, lastMonth - months + 1)
ret = mapzip(forward, x)
self.write(json.dumps(ret))
class FutureRatesHandler(tornado.web.RequestHandler):
def get(self, months):
months = int(months)
lastMonth = cache.rates[-1][0]
if months >= lastMonth:
return self.write("")
irates = interpolate(cache.rates)
forward = makeFutureCurve(months, irates)
x = range(1, lastMonth - months + 1)
ret = mapzip(forward, x)
self.write(json.dumps(ret))
class ProbHandler(tornado.web.RequestHandler):
def get(self):
YEARS = 7
PROB_SCALE = 5
irates = interpolate(cache.rates)
forward = makeForward(YEARS*12, irates)
prob = makeProb(5, forward)
lastMonth = cache.rates[-1][0]
x = range(1, lastMonth - YEARS*12 + 1)
ret = mapzip(prob, x)
self.write(json.dumps(ret))
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html",
title="Will the tech bubble pop?",
updated=cache.updated,
api="pop_probability")
class HowHandler(tornado.web.RequestHandler):
def get(self):
self.render("how.html",
title="How I got these numbers :)",
updated=cache.updated)
class ZeroCurveHandler(tornado.web.RequestHandler):
def get(self):
self.render("zero.html",
title="Zero Coupon Yield Curve",
updated=cache.updated,
api="zero_coupon_rates")
class ForwardCurveHandler(tornado.web.RequestHandler):
def get(self):
self.render("forward.html",
title="Forward Yield Curve",
updated=cache.updated,
api="forward_rates/84")
class FutureCurveHandler(tornado.web.RequestHandler):
def get(self):
self.render("future.html",
title="Future Yield Curve",
updated=cache.updated,
api="future_rates/12")
application = tornado.web.Application(
[
(r"/", IndexHandler),
(r"/how", HowHandler),
(r"/zero_coupon_curve", ZeroCurveHandler),
(r"/forward_curve", ForwardCurveHandler),
(r"/future_curve", FutureCurveHandler),
(r"/api/zero_coupon_rates", RatesHandler),
(r"/api/forward_rates/(\d+)", ForwardRateHandler),
(r"/api/future_rates/(\d+)", FutureRatesHandler),
(r"/api/pop_probability", ProbHandler),
],
static_path=os.path.join(os.path.dirname(__file__), "static"),
template_path=os.path.join(os.path.dirname(__file__), "templates"),
)
def update_cache(cb=None):
td = timedelta(minutes=30)
tornado.ioloop.IOLoop.instance().add_timeout(td, update_cache)
def handle_resp(data, update_date):
if data is None:
return
global cache
db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='interest_rates_hist')
def mongo_find_resp(response, error):
if error:
print error
return
# do nothing if data already exists for this time
if response:
return
def check_output(response, error):
if error:
print error
db.zero_coupon.insert({
'time': update_date,
'data': data
}, callback=check_output)
db.zero_coupon.find({'time': update_date}, callback=mongo_find_resp)
cache = Cache(rates=data, updated=update_date)
if cb:
cb()
getZeroData(handle_resp)
if __name__ == "__main__":
def listen():
application.listen(3001)
update_cache(listen)
tornado.ioloop.IOLoop.instance().start()