Ai
31 Star 83 Fork 43

EliteQuant/EliteQuant_Matlab

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
MovingAverageCrossStrategy.m 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
EliteQuant 提交于 2017-11-26 11:38 +08:00 . 初始化
classdef MovingAverageCrossStrategy < StrategyBase
properties
shortWindow
longWindow
nBars
invested
prices
end
methods (Access = public)
function self = MovingAverageCrossStrategy(symbols)
self = self@StrategyBase(symbols); % call base class
self.shortWindow = 50;
self.longWindow = 200;
self.reset();
end
function reset(self)
self.nBars = 0;
self.invested = false;
self.prices = [];
end
function onbar(self, barEvent)
if (self.symbols{1} == barEvent.fullSymbol)
self.prices = [self.prices, barEvent.adjClosePrice];
self.nBars = self.nBars + 1;
if (self.nBars >= self.longWindow)
shortSMA = mean(self.prices(end-self.shortWindow+1:end));
longSMA = mean(self.prices(end-self.longWindow+1:end));
if ((shortSMA > longSMA) && (~self.invested))
disp(['long ' datestr(barEvent.barendtime()) ' short sma ' num2str(shortSMA) ' long sma ' num2str(longSMA)]);
o = OrderEvent();
o.fullSymbol = self.symbols{1};
o.orderType = OrderType.MARKET;
o.size = 100;
self.placeorder(o);
self.invested = true;
elseif ((shortSMA < longSMA) && (self.invested))
disp(['short ' datestr(barEvent.barendtime()) ' short sma ' num2str(shortSMA) ' long sma ' num2str(longSMA)]);
o = OrderEvent();
o.fullSymbol = self.symbols{1};
o.orderType = OrderType.MARKET;
o.size = -100;
self.placeorder(o);
self.invested = false;
end
end
end
end
end
end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Matlab
1
https://gitee.com/EliteQuant/EliteQuant_Matlab.git
git@gitee.com:EliteQuant/EliteQuant_Matlab.git
EliteQuant
EliteQuant_Matlab
EliteQuant_Matlab
master

搜索帮助