Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "Summary description for {TWR}."
- author: ""
- date: "$Date$"
- revision: "$Revision$"
- class TWR create make
- feature
- --data--
- twrtransactionlist: LIST[TRANSACTION]
- fulltransactionlist: LIST[TRANSACTION]
- rs: LIST[DOUBLE]
- twrs: LIST[DOUBLE]
- yearlytwrs: LIST[DOUBLE]
- start_date: DATE
- end_date: DATE
- startindex: INTEGER
- endindex: INTEGER
- twrtuple: TUPLE[a_date: DATE; twr : DOUBLE]
- twrdate: LIST[TUPLE[a_date: DATE; twr : DOUBLE]]
- --calculated values--
- compounded_return_value: DOUBLE
- number_of_days: INTEGER
- total_return:DOUBLE
- evalutation_return:DOUBLE
- make(transactionlist: LIST[TRANSACTION]; t_startdate: DATE; t_enddate: DATE)
- do
- start_date:= t_startdate
- end_date:= t_enddate
- number_of_days := transactionlist.last.a_transaction.date.duration.day - transactionlist.first.a_transaction.date.duration.day
- twrtransaction(transactionlist, t_startdate, t_enddate)
- startindex:= getdateindex(transactionlist, start_date)
- endindex:= getdateindex(transactionlist, end_date)
- getAll(transactionlist) --make copy of full transaction list
- twrcalculator --calculates the twr for EVERY transaction
- annum --prints yearly TWR's
- totalreturn --sets the value of total return
- evalreturn --prints twr for eval period
- compounded_return --prints compounded return
- end
- compounded_return
- do
- compounded_return_value := ((1 + (total_return/100)) ^ ( (1/(number_of_days/365.2465))) - 1)* 100
- io.new_line
- end
- evalReturn
- local
- product:DOUBLE
- i:INTEGER
- do
- product := 1
- from
- i := startindex
- until
- i > endindex - 1
- loop
- product := product * twrs.at (i)
- i := i + 1
- end
- product := (product - 1) * 100
- evalutation_return := product
- end
- twrtransaction(transactionlist: LIST[TRANSACTION]; startdate: DATE; enddate: DATE) --GETS THE LIST OF TRANSACTIONS IN EVAL PERIOD--
- require
- non_empty_list:transactionlist /= VOID
- start_bigger_end: enddate.is_greater (startdate)
- local
- currentlocation: DATE
- index: INTEGER
- do
- create currentlocation.make_now
- create {LINKED_LIST[TRANSACTION]} twrtransactionlist.make
- from
- index:= 1
- currentlocation:= transactionlist.at (index).a_transaction.date
- until
- currentlocation.is_greater_equal (startdate)
- loop
- index:= index + 1
- currentlocation:= transactionlist.at (index).a_transaction.date
- end
- from
- currentlocation:= transactionlist.at (index).a_transaction.date
- until
- currentlocation.is_greater_equal (enddate) and index > transactionlist.count
- loop
- twrtransactionlist.extend (transactionlist.at (index))
- currentlocation:= transactionlist.at (index).a_transaction.date
- index:= index + 1
- end
- ensure
- empty_list: twrtransactionlist.is_empty /= TRUE
- end
- totalreturn --Calculates the total return
- local
- i:INTEGER
- product: DOUBLE
- do
- product:=1
- from
- i := 1
- until
- i > twrs.count
- loop
- product := product * twrs.at (i)
- i := i + 1
- end
- total_return := (product - 1) * 100
- end
- getAll(transactions: LIST[TRANSACTION]) --Makes a copy of the full transaction list
- local
- i: INTEGER
- do
- create {LINKED_LIST[TRANSACTION]} fulltransactionlist.make
- from
- i := 1
- until
- i > transactions.count
- loop
- fulltransactionlist.extend (transactions.at (i))
- i := i + 1
- end
- end
- getdateindex(transactionlist: LIST[TRANSACTION]; temp_date: DATE): INTEGER --returns the index of date argument
- local
- i: INTEGER
- flag: BOOLEAN
- do
- from
- i:= 1
- until
- i > transactionlist.count
- loop
- if transactionlist.at (i).a_transaction.date.is_greater_equal (temp_date) and not flag then
- Result:= i
- flag:= TRUE
- end
- i:= i + 1
- end
- end
- twrCalculator -- calculates the TWR for EVERY transaction
- require
- list_exists: fulltransactionlist /= VOID
- local
- i: INTEGER
- j: INTEGER
- mv0: DOUBLE
- mv1: DOUBLE
- cf: DOUBLE
- r: DOUBLE
- do
- create {LINKED_LIST[DOUBLE]} twrs.make
- create {LINKED_LIST[TUPLE[a_date: DATE; twr : DOUBLE]]} twrdate.make
- from
- i:= 1
- j:= 2
- until
- j > fulltransactionlist.count
- loop
- mv0:= fulltransactionlist.at (i).a_transaction.mv
- mv1:= fulltransactionlist.at (j).a_transaction.mv
- cf:= fulltransactionlist.at (i).a_transaction.fc
- if mv1 = void or mv1 = 0 then
- mv1:= mv0 + cf
- end
- if mv0 /= 0 then
- r:= (mv1/(mv0 + cf))
- twrs.extend (r)
- twrtuple := [fulltransactionlist.at (j).a_transaction.date, r]
- twrdate.extend (twrTuple)
- else
- print("Initial Market value cannot be zero\n")
- end
- i:= i + 1
- j:= j + 1
- end
- ensure
- completed: (twrdate.is_empty = FALSE)
- end
- annum --calculate the yearly TWRS
- local
- i: INTEGER
- j: INTEGER
- product: DOUBLE
- yeari: DOUBLE
- tmp: INTEGER
- do
- create {LINKED_LIST[DOUBLE]} yearlytwrs.make
- product:= 1
- tmp := twrdate.last.a_date.year
- from
- j:=1
- until
- j > twrdate.count
- loop
- yeari:= twrdate.at (j).a_date.year
- from
- i := j
- until
- (twrdate.at (i).a_date.year /= yeari)
- loop
- product := product.product (twrdate.at (i).twr)
- i := i + 1
- j := j + 1
- if(i > twrdate.count) then
- yeari:= yeari + 1
- i:=i-1
- end
- end
- if(i <= twrdate.count and (j /= twrdate.count +1)) then
- product := product.product (twrdate.at (i).twr)
- end
- yearlytwrs.extend ((product - 1.0) * 100)
- j:= j+1
- product := 1
- end
- end
- end
Add Comment
Please, Sign In to add comment