How can I reproduce the following finance table in r

Viewed 103

I want to recreate in R the following table : enter image description here

I have been provided with only these three parameters:

initial_deposit = 15000
contract_value = 250
closing_stock_indices = c(1000,  1002, 994,  998, 997)

My effort in R is this:


cl =c(1000,1002,994,998,997) # The closing stock indices.
re = c(0,diff(cl))
t = time(cl)
mtm = re*250 # The contract value of 250.
mb = 15000+mtm # The initial deposit of 15000.
vm = ifelse(mb>0,0,mtm)
d = data.frame(t,cl,re,mtm,mb,vm);d

but I cannot do the last two columns.Any help ?

1 Answers

You may do the following

library(tidyverse)

initial_deposit = 15000
contract_value = 250
closing_stock_indices = c(1000,  1002, 994,  998, 997)

(seq_along(closing_stock_indices) - 1) %>%
  as.data.frame() %>%
  setNames('Day') %>%
  mutate(Closing_SI = closing_stock_indices,
         Daily_change = c(0, diff(Closing_SI)),
         Marking_to_market = contract_value * Daily_change,
         Margin_balance = accumulate(Marking_to_market[-1], .init = initial_deposit,
                         ~ if (.x >= initial_deposit) .x + .y else initial_deposit + .y),
         Variation_Margin = -1 * pmin(Margin_balance - initial_deposit, 0),
         REquired_Deposit = c(initial_deposit, Variation_Margin[-n()]))

  Day Closing_SI Daily_change Marking_to_market Margin_balance Variation_Margin REquired_Deposit
1   0       1000            0                 0          15000                0            15000
2   1       1002            2               500          15500                0                0
3   2        994           -8             -2000          13500             1500                0
4   3        998            4              1000          16000                0             1500
5   4        997           -1              -250          15750                0                0

Check on another vector

initial_deposit = 15000
contract_value = 250
closing_stock_indices = c(1000,  1002, 994,  998, 997, 990, 1000)

(seq_along(closing_stock_indices) - 1) %>%
  as.data.frame() %>%
  setNames('Day') %>%
  mutate(Closing_SI = closing_stock_indices,
         Daily_change = c(0, diff(Closing_SI)),
         Marking_to_market = contract_value * Daily_change,
         Margin_balance = accumulate(Marking_to_market[-1], .init = initial_deposit,
                         ~ if (.x >= initial_deposit) .x + .y else initial_deposit + .y),
         Variation_Margin = -1 * pmin(Margin_balance - initial_deposit, 0),
         REquired_Deposit = c(initial_deposit, Variation_Margin[-n()]))

  Day Closing_SI Daily_change Marking_to_market Margin_balance Variation_Margin REquired_Deposit
1   0       1000            0                 0          15000                0            15000
2   1       1002            2               500          15500                0                0
3   2        994           -8             -2000          13500             1500                0
4   3        998            4              1000          16000                0             1500
5   4        997           -1              -250          15750                0                0
6   5        990           -7             -1750          14000             1000                0
7   6       1000           10              2500          17500                0             1000
Related