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

2 次查看(过去 30 天)
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

采纳的回答

Andy
Andy 2017-3-18
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...

更多回答(1 个)

Andy
Andy 2017-3-17
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?

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by