Trendline code customization

Hi;

Could you please help me in the below code modification:

//////////////chart & back ground color////////////////////
SetChartBkGradientFill( colorBlack, colorBlack, colorBlack );
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( Close, "C", colorWhite, styleCandle );
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorPink, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );
starttime = 091500;
endtime = 151500;
separator = Day() != Ref( Day(), -1 );
insession =  timenum() >= starttime && timenum() <= 151000;
endsession = timenum() >= endtime;
endsession = ( endsession - Ref( endsession, -1 ) ) == 1 ;//OR Ref( separator, 1 );
startsession = timenum() >= starttime;
startsession = ( startsession - Ref( startsession, -1 ) ) == 1;
timelmits =    insession  && !endsession;
Plot( endsession, "", colorWhite, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
Plot( startsession, "", colorViolet, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
//GraphXSpace = 85;

_SECTION_BEGIN("Long R/S");
farback = Param("How Far back to go", 100, 50, 5000, 10);
nBars = Param("Number of bars", 12, 5, 40);
aHPivs = H - H;
aLPivs = L - L;
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
aVisBars = Status("barvisible");
nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
curBar = (BarCount - 1);
curTrend = "";
if (aLLVBars[curBar] < aHHVBars[curBar])
{
    curTrend = "D";
}

else
{
    curTrend = "U";
}

for (i = 0; i < farback; i++)
{
    curBar = (BarCount - 1) - i;
    if (aLLVBars[curBar] < aHHVBars[curBar])
    {
        if (curTrend == "U")
        {
            curTrend = "D";
            curPivBarIdx = curBar - aLLVBars[curBar];
            aLPivs[curPivBarIdx] = 1;
            aLPivLows[nLPivs] = L[curPivBarIdx];
            aLPivIdxs[nLPivs] = curPivBarIdx;
            nLPivs++;
        }
    }
    else
    {
        if (curTrend == "D")
        {
            curTrend = "U";
            curPivBarIdx = curBar - aHHVBars[curBar];
            aHPivs[curPivBarIdx] = 1;
            aHPivHighs[nHPivs] = H[curPivBarIdx];
            aHPivIdxs[nHPivs] = curPivBarIdx;
            nHPivs++;
        }
    }
}

curBar = (BarCount - 1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx)
{
    candIdx = curBar - aHHVBars[curBar];
    candPrc = aHHV[curBar];
    if (lastHPH < candPrc AND candIdx > lastLPIdx AND candIdx < curBar)
    {
        aHPivs[candIdx] = 1;
        for (j = 0; j < nHPivs; j++)
        {
            aHPivHighs[nHPivs - j] = aHPivHighs[nHPivs - (j + 1)];
            aHPivIdxs[nHPivs - j] = aHPivIdxs[nHPivs - (j + 1)];
        }
        aHPivHighs[0] = candPrc;
        aHPivIdxs[0] = candIdx;
        nHPivs++;
    }
}

else
{
    candIdx = curBar - aLLVBars[curBar];
    candPrc = aLLV[curBar];
    if (lastLPL > candPrc AND candIdx > lastHPIdx AND candIdx < curBar)
    {
        aLPivs[candIdx] = 1;
        for (j = 0; j < nLPivs; j++)
        {
            aLPivLows[nLPivs - j] = aLPivLows[nLPivs - (j + 1)];
            aLPivIdxs[nLPivs - j] = aLPivIdxs[nLPivs - (j + 1)];
        }
        aLPivLows[0] = candPrc;
        aLPivIdxs[0] = candIdx;
        nLPivs++;
    }
}

for (k = 0; k < nHPivs; k++)
{
    _TRACE("High pivot no. " + k + " at barindex: " + aHPivIdxs[k] + ", " + WriteVal(ValueWhen(BarIndex() == aHPivIdxs[k], DateTime(), 1), formatDateTime) + ", " + aHPivHighs[k]);
}

a1 = ahpivs == 1;
a2 = alpivs == 1;

Para = ParamToggle("Plot Parallel Lines","Off,On");
ColorS= ParamColor("Support",colorLime);
ColorR= ParamColor("Resistance",colorRed);
x = Cum(1);
s1 = L;
s11 = H;
pS = a2 == 1;

endt = LastValue(ValueWhen(ps,x,1));
startt = LastValue(ValueWhen(ps,x,2));
ends = LastValue(ValueWhen(ps,S1,1));
starts = LastValue(ValueWhen(ps,S1,2));
dtS = endt - startt;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS *(x - endt) + bS;
g3 = IIf(x > startt - 10, trendlineS,  Null);
Plot(g3, "", colors, styleLine+styleDashed+styleNoRescale+styleNoLabel);
pR = a1 == 1;
endt1 = LastValue(ValueWhen(pr,x,1));
startt1 = LastValue(ValueWhen(pr,x,2));
endr = LastValue(ValueWhen(pr,S11,1));
startr = LastValue(ValueWhen(pr,S11,2));
dtR = endt1 - startt1;
aR = (endR - startR) / dtR;
bR = endR;
trendlineR = aR *(x - endt1) + bR;
g4 = IIf(x > startT1 - 10, trendlineR,  Null);
Plot(g4, "", colorr, styleLine+styleDashed+styleNoRescale+styleNoLabel);

_SECTION_END();

the above code produces the chart as below:

image

i want the below points to be achieved:

  1. If I move the cursor the red and green trend-lines also should move for the corresponding candles----is this possible?
  2. If both the trend-lines are i same direction then I should get alert as corresponding trend and in all other case it should alert as unclear trend.

Please guide/help me.

As to point #2:

finding the slope of each trendline , ie: both falling or both rising or 1 falling and 1 rising will give YOU the current direction of the trendlines

As to point #1:
Possibly lookup SelectedValue() in the help guide as a starting point.

Thanks a ton @Anthony for helping me out.

As to point #1:

I got the desired result by using your advise.

As to point #2:

I amended the code as per your advise ad below is the detailed code and snapshoot:

//////////////chart & back ground color////////////////////
SetChartBkGradientFill( colorBlack, colorBlack, colorBlack );
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( Close, "C", colorWhite, styleCandle );
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorPink, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );
starttime = 091500;
endtime = 151500;
separator = Day() != Ref( Day(), -1 );
insession =  timenum() >= starttime && timenum() <= 151000;
endsession = timenum() >= endtime;
endsession = ( endsession - Ref( endsession, -1 ) ) == 1 ;//OR Ref( separator, 1 );
startsession = timenum() >= starttime;
startsession = ( startsession - Ref( startsession, -1 ) ) == 1;
timelmits =    insession  && !endsession;
Plot( endsession, "", colorWhite, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
Plot( startsession, "", colorViolet, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
//GraphXSpace = 85;

_SECTION_BEGIN("Long R/S");

farback = Param("How Far back to go", 100, 50, 5000, 10);
nBars = Param("Number of bars", 12, 5, 40);
aHPivs = H - H;
aLPivs = L - L;
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
aVisBars = Status("barvisible");
nLastVisBar = SelectedValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
curBar = (BarCount - 1);
curTrend = "";
if (aLLVBars[curBar] < aHHVBars[curBar])
{
    curTrend = "D";
}

else
{
    curTrend = "U";
}

for (i = 0; i < farback; i++)
{
    curBar = (BarCount - 1) - i;
    if (aLLVBars[curBar] < aHHVBars[curBar])
    {
        if (curTrend == "U")
        {
            curTrend = "D";
            curPivBarIdx = curBar - aLLVBars[curBar];
            aLPivs[curPivBarIdx] = 1;
            aLPivLows[nLPivs] = L[curPivBarIdx];
            aLPivIdxs[nLPivs] = curPivBarIdx;
            nLPivs++;
        }
    }
    else
    {
        if (curTrend == "D")
        {
            curTrend = "U";
            curPivBarIdx = curBar - aHHVBars[curBar];
            aHPivs[curPivBarIdx] = 1;
            aHPivHighs[nHPivs] = H[curPivBarIdx];
            aHPivIdxs[nHPivs] = curPivBarIdx;
            nHPivs++;
        }
    }
}

curBar = (BarCount - 1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx)
{
    candIdx = curBar - aHHVBars[curBar];
    candPrc = aHHV[curBar];
    if (lastHPH < candPrc AND candIdx > lastLPIdx AND candIdx < curBar)
    {
        aHPivs[candIdx] = 1;
        for (j = 0; j < nHPivs; j++)
        {
            aHPivHighs[nHPivs - j] = aHPivHighs[nHPivs - (j + 1)];
            aHPivIdxs[nHPivs - j] = aHPivIdxs[nHPivs - (j + 1)];
        }
        aHPivHighs[0] = candPrc;
        aHPivIdxs[0] = candIdx;
        nHPivs++;
    }
}

else
{
    candIdx = curBar - aLLVBars[curBar];
    candPrc = aLLV[curBar];
    if (lastLPL > candPrc AND candIdx > lastHPIdx AND candIdx < curBar)
    {
        aLPivs[candIdx] = 1;
        for (j = 0; j < nLPivs; j++)
        {
            aLPivLows[nLPivs - j] = aLPivLows[nLPivs - (j + 1)];
            aLPivIdxs[nLPivs - j] = aLPivIdxs[nLPivs - (j + 1)];
        }
        aLPivLows[0] = candPrc;
        aLPivIdxs[0] = candIdx;
        nLPivs++;
    }
}

for (k = 0; k < nHPivs; k++)
{
    _TRACE("High pivot no. " + k + " at barindex: " + aHPivIdxs[k] + ", " + WriteVal(ValueWhen(BarIndex() == aHPivIdxs[k], DateTime(), 1), formatDateTime) + ", " + aHPivHighs[k]);
}

a1 = aHPivs == 1;
a2 = aLPivs == 1;

Para = ParamToggle("Plot Parallel Lines","Off,On");
ColorS= ParamColor("Support",colorLime);
ColorR= ParamColor("Resistance",colorRed);
x = Cum(1);
s1 = L;
s11 = H;

pS = a2 == 1;
endt = SelectedValue(ValueWhen(ps,x,1));//LastValue
startt = SelectedValue(ValueWhen(ps,x,2));
ends = SelectedValue(ValueWhen(ps,S1,1));
starts = SelectedValue(ValueWhen(ps,S1,2));
dtS = endt - startt;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS *(x - endt) + bS;
g3 = IIf(x > startt -1 , trendlineS,  Null);
Pi = 3.14159265 * atan( 1 );
SlopeAngles = atan( trendlineS ) * ( 180 / Pi );
g3Up = SlopeAngles > 0.7;
g3Dn = SlopeAngles < -0.7;
Trends = IIf( g3Up, colorLime, iif( g3Dn, colorRed, colorWhite ) ); 
Plot(g3, "", Trends , styleLine+styleDashed+styleNoRescale+styleNoLabel);

pR = a1 == 1;
endt1 = SelectedValue(ValueWhen(pr,x,1));
startt1 = SelectedValue(ValueWhen(pr,x,2));
endr = SelectedValue(ValueWhen(pr,S11,1));
startr = SelectedValue(ValueWhen(pr,S11,2));
dtR = endt1 - startt1;
aR = (endR - startR) / dtR;
bR = endR;
trendlineR = aR *(x - endt1) + bR;
g4 = IIf(x > startT1 -1, trendlineR,  Null);
Pi = 3.14159265 * atan( 1 );
SlopeAngler = atan( g4 ) * ( 180 / Pi );
g4Up = SlopeAngler > 0.7;
g4Dn = SlopeAngler < -0.7;
Trendr = IIf( g4Up, colorLime, iif( g4Dn, colorRed, colorWhite ) ); 
Plot(g4, "", Trendr , styleLine+styleDashed+styleNoRescale+styleNoLabel);

_SECTION_END();

image

But still I am not getting my below desired results:

  1. Both the trendlines appear in colorLime always. Please guide further where I am going wrong. The same method of change colour of lines work in my other codes, but is not working here and I do not know why even after a lot of try.

  2. At some points I see that the lines are not just connecting the two recent peaks / troughs. How to amend the code further to achieve it? The lines should just connect the two recent peaks / troughs not before or after extensions are required (in the image pasted after extensions i.e the lines are drawn till the current candle after passing the recent peak / trough, which I do not want )

Please help and guide.

seniors / experts please help me, as I am stuck in the above code.

Santy,

I ammended the coloring of the trend lines with some simple code and commented out your angle formula.

See if that gets the coloring correct for you.

//////////////chart & back ground color////////////////////
SetChartBkGradientFill( colorBlack, colorBlack, colorBlack );
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
Plot( Close, "C", colorWhite, styleCandle );
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorPink, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );
starttime = 091500;
endtime = 151500;
separator = Day() != Ref( Day(), -1 );
insession =  timenum() >= starttime && timenum() <= 151000;
endsession = timenum() >= endtime;
endsession = ( endsession - Ref( endsession, -1 ) ) == 1 ;//OR Ref( separator, 1 );
startsession = timenum() >= starttime;
startsession = ( startsession - Ref( startsession, -1 ) ) == 1;
timelmits =    insession  && !endsession;
Plot( endsession, "", colorWhite, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
Plot( startsession, "", colorViolet, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
//GraphXSpace = 85;

_SECTION_BEGIN("Long R/S");

farback = Param("How Far back to go", 100, 50, 5000, 10);
nBars = Param("Number of bars", 12, 5, 40);
aHPivs = H - H;
aLPivs = L - L;
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
aVisBars = Status("barvisible");
nLastVisBar = SelectedValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
curBar = (BarCount - 1);
curTrend = "";
if (aLLVBars[curBar] < aHHVBars[curBar])
{
    curTrend = "D";
}

else
{
    curTrend = "U";
}

for (i = 0; i < farback; i++)
{
    curBar = (BarCount - 1) - i;
    if (aLLVBars[curBar] < aHHVBars[curBar])
    {
        if (curTrend == "U")
        {
            curTrend = "D";
            curPivBarIdx = curBar - aLLVBars[curBar];
            aLPivs[curPivBarIdx] = 1;
            aLPivLows[nLPivs] = L[curPivBarIdx];
            aLPivIdxs[nLPivs] = curPivBarIdx;
            nLPivs++;
        }
    }
    else
    {
        if (curTrend == "D")
        {
            curTrend = "U";
            curPivBarIdx = curBar - aHHVBars[curBar];
            aHPivs[curPivBarIdx] = 1;
            aHPivHighs[nHPivs] = H[curPivBarIdx];
            aHPivIdxs[nHPivs] = curPivBarIdx;
            nHPivs++;
        }
    }
}

curBar = (BarCount - 1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx)
{
    candIdx = curBar - aHHVBars[curBar];
    candPrc = aHHV[curBar];
    if (lastHPH < candPrc AND candIdx > lastLPIdx AND candIdx < curBar)
    {
        aHPivs[candIdx] = 1;
        for (j = 0; j < nHPivs; j++)
        {
            aHPivHighs[nHPivs - j] = aHPivHighs[nHPivs - (j + 1)];
            aHPivIdxs[nHPivs - j] = aHPivIdxs[nHPivs - (j + 1)];
        }
        aHPivHighs[0] = candPrc;
        aHPivIdxs[0] = candIdx;
        nHPivs++;
    }
}

else
{
    candIdx = curBar - aLLVBars[curBar];
    candPrc = aLLV[curBar];
    if (lastLPL > candPrc AND candIdx > lastHPIdx AND candIdx < curBar)
    {
        aLPivs[candIdx] = 1;
        for (j = 0; j < nLPivs; j++)
        {
            aLPivLows[nLPivs - j] = aLPivLows[nLPivs - (j + 1)];
            aLPivIdxs[nLPivs - j] = aLPivIdxs[nLPivs - (j + 1)];
        }
        aLPivLows[0] = candPrc;
        aLPivIdxs[0] = candIdx;
        nLPivs++;
    }
}

for (k = 0; k < nHPivs; k++)
{
    _TRACE("High pivot no. " + k + " at barindex: " + aHPivIdxs[k] + ", " + WriteVal(ValueWhen(BarIndex() == aHPivIdxs[k], DateTime(), 1), formatDateTime) + ", " + aHPivHighs[k]);
}

a1 = aHPivs == 1;
a2 = aLPivs == 1;

Para = ParamToggle("Plot Parallel Lines","Off,On");
ColorS= ParamColor("Support",colorLime);
ColorR= ParamColor("Resistance",colorRed);
x = Cum(1);
s1 = L;
s11 = H;

pS = a2 == 1;
endt = SelectedValue(ValueWhen(ps,x,1));//LastValue
startt = SelectedValue(ValueWhen(ps,x,2));
ends = SelectedValue(ValueWhen(ps,S1,1));
starts = SelectedValue(ValueWhen(ps,S1,2));
dtS = endt - startt;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS *(x - endt) + bS;
g3 = IIf(x > startt -1 , trendlineS,  Null);

//Pi = 3.14159265 * atan( 1 );
//SlopeAngles = atan( trendlineS ) * ( 180 / Pi );

////////////////////////////////////////////////////
           Lowline=Ends-starts;//support
//////////////////////////////////////////////////////
g3Up = Lowline > 0;
g3Dn = Lowline < 0;

Trends = IIf( g3Up, colorlime, iif( g3Dn, colorred, colorWhite ) ); 
Plot(g3, "", Trends , styleLine+styleDashed+styleNoRescale+styleNoLabel);

pR = a1 == 1;
endt1 = SelectedValue(ValueWhen(pr,x,1));
startt1 = SelectedValue(ValueWhen(pr,x,2));
endr = SelectedValue(ValueWhen(pr,S11,1));
startr = SelectedValue(ValueWhen(pr,S11,2));
dtR = endt1 - startt1;
aR = (endR - startR) / dtR;
bR = endR;
trendlineR = aR *(x - endt1) + bR;
g4 = IIf(x > startT1 -1, trendlineR,  Null);

//Pi = 3.14159265 * atan( 1 );
//SlopeAngler = atan( g4 ) * ( 180 / Pi );
//////////////////////////////////////////////////
        Highline=endr-startr;//resistance
/////////////////////////////////////////////////
g4Up = highline > 0;
g4Dn = highline < 0;

Trendr = IIf( g4Up, colorred, iif( g4Dn, colorlime, colorWhite ) ); 
Plot(g4, "", Trendr , styleLine+styleDashed+styleNoRescale+styleNoLabel);

_SECTION_END();


1 Like

Thank you @Anthony again for helping me.

Yes, now problem 1 is sorted out.

Still waiting for a solution for problem 2, which is as below:

***At some points I see that the lines are not just connecting the two recent peaks / troughs. How to amend the code further to achieve it? The lines should just connect the two recent peaks / troughs not before or after extensions are required (in the image pasted after extensions i.e the lines are drawn till the current candle after passing the recent peak / trough, which I do not want )

I think there are many lines in your code that are redundant and useless , if you like to draw an automated trendline on your chart
you need first to identify the begin and end of swing in both direction, and when it come to swings there's no best than ZigZag indicator to spot the reversal points
thus considering this as the start point would faciliate writing a more effecient and concise code to realize your purpose as you would depend on arrays with no need to use loop

Here's a example code for achieving that

Percentage = Param("Percentage Change",5,0,100);
Extensions = ParamList("Extension Option","Right Extension|Left Extension|Right-Left Extension|Disable",3);
UpColor	   = ParamColor("Up-Trend Line's Color",colorLime);
DnColor	   = ParamColor("Down-Trend Line's Color",colorRed);

Extension = 0;
if ( Extensions == "Disable" ) 		  	    { Extension = 0;}
if ( Extensions == "Right Extension" )	    { Extension = 1;}
if ( Extensions == "Left Extension" )	    { Extension = 2;}
if ( Extensions == "Right-Left Extension" ) { Extension = 3;}


Zz = Zig(High,Percentage);
HL = IIf(ROC(Zz,1) < 0,Low,High);
Zz = Zig(HL,Percentage);



CH  = Ref(Zz,-1) < Ref(Zz,0) AND Ref(Zz,0) > Ref(Zz,1);
CL  = Ref(Zz,-1) > Ref(Zz,0) AND Ref(Zz,0) < Ref(Zz,1);

HY1 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-2 == Cum(CH),-1),High));
HX1 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-2 == Cum(CH),-1),BarIndex()));

HY2 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-1 == Cum(CH),-1),High));
HX2 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-1 == Cum(CH),-1),BarIndex()));

LY1 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-2 == Cum(CL),-1),Low));
LX1 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-2 == Cum(CL),-1),BarIndex()));

LY2 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-1 == Cum(CL),-1),Low));
LX2 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-1 == Cum(CL),-1),BarIndex()));


T1  = LineArray(HX1,HY1,HX2,HY2,Extension);
T2  = LineArray(LX1,LY1,LX2,LY2,Extension);
 
CT1 = IIf(ROC(T1,1) > 0,UpColor,DnColor);
CT2 = IIf(ROC(T2,1) > 0,UpColor,DnColor);


Plot(Close,"Close",colorDefault,styleBar);
Plot(Zz,"ZigZag",colorWhite,styleLine);

Plot(T1,"T1",CT1,styleNoRescale);
Plot(T2,"T2",CT2,styleNoRescale);

5 Likes

Thank you @Anthony and @Sebastian for your help and guidence to achieve my desired output for the above code.

May God Bless you !

Percentage = Param("Percentage Change",0.1,0,100);
Extensions = ParamList("Extension Option","Right Extension|Left Extension|Right-Left Extension|Disable",3);
UpColor	   = ParamColor("Up-Trend Line's Color",colorLime);
DnColor	   = ParamColor("Down-Trend Line's Color",colorRed);

Extension = 0;
if ( Extensions == "Disable" ) 		  	    { Extension = 0;}
if ( Extensions == "Right Extension" )	    { Extension = 1;}
if ( Extensions == "Left Extension" )	    { Extension = 2;}
if ( Extensions == "Right-Left Extension" ) { Extension = 3;}


Zz = Zig(High,Percentage);
HL = IIf(ROC(Zz,1) < 0,Low,High);
Zz = Zig(HL,Percentage);



CH  = Ref(Zz,-1) < Ref(Zz,0) AND Ref(Zz,0) > Ref(Zz,1);
CL  = Ref(Zz,-1) > Ref(Zz,0) AND Ref(Zz,0) < Ref(Zz,1);

HY1 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-2 == Cum(CH),-1),High));
HX1 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-2 == Cum(CH),-1),BarIndex()));

HY2 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-1 == Cum(CH),-1),High));
HX2 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-1 == Cum(CH),-1),BarIndex()));

LY1 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-2 == Cum(CL),-1),Low));
LX1 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-2 == Cum(CL),-1),BarIndex()));

LY2 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-1 == Cum(CL),-1),Low));
LX2 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-1 == Cum(CL),-1),BarIndex()));


T1  = LineArray(HX1,HY1,HX2,HY2,Extension);
T2  = LineArray(LX1,LY1,LX2,LY2,Extension);
 
CT1 = IIf(ROC(T1,1) > 0,UpColor,DnColor);
CT2 = IIf(ROC(T2,1) > 0,UpColor,DnColor);


Plot(Close,"Close",colorDefault,styleBar);
Plot(Zz,"ZigZag",colorWhite,styleLine);

Plot(T1,"T1",CT1,styleNoRescale);
Plot(T2,"T2",CT2,styleNoRescale);


@Sebastian, I used your above code and was trying to add buy / sell arrows when both the lines are green and red respectively. but the problem is that when ever the lines change the signals also disappear, is there any solution / workaround to it?

I want once the signal generated it should be stable irrespective of the future change of the trend-lines.

Please guide further.

alright but as i see you post my code again while you should post your code for making some revision and tell what you should do , unless that happen i will not be able to make any suggestion or advice .

1 Like

OK, I am in office now.

Once I go home , I will post that ---nearly after around 4 hours.

Thanks for helping me.

Hi @Sebastian ;

As discussed my revised code and the result chart image is as below:

Percentage = Param("Percentage Change",0.01,0,100);
Extensions = ParamList("Extension Option","Right Extension|Left Extension|Right-Left Extension|Disable",3);
UpColor	   = ParamColor("Up-Trend Line's Color",colorLime);
DnColor	   = ParamColor("Down-Trend Line's Color",colorRed);

Extension = 0;
if ( Extensions == "Disable" ) 		  	    { Extension = 0;}
if ( Extensions == "Right Extension" )	    { Extension = 1;}
if ( Extensions == "Left Extension" )	    { Extension = 2;}
if ( Extensions == "Right-Left Extension" ) { Extension = 3;}


Zz = Zig(High,Percentage);
HL = IIf(ROC(Zz,1) < 0,Low,High);
Zz = Zig(HL,Percentage);



CH  = Ref(Zz,-1) < Ref(Zz,0) AND Ref(Zz,0) > Ref(Zz,1);
CL  = Ref(Zz,-1) > Ref(Zz,0) AND Ref(Zz,0) < Ref(Zz,1);

HY1 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-2 == Cum(CH),-1),High));
HX1 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-2 == Cum(CH),-1),BarIndex()));

HY2 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-1 == Cum(CH),-1),High));
HX2 = LastValue(ValueWhen(Ref(LastValue(Cum(CH))-1 == Cum(CH),-1),BarIndex()));

LY1 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-2 == Cum(CL),-1),Low));
LX1 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-2 == Cum(CL),-1),BarIndex()));

LY2 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-1 == Cum(CL),-1),Low));
LX2 = LastValue(ValueWhen(Ref(LastValue(Cum(CL))-1 == Cum(CL),-1),BarIndex()));


T1  = LineArray(HX1,HY1,HX2,HY2,Extension);
T2  = LineArray(LX1,LY1,LX2,LY2,Extension);
 
CT1 = IIf(ROC(T1,1) > 0,UpColor,DnColor);
CT2 = IIf(ROC(T2,1) > 0,UpColor,DnColor);


Plot(Close,"Close",colorDefault,styleCandle);
Plot(Zz,"ZigZag",colorWhite,styleLine);

Plot(T1,"T1",CT1,styleNoRescale);
Plot(T2,"T2",CT2,styleNoRescale);



//////////MY ADDITIONS//////////////////////

Buy = (ROC(T1,1) > 0 ) && (ROC(T2,1) > 0); 
Short =  (ROC(T1,1) < 0 ) && (ROC(T2,1) < 0);
Sell =  Short;
Cover = Buy; 

BUY = ExRem( BUY, SELL );
SELL = ExRem( SELL, BUY );
COVER = ExRem( COVER, SHORT );
SHORT = ExRem( SHORT, COVER );

HaClose = EMA( ( O + H + L + C ) / 4, 3 );
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );

PlotShapes( IIf( Buy , shapeUpArrow, 0 ) , colorGreen , layer = 0, yposition = HaLow, offset = -8 );
PlotShapes( IIf( SHORT , shapeDownArrow, 0 ) , colorRed , layer = 0, yposition = HaHigh, offset = -8 );
PlotShapes( IIf( SELL , shapeHollowDownArrow, 0 ) , colorRed , layer = 0, yposition = HaHigh, offset = -8 );
PlotShapes( IIf( COVER, shapeHollowUpArrow, 0 ) , colorGreen , layer = 0, yposition = HaLow, offset = -8 );


image

AS you ca see i the image , only the last signal is there on the chart.

I want all generated buy / sell arrows once generated it should not be vanished and should be there on the chart. But the problem is that when ever the lines change, the arrows also disappear, is there any solution / workaround to it?

I want once the signal generated it should be stable irrespective of the future change of the trend-lines.

Please guide further.

HI Santy,

Just a informational note about Zig-Zag indicator: https://www.investopedia.com/terms/z/zig_zag_indicator.asp

Traders should be aware that the most recent zig zag line may not be permanent. When the price changes direction, the indicator starts to draw a new line. If that zig zag line does not reach the indicator’s percentage setting and the price of the security reverses direction - that line is removed and replaced by an extended zig zag line in the trend’s original direction. Given the lag, many traders use the zig zag indicator to confirm the direction of the trend rather than timing an entry or exit.

If you are going to use the zigzag indicator you should validate the swing levels before applying buy and sell rules.

Anthony

can you please give me a start for the above in code?

Santy,

Try google search.
Something like:

validate the zigzag indicator in amibroker

Thank you @Anthony for helping.

Hi @Sebastian,

I posted my tried code yesterday for the stable buy / sell signas as discussed. any update please?

First of all i would like to pay kindly your attention to an important issue ..
the humble code i provided above is for educational / learning purpose,that supposed to motivate you to search , study and to discover how these things work , so you have a chance to enhance your programming skills and push it up to a higher level

However using the code without well understanding will only result in a catastrophic outcome and in this case you ought to abide the consequences , so please keep this in your mind and don't forget it


In respect of the code , I see you have modified the first line , changing it from 1 to 0.01

Do you know what does the 2nd argument of Param () mean ?
Do you realize the effect of that on the rest of the code ?

Anyway to show the arrows for all trade you have entered you need to work with array as LineArray() could only draw one line , its parameter accept only number not arrays

Then you have to determine the beginning point for which you want to show trades you have previously entered

Here's an example code for achieving that

Percentage = Param("Percentage Change",1,0,100);
DateEntry  = ParamDate("Beginning Date for Exploration","1-1-2017",2); // You may like to replace this ParamTime if you are trading on Real-Time charts
UpColor	   = ParamColor("Up-Trend Line's Color",colorLime);
DnColor	   = ParamColor("Down-Trend Line's Color",colorRed);



Zz = Zig(High,Percentage);
HL = IIf(IsNull(ROC(Zz,1)),IIf(Close > Open,High,Low),IIf(ROC(Zz,1) > 0,High,Low));
Zz = Zig(HL,Percentage);
HL = IIf(IsNull(ROC(Zz,1)),IIf(Close > Open,High,Low),IIf(ROC(Zz,1) > 0,High,Low));
Zz = Zig(HL,Percentage);



CH  = ValueWhen(DateEntry <= DateTime(),Ref(Zz,-1) < Ref(Zz,0) AND Ref(Zz,0) > Ref(Zz,1));
CL  = ValueWhen(DateEntry <= DateTime(),Ref(Zz,-1) > Ref(Zz,0) AND Ref(Zz,0) < Ref(Zz,1));

S1  = (ValueWhen(CH,High,0)-ValueWhen(CH,High,1))/(ValueWhen(CH,BarIndex(),0)-ValueWhen(CH,BarIndex(),1));
S2  = (ValueWhen(CL,Low,0) -ValueWhen(CL,Low,1)) /(ValueWhen(CL,BarIndex(),0)-ValueWhen(CL,BarIndex(),1));

T1  = S1*BarsSince(CH == 1)+ValueWhen(CH,High);
T2  = S2*BarsSince(CL == 1)+ValueWhen(CL,Low);

CT1 = IIf(ROC(T1,1) > 0,UpColor,DnColor);
CT2 = IIf(ROC(T2,1) > 0,UpColor,DnColor);


Plot(Close,"Close",colorDefault,styleBar);


HaClose = EMA( ( O + H + L + C ) / 4, 3 );
HaOpen  = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh  = Max( H, Max( HaClose, HaOpen ) );
HaLow   = Min( L, Min( HaClose, HaOpen ) );

Buy 	= ROC(T1,1) > 0 AND ROC(T2,1) > 0;
Sell	= ROC(T1,1) < 0 AND ROC(T2,1) < 0;

Buy  	= ExRem(Buy,Sell);
Sell	= ExRem(Sell,Buy);


PlotShapes( IIf( Buy , shapeUpArrow, 0 ) , colorGreen , layer = 0, yposition = HaLow, offset = -8 );
PlotShapes( IIf( SELL , shapeHollowDownArrow, 0 ) , colorRed , layer = 0, yposition = HaHigh, offset = -8 );


3 Likes

Thanks and accepted your feedback @Sebastian .

I will do more indepth study and work on this.

Once again thank you for mentoring.

You are welcome and hope to learn something new from the above code :slight_smile:

1 Like