这个示例展示了如何使用时间表从模拟的每日股票数据可视化和计算每周统计数据。
步骤1。加载数据。
这个例子的数据在mat文件中SimulatedStock.mat
,加载以下内容:
与收盘价相对应的日期,TMW_DATES
期初存货的价格,TMW_OPEN
股票价格日高,TMW_HIGH
股票价格的每日最低点,TMW_LOW
关闭股票价格,TMW_CLOSE, TMW_CLOSE_MISSING
每日交易量,TMW_VOLUME
表中的数据,TMW_TB
负载SimulatedStock.matTMW_ *
步骤2。创建时间表。
在时间表中,你可以使用金融时间序列而不是向量。当使用一个时间表
,你可以很容易地追踪日期。您可以根据日期操作数据系列,因为时间表
对象跟踪时间序列的管理。
使用MATLAB®时间表
函数创建时间表
对象。或者,您可以使用MATLAB的转换函数table2timetable
把一张桌子转换成一张时刻表。在本例中,是时间表TMW_TT
由表构造,仅用于说明目的。在创建时间表
对象,则可以使用描述
场的时间表
对象存储有关时间表的元信息。
从向量输入创建一个时间表TMW =时间表(TMW_OPEN TMW_HIGH、TMW_LOW TMW_CLOSE_MISSING, TMW_VOLUME,...“VariableNames”, {“开放”,“高”,“低”,“关闭”,“体积”},“RowTimes”, TMW_DATES);%从表格转换为时间表TMW_TT = table2timetable (TMW_TB,“RowTimes”, TMW_DATES);TMW.Properties.Description =模拟股票数据。;TMW。属性
ans = TimetableProperties with properties:说明:“模拟股票数据。”UserData: [] DimensionNames: {'Time' 'Variables'} VariableNames: {'Open' ' 'High' ' 'Low' 'Close' ' 'Volume'} VariableDescriptions: {} VariableUnits: {} variablecontinity: [] RowTimes: [1000x1 datetime] StartTime: 04- 9月2012 SampleRate: NaN TimeStep: NaN CustomProperties:未设置自定义属性。使用addprop和rmprop修改CustomProperties。
步骤3。计算基础数据统计,填补缺失数据。
使用MATLAB总结
命令功能查看设备的基本统计信息时间表
数据。通过查看每个变量的摘要,您可以确定缺失的值。然后可以使用MATLABfillmissing
函数通过指定填充方法来填充时间表中缺失的数据。
summaryTMW =总结(TMW);summaryTMW。关闭
ans =结构体字段:Size: [1000 1] Type: 'double' Description: " Units: " Continuity: [] Min: 83.4200 Median: 116.7500 Max: 162.1100 NumMissing: 3
TMW = fillmissing (TMW,“线性”);summaryTMW =总结(TMW);summaryTMW。关闭
ans =结构体字段:尺寸:[1000 1]类型:“双”描述:“单位:”连续性:[]Min: 83.4200中位数:116.7050 Max: 162.1100 NumMissing: 0
summaryTMW。时间
ans =结构体字段:Size: [1000 1] Type: 'datetime' Min: 04- 9 -2012 Median: 31- 8 -2014 Max: 24- 8 -2016 NumMissing: 0 TimeStep: NaN
步骤4。可视化数据。
要可视化时间表数据,可以使用财务图表功能,例如highlow
或movavg
.对于这个例子,移动平均线信息被绘制在同一个图表上highlow
提供一个完整的可视化。要获得2014年的股票表现,使用MATLABtimerange
方法的行时间表
.要可视化技术指标,如MACD,通过该指标时间表
对象进macd
函数进行分析。
指数= timerange (datetime (' 01 - 1月- 2014,“场所”,“en_US”)、日期时间(2014年- 12月31日的,“场所”,“en_US”),“关闭”);highlow (TMW(指数:));持有在ema15 = movavg (TMW (:,“关闭”),“指数”15);ema25 = movavg (TMW (:,“关闭”),“指数”25);ema15 = ema15(指数);ema25 = ema25(指数);情节(ema15.Time ema15.Close,“r”);情节(ema25.Time ema25.Close,‘g’);持有从传奇(“价格”,“15天EMA”,“二十五天的教育津贴”)标题(“TMW的最低值图”)
[macdLine, signalLine] = macd(TMW(:,));“关闭”));情节(macdLine.Time macdLine.Close);持有在情节(signalLine.Time signalLine.Close);持有从标题(“TMW MACD”)传说(“MACD线”,信号线的)
第5步。创建一个每周收益和波动率系列。
要从每日股票价格计算每周回报,必须从每日到每周重新采样数据频率。在处理时间表时,使用MATLAB函数调整时间
或同步
用各种汇总方法计算每周统计数据。若要调整时间表数据以时间矢量为基础,请使用调整时间
和使用同步
与多个时间表。
weeklyOpen =调整时间(TMW (:,“开放”),“周”,“firstvalue”);weeklyHigh =调整时间(TMW (:,“高”),“周”,“马克斯”);weeklyLow =调整时间(TMW (:,“低”),“周”,“最小值”);weeklyClose =调整时间(TMW (:,“关闭”),“周”,“lastvalue”);weeklyTMW = [weeklyOpen weeklyHigh、weeklyLow weeklyClose);weeklyTMW =同步(weeklyTMW TMW (:,“体积”),“周”,“和”);头(weeklyTMW)
ans =8×5时间表时间开高开低收量___________ ______ ______ ______ ______ __________ 02- 9- 2012 100 102.38 98.45 99.51 2.7277 e+07 09- 9- 2012 99.72 101.55 96.52 97.52 2.8518e+07 16- 9- 2012 97.35 97.52 92.6 93.73 2.9151e+07 23- 9- 2012 93.55 98.03 92.25 97.35 3.179e+07 30- 9- 2012 97.3 103.15 96.68 99.66 3.3761e+07 07- 10 -2012 99.76 106.61 98.7104.23 3.1299e+07 14-Oct-2012 104.54 109.75 100.55 103.77 3.1534e+07 21-Oct-2012 103.84 104.32 96.95 97.41 3.1706e+07
对a中的条目进行计算时间表
,使用MATLABrowfun
函数将函数应用于每周频率时间表的每一行。
returnFunc = @(open,high,low,close,volume) log(close) - log(open); / /打开weeklyReturn = rowfun (returnFunc weeklyTMW,“OutputVariableNames”, {“返回”});weeklyStd =调整时间(TMW (:,“关闭”),“周”, @std);weeklyStd.Properties.VariableNames {“关闭”} =“波动”;weeklyTMW = [weeklyReturn、weeklyStd weeklyTMW]
weeklyTMW =208×7时间表时间回报波动率开高开低收量___________ ___________ __________ ______ ______ ______ ______ __________ 02- 9- 2012 0.59386 100 102.38 98.45 99.51 2.7279e+07 09- 9- 2012 -0.022309 0.63563 99.72 101.55 96.52 97.52 2.8518e+07 16- 9- 2012 -0.037894 0.93927 97.35 97.52 92.6 93.73 2.9151e+07 23- 9- 2012 0.039817 2.015693.55 98.03 92.25 97.35 3.1706e+07 - 10 -2012 0.023965 1.1014 97.3 103.15 96.68 99.66 3.3761e+07 07- 10 -2012 0.043833 1.3114 99.76 106.61 98.7 104.23 3.1299e+07 14- 10 -2012 -0.0073929 1.8097 104.54 109.75 100.55 103.77 3.1534e+07 21- 10 -2012 0.063922 2.1603 103.84 104.32 96.95 97.41 3.1706e+07 28- 10 -2012 -0.028309 0.9815 97.45 99.1 92.58 94.731.9866e+07 04- 11- 2012 - 0.077244 2.4854 94.39 103.98 93.84 101.97 3.0624e+07 18- 11- 2012 0.022823 0.55896 102.23 105.27 101.24 104.59 2.5803e+07 25- 11- 2012 -0.012789 1.337 104.66 106.02 100.85 103.33 3.1402e+07 02- 12 -2012 -0.043801 0.2783 103.37 103.37 97.69 98.94 3.2136e+0709- 12 -2012 0.063475 1.9826 99.02 99.09 91.34 92.93 3.4447e+07
diff
|fillmissing
|isregular
|issorted
|调整时间
|rmmissing
|sortrows
|同步
|timerange
|时间表
|独特的
|vartype
|withtol