Matlab调用天软前,需先创建一个天软COM对象,代码为:
ts=actxserver('TSExpert.CoExec');
1、取单支股票某时点行情数据
范例1: 取SZ000002 在20170208日的收盘价
方法一:调用close()函数
mCode:
ts.SetSysParam('StockID','SZ000002')%设置当前股票
ts.SetSysParam('CurrentDate',datenum(2017,2,8)-693960) %设置当前时间
ts.RemoteCallFunc('Close',{}) %调用取收盘价函数close()
mCode;
执行结果:
方法二:调用stockclose(endt)函数
mCode:
ts.SetSysParam('StockID','SZ000002')
ts.RemoteCallFunc('stockClose',{datenum(2017,2,8)-693960}) %调用函数并传入对应的参数
mCode;
方法三:封取数模型,直接调用-一劳永逸的办法
天软函数:
tsCode:
Function GetData_02(StockID,EndTime,cy,rateType,rateDate);
Begin
ov:=BackupSystemParameters2();
setsysparam(pn_stock(),StockID);
setsysparam(pn_cycle(),cy);
if ifString(EndTime) then EndTime:=strtodatetime(EndTime); //'2017-02-09 10:30:00'
setsysparam(pn_date(),EndTime);
setsysparam(pn_rate(),rateType);
if ifString(rateDate) then rateDate:=strtodate(rateDate);
if rateType<>0 then
setsysparam(pn_rateDay(),rateDate);
return close();
End;
tsCode;
Matlab中调用代码:
mCode:
ts.RemoteCallFunc('GetData_02',{'SZ000002',datenum(2017,2,8)-693960,'日线',0,0})
mCode;
结果:
注:此种方法才是长期使用简单方便不易出错的最佳方式,既能累积自己的函数库,又能方便灵活调用
2、取单支股票时间序列行情数据
实例2: 提取SZ000002自2016-3-1到2016-12-31的周线收盘数据
方法一:Matlab中调用天软语句
mCode:
ts.SetSysParam('Cycle','周线')
ts.RemoteExecute('return select datetostr(["date"]) as "date",["close"] from markettable datekey 20160301T to 20161231T of "SZ000002" end;')
mCode;
结果:
方法二:调用nday的实现方法,天软语句段的调用方法
mCode:
temp_1=ts.RemoteExecute(['SetSysParam(PN_Stock(),"SZ000002");',...
'SetSysParam(PN_Date(),20161231T);',...
'days:=tradedays(20160301T,20161231T);',...
'n:=tradedays(20160301T,20161231T);',...
'return nday(n,"date",datetostr(sp_time()),"close",close());']);
mCode;
结果:
方法三:封天软模型,再调用
天软中的代码
tsCode:
Function GetData_03(StockID,begT,endT,cy);
Begin
ov:=BackupSystemParameters2();
SetSysParam(pn_stock(),StockID) ;
SetSysParam(PN_Cycle(),cy);
setsysparam(pn_date(),endT+0.99);
n:=tradedays(begT,endT+0.99);
return nday(n,'date',datetostr(sp_time()),'close',close());
End;
tsCode;
matlab中的代码:
mCode:
ts.RemoteCallFunc('GetData_03',{'SZ000002',datenum(2016,3,1)-693960,datenum(2016,12,31)-693960,'周线'})
mCode;
结果:
3、取多支股票时间序列行情数据
范例3:提取rb1701,rb1705 自2016-3-1到2016-12-31的1分钟线高,开,低,收
方法一:在天软中封好取数模型,然后在matlab中调用该模型天软中模型代码:
tsCode:
Function GetData_01(StockStr,begT,endT,cy);
Begin
ov:=BackupSystemParameters2();
StockArr:=str2Array(StockStr);//将字符串转为一维字符串数组->避免需要传入一维数组
setsysparam(pn_cycle(),cy);
return select ['StockID'],datetimetostr(['date']) as 'date',
['high'],['open'],['low'],['close']
from markettable datekey begT to endT+0.999 of StockArr end;
End;
tsCode;
Matlab中调用代码:
mCode:
ts=actxserver('TSExpert.CoExec');
data=ts.RemoteCallFunc('GetData_01',{'rb1705;rb1701',datenum(2016,3,1)-693960,datenum(2016,12,31)-693960,'1分钟线'});
mCode;
结果:
方法二:直接调用天软语句实现数据的提取得
在matlab中的代码:->注意字符串中的字符串需用双引号进行区分
mCode:
ts=actxserver('TSExpert.CoExec');
ts.SetSysParam('Cycle','1分钟线');
data2= ts.RemoteExecute('return select ["stockid"],datetimetostr(["date"]) as "date",["high"],["open"],["low"],["close"] from markettable datekey 20160301T to 20161231T+0.999 of array("rb1705","rb1701") end;');
mCode;
结果:
