Keep last color in Donchian Channel

I stuck again in a little problem with following code

_SECTION_BEGIN("Donchian channel"); 
Pds = Param("Periods",20,2,200,1); 
bullcolor=ParamColor("bullcolor", colorbrightgreen);
bearcolor=ParamColor("bearcolor", colorred);
drawcolor= colorWhite;
DonchianUpper =HHV(Ref(H,0),pds);
DonchianLower = LLV(Ref(L,0),pds);
DonchianMiddle = (DonchianUpper+DonchianLower)/2;
uptrend = (DonchianUpper>Ref(DonchianUpper,-1) and DonchianLower>=Ref(DonchianLower,-1)) 
or (DonchianLower>Ref(DonchianLower,-1) and DonchianUpper>=Ref(DonchianUpper,-1));
downtrend = (DonchianUpper<Ref(DonchianUpper,-1) and DonchianLower<=Ref(DonchianLower,-1)) 
or (DonchianLower<Ref(DonchianLower,-1) and DonchianUpper<=Ref(DonchianUpper,-1));
drawcolor = IIf(uptrend,bullcolor,IIf(downtrend,bearcolor,Ref(drawcolor,-1)));
Plot(DonchianUpper,"DU",drawcolor,styleLine);
Plot(DonchianMiddle,"DM",drawcolor,styleLine);
Plot(DonchianLower,"DL",drawcolor,styleLine); 
_SECTION_END();

I want simply that always the last trend of the Donchian channel reflects in the color,
and the last trendcolor remains also if the channel continues horizontal but it just doesn't work.
I also tried with

drawcolor = Flip( bullcolor,bearcolor);

as it was recommanded elsewhere, but this doesn't work neither.
Thank you for your attention and help.

Flip() is an array function and bullcolor and bearcolor are not arrays but type number. Also Flip expects true/false arrays. So it does not make sense to bring them together.

Also you do not need Flip().
AFAIU you just want a change between green and red color.
So just ignore neutral color via

drawcolor = ValueWhen(drawcolor != neutralcolor, drawcolor);

So do like this

_SECTION_BEGIN( "Donchian channel" );
Pds = Param( "Periods", 20, 2, 200, 1 );
bullcolor = ParamColor( "bullcolor", colorbrightgreen );
bearcolor = ParamColor( "bearcolor", colorred );
neutralcolor = colorWhite;
alt_color = ParamToggle("Color mode", "Mode1|Mode2", 1);

DonchianUpper = HHV( Ref( H, 0 ), pds );
DonchianLower = LLV( Ref( L, 0 ), pds );
DonchianMiddle = ( DonchianUpper + DonchianLower ) / 2;

uptrend = ( DonchianUpper > Ref( DonchianUpper, -1 ) and DonchianLower >= Ref( DonchianLower, -1 ) )
          or ( DonchianLower > Ref( DonchianLower, -1 ) and DonchianUpper >= Ref( DonchianUpper, -1 ) );

downtrend = ( DonchianUpper < Ref( DonchianUpper, -1 ) and DonchianLower <= Ref( DonchianLower, -1 ) )  
		or ( DonchianLower < Ref( DonchianLower, -1 ) and DonchianUpper <= Ref( DonchianUpper, -1 ) );

drawcolor = IIf( uptrend, bullcolor, IIf( downtrend, bearcolor, neutralcolor ) );
if ( alt_color ) 
	drawcolor = ValueWhen(drawcolor != neutralcolor, drawcolor);

Plot( DonchianUpper, "DU", drawcolor, styleLine );
Plot( DonchianMiddle, "DM", drawcolor, styleLine );
Plot( DonchianLower, "DL", drawcolor, styleLine );
_SECTION_END();

6

1 Like

Indeed, this corresponds perfectly.
Thank you very much !