How to add column values of different tables based on dates column

5 views (last 30 days)
Hello, I have a table of daily stock prices by date:
Date: Price
01/01/2010 50.6
02/01/2010 51.2
... ...
and another table with dividends by their payment date:
Date: Dividend:
02/01/2010 2.5
02/05/2010 1.6
... ...
I want to adjust the prices with the paid dividends by checking whether a dividend was paid for every Date in table1 and where the Date in table1 and table2 match, I want to overwrite the price in table1 to equals = Price.table1 + Dividen.table2 for that specific date.
So the end result should be:
Date: Price:
01/01/2010 53.1
02/01/2010 51.2
... ...
I know this can be done with a for and if loop, I just can't get my head around it.
Please advise

Accepted Answer

Star Strider
Star Strider on 6 Mar 2018
If you have R2016b or later, either the retime (link) or synchronize (link) function will probably work.
  4 Comments
Sara Dabour
Sara Dabour on 6 Mar 2018
synchronise() gives me the same error. I'm using the R2017b version, if that is useful. Regardless, I found out that retime(a,b,'union') brings me closer to what I want. It's not ideal but it results in a new timetable where it a and b are arranged by date as separate columns. Timetable p:
Date Dividend Price
'01/01/2010' NaN 30.1136000000000
'04/01/2010' NaN 30.6121000000000
'05/01/2010' NaN 30.6236000000000
'06/01/2010' NaN 30.1336000000000
From here I can just use the sum() function to get the totals across rows. I've found that I have to convert p into a double for that to work. Now my problem is how to replace the summed values in a, according to the right date.
Almost there :D
Star Strider
Star Strider on 6 Mar 2018
I appreciate your update.
I can only direct you to what appear to me are appropriate ways to solve your problem, from your description of it and your description of your data.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 8 Mar 2018
I think 'sum', as in "the input to retime or synchronize", is a red herring here. That parameter sums across time. Assuming you've made two timetables, I think what you want is something like
ttDividends = retime(ttDividends,ttProces.Time,'FillWithConstant','Constant',0)
ttPrices.Price = tt.Price + ttDividends.Dividend
But it's probably useful to combine prices and dividends. Assuming the dividend dates are a subset of the price dates, this
ttCombined = synchronize(ttProcess,ttDividends,'first')
will NaN-fill the dates with no dividends. Then
hadDividend = ~isnan(ttCombined.Dividend);
ttCombined.NewVarName(i) = ttCombined.Price(i) + ttCombined.Dividend(i)

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!