{连结macd相邻红绿柱区间内高低点画趋势线 通达信}
DIFF:=EMA(CLOSE,12)-EMA(CLOSE,26);
DEA:=EMA(DIFF,9);
N1:=BArslAST(crOSS(DIFF,DEA))+1;{最近一次满足条件DIFF上穿DEA的k线到当前的距离}
N2:=BARSLAST(CROSS(DEA,DIFF))+1;{最近一次满足条件DIFF下穿DEA的K线到当前的距离}
{当前是DIFF>DEA的状态,连结最近两个DIFF>DEA区间(不包含当前区间)内最高点的趋势线}
A1:=BacKSET(ISLASTBAR && N1<N2,REF(REF(HHVBARS(H,N1)+1,N2),N2)+REF(N2,N2)+N2);
B1:=BACKSET(ISLASTBAR && N1<N2,REF(HHVBARS(H,N1),N2)+N2+1);
DRAWLINE(CROSS(A1,0.5),H,CROSS(B1,0.5),H,1),COLORRED;
{当前是DIFF>DEA的状态,连结最近两个DIFF<DEA区间内最低点的趋势线}
A2:=BACKSET(ISLASTBAR && N1<N2,REF(REF(LLVBARS(L,N2)+1,N1),N2)+REF(N1,N2)+N2);
B2:=BACKSET(ISLASTBAR && N1<N2,REF(LLVBARS(L,N2)+1,N1)+N1);
DRAWLINE(CROSS(A2,0.5),L,CROSS(B2,0.5),L,1),COLORGREEN;
{当前是DIFF<DEA的状态,连结最近两个DIFF<DEA区间(不包含当前区间)内最低点的趋势线}
A3:=BACKSET(ISLASTBAR && N1>N2,REF(REF(LLVBARS(L,N2)+1,N1),N1)+REF(N1,N1)+N1);
B3:=BACKSET(ISLASTBAR && N1>N2,REF(LLVBARS(L,N2),N1)+N1+1);
DRAWLINE(CROSS(A3,0.5),L,CROSS(B3,0.5),L,1),COLORGREEN;
{当前是DIFF<DEA的状态,连结最近两个DIFF>DEA区间内最高点的趋势线}
A4:=BACKSET(ISLASTBAR && N1>N2,REF(REF(HHVBARS(H,N1)+1,N2),N1)+REF(N2,N1)+N1);
B4:=BACKSET(ISLASTBAR && N1>N2,REF(HHVBARS(H,N1)+1,N2)+N2);
DRAWLINE(CROSS(A4,0.5),H,CROSS(B4,0.5),H,1),COLORRED;