Python for Finance: Analyze Big Financial Data

(Elle) #1
In  [ 86 ]: sr.disc_list
Out[86]: array([[ 1. , 0.97530991, 0.95122942, 0.92774349, 0.90483742
]])

Cash Flow Series Class with GUI


The last example in this section is about the cash_flow_series class. In principle, we


have seen in the previous example the basic workings of traits when it comes to


presenting results within a GUI window. Here, we only want to add some twists to the


story: for example, a slider to easily change the value for the short rate. In the class


definition that follows, this is accomplished by using the Range function, where we


provide a minimum, a maximum, and a default value. There are also more output fields to


account for the calculation of the present values and the net present value:


In  [ 87 ]: class cash_flow_series(trapi.HasTraits):
name = trapi.Str
short_rate = trapi.Range(0.0, 0.5, 0.05)
time_list = trapi.Array(dtype=np.float, shape=( 1 , 6 ))
cash_flows = trapi.Array(dtype=np.float, shape=( 1 , 6 ))
disc_values = trapi.Array(dtype=np.float, shape=( 1 , 6 ))
present_values = trapi.Array(dtype=np.float, shape=( 1 , 6 ))
net_present_value = trapi.Float
update = trapi.Button
def _update_fired(self):
self.disc_values = np.exp(-self.short_rate * self.time_list)
self.present_values = self.disc_values * self.cash_flows
self.net_present_value = np.sum(self.present_values)
v = trui.View(trui.Group(trui.Item(name = ‘name’),
trui.Item(name=‘short_rate’),
trui.Item(name=‘time_list’, label=‘Time List’),
trui.Item(name=‘cash_flows’, label=‘Cash Flows’),
trui.Item(‘update’, show_label=False),
trui.Item(name=‘disc_values’,
label=‘Discount Factors’),
trui.Item(name=‘present_values’,
label=‘Present Values’),
trui.Item(name=‘net_present_value’,
label=‘Net Present Value’),
show_border=True, label=‘Calculate Present Values’),
buttons = [trui.OKButton, trui.CancelButton],
resizable = True)

Apart from the slightly more complex class definition, the usage is still the same:


In  [ 88 ]: cfs =   cash_flow_series()
In [ 89 ]: cfs.configure_traits()

Figure 13-8 shows the new GUI without any actions taken so far (i.e., empty). Notice the


slider and all the new fields for the cash flow values, the present values, and the net


present value.


Figure 13-9 shows a version of the GUI where input data has been typed in already, but no


other action has taken place.


Finally, Figure 13-10 presents the GUI with both input data and results data — i.e., after


pushing the Update button. Although this is still quite a simple example, the result can


almost be considered an application. We have:


Input


The GUI allows for inputting data to initialize all object attributes.


Logic


There is application logic that calculates discount factors, present values, and an

Free download pdf