您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 其它办公文档 > 基于MATLAB的策略回测模板
%%简介:系统基于布林通道原理,是一个趋势追踪系统。%入场条件:%ROC大于0且价格突破布林带上轨就开多仓;%ROC小于0且价格跌破布林带下轨就开空仓;%关键参数:%买卖滑点参数Slip%布林带的周期数BollLength;%布林带标准差的倍数Offset;%ROC的周期数ROCLength;%跟踪止损算法的周期数ExitLength;%%--提取数据--user=input('请输入数据库用户名:','s');password=input('请输入数据库密码:','s');commodity=input('请输入商品(如RB888):','s');Freq=input('请输入周期(如M5):','s');conna=database('Futures_matlab',user,password);cursor=exec(conna,strcat('select*from',32,commodity,'_',Freq));%32是指空格的ASCLL码cursor=fetch(cursor);data=cursor.Data;Date=datenum(data(:,1));%日期时间Open=cell2mat(data(:,2));%开盘价High=cell2mat(data(:,3));%最高价Low=cell2mat(data(:,4));%最低价Close=cell2mat(data(:,5));%收盘价Volume=cell2mat(data(:,6));%成交量OpenInterest=cell2mat(data(:,7));%持仓量%%--定义参数(常量)--%策略参数Slip=2;%滑点BollLength=50;%布林线长度Offset=1.25;%布林线标准差倍数ROCLength=30;%ROC的周期数%品种参数MinMove=1;%商品的最小变动量PriceScale=1;%商品的计数单位TradingUnits=10;%交易单位Lots=1;%交易手数MarginRatio=0.07;%保证金率TradingCost=0.0003;%交易费用设为成交金额的万分之三RiskLess=0.035;%无风险收益率(计算夏普比率时需要)%%--定义变量--%策略变量UpperLine=zeros(length(data),1);%上轨LowerLine=zeros(length(data),1);%下轨MidLine=zeros(length(data),1);%中间线Std=zeros(length(data),1);%标准差序列RocValue=zeros(length(data),1);%ROC值%交易记录变量MyEntryPrice=zeros(length(data),1);%买卖价格MarketPosition=0;%仓位状态,-1表示持有空头,0表示无持仓,1表示持有多头pos=zeros(length(data),1);%记录仓位情况,-1表示持有空头,0表示无持仓,1表示持有多头Type=zeros(length(data),1);%买卖类型,1标示多头,-1标示空头OpenPosPrice=zeros(length(data),1);%记录建仓价格ClosePosPrice=zeros(length(data),1);%记录平仓价格OpenPosNum=0;%建仓价格序号ClosePosNum=0;%平仓价格序号OpenDate=zeros(length(data),1);%建仓时间CloseDate=zeros(length(data),1);%平仓时间NetMargin=zeros(length(data),1);%净利CumNetMargin=zeros(length(data),1);%累计净利RateOfReturn=zeros(length(data),1);%收益率CumRateOfReturn=zeros(length(data),1);%累计收益率CostSeries=zeros(length(data),1);%记录交易成本BackRatio=zeros(length(data),1);%记录回测比例%记录资产变化变量LongMargin=zeros(length(data),1);%多头保证金ShortMargin=zeros(length(data),1);%空头保证金Cash=repmat(1e6,length(data),1);%可用资金,初始资金为10WDynamicEquity=repmat(1e6,length(data),1);%动态权益,初始资金为10WStaticEquity=repmat(1e6,length(data),1);%静态权益,初始资金为10W%%--计算布林带和ROC--[UpperLineMidLineLowerLine]=BOLL(Close,BollLength,Offset,0);RocValue=ROC(Close,ROCLength);%%--策略仿真--fori=BollLength:length(data)ifMarketPosition==0LongMargin(i)=0;%多头保证金ShortMargin(i)=0;%空头保证金StaticEquity(i)=StaticEquity(i-1);%静态权益DynamicEquity(i)=StaticEquity(i);%动态权益Cash(i)=DynamicEquity(i);%可用资金endifMarketPosition==1LongMargin(i)=Close(i)*Lots*TradingUnits*MarginRatio;StaticEquity(i)=StaticEquity(i-1);DynamicEquity(i)=StaticEquity(i)+(Close(i)-OpenPosPrice(OpenPosNum))*TradingUnits*Lots;Cash(i)=DynamicEquity(i)-LongMargin(i);endifMarketPosition==-1ShortMargin(i)=Close(i)*Lots*TradingUnits*MarginRatio;StaticEquity(i)=StaticEquity(i-1);DynamicEquity(i)=StaticEquity(i)+(OpenPosPrice(OpenPosNum)-Close(i))*TradingUnits*Lots;Cash(i)=DynamicEquity(i)-ShortMargin(i);end%开仓模块%开多头ifMarketPosition~=1&&RocValue(i-1)0&&High(i)=UpperLine(i-1)%用i-1,避免未来函数%平空开多ifMarketPosition==-1MarketPosition=1;ShortMargin(i)=0;%平空后空头保证金为0了MyEntryPrice(i)=UpperLine(i-1);ifOpen(i)MyEntryPrice(i)%考虑是否跳空MyEntryPrice(i)=Open(i);endMyEntryPrice(i)=MyEntryPrice(i)+Slip*MinMove*PriceScale;%建仓价格(也是平空仓的价格)ClosePosNum=ClosePosNum+1;ClosePosPrice(ClosePosNum)=MyEntryPrice(i);%记录平仓价格CloseDate(ClosePosNum)=Date(i);%记录平仓时间OpenPosNum=OpenPosNum+1;OpenPosPrice(OpenPosNum)=MyEntryPrice(i);%记录开仓价格OpenDate(OpenPosNum)=Date(i);%记录开仓时间Type(OpenPosNum)=1;%方向为多头StaticEquity(i)=StaticEquity(i-1)+(OpenPosPrice(OpenPosNum-1)-ClosePosPrice(ClosePosNum))...*TradingUnits*Lots-OpenPosPrice(OpenPosNum-1)*TradingUnits*Lots*TradingCost...-ClosePosPrice(ClosePosNum)*TradingUnits*Lots*TradingCost;%平空仓时的静态权益DynamicEquity(i)=StaticEquity(i)+(Close(i)-OpenPosPrice(OpenPosNum))*TradingUnits*Lots;end%空仓开多ifMarketPosition==0MarketPosition=1;MyEntryPrice(i)=UpperLine(i-1);ifOpen(i)MyEntryPrice(i)%考虑是否跳空MyEntryPrice(i)=Open(i);endMyEntryPrice(i)=MyEntryPrice(i)+Slip*MinMove*PriceScale;%建仓价格OpenPosNum=OpenPosNum+1;OpenPosPrice(OpenPosNum)=MyEntryPrice(i);%记录开仓价格OpenDate(OpenPosNum)=Date(i);%记录开仓时间Type(OpenPosNum)=1;%方向为多头StaticEquity(i)=StaticEquity(i-1);DynamicEquity(i)=StaticEquity(i)+(Close(i)-OpenPosPrice(OpenPosNum))*TradingUnits*Lots;endLongMargin(i)=Close(i)*Lots*TradingUnits*MarginRatio;%多头保证金Cash(i)=DynamicEquity(i)-LongMargin(i);end%开空头%平多开空ifMarketPosition~=-1&&RocValue(i-1)0&&Low(i)=LowerLine(i-1)ifMarketPosition==1MarketPosition=-1;LongMargin(i)=0;%平多后多头保证金为0了MyEntryPrice(i)=LowerLine(i-1);ifOpen(i)MyEntryPrice(i)MyEntryPrice(i)=Open(i);endMyEntryPrice(i)=MyEntryPrice(i)-Slip*MinMove*PriceScale;%建仓价格(也是平多仓的价格)ClosePosNum=ClosePosNum+1;ClosePosPrice(ClosePosNum)=MyEntryPrice(i);%记录平仓价格CloseDate(ClosePosNum)=Date(i);%记录平仓时间OpenPosNum=OpenPosNum+1;OpenPosPrice(OpenPosNum)=MyEntryPrice(i);%记录开仓价格OpenDate(OpenPosNum)=Date(i);%记录开仓时间Type(OpenPosNum)=-1;%方向为空头StaticEquity(i)=StaticEquity(i-1)+(ClosePosPrice(ClosePosNum)-OpenPosPrice(OpenPosNum-1))...*TradingUnits*Lots-OpenPosPrice(OpenPosNum-1)*TradingUnits*Lots*TradingCost...-ClosePosPrice(ClosePosNum)*TradingUnits*Lots*TradingCost;%平多
本文标题:基于MATLAB的策略回测模板
链接地址:https://www.777doc.com/doc-6788681 .html