Main Content

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

绘制 MACD 指标

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

读取数据

ThingSpeak通道471839 包含 Google 延迟的财务数据(纳斯达克:谷歌)。数据每天更新一次。前四个字段分别包含开盘价、最高价、最低价和收盘价。第五字段包含每日交易量。使用 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

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

另请参阅

函数

相关主题