Can give the channel color? Thanks 🙏

Can you give the channel color? Thanks

// ZigZag Hi Lo Barcount
//#include "C:\Program Files\AmiBroker\Formulas\Include\AMDG.afl"
#include "C:\Program Files\AmiBroker\Formulas\Include\Setup.afl"
// --------------------------------------------------
//OptimizerSetEngine( "cmae" );

// --------------------------------------------------
_SECTION_BEGIN( "Corrected HL" );
// ACTUAL VALUES - Corrected
// Correct some L and H values that are wrong due to faulty Yahoo data
corH = IIf( H < 0 OR H == Null OR H < Max( O, C ), Max( O, C ), H );
corL = IIf( L < 0 OR L == Null OR L > Min( O, C ), Min( O, C ), L );
Range = IIf( ( corH - corL ) > 0, Prec( corH - corL, 0 ), 0.001 );
Body = Nz( abs( O - C ), 0.001 ); 
_SECTION_END();
// --------------------------------------------------
Title = GetFnData( "FullName" ) + " :+: " + StrFormat( "{{NAME}} :+:
{{INTERVAL}} = {{DATE}} :+: ZigZag - Days, Avg Volumes & Channels :+: O: %g, H:
%g, L: %g, C: %g (%.1f%%), Avg: %g (%.1f%%), Vol(mil): %.2f (%.0f%%)
{{VALUES}}", 
O, corH, corL, C, SelectedValue( ROC( C, 1 ) ), Avg, SelectedValue( ROC( Avg, 1
) ), V / 1000000, SelectedValue( ROC( V, 1 ) ) );

// --------------------------------------------------
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0,
0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, colorBrightGreen, IIf( C <= O, colorRed, colorLightGrey
) ), styleCandle, Null, Null, 0, 0, 1 );
	
_SECTION_BEGIN( "ZigZag BarCount, Avg Volumes && Channels" );
showZZ = ParamToggle( "Show ZigZag ", "No|Yes", 0 );
showVols = ParamToggle( "Show Volumes ", "No|Yes", 0 );
showChan = ParamToggle( "Show Channels ", "No|Yes", 0 );
Price = ParamField( "Price", 4 ); // -1=first, 0=O, 1=H, 2=L, 3=C, 4=Avg, 5=Vol,
6=Open Int, 7...
ZigPerc = Param( "ZigZag %", 5, 1, 20, 0.25 );
ZigPerc = Optimize( "ZigZag %", ZigPerc, 1, 20, 0.25 );
fontSize = Param( "Font Size", 7, 3, 15, 1 );
distTop = 2 * ATR(10);
distBot = 1 * ATR(10);
za2 = Zig( Price, ZigPerc );
upzig = za2 >= Ref( za2, -1 );
dnzig = za2 <= Ref( za2, -1 );
Peakzig = za2 > Ref( za2, -1 ) AND za2 > Ref( za2, 1 );
Troughzig = za2 < Ref( za2, -1 ) AND za2 < Ref( za2, 1 ) ;
// Day count
BarsUp = BarsSince( dnzig );
BarsDn = BarsSince( upzig );
// Volume Averages up/dn
avgVup = IIf( upzig, Sum( V / 1000000, BarsUp ), 0 );
avgVdn = IIf( dnzig, Sum( V / 1000000, BarsDn ), 0 );
// Peak averages
aPk1 = ValueWhen( Peakzig, avgVup, 1 );
aPk2 = ValueWhen( Peakzig, avgVup, 2 );
aPk3 = SafeDivide( aPk1 - aPk2, aPk2, 0.001 ) * 100;
// Trough averages
aTr1 = ValueWhen( Troughzig, avgVdn, 1 );
aTr2 = ValueWhen( Troughzig, avgVdn, 2 );
aTr3 = SafeDivide( aTr1 - aTr2, aTr2, 0.001 ) * 100;
// Peak volume
vPk1 = ValueWhen( Peakzig, V, 1 ) / 1000000;
vPk2 = ValueWhen( Peakzig, V, 2 ) / 1000000;
vPk3 = SafeDivide ( vPk1 - vPk2, vPk2, 0.001 ) * 100;
// Trough volume
vTr1 = ValueWhen( Troughzig, V, 1 ) / 1000000;
vTr2 = ValueWhen( Troughzig, V, 2 ) / 1000000;
vTr3 = SafeDivide( vTr1 - vTr2, vTr2, 0.001 ) * 100;
//
fvb = Status( "firstvisiblebar" ) ;
// Channels
SECUpColor = ParamColor( "Channel-UpColor", colorBrightGreen );
SECDnColor = ParamColor( "Channel-DownColor", colorRed );
SECLastColor = ParamColor( "Channel-LastColor", colorCustom11 );
BckGndColor = ParamColor( "BackGroundColor", colorBlack );
BarPK = BarsSince( Zig( Price, ZigPerc ) == Peak( Price, ZigPerc ) );
BarTGH = BarsSince( Zig( Price, ZigPerc ) == Trough( Price, ZigPerc ) );
Turnpoint = Min( BarPK, BarTGH );
x = Cum( 1 );
lastx = ValueWhen( Turnpoint == 0, ( x ) );
Daysback = ValueWhen( Turnpoint == 0, ( Max( PeakBars( Price, ZigPerc, 1 ),
TroughBars( Price, ZigPerc, 1 ) ) ) ) + 1;
aa = ValueWhen( Turnpoint == 0, LinRegIntercept( Close, Daysback ) );
bb = ValueWhen( Turnpoint == 0, LinRegSlope( Close, Daysback ) );
y = Aa + bb * ( x - ( Lastx - DaysBack + 1 ) );
e = ValueWhen( Turnpoint == 0, StdErr( Price, Daysback ) );
DaysbackL = LastValue( Min( PeakBars( Price, ZigPerc, 1 ), TroughBars( Price,
ZigPerc, 1 ) ) );
Slope = ValueWhen( Turnpoint == 0, bb, 0 );
Level = ValueWhen( Turnpoint == 0, aa, 0 );
Err = ValueWhen( Turnpoint == 0, e, 0 );
Mback = Level + slope * ( Turnpoint ) ;
Uback = MBack + 2 * Err;
Lback = MBack - 2 * Err;
ColorSEC2 = IIf( Cross( bb, 0 ) OR Cross( 0, bb ), BckGndColor, IIf( bb < 0,
SECUpColor, SECDnColor ) );
// Last Channel
x = Cum( 1 );
lastxL = LastValue( x );
DaysbackL = LastValue( Min( PeakBars( Price, ZigPerc, 1 ), TroughBars( Price,
ZigPerc, 1 ) ) ) + 1;
Endbars = IIf( PeakBars( Price, ZigPerc, 1 ) < TroughBars( Price, ZigPerc, 1 ),
LastValue( ValueWhen( Price == LLV( Price, DaysbackL ), X ) ), LastValue(
ValueWhen( Price == HHV( Price, DaysbackL ), X ) ) );
BarsL = Min( PeakBars( Price, ZigPerc, 1 ), TroughBars( Price, ZigPerc, 1 ) ) ;
aaL = ValueWhen( Ref( Endbars, -1 ) != Ref( x, -1 ) AND Endbars == x ,
LinRegIntercept( Close, DaysbackL - ( LastValue( x ) - Endbars ) ) );
bbL = ValueWhen( Ref( Endbars, -1 ) != Ref( x, -1 ) AND Endbars == x ,
LinRegSlope( Close, DaysbackL - ( LastValue( x ) - Endbars ) ) );
eL = ValueWhen( Ref( Endbars, -1 ) != Ref( x, -1 ) AND Endbars == x , StdErr(
Price, DaysbackL - ( LastValue( x ) - Endbars ) ) );
SlopeL = LastValue( bbL );
LevelL = LastValue( aaL );
ErrL = LastValue( eL );
MbackL = LevelL + slopeL * ( BarsL ) ;
UbackL = MBackL + 2 * ErrL;
LbackL = MBackL - 2 * ErrL;
//
if ( showZZ )
{
    Plot( za2, "\nZig" + _PARAM_VALUES() , colorAqua, styleLine | styleNoLabel
);
}
if( showVols )
{
for ( i = fvb; i < BarCount; i++ )
	{
	// Plot text at peaks/troughs
    if ( Peakzig [i] )
        PlotTextSetFont( "" + BarsUp[i] + "d\n" + NumToStr( avgVup[i], 1.0 ) +
"m/d (" + NumToStr( aPk3[i], 1.0 ) + "%)\n" + 
			NumToStr( V[i] / 1000000, 1.0, True, True )  + "m (" + NumToStr( vPk3[i], 1.0
) + "%)", 
			"Arial", fontSize, i - 2, H[i] + distTop[i], colorBrightGreen ); 
     
    if ( Troughzig [i] )
        PlotTextSetFont( "" + BarsDn [i] + "d\n" + NumToStr( avgVdn[i], 1.0 ) +
"m/d (" + NumToStr( aTr3[i], 1.0 ) + "%)\n" +
			NumToStr( V[i] / 1000000, 1.0 ) + "m (" + NumToStr( vTr3[i], 1.0 ) + "%)",  
			"Arial", fontSize, i - 2, L[i] - distBot[i], colorRed );
	}
	// Last Leg
	if ( LastValue( upzig ) )
		{
		aLLup = ( ( LastValue( avgVup ) - aPk1 ) / aPk1 ) * 100;
		curV =  V[i-1] / 1000000;
		vLLup = ( ( curV - vPk1 ) / vPk1 ) * 100;
		PlotTextSetFont( "" + LastValue( BarsUp ) + "d\n" + NumToStr( LastValue(
avgVup ), 1.0 ) + "m/d (" + NumToStr( aLLup, 1.1 ) + "%)\n" +
			NumToStr( curV, 1.0 ) + "m (" + NumToStr( vLLup, 1.0 ) + "%)", 
			"Arial", fontSize, i -2, H[i-1] + distTop[i-1], colorBrightGreen );
		}
	else
		{
		aLLdn = SafeDivide( LastValue( avgVdn ) - aTr1, aTr1, 0.001 ) * 100;
		curV =  V[i-1] / 1000000;
		vLLdn = ( ( curV - vTr1 ) / vTr1 ) * 100;
		PlotTextSetFont( "" + LastValue( BarsDn ) + "d\n" + NumToStr( LastValue(
avgVdn ), 1.1 ) + "m/d (" + NumToStr( aLLdn, 1.0 ) + "%)\n" +
			NumToStr( curV, 1.0 ) + "m (" + NumToStr( vLLdn, 1.0 ) + "%)", 
			"Arial", fontSize, i -2, L[i-1] - distBot[i-1], colorRed );
		}
}
if( showChan )
{
	Plot( IIf( ( X > ( LastValue( x ) - DaysbackL ) ), Null , Uback ) , "",
ColorSEC2 );
	Plot( IIf( ( X > ( LastValue( x ) - DaysbackL ) ), Null , Lback ) , "",
ColorSEC2 );
	Plot( IIf( X > ( LastValue( x ) - DaysbackL ) AND MbackL != 0, UbackL, Null ) ,
"", SECLastColor, styleNoLabel );
	Plot( IIf( X > ( LastValue( x ) - DaysbackL ) AND MbackL != 0, LbackL, Null ) ,
"", SECLastColor, styleNoLabel );

}
_SECTION_END();

Please help, may his help reap goodness. Thanks and Gbu

@Beuty the standard way to do it is using a PlotOHLC() as described in this KB article:

How to fill the area between two lines with a solid-color.

In your case, it will be a little tricky to decide how to handle the color changes around the zig-zag inversion points.

From the above-linked web page, be sure also to read the "Related articles" to learn how to (eventually) plot the color behind the grid lines.

Thank you very much Mr Beppe, I've tried it, but the error persists. Hopefully someone is kind enough to help. Thanks :pray:

@Beuty, you need to play a bit and eventually change code similar to the following one:

if( showChan )
{
    ColorSEC3 = IIf( Cross( bb, 0 ) OR Cross( 0, bb ), Null, IIf( bb < 0, SECUpColor, SECDnColor ) );
    // ColorSEC3 = ColorSEC2; // Uncomment to see how the original code will show the colored channel

    condition =  X >= ( LastValue( x ) - DaysbackL );
    plotU = IIf( condition, Null, UBack );  // split  on 2 lines only to make it more easy to understand
    plotU = IIf( colorSEC3, plotU, Null );

    plotL = IIf( condition, Null, LBack );
    plotL = IIf( colorSEC3, plotL, Null );

    PlotOHLC( plotU, plotU, plotL, plotL, "", colorSEC3, styleCloud, Null, Null, 0, -2 ); // Z-order -2


    condition = X > ( LastValue( x ) - DaysbackL ) AND MbackL != 0;
    plotU = IIf( condition, UBackL, Null );
    plotL = IIf( condition, LBackL, Null );
    PlotOHLC( plotU, plotU, plotL, plotL, "", SECLastColor, styleCloud, Null, Null, 0, -2 ); // Z-order -2
}

image

1 Like

Thank you for your help and kindness Mr. Beppe :pray::pray::pray:

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.