自动画MACD背离线通达信指标公式源码
- 格式:doc
- 大小:510.50 KB
- 文档页数:2
MACD完美波段通达信指标公式源码通达信是一种股票分析软件,可以对股票进行技术分析,并提供一些指标来判断股票的走势。
MACD(Moving Average Convergence Divergence)是一种常用的股票技术指标,用于确定股票价格的趋势和可能的转折点。
以下是通达信中计算MACD指标的公式源码。
#定义MACD函数def MACD(close, fastPeriod=12, slowPeriod=26,signalPeriod=9):#计算快速移动平均线ema_fast = EMA(close, fastPeriod)#计算慢速移动平均线ema_slow = EMA(close, slowPeriod)#计算DIF(差离值)dif = ema_fast - ema_slow#计算DEA(信号线)dea = EMA(dif, signalPeriod)#计算MACD(柱状线)macd = (dif - dea) * 2return dif, dea, macd#计算移动平均线def EMA(close, period):ema = [0] * len(close)ema[period-1] = sum(close[:period]) / periodfor i in range(period, len(close)):ema[i] = ((2 / (period + 1)) * close[i]) + ((1 - (2 / (period + 1))) * ema[i-1])return ema#示例用法close = [10, 12, 14, 16, 18, 20, 22, 24, 26, 28]dif, dea, macd = MACD(close)print("DIF:", dif)print("DEA:", dea)print("MACD:", macd)在上面的代码中,我们首先定义了一个MACD函数,接受一个收盘价的列表作为参数,以及可选的快速移动平均线期间、慢速移动平均线期间、信号线期间参数,默认值分别为12、26、9、然后,在MACD函数中,我们调用了一个EMA函数来计算移动平均线的值。
自动画MACD背离线通达信指标公式源码以下是通达信软件中计算MACD背离指标的源码:```/*功能:计算MACD背离线指标公式:当股价形成新高而MACD指标不形成新高时,出现向下背离当股价形成新低而MACD指标不形成新低时,出现向上背离参数:无返回值:无*/indicator fn_DEVIATION_MACDMA1:=EMA(CLOSE,12);MA2:=EMA(CLOSE,26);DIFF:=MA1-MA2;DEA:=EMA(DIFF,9);MACD:=2*(DIFF-DEA);PeakArr:=[];TroughArr:=[];PeakCount:=0;TroughCount:=0;//寻找股价的峰值和谷底Loop:=BARSSINCE(HIGHESTBARS(HIGH,9)) + 1;FOR i FROM Loop TO BARCOUNT-1BEGINIFHIGH[i]>REF(HIGH,1)[i]ANDHIGH[i]>HIGH[i+1]THENBEGINPeakCount := PeakCount + 1;PeakArr[PeakCount] := i;END;IFLOW[i]<REF(LOW,1)[i]ANDLOW[i]<LOW[i+1]THENBEGINTroughCount := TroughCount + 1;TroughArr[TroughCount] := i;END;END;//判断是否存在背离BEARTrend := IF(REF(MACD,1) > REF(MACD,2),1,-1); // 1表示下跌趋势,-1表示上涨趋势PeakPos := 0;TroughPos := 0;Signal := "";FOR i FROM 1 TO MIN(PeakCount, TroughCount) BEGINIF BEARTrend = 1 THENBEGINIF HIGH[PeakArr[i]] > HIGH[PeakArr[i-1]] THEN BEGINIF LOW[TroughArr[i]] > LOW[TroughArr[i-1]] THEN BEGINPeakPos := PeakArr[i];TroughPos := TroughArr[i];Signal := "向下背离";BREAK;END;END;END;ELSE IF BEARTrend = -1 THENBEGINIF HIGH[PeakArr[i]] < HIGH[PeakArr[i-1]] THENBEGINIF LOW[TroughArr[i]] < LOW[TroughArr[i-1]] THENBEGINPeakPos := PeakArr[i];TroughPos := TroughArr[i];Signal := "向上背离";BREAK;END;END;END;END;//输出背离信号IF PeakPos > 0 AND TroughPos > 0 THENBEGINDRAWICON(PeakPos, HIGH[PeakPos], CONCAT(Signal, "顶背离"),IC_TRIANGLE, 0, RED);DRAWICON(TroughPos, LOW[TroughPos], CONCAT(Signal, "底背离"), IC_TRIANGLE, 1, GREEN);END;//返回背离信号位置RETURN PeakPos, TroughPos;```以上代码使用通达信魔法公式语言编写,通过计算MACD指标的DIF、DEA、MACD三个值,并寻找股价的峰值和谷底来判断是否存在背离信号。
背离准确率100%N:=60;K值:="KDJ.K";DD:=C=LLV(C,N);{60日最低点}DDSJ:=BARSLAST(REF(DD,1));{上一低点时间}DDGJ:=REF(CLOSE,DDSJ+1)>CLOSE;KDZB:=REF(K值,DDSJ+1);K底背离:=DDGJ AND KDZB;GD:=C=HHV(C,N);{60日最高点}GDSJ:=BARSLAST(REF(GD,1));{上一高点时间}GDGJ:=REF(CLOSE,GDSJ+1);KGZB:=REF(K值,GDSJ+1)>K值;K顶背离:=GDGJ AND KGZB;MACD:="MACD.MACD";MDZB:=REF(MACD,DDSJ+1);M底背离:=DDGJ AND MDZB;MGZB:=REF(MACD,GDSJ+1)>MACD;M顶背离:=GDGJ AND MGZB;RSI:="RSI.RSI1";RDZB:=REF(RSI,DDSJ+1);R底背离:=DDGJ AND RDZB;RGZB:=REF(RSI,GDSJ+1)>RSI;R顶背离:=GDGJ AND RGZB;TYP:=(HIGH + LOW + CLOSE)/3;CCI:=(TYP-MA(TYP,N))/(0.015*AVEDEV(TYP,N));CDZB:=REF(CCI,DDSJ+1);C底背离:=DDGJ AND CDZB;CGZB:=REF(CCI,GDSJ+1)>CCI;C顶背离:=GDGJ AND CGZB;REFLINE: -5,15,30,COLORBLACK;DRAWTEXT(K顶背离,IF(ISLASTBAR,27.5,25),'◆'),LINETHICK2 ; DRAWTEXT(M顶背离,(ISLASTBAR,24.5,20),'◆'),LINETHICK2 ; DRAWTEXT(R顶背离,(ISLASTBAR,20.5,15.1),'◆'),LINETHICK2 ; DRAWTEXT(C顶背离,(ISLASTBAR,16.6,10.1),'◆'),LINETHICK2 ; DRAWTEXT(ISLASTBAR,10.1 ,'CCI '),LINETHICK2,COLORGREEN; DRAWTEXT(C底背离,IF(ISLASTBAR,12,10.1 ),'◆'),LINETHICK2,COLORRED; DRAWTEXT(ISLASTBAR,15,'RSI '),LINETHICK2,COLORRED; DRAWTEXT(R底背离,IF(ISLASTBAR,8.5,15),'◆'),LINETHICK2,COLORRED; DRAWTEXT(ISLASTBAR,20,'MACD '),LINETHICK2,COLORMAGENTA; DRAWTEXT(M底背离,IF(ISLASTBAR,4.5,20),'◆'),LINETHICK2,COLORRED; DRAWTEXT(ISLASTBAR,25,'KDJ '),LINETHICK2,COLORRED; DRAWTEXT(K底背离,IF(ISLASTBAR,1.5,25),'◆'),LINETHICK2,COLORRED;。
MACD背离自动画线通达信指标公式源码MACD(Moving Average Convergence Divergence)是一种常用的股票技术指标,用于分析股票的趋势和交易信号。
MACD背离是一种常见的技术分析方法,用于判断趋势的反转点,即价格走势与MACD指标走势不一致的情况。
下面是使用通达信编写MACD背离自动画线指标的源码。
```c/*MACD背离自动画线指标*功能:自动识别MACD背离点并在图表中标注出来*/#include "stdafx.h"#include "stdio.h"#include "string.h"#include "windows.h"//确定最低点、最高点时使用的价格周期数#define MIN_PRICE_DURATION 9#define MAX_PRICE_DURATION 10/**输入参数:*MACD:一个包含MACD数据的数组* price:一个包含价格数据的数组*返回值:*0:没有背离*1:向上背离*-1:向下背离*/int CheckDivergence(float *MACD, float *price) int i, j, k;int start, end;int low_index, high_index;float min_price, max_price;int MACD_bottom, MACD_top;int price_bottom, price_top;float MACD_bottom_value, MACD_top_value;float price_bottom_value, price_top_value;/**阶段1:寻找MACD底背离*/start = MAX_PRICE_DURATION + 1;end = start + MIN_PRICE_DURATION;low_index = -1;high_index = -1;min_price = price[start];max_price = price[start];for (i = start; i < end; i++)//查找最低点if (price[i] < min_price)low_index = i;min_price = price[i];}//查找最高点if (price[i] > max_price)high_index = i;max_price = price[i];}}//无法找到底背离点if (low_index == -1 , high_index == -1)}MACD_bottom = 0;price_bottom = 0;//寻找MACD底背离for (i = start; i < end; i++)if (i == low_index)//找到最低点对应的MACD数值MACD_bottom = i;MACD_bottom_value = MACD[i];}if (i == high_index)//找到最高点对应的MACD数值price_bottom = i;price_bottom_value = price[i];}}//如果最低点出现在最高点之后,则没有底背离if (MACD_bottom > price_bottom)}/**阶段2:寻找MACD顶背离*/start = MAX_PRICE_DURATION + 1; end = start + MIN_PRICE_DURATION; low_index = -1;high_index = -1;min_price = price[start];max_price = price[start];for (i = start; i < end; i++)//查找最低点if (price[i] < min_price)low_index = i;min_price = price[i];}//查找最高点if (price[i] > max_price)max_price = price[i];}}//无法找到顶背离点if (low_index == -1 , high_index == -1) return 0;}MACD_top = 0;price_top = 0;//寻找MACD顶背离for (i = start; i < end; i++)if (i == low_index)//找到最低点对应的MACD数值MACD_top = i;MACD_top_value = MACD[i];}if (i == high_index)//找到最高点对应的MACD数值price_top_value = price[i];}}//如果最高点出现在最低点之后,则没有顶背离if (MACD_top > price_top)return 0;}//判断背离类型if (price_bottom_value < price_top_value && MACD_bottom_value > MACD_top_value)return 1; // 向上背离}if (price_bottom_value > price_top_value && MACD_bottom_value < MACD_top_value)return -1; // 向下背离}return 0;int mainfloat MACD[100] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 4.0, 3.0, 2.0, 1.0};float price[100] = {1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 0.0, -1.0, -2.0};int result = CheckDivergence(MACD, price);printf("Result: %d\n", result);return 0;```该源码实现了一个简单的MACD背离自动画线指标。
常用的MACD三次背离副图指标通达信源码自己常用的MACD,严重底背离红柱子和底背离绿柱子,三次背离底部划线等, 主要看图比较清晰!DIFIF1:(EMA(CLOSE,12) - EMA(CLOSE,26))*100/EMA(CLOSE Z26); DEAEA1:EMA(DIFIF1,9);MACD1:=2*(DIFIF1-DEAEA1),STICK;MACD2:=EMA(MACD1,3);STICKLINE(MACD 1 >MACD2 AND MACD1>0,0,MACD1,3,0),COLORRED; STICKLINE(MACD 1 >MACD2 AND MACD1<O Z O,MACD1,3,0),COLORGREEN; STICKLINE(MACD1<MACD2,0,MACD1Z3,0),COLORCBCBCO;STICKLINE(MACDl>=0 OR MACDl<=O z O z O z50,l)z COLORWHITE;DIFIF1,COLOR33FF33,LINETHICK2;DEAEAl’COLORYELLOW 丄INETHICK2;MACD: MACD1,NODRAW,COLORMAGENTA;强:IF(DIFIF1>=DEAEA1,DIFIF1QRAWNULL),COLORRED 丄INETHICK2;弱:IF(DEAEA1>=DIFIF1Z DEAEA1,DRAWNULL)Z COLORFF9900,LINETHICK2; DRAWICON(CROSS(DIFIF1,DEAEA1),DIFIF1+O.l,l);DRAWICON(CROSS(DEAEA1,DIFIF1),0.4,2);Al:=BARSLAST(REF(CROSS(n MACD. DIF H z n MACD. DEA n)z 1));B1:=REF(C,A1+1)>C AND REF(H MACD.DIF,,,Al+l)<H MACD.DIF n AND CROSS(n MACD.DIF,,,n MACD.DEA n);DRAWTEXT(Bl>0z-0.8;MACD 底背'),COLORFFOOFF;A2:=BARSLAST(REF(CROSS(,,KDJ.K,,/,,KDJ.D,,)Z1));B2:=REF(C,A2+1)>C AND REF(^^KDJ.K^,,A24-1)<^^KDJ.K,, ANDCROSS(,,KDJ.K,,z,,KDJ.D n);DRAWTEXT(B2z-0.6;KDJ 底离'),COLORYELLOW;{KDJ 底背离};A3:=BARSLAST(REF(CROSS(,,RSLRSI1,,Z,,RSI.RSI2,,),1));B3:=REF(C,A3+1)>C AND REF(,,RSLRSIl,,z A3+l)<,,RSI.RSir,AND CROSS(”RSI・RSI1TRSI・RSI2J;DRAWTEXT(B3>0,-0.3z'RSI 底离'),COLORCYAN;{RSI 底背离};C1:=BARSLAST(REF(CROSS(,,MACD.DEA,,/,MACD.DIF,,),1));Dl:=REF(C,Cl+l)vC AND [^=(“1^。
通达信DMI,MACD.RSI背离指标公式N:=14;MM:=6;RSV:=(CLOSE-LLV(LOW,10))/(HHV(HIGH,10)-LLV(LOW,10))*100;K:=SMA(RSV,3,1);D:=SMA(K,3,1);J:=3*K-2*D;DIFF:=EMA(CLOSE,12) - EMA(CLOSE,26);DEA:=EMA(DIFF,9);MACD:=2*(DIFF-DEA), COLORSTICK;LC:=REF(CLOSE,1);RSI1:=SMA(MAX(CLOSE-LC,0),6,1)/SMA(ABS(CLOSE-LC),6,1)*100;RSI2:=SMA(MAX(CLOSE-LC,0),12,1)/SMA(ABS(CLOSE-LC),12,1)*100;RSI3:=SMA(MAX(CLOSE-LC,0),24,1)/SMA(ABS(CLOSE-LC),24,1)*100;A1:=BARSLAST(REF(CROSS(DIFF,DEA),1));B1:=REF(CLOSE,A1+1)>CLOSE AND REF(DIFF,A1+1)<DIFF AND CROSS(DIFF,DEA);MACD底背离:FILTER(B1>0,5);STICKLINE(FILTER(B1>0,5),0,10,1,0),COLOR00FF00;DRAWTEXT(FILTER(B1>0,5),10,'MACD底背'),COLOR00FF00;A3:=BARSLAST(REF(CROSS(RSI1,RSI2),1));B3:=REF(CLOSE,A3+1)>CLOSE AND REF(RSI1,A3+1)<RSI1 AND CROSS(RSI1,RSI2);RSI底背离:FILTER(B3>0,5),LINETHICK0;STICKLINE(FILTER(B3>0,5),0,30,1,0),COLORFFFF00;DRAWTEXT(FILTER(B3>0,5),30,'RSI底背'),COLORFFFF00;C1:=BARSLAST(REF(CROSS(DEA,DIFF),1));D1:=REF(CLOSE,C1+1)<CLOSE AND REF(DIFF,C1+1)>DIFF AND CROSS(DEA,DIFF);MACD顶背离:FILTER(D1>0,5),LINETHICK0;STICKLINE(FILTER(D1>0,5),100,80,1,0),COLOR0066FF;DRAWTEXT(FILTER(D1>0,5),80,'MACD顶背'),COLOR0066FF;C3:=BARSLAST(REF(CROSS(RSI2,RSI1),1));D3:=REF(CLOSE,C3+1)<CLOSE AND REF(RSI1,C3+1)>RSI1 AND CROSS(RSI2,RSI1);RSI顶背离:FILTER(D3>0,5),LINETHICK0;STICKLINE(FILTER(D3>0,5),100,80,1,0),COLOR15F70A;DRAWTEXT(FILTER(D3>0,5),80,'RSI顶背'),COLOR15F70A;{以上不动}TR1:=EMA(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(REF(CLOSE,1)-LOW)),21);HD :=HIGH-REF(HIGH,1);LD :=REF(LOW,1)-LOW;DMP:=EMA(IF(HD>0 AND HD>LD,HD,0),21);DMM:=EMA(IF(LD>0 AND LD>HD,LD,0),21);PDI:= DMP*100/TR1;MDI:= DMM*100/TR1;ADX: EMA((PDI-MDI)/(MDI+PDI)*100,6)*2;ADXR:EMA(ADX,6);ADMA:=EMA(ADX,20);OMD:(ADX-2*ADMA+ADXR)/2, COLORSTICK;DRAWBAND(ADX,RGB(155,50,50),ADXR,RGB(0,100,50));STICKLINE(OMD>REF(OMD,1) AND OMD>0,0,OMD,1,0),COLORRED;STICKLINE(OMD>REF(OMD,1) ANDOMD<0,0,OMD,1,0),COLORGREEN;STICKLINE(OMD<REF(OMD,1),0,OMD,1,0),COLORCBCBC0;STICKLINE(OMD>=0 OR OMD<=0,0,0,50,1),COLORWHITE;ADX1:ADX,COLORRED;ADXR1:ADXR,COLORYELLOW;{DMI顶底背离}DM1:=BARSLAST(REF(CROSS(ADX1,ADXR1),1));DM2:=REF(CLOSE,DM1+1)>CLOSE AND REF(ADX1,DM1+1)<ADX1 AND CROSS(ADX1,ADXR1);DMI底背离:FILTER(DM2>0,5),LINETHICK0;DRAWTEXT(FILTER(DM2>0,5),ADX1,'DMI底背'),COLORFFFF00;DM3:=BARSLAST(REF(CROSS(ADXR1,ADX1),1));DM4:=REF(CLOSE,DM3+1)<CLOSE AND REF(ADX1,DM3+1)>ADX1 AND CROSS(ADXR1,ADX1);DMI顶背离:FILTER(DM4>0,5),LINETHICK0;DRAWTEXT(FILTER(DM4>0,5),ADX1,'DMI顶背'),COLORGREEN;。
UPD:COUNT(C>=O OR C>=REF(C,1),8) NODRAW;CM60:BARSLAST(LONGCROSS(C,MA(C,60),8))+1 NODRAW COLORRED; DIFF:=EMA(CLOSE,12)-EMA(CLOSE,24);DEA:=EMA(DIFF,9);ADIF:ATAN((DIFF/REF(DIFF,1)-1)*100)*180/3.1416 NODRAW; RADIF:REF(ADIF,1) NODRAW;C2:=IF(V>MA(V,150)*0.98,0.1,-0.1);C1:=IF(V<MA(V,150)*0.98,-0.1,0.1);B2:=EMA(CLOSE,55)-MA(CLOSE,60) DOTLINE COLORGREEN;B3:=EMA(B2,9) DOTLINE COLORCYAN;DRAWBAND(B2,RGB(120,50,50),B3,RGB(50,120,50));MACD:=2*(DIFF-DEA), STICK,LINETHICK1 COLORCYAN;N:=20;MID:=MA(C,20);VART1:=POW((C-MID),2);VART2:=MA(VART1,N);VART3:=SQRT(VART2);UPPER:=MID+2*VART3;LOWER:=MID-2*VART3;BOLL:=REF(MID,1),COLORGREEN;UB:=REF(UPPER,1),COLOR00FFFF;LB:=REF(LOWER,1),COLOR00FFFF NODRAW;BLTJ:=REF(L,1)<REF(LB,1) AND L>LB;BLLDT:BARSLAST(BLTJ ) NODRAW;JCCOUNT:=COUNT(CROSS(DIFF,DEA),BARSLAST(DEA>=0));A1:=BARSLAST(REF(CROSS(DIFF,DEA),1)) COLORRED NODRAW;底背离:REF(CLOSE,A1+1)>CLOSE AND DIFF>REF(DIFF,A1+1) AND CROSS(DIFF,DEA) NODRAW;STICKLINE(底背离,0,0.03,1.2,1),COLOR00FF00;{底背:DRAWLINE(A1=0,DEA,底背离,DEA,0),COLORRED,LINETHICK2 CROSSDOT;}DRAWTEXT(底背离,0.1,' 底背离'),COLORFF00FF;A2:=BARSLAST(REF(CROSS(DEA,DIFF),1)) ;顶背离:REF(CLOSE,A2+1)<CLOSE AND REF(DIFF,A2+1)>DIFF AND CROSS(DEA,DIFF) CROSSDOT COLORRED NODRAW;{顶背:DRAWLINE(A2=0,DEA,顶背离,DEA,0),COLORBLUE,LINETHICK2 CROSSDOT;}底背D:BARSLAST(底背离)+1 NODRAW COLORRED;顶背D:A2+1 COLORGREEN NODRAW;{判断天地量}AA:=SUMBARS(VOL,CAPITAL);地量:=VOL<MA(VOL,AA)*21/100 AND VOL>0;天量:=VOL>MA(VOL,AA)*34/10 ;MACDA:2*(DIFF-DEA), STICK,LINETHICK1 COLORCYAN;NOTEXT3:IF(地量AND PERIOD>4,MACD*1.5,DRAWNULL) STICK LINETHICK6 COLORYELLOW;NOTEXT4:IF(天量AND PERIOD>4,MACD*1.5,DRAWNULL) STICK LINETHICK6 COLORAAAAFF;NOTEXT5:IF(V<=MA(V,5)*0.22,MACD,DRAWNULL) STICK LINETHICK4 COLORBLUE;NOTEXT:IF(REF(MACD,1)<=MACD,MACD,DRAWNULL) STICK COLORRED; NOTEXT1:0 LINETHICK3 COLORGRAY;NOTEXT8:IF(V>MA(V,150)*0.98,0,DRAWNULL) COLORRED LINETHICK2 CIRCLEDOT ;DEAA:DEA COLORYELLOW;DIFA:DIFF COLORWHITE ;DIFF1:EMA(CLOSE,12)-EMA(CLOSE,26) DOTLINE COLORGREEN NODRAW; DEA1:EMA(DIFF1,9) DOTLINE COLORCYAN NODRAW;底背:DRAWLINE(A1=0,DEA,底背离,DEA,0),COLORRED,LINETHICK2 CROSSDOT;顶背:DRAWLINE(A2=0,DEA,顶背离,DEA,0),COLORBLUE,LINETHICK2 CROSSDOT;强:IF(DIFF>MA(DIFF,2),DIFF,DRAWNULL),COLORRED LINETHICK2;弱:IF(DEA>MA(DEA,2),DEA,DRAWNULL),COLORMAGENTA LINETHICK2;必买: DRAWICON(REF(TROUGHBARS(3,15,1),1)=0 ANDHIGH>LOW,0,25),COLORRED ,LINETHICK4;必卖:DRAWICON(REF(PEAKBARS(3,15,1),1)=1,0,27),COLORRED,LINETHICK8;B1:A1 NODRAW;TOP:A2 NODRAW;BSR:((BIDORDERVOL-BIDCANCELVOL)/(OFFERORDERVOL-OFFERCANCELVOL )-1)*10 NODRAW;NOTEXTBRS:IF(BSR>0,DEA,DRAWNULL) COLORFFFFFF LINETHICK2 CIRCLEDOT;HSJ:=BARSLAST(CROSS(DIFF,0) AND DIFF>=0 )+1 NODRAW;LSJ:=BARSLAST(CROSS(0,DIFF) AND DIFF<0 )+1 NODRAW;T:IF(DIFF>0,HSJ,LSJ) NODRAW;IF(T=1,ABS(MACD),0) STICK LINETHICK2 COLORMAGENTA;IF(CROSS(DEA,0),ABS(MACD),0) STICK LINETHICK2 COLORYELLOW;S:IF(HSJ>1,SUM(ABS(MACD),T),DRAWNULL) NODRAW ; DRAWNUMBER(ISLASTBAR ,ABS(MACD*1.02),S) COLORYELLOW; DRAWNUMBER(T=1 ,ABS(MACD*1.02),REF(S,1)) ;T2:REF(T,T)+T NODRAW;Q1S:REF(S,T) NODRAW;Q2S:=REF(S,T2) NODRAW;BL:=Q1S/Q2S NODRAW;{E2T:BARSLAST(CROSS(DIF,DEA) AND FILTER(CROSS(DIF,DEA) ,5) )+1 NODRAW;E6T:BARSLAST(CROSS(DEA,DIF) AND FILTER(CROSS(DEA,DIF) ,5) )+1 NODRAW;ET:IF(DIF>=DEA,E2T,E6T) NODRAW;ES:SUM(ABS(DIF-DEA),ET) NODRAW;BZ:=MAX(ABS(DEA),ABS(DIF));XS:=IF(DIF<0,-1,1);IF(ET=1,DIF,DRAWNULL) CIRCLEDOT LINETHICK2;DRAWNUMBER(ISLASTBAR ,DIF*1.05,ES) COLORYELLOW; DRAWNUMBER(ET=1 ,BZ*XS*1.05,REF(ES,1)) COLORYELLOW;};TTJ:=IF(BARSLAST(顶背离=1)+1=144 OR BARSLAST(顶背离=1)+1=89 OR BARSLAST(顶背离=1)+1=13 OR BARSLAST(顶背离=1)+1=47 ORBARSLAST(顶背离=1)+1=1 OR BARSLAST(顶背离=1)+1=8 OR BARSLAST(顶背离=1)+1=21 OR BARSLAST(顶背离=1)+1=34 OR BARSLAST(顶背离=1)+1=55,1,O) ;TTJA:=IF( BARSLAST(底背离=1)+1=144 OR BARSLAST(底背离=1)+1=89 OR BARSLAST(底背离=1)+1=8 OR BARSLAST(底背离=1)+1=1 OR BARSLAST(底背离=1)+1=47 OR BARSLAST(底背离=1)+1=13 OR BARSLAST(底背离=1)+1=21 OR BARSLAST(底背离=1)+1=34 OR BARSLAST(底背离=1)+1=55,1,O) ;DRAWNUMBER((BARSLAST(顶背离=1)+1<144 AND TTJ=1 AND CURRBARSCOUNT<233) OR ISLASTBAR,MIN(DIFF,DEA)*0.98,BARSLAST(顶背离=1)+1) COLORGREEN;DRAWNUMBER((BARSLAST(底背离=1)+1<144 AND TTJA=1 AND CURRBARSCOUNT<233)OR ISLASTBAR,0,BARSLAST(底背离=1)+1) COLORYELLOW;。
MACD顶底背离通达信指标公式源码
MACD顶底背离通达信指标公式源码
软件介绍
本公式在日线,小时线,半小时线等周期中根据MACD红柱的缩小都能及时给我们的操作做出风险提示,尤其是在短周期中提示更有效。
此公式不是万能的,还需要结合其他操盘指标才更有效。
DIF:EMA(CLOSE,12)-EMA(CLOSE,26);
DEA:EMA(DIF,9);
MACD:(DIF-DEA)*2,COLORSTICK;
H1:=REF(MACD,1)<0 AND MACD>0,NODRAW;
H1T:=BARSLAST(H1)+1,NODRAW;
H2:=IF(MACD>0,H1T,0),NODRAW;
H3:=HHV(MACD,H2),NODRAW;
L1:=REF(MACD,1)>0 AND MACD<0,NODRAW;
L1T:=BARSLAST(L1)+1,NODRAW;
HJ:=(H1T+L1T),NODRAW;
H4:=REF(H3,HJ),NODRAW;
顶背离:H3<H4 AND C>REF(C,HJ) AND MACD>0 AND MACD<REF(MACD,1),COLORGREEN,NODRAW; STICKLINE(顶背离,0,MACD,2,0),COLORGREEN;
下载地址。
MACD2:=2*(MACD.DIF-MACD.DEA);STICKLINE(MACD2>=0 AND MACD2>REF(MACD2,1),0,MACD2,1.2,0),COLOR0000A5; STICKLINE(MACD2>=0 AND MACD2<REF(MACD2,1),0,MACD2,1.2,0),COLORAA7800; STICKLINE(MACD2<0 AND MACD2<REF(MACD2,1),0,MACD2,1.2,0),COLOR00A500; STICKLINE(MACD2<0 AND MACD2>REF(MACD2,1),0,MACD2,1.2,0),COLORA500A5; DIF:EMA(CLOSE,12)-EMA(CLOSE,26);DEA:EMA(DIF,9);MACD:(DIF-DEA)*2,NODRAW;{MACD日线二次金叉}JCCOUNT:=COUNT(CROSS(DIF,DEA),BARSLAST(DEA>=0));二次金叉:=CROSS(DIF,DEA) AND DEA<0 AND COUNT(JCCOUNT=2,21)=1; DRAWTEXT(二次金叉,DEA*1.25,'零下二金'),COLORWHITE;{MACD日线底背离}A1:=BARSLAST(REF(CROSS(DIF,DEA),1));底背离:=REF(CLOSE,A1+1)>CLOSE AND DIF>REF(DIF,A1+1) AND CROSS(DIF,DEA); DRAWTEXT(底背离,DIF*0.5,'日底背'),COLORRED;{日底背画线:DRAWLINE(A1=0,DEA,底背离,DEA,0),COLORRED,DOTLINE;}{MACD日线顶背离}A2:=BARSLAST(REF(CROSS(DEA,DIF),1));顶背离:=REF(CLOSE,A2+1)<CLOSE AND REF(DIF,A2+1)>DIF AND CROSS(DEA,DIF); DRAWTEXT(顶背离,DIF*1.12,'日顶背'),COLORGREEN;{日顶背画线:DRAWLINE(A2=0,DEA,顶背离,DEA,0),COLORGREEN,DOTLINE;}{MACD_KDJ的金叉共振}{DIF:EMA(CLOSE,12)-EMA(CLOSE,26);DEA:EMA(DIF,9);}RSV:=(CLOSE-LLV(LOW,9))/(HHV(HIGH,9)-LLV(LOW,9));K:=SMA(RSV,9,3),NODRAW;D:=SMA(K,9,3),NODRAW;J:=3*K-2*D,NODRAW;M&K金共:=CROSS(DIF,DEA) AND CROSS(K,D) AND CROSS(J,D);DRAWTEXT(M&K金共,DEA*1.12,'金叉共振'),COLORYELLOW;{MACD柱背离}MACD高:=HHV(MACD,5);C1:=BARSLAST(REF(CROSS(DEA,DIF),1));E:=REF(C,C1+1)>C AND DEA>0 AND REF(MACD高,C1+1)<MACD高AND CROSS(DEA,DIF) ;DRAWTEXT(E>0,-0.5,'柱顶背'),COLORCYAN;F:=REF(C,A1+1)>C AND DEA<0 AND REF(MACD高,A1+1)<MACD高AND CROSS(DIF,DEA);DRAWTEXT(F>0,0.5,'柱底背'),COLORLIRED;{MACD积背离}JC:= DEA-DIF;绿柱面积:=IF(MACD<0,SUM(MACD,BARSLAST(JC<0)),0);红柱面积:=IF(MACD>0,SUM(MACD,BARSLAST(JC>0)),0);本次最低价:=LLV(L,BARSLAST(JC<0));本次最高价:=HHV(H,BARSLAST(JC>0));X1:=IF (MACD<0,BARSLAST(CROSS(DIF,DEA)),0);前次绿柱面积:=REF(绿柱面积,X1+1);前次最低价:=REF(本次最低价,X1+1);Y1:=IF((绿柱面积<0 AND ABS(绿柱面积)<ABS(前次绿柱面积) AND 本次最低价<前次最低价),1,0);RS1:= MACD<0 AND REF(MACD,1)<0 AND C<前次最低价AND ABS(绿柱面积)<ABS(前次绿柱面积) AND ABS(MACD)<ABS(REF(MACD,1));底背驰:= IF (CROSS(DIF,DEA),REF(Y1,1),0);DRAWTEXT(底背驰,DEA*0.8,'积底背'),COLORFF80FF;X2:=IF (MACD>0,BARSLAST(CROSS(DEA,DIF)),0);前次红柱面积:=REF(红柱面积,X2+1);前次最高价:=REF(本次最高价,X2+1);Y2:=IF((红柱面积>0 AND 红柱面积<前次红柱面积AND 本次最高价>前次最高价),1,0); RS2:= MACD>0 AND REF(MACD,1)>0 AND C>前次最高价AND ABS(红柱面积)<ABS(前次红柱面积) AND ABS(MACD)<ABS(REF(MACD,1));顶背驰:= IF (CROSS(DEA,DIF),REF(Y2,1),0);DRAWTEXT(顶背驰,DEA*0.8,'积顶背'),COLORCYAN;。
macd双背离DIFF: EMA(CLOSE,12) - EMA(CLOSE,26);DEA: EMA(DIFF,9);MACD: 2*(DIFF-DEA),COLORSTICK;JC:=DEA-DIFF;绿柱面积:=IF(MACD<0,SUM(MACD,BARSLAST(JC<0)),0);红柱面积:=IF(MACD>0,SUM(MACD,BARSLAST(JC>0)),0);本次最低价:=LLV(L,BARSLAST(JC<0));本次最高价:=HHV(H,BARSLAST(JC>0));X1:=IF (MACD<0,BARSLAST(CROSS(DIFF,DEA)),0);前次绿柱面积:=REF(绿柱面积,X1+1);前次最低价:=REF(本次最低价,X1+1);Y1:=IF((绿柱面积<0 AND ABS(绿柱面积)<ABS(前次绿柱面积)AND 本次最低价<前次最低价),1,0);RS1:= MACD<0 AND REF(MACD,1)<0 AND C<前次最低价AND ABS(绿柱面积)<ABS(前次绿柱面积) AND ABS(MACD)<ABS(REF(MACD,1)); 底背驰:= IF (CROSS(DIFF,DEA),REF(Y1,1),0);DRAWTEXT(底背驰,-0.01,'积底'),COLORRED;{DRAWBAND(DIFF,RGB(200,50,55),DEA,RGB(50,100,10));}X2:=IF (MACD>0,BARSLAST(CROSS(DEA,DIFF)),0);前次红柱面积:=REF(红柱面积,X2+1);前次最高价:=REF(本次最高价,X2+1);Y2:=IF((红柱面积>0 AND 红柱面积<前次红柱面积AND 本次最高价>前次最高价),1,0); RS2:= MACD>0 AND REF(MACD,1)>0 AND C>前次最高价AND ABS(红柱面积)<ABS(前次红柱面积) AND ABS(MACD)<ABS(REF(MACD,1));顶背驰:= IF (CROSS(DEA,DIFF),REF(Y2,1),0);DRAWTEXT(顶背驰,0.01,'积顶'),COLORGREEN;A11:=BARSLAST(REF(CROSS(DIFF,DEA),1));JCCOUNT:=COUNT(CROSS(DIFF,DEA),BARSLAST(DEA>=0));底背离:=REF(CLOSE,A11+1)>CLOSE AND DIFF>REF(DIFF,A11+1) ANDCROSS(DIFF,DEA);DRAWTEXT(底背离,DEA,' 线底'),COLORRED;A22:=BARSLAST(REF(CROSS(DEA,DIFF),1));顶背离:=REF(CLOSE,A22+1)<CLOSE AND REF(DIFF,A22+1)>DIFF ANDCROSS(DEA,DIFF);DRAWTEXT(顶背离,DEA,'线顶'),COLORGREEN;A1:=DIFF<0 AND DEA<0;A2:=A1>REF(A1,1);A3:=BARSLAST(A2);。
今天继续发一个MACD底背离,这是一位前辈的理念,MACD三堆量柱,第一堆绿柱第二堆红柱第三堆绿柱,要求三堆量柱每一堆至少10天以上连续形成。
然后比较两堆绿柱,要求股价下跌,现在的绿柱比前一堆量柱要缩短。
形成量价底背离,这只是一种MACD底背离形态,看看和前面的KDJ底背离能不能找到共振呢?或者加入其他条件和MACD底背离共振出票?一切留给喜欢MACD底背离的朋友自己去挖掘。
我只是写出这个形态,并配上直观主图幅图加上选股,其余自己拿捏吧!{主图}DIF:=EMA(CLOSE,12)-EMA(CLOSE,26);DEA:=EMA(DIF,9);MACD:=(DIF-DEA)*2;T:=BARSLAST(CROSS(0,MACD));TA:=BARSLAST(CROSS(MACD,0));T2:=REF(T,TA+1)+TA+1;L1:=LLV(L,T+1);L2:=REF(L1,T+1);L1T:=BARSLAST(L=L1);M1:=REF(MACD,BARSLAST(L=L1));M2:=REF(M1,T+1+T+1);L2T:=BARSLAST(L=CONST(L2));TJ:=(T2-TA)>=10 AND (TA-T)>=10;TJ1:=L1<L2 AND M1>M2;EXT:DRAWLINE(L2T=0,L,L1T=0 AND TJ AND TJ1 AND T>=10 ANDL2T>20 ,L,0),COLORYELLOW;DRAWTEXT(CROSS(EXT!=DRAWNULL,0.5),EXT*1.05,'底背离'),COLORYELLOW;{幅图}DIF:EMA(CLOSE,12)-EMA(CLOSE,26);DEA:EMA(DIF,9);MACD:(DIF-DEA)*2,COLORSTICK;T:=BARSLAST(CROSS(0,MACD));TA:=BARSLAST(CROSS(MACD,0));T2:=REF(T,TA+1)+TA+1;L1:=LLV(L,T+1);L2:=REF(L1,T+1);L1T:=BARSLAST(L=L1);M1:=REF(MACD,BARSLAST(L=L1));M2:=REF(M1,T+1+T+1);L2T:=BARSLAST(L=CONST(L2));TJ:=(T2-TA)>=10 AND (TA-T)>=10;TJ1:=L1<L2 AND M1>M2;EXT:DRAWLINE(L2T=0,MACD,L1T=0 AND TJ AND TJ1 AND T>=10 ANDL2T>20,MACD,0),DOTLINE,COLORMAGENTA;DRAWTEXT(CROSS(EXT!=DRAWNULL,0.5),MACD,'底背离'),COLORMAGENTA; {选股}DIF:=EMA(CLOSE,12)-EMA(CLOSE,26);DEA:=EMA(DIF,9);MACD:=(DIF-DEA)*2;T:=BARSLAST(CROSS(0,MACD));TA:=BARSLAST(CROSS(MACD,0));T2:=REF(T,TA+1)+TA+1;L1:=LLV(L,T+1);L2:=REF(L1,T+1);L1T:=BARSLAST(L=L1);M1:=REF(MACD,BARSLAST(L=L1));M2:=REF(M1,T+1+T+1);L2T:=BARSLAST(L=CONST(L2));TJ:=(T2-TA)>=10 AND (TA-T)>=10;TJ1:=L1<L2 AND M1>M2;XG:TJ AND TJ1 AND L1T=0 AND T>=10 AND L2T>20;。
DIF:MACD.DIF(12,26,9),LINETHICK2;DEA:MACD.DEA(12,26,9);MACD:(DIF-DEA)*2,COLORSTICK;DRAWICON(REF(DIF,1)>REF(DIF,2) AND DIF<REF(DIF,1) AND DIF>0 AND DIF>DEA,DIF*1.1,2),COLORGREEN;DRAWICON(CROSS(DEA,DIF) AND DIF>0,DIF,37),COLORGREEN;DRAWICON(REF(DIF,1)<REF(DIF,2) AND DIF>REF(DIF,1) AND DIF<0 AND DIF<DEA,DIF*1.1,1),COLORRED;DRAWICON(CROSS(DIF,DEA) AND DIF<0,DIF,36),COLORRED;XA_1:=MACD<0 AND MACD>REF(MACD,1) AND REF(MACD,1)<REF(MACD,2) AND REF(MACD,2)<REF(MACD,3) AND REF(MACD,3)<REF(MACD,4);STICKLINE(XA_1,0,MACD,0.3,0),COLORRED;XA_2:=MACD>0 AND MACD<REF(MACD,1) AND REF(MACD,1)>REF(MACD,2) AND REF(MACD,2)>REF(MACD,3) AND REF(MACD,3)>REF(MACD,4);STICKLINE(XA_2,0,MACD,0.3,0),COLORGREEN;XA_3:=100-100*(HHV(HIGH,34)-CLOSE)/(HHV(HIGH,34)-LLV(LOW,34))-50;XA_4:=EMA(XA_3,3);NOTEXTLU:IF(XA_4>24.99,DIF,DRAWNULL),COLORRED,LINETHICK2;NOTEXTLU2:IF(XA_4<(-40.009998),DIF,DRAWNULL),COLORGREEN,LINETHICK2; NOTEXT0:0,DOTLINE,COLORLIGRAY;XA_5:=IF(PERIOD=0,0.7,IF(PERIOD=1,1.2,IF(PERIOD=2,2,IF(PERIOD=3,2.5,IF(PERIOD =4,3,IF(PERIOD=5,5.5,IF(PERIOD=6,6,IF(PERIOD=7,7,IF(PERIOD=8,3.5,4)))))))));XA_6:=BARSLAST(CROSS(MACD,0));XA_7:=REF(XA_6,1)>0 AND XA_6=0;XA_8:=BARSLAST(XA_7);XA_9:=REF(XA_6,XA_8+1);XA_10:=HHVBARS(MACD,XA_8+XA_9);XA_11:=IF(XA_10>XA_8,REF(MACD,XA_10),0);XA_12:=IF(XA_10>XA_8,REF(HIGH,XA_10),0);XA_13:=IF(REF(HIGH,1)=HHV(HIGH,XA_8+XA_9),REF(HIGH,1),0);XA_14:=(XA_13-XA_12)/XA_12*100;XA_15:=COUNT(CROSS(0,MACD),XA_8+XA_9);XA_16:=MACD>0 AND MACD<XA_11 AND HIGH=HHV(HIGH,XA_8+XA_9) AND XA_11>0 AND XA_15=1;XA_17:=REF(XA_16,1) AND HIGH<REF(HIGH,1) AND MACD>0 AND XA_11>0;XA_18:=LLVBARS(MACD,XA_8+XA_9);XA_19:=IF(XA_18>XA_8,REF(MACD,XA_18),0);XA_20:=IF(XA_18>XA_8,REF(LOW,XA_18),0);XA_21:=IF(REF(LOW,1)=LLV(LOW,XA_8+XA_9),REF(LOW,1),0);XA_22:=(XA_20-XA_21)/XA_20*100;XA_23:=COUNT(CROSS(MACD,0),XA_8+XA_9);XA_24:=MACD<0 AND MACD>XA_19 AND LOW=LLV(LOW,XA_8+XA_9) AND XA_19<0 AND COUNT(CROSS(MACD,0),XA_8+XA_9)<=3;XA_25:=REF(XA_24,1) AND LOW>REF(LOW,1) AND MACD<0 AND XA_19<0;前高:IF(XA_17,XA_12,DRAWNULL),NODRAW,COLOR7FFF00;现高:IF(XA_17,XA_13,DRAWNULL),NODRAW,COLORGREEN;幅度_顶:IF(XA_17,XA_14,DRAWNULL),NODRAW,COLORYELLOW;原低:IF(XA_25,XA_20,DRAWNULL),NODRAW,COLORLIMAGENTA;现低:IF(XA_25,XA_21,DRAWNULL),NODRAW,COLORRED;幅度_底:IF(XA_25,XA_22,0),NODRAW,COLORYELLOW;DRAWICON(XA_25,MACD,26);DRAWICON(XA_17,MACD,27);XA_26:=BACKSET(XA_17,XA_10+1);XA_27:=BACKSET(XA_25,XA_18+1);NOTEXTMACDL:DRAWLINE(REF(XA_27,1)=0 AND XA_27=1 AND MACD<0,MACD,XA_25,MACD,0),COLORRED;NOTEXTMACDH:DRAWLINE(REF(XA_26,1)=0 AND XA_26=1 AND MACD>0,MACD,XA_17,MACD,0),COLORGREEN;涨家数:INDEXADV,NODRAW,COLORRED;跌家数:INDEXDEC,NODRAW,COLORGREEN;正量能:涨家数/(涨家数+跌家数)*100,NODRAW,COLORMAGENTA;负量能:跌家数/(涨家数+跌家数)*100,NODRAW,COLORYELLOW;。
买入信号奇准的MACD指标通达信指标公式源码MACD指标又称为移动平均收敛差异指标,是一种趋势跟踪指标,用于分析股票或其他金融资产的价格动态。
它是由两条线组成的,分别是DIF线和DEA线,同时还有一个柱状图。
下面是通达信指标公式源码:```python/*定义变量*/period1 = 12period2 = 26period3 = 9/*计算EMA移动平均线*/ema1 = EMA(CLOSE, period1) //收盘价的12日指数移动平均线ema2 = EMA(CLOSE, period2) //收盘价的26日指数移动平均线dif = ema1 - ema2 //DIF线/*计算DEA移动平均线*/dea = EMA(dif, period3) //DIF线的9日指数移动平均线/*计算MACD柱状图*/macd = (dif - dea) * 2 //柱状图/*绘制DIF线、DEA线和MACD柱状图*/Plot(dif, "DIF", color=red)Plot(dea, "DEA", color=blue)Plot(macd, "MACD", color=black, style=histogram)```以上是MACD指标的通达信指标公式源码。
其中,period1表示DIF 线的计算周期,通常为12日;period2表示DEA线的计算周期,通常为26日;period3表示MACD柱状图的计算周期,通常为9日。
根据MACD指标的分析方法,一般当DIF线向上突破DEA线时,形成金叉信号,意味着股票价格可能上涨,这时候可以考虑买入;相反,当DIF线向下突破DEA线时,形成死叉信号,意味着股票价格可能下跌,这时候可以考虑卖出。
此外,MACD柱状图的变化也可以用来判断股票的买卖信号。
需要注意的是,以上源码仅提供了MACD指标的计算和绘制部分,具体的买入或卖出策略需要根据个人的投资策略和风险偏好进行进一步的判断和调整。
背离指标顶背离出局,底背离入手。
简单实用。
DIFF:=EMA(CLOSE,12) - EMA(CLOSE,26);DEA:=EMA(DIFF,9);MACD:=2*(DIFF-DEA),COLORSTICK;A1:=BARSLAST(REF(CROSS(DIFF,DEA),1));B1:=REF(C,A1+1)>C AND REF(DIFF,A1+1)<DIFF AND CROSS(DIFF,DEA);A2:=BARSLAST(REF(CROSS("KDJ.K"(9,3,3),"KDJ.D"(9,3,3)),1));B2:=REF(C,A2+1)>C AND REF("KDJ.K"(9,3,3),A2+1)<"KDJ.K"(9,3,3) ANDCROSS("KDJ.K"(9,3,3),"KDJ.D"(9,3,3));A3:=BARSLAST(REF(CROSS("RSI.RSI1"(6,12,24),"RSI.RSI2"(6,12,24)),1));B3:=REF(C,A3+1)>C AND REF("RSI.RSI1"(6,12,24),A3+1)<"RSI.RSI1"(6,12,24) AND CROSS("RSI.RSI1"(6,12,24),"RSI.RSI2"(6,12,24));C1:=BARSLAST(REF(CROSS(DEA,DIFF),1));D1:=REF(C,C1+1)<C AND REF(DIFF,C1+1)>DIFF AND CROSS(DEA,DIFF);C2:=BARSLAST(REF(CROSS("KDJ.D"(9,3,3),"KDJ.K"(9,3,3)),1));D2:=REF(C,C2+1)<C AND REF("KDJ.K"(9,3,3),C2+1)>"KDJ.K"(9,3,3) ANDCROSS("KDJ.D"(9,3,3),"KDJ.K"(9,3,3));C3:=BARSLAST(REF(CROSS("RSI.RSI2"(6,12,24),"RSI.RSI1"(6,12,24)),1));D3:=REF(C,C3+1)<C AND REF("RSI.RSI1"(6,12,24),C3+1)>"RSI.RSI1"(6,12,24) AND CROSS("RSI.RSI2"(6,12,24),"RSI.RSI1"(6,12,24));MACD底背:=IF(B1>0,-0.1,0),COLORRED,POINTDOT;KDJ底背:=IF(B2>0,-0.1,0),COLORFF00FF,POINTDOT;RSI底背:=IF(B3>0,-0.1,0),COLORYELLOW,POINTDOT;MACD顶背:=IF(D1>0,0.1,0),COLOR00FF00,POINTDOT;KDJ顶背:=IF(D2>0,0.1,0),COLORFFCC66,POINTDOT;RSI顶背:=IF(D3>0,0.1,0),COLORWHITE,POINTDOT;DRAWTEXT(FILTER(B1>0,5),L-0.38,'MACD底背'),COLORGREEN; DRAWTEXT(FILTER(B2>0,5),L-0.39,'KDJ底背'),COLORYELLOW; DRAWTEXT(FILTER(B3>0,5),L,'RSI底背'),COLORCYAN;DRAWTEXT(FILTER(D1>0,5),L+0.08,'MACD顶背'),COLORWHITE; DRAWTEXT(FILTER(D2>0,5),L+0.32,'KDJ顶背'),COLORWHITE; DRAWTEXT(FILTER(D3>0,5),L+0.1,'RSI顶背'),COLORMAGENTA; DRAWBAND(MA(CLOSE,3),RGB(200,50,160),MA(CLOSE,5),RGB(0,120,0));。
VAR1:=EMA(SLOPE(C,21)+C,55);VAR2:=EMA(SLOPE(O,21)+O,55);VAR3:=EMA(SLOPE(H,21)+H,55);VAR4:=EMA(SLOPE(L,21)+L,55);VAR5:=EMA(C,3);VAR6:=EMA(O,3);VAR7:=EMA(H,3);VAR8:=EMA(L,3);VAR9:=VAR1-VAR5,LINETHICK2;VAR10:=VAR2-VAR6,LINETHICK2;VAR11:=VAR3-VAR7,LINETHICK2;VAR12:=VAR4-VAR8,LINETHICK2;ZJC:=REVERSE(VAR9);ZJO:=REVERSE(VAR10);ZJH:=REVERSE(VAR11);ZJL:=REVERSE(VAR12);DIF:EMA(ZJC*5,12)-EMA(ZJC*5,26);DEA:EMA(DIF,9);MACD:(DIF-DEA)*2,COLORSTICK;STICKLINE(MACD>REF(MACD,1) AND MACD>0,0,MACD,0,0),COLORRED; STICKLINE(MACD<REF(MACD,1) AND MACD>0,0,MACD,0,0),COLORMAGENTA;STICKLINE(MACD>REF(MACD,1) AND MACD<0,0,MACD,0,0),COLORYELLOW;STICKLINE(MACD<REF(MACD,1) AND MACD<0,0,MACD,0,0),COLORLIGREEN; STICKLINE(ZJC> ZJO,ZJH,ZJL,0.0,0),COLORFF66FF;STICKLINE(ZJC> ZJO,ZJC,ZJO,2.1,0),COLORAA00AA;STICKLINE(ZJC> ZJO,ZJC,ZJO,1.9,0),COLORCC11CC;STICKLINE(ZJC> ZJO,ZJC,ZJO,1.6,0),COLORFF22FF;STICKLINE(ZJC> ZJO,ZJC,ZJO,1.2,0),COLORFF44FF;STICKLINE(ZJC> ZJO,ZJC,ZJO,0.7,0),COLORFF66FF;STICKLINE(ZJC> ZJO,ZJC,ZJO,0.2,0),COLORFF99FF;STICKLINE(ZJC<=ZJO,ZJL,ZJC,0.0,0),COLORGREEN;STICKLINE(ZJC<=ZJO,ZJO,ZJC,2.1,0),COLOR005500;STICKLINE(ZJC<=ZJO,ZJO,ZJC,1.9,0),COLOR007700;STICKLINE(ZJC<=ZJO,ZJO,ZJC,1.6,0),COLOR009900;STICKLINE(ZJC<=ZJO,ZJO,ZJC,1.2,0),COLOR00BB00;STICKLINE(ZJC<=ZJO,ZJO,ZJC,0.7,0),COLOR00DD00;STICKLINE(ZJC<=ZJO,ZJO,ZJC,0.2,0),COLOR44FF00;ZTB:=(C-REF(C,1))/REF(C,1)*100>9.9;DRAWTEXT(ZTB,IF(ZJC<0,ZJC*0.9,ZJC*1.1),'涨停板')COLORYELLOW; STICKLINE(ZTB,ZJC,ZJO,2.1,0),COLOR006666;STICKLINE(ZTB,ZJC,ZJO,1.9,0),COLOR008888;STICKLINE(ZTB,ZJC,ZJO,1.6,0),COLOR00AAAA;STICKLINE(ZTB,ZJC,ZJO,1.2,0),COLOR00CCCC;STICKLINE(ZTB,ZJC,ZJO,0.7,0),COLOR00EEEE;STICKLINE(ZTB,ZJC,ZJO,0.2,0),COLOR33FFFF;DTB:=(C-REF(C,1))/REF(C,1)*100<-9.9;DRAWTEXT(DTB,IF(ZJC>0,ZJC*0.9,ZJC*1.1),'跌停板')COLORFF8800; STICKLINE(DTB,ZJO,ZJC,2.1,0),COLORFF0000;{蓝色}STICKLINE(DTB,ZJO,ZJC,1.9,0),COLORFF2200;STICKLINE(DTB,ZJO,ZJC,1.6,0),COLORFF4400;STICKLINE(DTB,ZJO,ZJC,1.2,0),COLORFF6600;STICKLINE(DTB,ZJO,ZJC,0.7,0),COLORFF8800;STICKLINE(DTB,ZJO,ZJC,0.2,0),COLORFFAA00;。
MACD背离自动画线通达信指标公式源码通达信(MetaStock)是一种常用的股票分析软件,提供了丰富的指标库。
在通达信中编写指标公式可以使用通达信自带的公式语言-支持C语言语法。
为了实现MACD背离自动画线指标,我们首先要计算MACD指标的数值,并判断是否出现背离。
接下来,我们可以使用通达信的画线函数实现在背离点位置上自动画线。
下面是该指标的示例源码:```javascript/*时间:2024-01-01*/#include "stdio.h"#include "stdlib.h"#include "string.h"#include "windows.h"/*定义MACD指标相应变量*/double EMA_SHORT;double EMA_LONG;double DIFF;double DEA;double MACD;/*初始化操作,在指标开始计算之前调用*/void InitEMA_SHORT=0;EMA_LONG=0;DIFF=0;DEA=0;MACD=0;/*计算MACD指标的数值*/void CalculateMACD(double close[], int n, int shortPeriod, int longPeriod, int signalPeriod)double EMA_SHORT_SMOOTH = 2.0 / (shortPeriod + 1);double EMA_LONG_SMOOTH = 2.0 / (longPeriod + 1);//计算短期EMAdouble emaShortPrev = close[0];for (int i = 0; i < shortPeriod; i++)emaShortPrev = emaShortPrev * (1 - EMA_SHORT_SMOOTH) +close[i] * EMA_SHORT_SMOOTH;}//计算长期EMAdouble emaLongPrev = close[0];for (int i = 0; i < longPeriod; i++)emaLongPrev = emaLongPrev * (1 - EMA_LONG_SMOOTH) + close[i] * EMA_LONG_SMOOTH;}//计算DIFF和DEADIFF = emaShortPrev - emaLongPrev;DEA=0;double DEA_SMOOTH = 2.0 / (signalPeriod + 1);for (int i = 0; i < signalPeriod; i++)DEA=DEA*(1-DEA_SMOOTH)+DIFF*DEA_SMOOTH;}//计算MACDMACD=(DIFF-DEA)*2;/*判断是否出现背离*/int IsDivergence(double close[], int n, int direction)for (int i = 2; i < n; i++)double prevClose1 = close[i - 2];double prevClose2 = close[i - 1];double currentClose = close[i];if (direction == 1) // 上涨背离:前两个低点价格下降,当前高点价格上升if (prevClose1 > prevClose2 && prevClose2 > currentClose) return i;}}else if (direction == -1) // 下跌背离:前两个高点价格上升,当前低点价格下降if (prevClose1 < prevClose2 && prevClose2 < currentClose)return i;}}}return -1;/*自动画线函数,在背离位置上自动画线*/void AutoDrawLine(int barIndex, int direction)//自动画线的相关处理,这里仅做示例printf("在第 %d 根K线上画上涨背离线。
MACD底图加几个背离指标组合而成DIF:EMA(CLOSE,12) - EMA(CLOSE,26);DEA:EMA(DIF,9);MACD:2*(DIF-DEA),COLORSTICK;TRR :=SUM(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(LOW-REF(CLOSE,1))),14); HD := HIGH-REF(HIGH,1);LD := REF(LOW,1)-LOW;PDM:= SUM(IF(HD>0 AND HD>LD,HD,0),14);MDM:= SUM(IF(LD>0 AND LD>HD,LD,0),14);PDI:= PDM*100/TRR;MDI:= MDM*100/TRR;A1:=BARSLAST(REF(CROSS(DIF,DEA),1));B1:=REF(C,A1+1)>C AND REF(DIF,A1+1)<DIF AND CROSS(DIF,DEA);A2:=BARSLAST(REF(CROSS("KDJ.K"(9,3,3),"KDJ.D"(9,3,3)),1));B2:=REF(C,A2+1)>C AND REF("KDJ.K"(9,3,3),A2+1)<"KDJ.K"(9,3,3) ANDCROSS("KDJ.K"(9,3,3),"KDJ.D"(9,3,3));A3:=BARSLAST(REF(CROSS("RSI.RSI1"(6,12,24),"RSI.RSI2"(6,12,24)),1));B3:=REF(C,A3+1)>C AND REF("RSI.RSI1"(6,12,24),A3+1)<"RSI.RSI1"(6,12,24) AND CROSS("RSI.RSI1"(6,12,24),"RSI.RSI2"(6,12,24));C1:=BARSLAST(REF(CROSS(DEA,DIF),1));D1:=REF(C,C1+1)<C AND REF(DIF,C1+1)>DIF AND CROSS(DEA,DIF);C2:=BARSLAST(REF(CROSS("KDJ.D"(9,3,3),"KDJ.K"(9,3,3)),1));D2:=REF(C,C2+1)<C AND REF("KDJ.K"(9,3,3),C2+1)>"KDJ.K"(9,3,3) ANDCROSS("KDJ.D"(9,3,3),"KDJ.K"(9,3,3));C3:=BARSLAST(REF(CROSS("RSI.RSI2"(6,12,24),"RSI.RSI1"(6,12,24)),1));D3:=REF(C,C3+1)<C AND REF("RSI.RSI1"(6,12,24),C3+1)>"RSI.RSI1"(6,12,24) AND CROSS("RSI.RSI2"(6,12,24),"RSI.RSI1"(6,12,24));C4:=BARSLAST(REF(CROSS(MDI,PDI),1));D4:=REF(C,C+1)<C AND REF(MDI,C4+1)>MDI AND CROSS(MDI,PDI);A4:=BARSLAST(REF(CROSS(PDI,MDI),1));B4:=REF(C,A4+1)>C AND REF(PDI,A4+1)<PDI AND CROSS(PDI,MDI);MACD底背:IF(B1>0,-0.1,0),COLORRED,POINTDOT;KDJ底背:IF(B2>0,-0.1,0),COLORFF00FF,POINTDOT;RSI底背:IF(B3>0,-0.1,0),COLORYELLOW,POINTDOT;DMI底背:IF(B4>0,-0.1,0),COLOR0099FF,POINTDOT;MACD顶背:IF(D1>0,0.1,0),COLOR00FF00,POINTDOT;KDJ顶背:IF(D2>0,0.1,0),COLORFFCC66,POINTDOT;RSI顶背:IF(D3>0,0.1,0),COLORWHITE,POINTDOT;DMI顶背:IF(D4>0,0.1,0),COLORBLUE,POINTDOT;STICKLINE(B1>0,-0.03,0,1,0),COLORRED;DRAWTEXT(B1>0,-0.03,'MACD底背'),COLORRED;STICKLINE(B2>0,-0.06,0,1,0),COLORFF00FF;DRAWTEXT(B2>0,-0.06,'KDJ底背'),COLORFF00FF;STICKLINE(B3>0,-0.09,0,1,0),COLORYELLOW;DRAWTEXT(B3>0,-0.09,'RSI底背'),COLORYELLOW;STICKLINE(B4>0,-0.12,0,1,0),COLOR0099FF;DRAWTEXT(B4>0,-0.12,'DMI底背'),COLOR0099FF;STICKLINE(D1>0,0.04,0,0.5,0),COLOR00FF00;DRAWTEXT(D1>0,0.03,'MACD顶背'),COLOR00FF00;STICKLINE(D2>0,0.06,0,1,0),COLORFFCC66;DRAWTEXT(D2>0,0.06,'KDJ顶背'),COLORFFCC66;STICKLINE(D3>0,0.09,0,1,0),COLORWHITE;DRAWTEXT(D3>0,0.09,'RSI顶背'),COLORWHITE;STICKLINE(D4>0,0.12,0,1,0),COLORBLUE;DRAWTEXT(D4>0,0.12,'DMI顶背'),COLORBLUE;平空开多:=CROSS(MACD,0);平多开空:=CROSS(0,MACD);DRAWICON(平空开多,DIF,1);DRAWICON(平多开空,DIF,2);AUTOFILTER;低位金叉:=CROSS(DIF,DEA) AND DIF<-0.1;STICKLINE(低位金叉,0,0.12,4,0),COLORYELLOW;DRAWTEXT(低位金叉,0.05,' 低位金叉'),COLORWHITE;JCCOUNT:=COUNT(CROSS(DIF,DEA),BARSLAST(DEA>=0));二次金叉:=CROSS(DIF,DEA) AND DEA<0 AND COUNT(JCCOUNT=2,30)=1; STICKLINE(二次金叉,0,0.06,4,0),COLORFF00FF;DRAWICON(二次金叉,DEA*0.9,1) ;DRAWTEXT(二次金叉,0.1,' 二次金叉'),COLORYELLOW;A8:=BARSLAST(REF(CROSS(DIF,DEA),1));底背离:=REF(CLOSE,A8+1)>CLOSE AND DIF>REF(DIF,A1+1) AND CROSS(DIF,DEA) AND DEA<0;STICKLINE(底背离,0,0.03,5,0),COLOR00FF00;DRAWTEXT_FIX(1,0.04,0.1,0,'小心牛眼和熊眼'),COLORYELLOW;。
自动画MACD背离的指标:一共两个指标,一个是通达信用的,一个是大智慧用的
{通达信自动划MACD指标背离线}
DIFF:EMA(CLOSE,12) - EMA(CLOSE,26);
DEA:EMA(DIFF,9);
MACD:2*(DIFF-DEA), COLORSTICK;
A1:=BARSLAST(REF(CROSS(DIFF,DEA),1));
底背离:=REF(CLOSE,A1+1)>CLOSE AND DIFF>REF(DIFF,A1+1) AND CROSS(DIFF,DEA); STICKLINE(底背离,0,0.1,8,0),COLORRED;
DRAWLINE(A1=0,DEA,底背离,DEA,0),COLORRED,LINETHICK2;
A2:=BARSLAST(REF(CROSS(DEA,DIFF),1));
顶背离:=REF(CLOSE,A2+1)<CLOSE AND REF(DIFF,A2+1)>DIFF AND CROSS(DEA,DIFF); DRAWLINE(A2=0,DEA,顶背离,DEA,0),COLORGREEN,LINETHICK2;
{大智慧自动划MACD指标背离线
(适用于大智慧新一代)}
DIF:EMA(CLOSE,12)-EMA(CLOSE,26);
DEA:EMA(DIF,9);
MACD:(DIF-DEA)*2,COLORSTICK;
A:=SUMBARS(CROSS(DEA,DIF),2);
B:=SUMBARS(CROSS(DEA,DIF),1);
E:=REF(DIF,LLVBARS(DIF,A))<REF(DIF,LLVBARS(DIF,B)) AND LLVBARS(DIF,B)<LLVBARS(DIF,A) AND
CROSS(DIF,DEA);
F:=COUNT(BACKSET(E,LLVBARS(DIF,A)+1),LLVBARS(DIF,A)+1)=1; G:=COUNT(BACKSET(E,LLVBARS(DIF,B)+1),LLVBARS(DIF,B)+1)=1; DRAWLINE(F,DIF,G,DIF,0),COLOERED;。