Guest User

Untitled

a guest
Jan 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. note
  2. description: "Summary description for {TWR}."
  3. author: ""
  4. date: "$Date$"
  5. revision: "$Revision$"
  6.  
  7. class TWR create make
  8.  
  9.  
  10. feature
  11.  
  12. --data--
  13. twrtransactionlist: LIST[TRANSACTION]
  14. fulltransactionlist: LIST[TRANSACTION]
  15. rs: LIST[DOUBLE]
  16. twrs: LIST[DOUBLE]
  17. yearlytwrs: LIST[DOUBLE]
  18. start_date: DATE
  19. end_date: DATE
  20. startindex: INTEGER
  21. endindex: INTEGER
  22. twrtuple: TUPLE[a_date: DATE; twr : DOUBLE]
  23. twrdate: LIST[TUPLE[a_date: DATE; twr : DOUBLE]]
  24.  
  25. --calculated values--
  26. compounded_return_value: DOUBLE
  27. number_of_days: INTEGER
  28. total_return:DOUBLE
  29. evalutation_return:DOUBLE
  30.  
  31. make(transactionlist: LIST[TRANSACTION]; t_startdate: DATE; t_enddate: DATE)
  32.  
  33. do
  34. start_date:= t_startdate
  35. end_date:= t_enddate
  36. number_of_days := transactionlist.last.a_transaction.date.duration.day - transactionlist.first.a_transaction.date.duration.day
  37.  
  38.  
  39. twrtransaction(transactionlist, t_startdate, t_enddate)
  40. startindex:= getdateindex(transactionlist, start_date)
  41. endindex:= getdateindex(transactionlist, end_date)
  42.  
  43.  
  44. getAll(transactionlist) --make copy of full transaction list
  45. twrcalculator --calculates the twr for EVERY transaction
  46.  
  47.  
  48. annum --prints yearly TWR's
  49.  
  50. totalreturn --sets the value of total return
  51.  
  52. evalreturn --prints twr for eval period
  53.  
  54. compounded_return --prints compounded return
  55. end
  56.  
  57. compounded_return
  58.  
  59. do
  60. compounded_return_value := ((1 + (total_return/100)) ^ ( (1/(number_of_days/365.2465))) - 1)* 100
  61. io.new_line
  62.  
  63. end
  64.  
  65. evalReturn
  66.  
  67. local
  68. product:DOUBLE
  69. i:INTEGER
  70. do
  71. product := 1
  72. from
  73. i := startindex
  74. until
  75. i > endindex - 1
  76. loop
  77. product := product * twrs.at (i)
  78. i := i + 1
  79. end
  80. product := (product - 1) * 100
  81.  
  82. evalutation_return := product
  83. end
  84.  
  85. twrtransaction(transactionlist: LIST[TRANSACTION]; startdate: DATE; enddate: DATE) --GETS THE LIST OF TRANSACTIONS IN EVAL PERIOD--
  86.  
  87. require
  88. non_empty_list:transactionlist /= VOID
  89. start_bigger_end: enddate.is_greater (startdate)
  90.  
  91. local
  92. currentlocation: DATE
  93. index: INTEGER
  94.  
  95. do
  96. create currentlocation.make_now
  97. create {LINKED_LIST[TRANSACTION]} twrtransactionlist.make
  98.  
  99. from
  100.  
  101. index:= 1
  102. currentlocation:= transactionlist.at (index).a_transaction.date
  103. until
  104. currentlocation.is_greater_equal (startdate)
  105. loop
  106. index:= index + 1
  107. currentlocation:= transactionlist.at (index).a_transaction.date
  108. end
  109.  
  110. from
  111.  
  112. currentlocation:= transactionlist.at (index).a_transaction.date
  113.  
  114. until
  115. currentlocation.is_greater_equal (enddate) and index > transactionlist.count
  116.  
  117. loop
  118. twrtransactionlist.extend (transactionlist.at (index))
  119.  
  120. currentlocation:= transactionlist.at (index).a_transaction.date
  121.  
  122. index:= index + 1
  123.  
  124. end
  125.  
  126. ensure
  127.  
  128. empty_list: twrtransactionlist.is_empty /= TRUE
  129.  
  130.  
  131. end
  132.  
  133.  
  134. totalreturn --Calculates the total return
  135.  
  136. local
  137. i:INTEGER
  138. product: DOUBLE
  139.  
  140. do
  141. product:=1
  142. from
  143. i := 1
  144. until
  145. i > twrs.count
  146. loop
  147. product := product * twrs.at (i)
  148. i := i + 1
  149. end
  150. total_return := (product - 1) * 100
  151. end
  152.  
  153.  
  154. getAll(transactions: LIST[TRANSACTION]) --Makes a copy of the full transaction list
  155.  
  156. local
  157. i: INTEGER
  158.  
  159. do
  160. create {LINKED_LIST[TRANSACTION]} fulltransactionlist.make
  161.  
  162. from
  163. i := 1
  164. until
  165. i > transactions.count
  166. loop
  167. fulltransactionlist.extend (transactions.at (i))
  168. i := i + 1
  169. end
  170.  
  171. end
  172.  
  173.  
  174. getdateindex(transactionlist: LIST[TRANSACTION]; temp_date: DATE): INTEGER --returns the index of date argument
  175. local
  176. i: INTEGER
  177. flag: BOOLEAN
  178. do
  179. from
  180. i:= 1
  181. until
  182. i > transactionlist.count
  183. loop
  184. if transactionlist.at (i).a_transaction.date.is_greater_equal (temp_date) and not flag then
  185. Result:= i
  186. flag:= TRUE
  187. end
  188. i:= i + 1
  189. end
  190. end
  191.  
  192. twrCalculator -- calculates the TWR for EVERY transaction
  193.  
  194. require
  195.  
  196. list_exists: fulltransactionlist /= VOID
  197.  
  198. local
  199. i: INTEGER
  200. j: INTEGER
  201. mv0: DOUBLE
  202. mv1: DOUBLE
  203. cf: DOUBLE
  204. r: DOUBLE
  205.  
  206. do
  207.  
  208. create {LINKED_LIST[DOUBLE]} twrs.make
  209. create {LINKED_LIST[TUPLE[a_date: DATE; twr : DOUBLE]]} twrdate.make
  210.  
  211.  
  212. from
  213. i:= 1
  214. j:= 2
  215. until
  216. j > fulltransactionlist.count
  217. loop
  218. mv0:= fulltransactionlist.at (i).a_transaction.mv
  219. mv1:= fulltransactionlist.at (j).a_transaction.mv
  220. cf:= fulltransactionlist.at (i).a_transaction.fc
  221.  
  222. if mv1 = void or mv1 = 0 then
  223. mv1:= mv0 + cf
  224. end
  225.  
  226. if mv0 /= 0 then
  227. r:= (mv1/(mv0 + cf))
  228. twrs.extend (r)
  229.  
  230. twrtuple := [fulltransactionlist.at (j).a_transaction.date, r]
  231. twrdate.extend (twrTuple)
  232.  
  233. else
  234. print("Initial Market value cannot be zero\n")
  235.  
  236. end
  237.  
  238. i:= i + 1
  239. j:= j + 1
  240.  
  241. end
  242.  
  243. ensure
  244. completed: (twrdate.is_empty = FALSE)
  245. end
  246.  
  247.  
  248.  
  249. annum --calculate the yearly TWRS
  250.  
  251. local
  252.  
  253. i: INTEGER
  254. j: INTEGER
  255. product: DOUBLE
  256. yeari: DOUBLE
  257. tmp: INTEGER
  258.  
  259.  
  260. do
  261.  
  262. create {LINKED_LIST[DOUBLE]} yearlytwrs.make
  263. product:= 1
  264.  
  265. tmp := twrdate.last.a_date.year
  266.  
  267. from
  268. j:=1
  269. until
  270. j > twrdate.count
  271. loop
  272.  
  273. yeari:= twrdate.at (j).a_date.year
  274.  
  275. from
  276. i := j
  277. until
  278.  
  279. (twrdate.at (i).a_date.year /= yeari)
  280.  
  281. loop
  282. product := product.product (twrdate.at (i).twr)
  283. i := i + 1
  284. j := j + 1
  285. if(i > twrdate.count) then
  286. yeari:= yeari + 1
  287. i:=i-1
  288. end
  289. end
  290.  
  291. if(i <= twrdate.count and (j /= twrdate.count +1)) then
  292. product := product.product (twrdate.at (i).twr)
  293.  
  294. end
  295. yearlytwrs.extend ((product - 1.0) * 100)
  296. j:= j+1
  297. product := 1
  298.  
  299. end
  300.  
  301. end
  302.  
  303.  
  304. end
Add Comment
Please, Sign In to add comment