# How to draw only the first signal of a serie of identical signals

I have a relatively simple code of 3 supertrends and I would like to avoid
repetitions of identical signals.
To simplify if all 3 supertrends are aligned in long modus I want to see
the buy-arrow and when all 3 are alligned in short modus I want to see
the short - arrow and no others in between.
I tried this by simply adding a variable called "lastP''' which is either
positive or negative [or 0 after initialisation] and which is adapted
directly after the first occurence of a given signal.
But unfortunately it doesn't worl as expected.

``````_SECTION_BEGIN("SuperTrend Custom 3 Unified");
SetChartOptions(0,chartShowArrows|chartShowDates);
PositionSize = 100000;
Factor=Param("Factor",1,2,10,1);
Pd=Param("ATR Periods",7,1,20,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
Lines = ParamToggle( "Drawlines", "No|Yes", 1 );
iATR=ATR(Pd);
TrendUp=TrendDown=NumTrendUp=NumTrendDown=lastP=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount-1; i++) {
TrendUp[i] = Null;
TrendDown[i] = Null;
trend[i]=1;
if (Close[i]>Up[i-1]) {
trend[i]=1;
if (trend[i-1] == -1) changeOfTrend = 1;
}
else if (Close[i]<Dn[i-1]) {
trend[i]=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend[i]=1;
changeOfTrend = 0;
}
else if (trend[i-1]==-1) {
trend[i]=-1;
changeOfTrend = 0;
}
if (trend[i]<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}
if (trend[i]>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}
if (trend[i]>0 && Dn[i]<Dn[i-1]){
Dn[i]=Dn[i-1];
}
if (trend[i]<0 && Up[i]>Up[i-1])
{ Up[i]=Up[i-1];
}
if (flag==1)
{Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
}
if (flagh==1)
{ Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
}
if (trend[i]==1) {
TrendUp[i]=Dn[i];
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=Up[i];
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
}

if ( Lines )
{
Plot(TrendUp,"Trend",colorBrightGreen,styleThick);
Plot(TrendDown,"Down",colorOrange,styleThick);
}
Sell=trend==-1;
Short=Sell;
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);

Factor2=Param("Factor2",1.35,2,10,1);
Pd2=Param("ATR Periods2",5,1,20,1);
Up2=(H+L)/2+(Factor2*ATR(Pd2));
Dn2=(H+L)/2-(Factor2*ATR(Pd2));
iATR2=ATR(Pd2);
TrendUp2=TrendDown2=Null;
trend2[0]=1;
changeOfTrend2=0;
flag2=flagh2=0;

for (i = 1; i <BarCount-1; i++) {
TrendUp2[i] = Null;
TrendDown2[i] = Null;
trend2[i]=1;
if (Close[i]>Up2[i-1]) {
trend2[i]=1;
if (trend2[i-1] == -1) changeOfTrend2 = 1;
}
else if (Close[i]<Dn2[i-1]) {
trend2[i]=-1;
if (trend2[i-1] == 1) changeOfTrend2 = 1;
}
else if (trend2[i-1]==1) {
trend2[i]=1;
changeOfTrend2 = 0;
}
else if (trend2[i-1]==-1) {
trend2[i]=-1;
changeOfTrend2 = 0;
}
if (trend2[i]<0 && trend2[i-1]>0) {
flag2=1;
}
else {
flag2=0;
}
if (trend2[i]>0 && trend2[i-1]<0) {
flagh2=1;
}
else {
flagh2=0;
}
if (trend2[i]>0 && Dn2[i]<Dn2[i-1]){
Dn2[i]=Dn2[i-1];
}
if (trend2[i]<0 && Up2[i]>Up2[i-1])
{ Up2[i]=Up2[i-1];
}
if (flag2==1)
{Up2[i]=(H[i]+L[i])/2+(Factor2*iATR2[i]);;
}
if (flagh2==1)
{ Dn2[i]=(H[i]+L[i])/2-(Factor2*iATR2[i]);;
}
if (trend2[i]==1) {
TrendUp2[i]=Dn2[i];
if (changeOfTrend2 == 1) {
TrendUp2[i-1] = TrendDown2[i-1];
changeOfTrend2 = 0;
}
}
else if (trend2[i]==-1) {
TrendDown2[i]=Up2[i];
if (changeOfTrend2 == 1) {
TrendDown2[i-1] = TrendUp2[i-1];
changeOfTrend2 = 0;
}
}
}

if ( Lines )
{
Plot(TrendUp2,"Trend2",colorBrightGreen,styleThick);
Plot(TrendDown2,"Down2",colorOrange,styleThick);
}
Sell2=trend2==-1;
Short2=Sell2;
SellPrice2=ValueWhen(Sell2,C);
ShortPrice2=ValueWhen(Short2,C);
CoverPrice2=ValueWhen(Cover2,C);

Factor3=Param("Factor3",1.7,2,10,1);
Pd3=Param("ATR Periods3",7,1,20,1);
Up3=(H+L)/2+(Factor3*ATR(Pd3));
Dn3=(H+L)/2-(Factor3*ATR(Pd3));
iATR3=ATR(Pd3);
TrendUp3=TrendDown3=Null;
trend3[0]=1;
changeOfTrend3=0;
flag3=flagh3=0;

for (i = 1; i <BarCount-1; i++) {
TrendUp3[i] = Null;
TrendDown3[i] = Null;
trend3[i]=1;
if (Close[i]>Up3[i-1]) {
trend3[i]=1;
if (trend3[i-1] == -1) changeOfTrend3 = 1;
}
else if (Close[i]<Dn3[i-1]) {
trend3[i]=-1;
if (trend3[i-1] == 1) changeOfTrend3 = 1;
}
else if (trend3[i-1]==1) {
trend3[i]=1;
changeOfTrend3 = 0;
}
else if (trend3[i-1]==-1) {
trend3[i]=-1;
changeOfTrend3 = 0;
}
if (trend3[i]<0 && trend3[i-1]>0) {
flag3=1;
}
else {
flag3=0;
}
if (trend3[i]>0 && trend3[i-1]<0) {
flagh3=1;
}
else {
flagh3=0;
}
if (trend3[i]>0 && Dn3[i]<Dn3[i-1]){
Dn3[i]=Dn3[i-1];
}
if (trend3[i]<0 && Up3[i]>Up3[i-1])
{ Up3[i]=Up3[i-1];
}
if (flag3==1)
{Up3[i]=(H[i]+L[i])/2+(Factor3*iATR3[i]);;
}
if (flagh3==1)
{ Dn3[i]=(H[i]+L[i])/2-(Factor3*iATR3[i]);;
}
if (trend3[i]==1) {
TrendUp3[i]=Dn3[i];
if (changeOfTrend3 == 1) {
TrendUp3[i-1] = TrendDown3[i-1];
changeOfTrend3 = 0;
}
}
else if (trend3[i]==-1) {
TrendDown3[i]=Up3[i];
if (changeOfTrend3 == 1) {
TrendDown3[i-1] = TrendUp3[i-1];
changeOfTrend3 = 0;
}
}
}

if ( Lines )
{
Plot(TrendUp3,"Trend3",colorBrightGreen,styleThick);
Plot(TrendDown3,"Down3",colorOrange,styleThick);
}
Sell3=trend3==-1;
Short3=Sell3;
SellPrice3=ValueWhen(Sell3,C);
ShortPrice3=ValueWhen(Short3,C);
CoverPrice3=ValueWhen(Cover3,C);

NumTrendUp = (trendup AND trendup2 AND trendup3) AND (Ref(trenddown,-1) OR Ref(trenddown2,-1) OR Ref(trenddown3,-1)) AND (lastP=-1 OR lastP==Null);
IIf(NumTrendUp,lastP=1,lastP=Ref(lastP,-1));
NumTrendDown = (trenddown AND trenddown2 AND trenddown3) AND (Ref(trendup,-1) OR Ref(trendup2,-1) OR Ref(trendup3,-1)) AND (lastP=1 OR lastP==Null);
IIf(NumTrendDown,lastP=-1,lastP=Ref(lastP,-1));
PlotShapes(IIf(NumTrendUp, shapeSquare, shapeNone),colorSeaGreen, 0, L, Offset=-40);
PlotShapes(IIf(NumTrendUp, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(NumTrendUp, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(NumTrendDown, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(NumTrendDown, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(NumTrendDown, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
``````

“=” (assignment) vs “==” (equality check)

May or may not be the cause of your issue but no harm in fixing those little oversights.

1 Like

Indeed I checked this before being aware of the difference between '=' and '=='.
Paradoxically if I use here '==' ALL the signalarrows disappear, that's why
I kept the '='.
So the following code near the end makes disappear all the signals for a reason I ignore...

``````NumTrendUp = (trendup AND trendup2 AND trendup3) AND (Ref(trenddown,-1) OR Ref(trenddown2,-1) OR Ref(trenddown3,-1)) AND (lastP==-1 OR lastP==Null);
IIf(NumTrendUp,lastP==1,lastP==Ref(lastP,-1));
NumTrendDown = (trenddown AND trenddown2 AND trenddown3) AND (Ref(trendup,-1) OR Ref(trendup2,-1) OR Ref(trendup3,-1)) AND (lastP==1 OR lastP==Null);
IIf(NumTrendDown,lastP==-1,lastP==Ref(lastP,-1));
``````
1 Like

You might need to add this part to your code to eliminate the excessive signals

``````X = ExRem(numtrendup,numtrenddown);
Y = ExRem(numtrenddown,numtrendup);
``````

https://www.amibroker.com/guide/afl/exrem.html

my reply is forwarded to Achalendra , i'm sorry for this mistake :))

3 Likes

Hi, thank you very much, this is indeed the solution.
I modified the end of my code as follows and now it works
exactly as it should. Have a nice day !

``````NumTrendUp = (trendup AND trendup2 AND trendup3) AND (Ref(trenddown,-1) OR Ref(trenddown2,-1) OR Ref(trenddown3,-1));
NumTrendDown = (trenddown AND trenddown2 AND trenddown3) AND (Ref(trendup,-1) OR Ref(trendup2,-1) OR Ref(trendup3,-1));
X = ExRem(numtrendup,numtrenddown);
Y = ExRem(numtrenddown,numtrendup);
PlotShapes(IIf(X, shapeSquare, shapeNone),colorSeaGreen, 0, L, Offset=-40);
PlotShapes(IIf(X, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(X, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Y, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Y, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Y, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
``````
1 Like

2 Likes

Please tag @Sebastian 's post as the "Solution" in token of your appreciation AND a quick jump for those who are in a hurry

3 Likes