刚刚开始学习,有从事这方面研究的可以加我,我菜鸟,代码是照搬的,正在学习中,学到哪里,更新到哪里
%清空变量
clear all
clc
%声明帐号登录的CTP全局变量
global actctp;
actctp=actxserver('ctpcom.ICTPClientAPI');
%请求帐号信息(权益手续费保证金等)
global accountinformation
accountinformation=cell(2,9);
accountinformation(1,1:9)=[{'PreBalance'},{'Balance '},{'Available'},{'Commission'},...
{'FrozenCommission '},{'Margin'},{'FrozenMargin'},{'CloseProfit'},{'PositionProfit '}];
%请求持仓信息(盈亏持仓等)
global positioninfo
positioninfo=cell(2,9);
positioninfo(1,1:9)=[{'InstrumentID'},{'IsLong'},{'Volume'},{'CloseProfit'},...
{'PositionProfit '},{'AvgPositionPrice'},{'AvgOpenPrice '},{'TotalClosable'},{'TodayClosable '}];
%请求行情信息 countrow是加一行加上1,初始值为1
global instrumengtid countrow countrow2
instrumengtid={'ag1506','IF1505'};
global tick tick2
tick=cell(300000,9);
tick(1,1:9)=[{'InstrumentID'},{'time '},{'UpdateTime'},{'LastPrice'},...
{'Volume '},{'AskPrice1'},{'AskVolume1'},{'BidPrice1'},{'BidVolume1 '}];
countrow=1;
tick2=cell(300000,9);
tick2(1,1:9)=[{'InstrumentID'},{'time '},{'UpdateTime'},{'LastPrice'},...
{'Volume '},{'AskPrice1'},{'AskVolume1'},{'BidPrice1'},{'BidVolume1 '}];
countrow2=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%帐号登录
config='瑞龙期货.xml';
userID='帐号';
password='密码';
verify=Login(actctp,config,userID,password);
%登录后返回信息
if verify==0
display('登录成功');
else
display('登录失败');
%type error.xml;
%return;
end
%行情返回char是由字符串转为字符
SubscribeMD(actctp,char(instrumengtid(1,1)));
SubscribeMD(actctp,char(instrumengtid(1,2)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%函数调用
actctp.registerevent({'OnAccount' ctpcallback('funOnAcc')});
actctp.registerevent({'OnPosition' ctpcallback('funOnPosition')});
actctp.registerevent({'OnMarketData' ctpcallback('funOnMarketData')});
% 清空变量/关闭程序
% actctp.unregisterallevents;
% actctp.delete;
% clear;
% clc;
下面是 ctpcallback.M
function a=ctpcallback(str)
a=str2func(str)
%接受返回帐号信息(权益手续费保证金等)
function funOnAcc(varargin)
disp('account information received');
global accountinformation;
PreBalance=varargin{3};
Balance =varargin{4};
Available =varargin{5};
Commission =varargin{6};
FrozenCommission =varargin{7};
Margin =varargin{8};
FrozenMargin =varargin{9};
CloseProfit =varargin{10};
PositionProfit =num2cell(varargin{11});
accountinformation(2,1:9)=[PreBalance,Balance,Available,Commission ,FrozenCommission,Margin ,FrozenMargin,CloseProfit,PositionProfit];
%接受返回持仓信息(盈亏持仓等)
function funOnPosition(varargin)
disp('account position received');
global positioninfo
InstrumentID=varargin{3};
IsLong =varargin{4};
Volume =varargin{5};
CloseProfit =varargin{6};
PositionProfit =varargin{7};
AvgPositionPrice =varargin{8};
AvgOpenPrice =varargin{9};
TotalClosable =varargin{10};
TodayClosable=num2cell(varargin{11});
positioninfo(2,1:9)=[InstrumentID,IsLong,Volume,CloseProfit,PositionProfit ,AvgPositionPrice,AvgOpenPrice ,TotalClosable,TodayClosable ];
%接收行情TICK
function funOnMarketData(varargin)
disp('maket data received');
global instrumentid tick countrow tick2 countrow2
InstrumentID=varargin{3};
time=varargin{18};
UpdateTime=varargin{19};
LastPrice=varargin{11};
Volume=varargin{13};
AskPrice1=varargin{6};
AskVolume1=varargin{7};
BidPrice1=varargin{4};
BidVolume1=num2cell(varargin{5});
%strcmp比较字符串,如果返回值的ID和第1行第一列同样,则保存到tick
if strcmp(InstrumentID,char(instrumentid(1,1)))
countrow=countrow+1;
tick(countrow,1:9)=[InstrumentID,time ,UpdateTime,LastPrice,Volume ,AskPrice1,AskVolume1,BidPrice1,BidVolume1];
end
%strcmp比较字符串,如果返回值的ID和第2行第一列同样,则保存到tick
if strcmp(InstrumentID,char(instrumentid(1,2)))
countrow2=countrow2+1;
tick2(countrow2,1:9)=[InstrumentID,time ,UpdateTime,LastPrice,Volume ,AskPrice1,AskVolume1,BidPrice1,BidVolume1];
end
%下单
function funonorder(varargin)
%撤单
function funonordercanceled(varargin)
%BAR完成
function funonorderfinished(varargin)
%相关初始化工作完成
function funoninitfinished (varargin)
%当交易连线后,此事件被触发
function OnTradeConnected(varargin)
%当交易断线后,此事件被触发
function funOnTradeDisconnected(varargin)
%当行情连线后,此事件被触发
function funOnMDConnected(varargin)
%当行情断线后,此事件被触发
function funOnMDDisconnected(varargin)
%当撤单失败时,此事件被触发
function funOnOrderActionFailed(varargin)
%交易所状态信息通知
function funOnInstrumentStatus(varargin)
%相关初始化工作完成,在这个事件通知之后,可以通过GetInstruments方法获得所有合约列表。
function funOnInitFinished(varargin)
function funOnMarginRate(varargin)
function funOnBar(varargin)
%订阅某个合约的行情数据。在订阅了后,会通过OnMarketData事件将该合约的行情发出。