分析一下TD官方的MACD代码
颜色的预定义值查看这里:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look—Feel/GetColor
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#
# Study作用于lower区域
declare lower;
# 快线周期
input fastLength = 12;
# 慢线周期
input slowLength = 26;
# 信号周期
input MACDLength = 9;
# 移动平均类型 - 指数移动平均
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
# 快慢线移动平均值差 = 12天的指数移动平均 - 26天的指数移动平均
plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
# 快慢线移动平均值差的9天指数平均值
plot Avg = MovingAverage(averageType, Value, MACDLength);
# 差离值 = 快慢线移动平均值差 - 快慢线移动平均值差的9天指数平均值
plot Diff = Value - Avg;
# 零轴
plot ZeroLine = 0;
# 向上的信号 = 差离值Diff向上穿过零轴
plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
# 向下的信号 = 差离值Diff向下穿过零轴
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;
# 开关(先忽略)
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
# 快慢线移动平均值差设置颜色值1
Value.SetDefaultColor(GetColor(1));
# 快慢线移动平均值差9日指数移动平均先设置颜色值8
Avg.SetDefaultColor(GetColor(8));
# 差离值设置默认颜色值5
Diff.SetDefaultColor(GetColor(5));
#
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#
Diff.SetLineWeight(3);
# 定义颜色别名
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
# 设定颜色
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
# 零轴先设置默认颜色值0
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);