主要内容

本页采用了机器翻译。点击此处可查看英文原文。

绘制 MACD 指标

此示例显示如何构建时间表并绘制移动平均收敛/发散 (MACD) 指标。MACD 是用于股票价格技术分析的交易指标。MACD 可以指示是否买入或卖出股票、何时出现超买或超卖情况以及何时趋势可能结束。

读取数据

ThingSpeak 通道 471839 包含 Google (纳斯达克股票代码:GOOGL).数据每天更新一次。前四个字段分别包含开盘价、最高价、最低价和收盘价。第五个字段包含每日交易量。使用 thingSpeakRead 函数从通道 471939 读取数据。

stockTimeTable=thingSpeakRead(471839,'numDays',150,'outputFormat','TimeTable');

生成 MCAD 数据并标记交叉点

当 MACD 与信号线刚刚交叉,且 MACD 线跌破移动平均线时,是卖出时机。使用 macd 函数构建 MACD 线和移动平均线或信号线数据。然后扫描数据点以寻找线条交叉的点,并将这些点保存到数组中以进行绘图。

[MACDLine, signalLine]= macd(stockTimeTable);

index=26;
crossUp=[];
crossDown=[];

while (index<height(stockTimeTable))
    if and(MACDLine.Close(index)>signalLine.Close(index),MACDLine.Close(index-1) < signalLine.Close(index-1))
       crossUp=[crossUp index];
    end

    if and(MACDLine.Close(index)<signalLine.Close(index),MACDLine.Close(index-1) > signalLine.Close(index-1))
       crossDown=[crossDown index];
    end
    index=index+1;
end

绘制 MACD 和信号指标

使用 plot 函数为 Google 绘制 MACD。使用 text 函数添加跨线标签。

plot(MACDLine.Time,MACDLine.Close,'r+-',signalLine.Time,signalLine.Close, 'b--');
legend('MACD Line','Nine Per MA')
title('MACD for GOOG')
ylabel("Price Averages");
hold;
text(MACDLine.Time(crossUp),MACDLine.Close(crossUp),'B');
text(MACDLine.Time(crossDown),MACDLine.Close(crossDown),'S');
Current plot held

该图显示了谷歌的 MACD。MACD 线在几个位置与信号线相交,表明存在一些潜在的盈利交易机会。由于该图是根据实时数据生成的,因此您的图与此处显示的示例不同。

另请参阅

函数

主题