买入信号赋值为entrysig 卖出信号赋值为exitsig
时间: 2024-02-12 11:06:21 浏览: 62
信号的抽取与插值
5星 · 资源好评率100%
好的,以下是将买入信号赋值为entrysig,将卖出信号赋值为exitsig的代码:
```matlab
% 计算KDJ指标
% 输入参数
high = [10, 12, 15, 14, 16, 18, 20, 22, 25, 24]; % 最高价
low = [8, 9, 11, 10, 12, 15, 17, 18, 19, 22]; % 最低价
close = [9, 11, 14, 12, 15, 17, 19, 21, 23, 23]; % 收盘价
n = 9; % 周期
m1 = 3; % 平滑因子1
m2 = 3; % 平滑因子2
% 计算RSV值
for i = n:length(close)
Cn = close(i);
Hn = max(high(i-n+1:i));
Ln = min(low(i-n+1:i));
RSV(i) = (Cn - Ln) / (Hn - Ln) * 100;
end
% 计算K值、D值、J值
K(1) = 50;
D(1) = 50;
for i = 2:length(close)
K(i) = (m1-1)/m1*K(i-1) + 1/m1*RSV(i);
D(i) = (m2-1)/m2*D(i-1) + 1/m2*K(i);
J(i) = 3*K(i) - 2*D(i);
end
% 计算交易信号
for i = 2:length(close)
if K(i) > D(i) && K(i-1) < D(i-1) % K线上穿D线
entrysig(i) = 1; % 买入信号
exitsig(i) = 0;
elseif K(i) < D(i) && K(i-1) > D(i-1) % K线下穿D线
entrysig(i) = 0;
exitsig(i) = 1; % 卖出信号
else
entrysig(i) = 0; % 无信号
exitsig(i) = 0;
end
end
```
在上述代码中,买入信号被赋值为entrysig,卖出信号被赋值为exitsig,同时也对无信号进行了赋值。
阅读全文