本文主要是介绍matlab rsi的交易策略源码,RSI择时 附策略源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
入场:当RSI下降到30以下进入超卖区,回到50的时候买入
出场:当RSI上升到70之上进入超买区,直接卖出
资金分配:买入的股票采取等资金分配
股票池:沪深300所有股票
策略源码:
function RSI1(n1,n2,n4) % 威廉指标.%获取目标资产信息targetList = traderGetTargetList(); % 在RunBackTest中选择好的标的.%获取账户信息HandleList = traderGetHandleList();%=================================================================% RunBackTest的参数设置% n1=12; % RSI的短期参数% n2=24; % RSI的长期参数% n4=2; % 几倍ATR%=================================================================% 定义持有的账户为全局变量global holdingList;if isempty(holdingList) % 判断cc是否为空值 holdingList(1).Market=0; holdingList(1).Code=0; holdingList(1).OpenBar=0; holdingList(1).OpenPrice=0; holdingList(1).Sharebum=0; holdingList(1).StopLoss=0;end% 定义可用金额global available;if isempty(available) % 判断cc是否为空值 available=100000000;end% 定义初始金额,每只股票的最大仓位为初始资金的1/300initial=100000000;initialeach=initial/length(targetList);k1=0;%------------------------------------------------------------------------------------------------------------------------------------------------------------------------------for i = 1:length(targetList) % 每个股票过一遍 % lags为策略需要往前获取多少天 lags=n2+2; %策略中每次取数据的长度 barnum=traderGetCurrentBar(targetList(i).Market,targetList(i).Code); % K线的序号,后面会增加,前面的值对应的日期固定. % 数据长度限制,排除了前lags根k线 if(barnum=datenum('1-Jan-2014') if length(close)LRSI(end) && SRSI(end-1)50 && SRSI(end)<80) || (SRSI(end)<20); buy1=a1 && a2; if buy1 k1=k1+1; stockList(k1).Market=targetList(i).Market; stockList(k1).Code=targetList(i).Code; stockList(k1).Price=close(end); [ATR1, ~]=traderATR(14,targetList(i).Market,targetList(i).Code,'day',1,0-lags,0,false,'FWard'); stockList(k1).ATR=ATR1(end); end endend%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------% 判断股票池里面的股票是否要买入,买入多少手if k1~=0 for i=1:length(stockList) % 等权重买入 sharenum=floor(initialeach/stockList(i).Price); % 设置每只股票的交易手数,使得最大的亏损不大于初始账户的 n2%.% sharenum=floor(stockList(i).Price*initialeach/stockList(i).ATR/10000); % 获取当前bar序号 barnum=traderGetCurrentBar(stockList(i).Market,stockList(i).Code); %获取当前仓位 [marketposition,~,~]=traderGetAccountPosition(HandleList(1),stockList(i).Market,stockList(i).Code); if marketposition ==0 && (sharenum*stockList(i).Price)<=1*available && time(end)>=datenum('1-Jan-2014') orderID1=traderBuy(HandleList(1),stockList(i).Market,stockList(i).Code,sharenum,0,'market','buy1'); % 开多单 if orderID1~=0 [~,~,price] = traderGetAccountPosition(HandleList(1),stockList(i).Market,stockList(i).Code); % 记录开仓的价格 available=available-sharenum*price; if holdingList(1).Market==0 holdingList(1).Market=stockList(i).Market; holdingList(1).Code=stockList(i).Code; holdingList(1).OpenBar=barnum+1; holdingList(1).OpenPrice=price; holdingList(1).Sharebum=sharenum; holdingList(1).StopLoss=price-n4*stockList(i).ATR; else holdingList1(1).Market=stockList(i).Market; holdingList1(1).Code=stockList(i).Code; holdingList1(1).OpenBar=barnum+1; holdingList1(1).OpenPrice=price; holdingList1(1).Sharebum=sharenum; holdingList1(1).StopLoss=price-n4*stockList(i).ATR; holdingList=[holdingList,holdingList1]; end end end endend%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------% 出场设置if holdingList(1).Code~=0 for i=1:length(holdingList) [ATR2, ~]=traderATR(14,holdingList(i).Market,holdingList(i).Code,'day',1,0-lags,0,false,'FWard'); [time1,open1,high1,low1,close1,volume1,turnover1,openinterest1] = traderGetKData(holdingList(i).Market,holdingList(i).Code,'day',1, 0-lags, 0,false,'FWard'); barnum=traderGetCurrentBar(holdingList(i).Market,holdingList(i).Code); if barnum>holdingList(i).OpenBar && high1(end-1)>high1(end-2) holdingList(i).StopLoss=high1(end-1)-n4*ATR2(end-1); end exitlong1=close1(end)LRSI(end-1); % 卖出信号: exitlong2=a1; exitlong=exitlong1 || exitlong2; if barnum>=holdingList(i).OpenBar && exitlong2 orderID2=traderPositionTo(HandleList(1),holdingList(i).Market,holdingList(i).Code,0,0,'market','sell1'); if orderID2~=0 exitprice=traderOrderFilledPrice(HandleList(1),orderID2); available=available+exitprice*holdingList(i).Sharebum; holdingList(i).Sharebum=0; end end end % 删除已经卖出的股票 holdingList2(1).Market=0; holdingList2(1).Code=0; holdingList2(1).OpenBar=0; holdingList2(1).OpenPrice=0; holdingList2(1).Sharebum=0; holdingList2(1).StopLoss=0; for i=1:length(holdingList) if holdingList(i).Sharebum~=0 holdingList2=[holdingList2,holdingList(i)]; end end holdingList=holdingList2(2:end);end%--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------end
下载完整策略源码及执行代码请登录DigQuant社区-策略资源下载:http://www.digquant.com.cn/stra.php
这篇关于matlab rsi的交易策略源码,RSI择时 附策略源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!