for an existing financial time-series object, is there a simple way to add columns?

2 views (last 30 days)
I'm referring to pandas DataFrame as an example of adding a new ('return') column:
spy['return'] = spy['close'].pct_change()
What's the equivalent in matlab for the line above?
thx

Accepted Answer

Andy
Andy on 18 Mar 2017
digging a bit more, it seems fts behave a lot like structures ... so:
>> class(dt)
ans =
fints
>> dt.MA10 = fts2mat(tsmovavg(dt.Close, 's', 10))
dt =
desc: (none)
freq: Daily (1)
'dates: (2711)' 'Open: (2711)' 'High: (2711)' 'Low: (2711)' 'Close: (2711)' 'Vol: (2711)' 'OI: (2711)' 'MA10: (2711)'
'20-Mar-2006' [ 138.63] [ 139.44] [ 138.4] [ 139.31] [ 11992] [ 219881] [ NaN]
'21-Mar-2006' [ 140.59] [ 140.73] [ 139.85] [ 140.24] [ 15357] [ 214085] [ NaN]
will just add a new column ... or:
col_name = 'ma_20'
dt.(col_name) = fts2mat(tsmovavg(dt.Close, 's', 20))
dt =
desc: (none)
freq: Daily (1)
'dates: (2711)' 'Open: (2711)' 'High: (2711)' 'Low: (2711)' 'Close: (2711)' 'Vol: (2711)' 'OI: (2711)' 'MA10: (2711)' 'ma_20: (2711)'
'20-Mar-2006' [ 138.63] [ 139.44] [ 138.4] [ 139.31] [ 11992] [ 219881] [ NaN] [ NaN]
'21-Mar-2006' [ 140.59] [ 140.73] [ 139.85] [ 140.24] [ 15357] [ 214085] [ NaN] [ NaN]
'22-Mar-2006' [ 140.99] [ 141.33] [ 140.71] [ 140.87] [ 8884] [ 204050] [ NaN] [ NaN]
Simple after all...

More Answers (1)

Andy
Andy on 17 Mar 2017
I'll just give a partial answer I've found. For an object such as:
dt =
desc: (none)
freq: Daily (1)
'dates: (2715)' 'times: (2715)' 'Open: (2715)' 'High: (2715)' 'Low: (2715)' 'Close: (2715)' 'Vol: (2715)' 'OI: (2715)'
'19-Mar-2006' '22:00' [ 1164.25] [ 1186.5] [ 1160.5] [ 1183.75] [ 1149131] [ 1064369]
do:
ma_10 = chfield(tsmovavg(dt.Close,'s',10), 'Close', 'ma_10') dt = merge(dt, ma_10)
As you can see this is overkill. Questions:
1) When one does { ma_10 = tsmovavg(dt.Close,'s',10) } it seems the new fts ma_10 object inherits the column name from the object use (="Close"). Can I actually assign a new name to my new object?
2) I cannot ignore the comparison between the above (which it's hard to say it's elegant) with the below:
[pandas] ma_portfolio['ma1'] = ma_portfolio['close'].rolling(window=50).mean()
Is there anything I'm missing in terms of adding more elegantly a simple mov.avg. to an fts object?

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!