@uvdsatish although I am not certain I understand everything you are attempting to do, perhaps something like this can help get you started.
UpTrend = EMA(Close,8) > EMA(Close,21) AND EMA(Close,21) > EMA(Close,50);
DownTrend = EMA(Close,8) < EMA(Close,21) AND EMA(Close,21) < EMA(Close,50);
DynamicColor = IIf(UpTrend, colorGreen, IIf(DownTrend, colorRed, colorBlack));
// are you trying to sum up the number of UP and DOWN bars in past 20?
Up20 = Sum(UpTrend, 20);
Down20 = Sum(DownTrend, 20);
Plot( UpTrend or DownTrend, "Trend", DynamicColor, styleHistogram | styleThick, Null, Null, 0, 2, 7 );
// or simple Histogram
// Plot( UpTrend or DownTrend, "Trend", DynamicColor, styleHistogram | styleThick );
Thank you - one more follow up question - in the same plot, I want to plot a thick grey line for the bar that is 30 bars prior to the current last bar (probably with a different color so it is visible with the overlay); I believe i can use barindex() for that as follows - but not sure how:
I tried - but am stuggling. I wanted to use your prior code where you populated either ribbon or bars and want to populate a thick grey bar, (for the bar which is 30 bars prior to current bar) - can you pls help?
Not sure which function i should use - BarIndex() ??
Re the first question, I hope this code example may clarify the meaning of the lines of code that you did not understand
// BC20 is the result of the expression (Cum( 1 ) == ( LastValue( Cum( 1 ) ) - 20 )) that compares
// an array to a (constant) number and will return an array filled by 0 (False) elements except for a
// a single element set to 1 (True) - in this case it will be the 21st element counting backward
B20 = Cum( 1 ) == ( LastValue( Cum( 1 ) ) - 20 );
//// Exploration - Apply to *Current - All Quotes
Filter = 1;
AddColumn( Cum( 1 ), "Cum(1) array ", 1 );
// The next column does not use an array since the LastValue() function returns a NUMBER that is
// the last calculated value of the specified ARRAY - so the same number will fill the entire column.
AddColumn( LastValue( Cum( 1 ) ) - 20, "LastValue(Cum(1)) -20)", 1 );
// When an element of the B20 array is (True/1) i.e. (Cum( 1 ) == ( LastValue( Cum( 1 ) ) - 20 )) is True
// we will highlight it (this will happen only at the row when the 3rd and 4th column values are identical)
colorBkg = IIf( B20, colorYellow, colorDefault );
AddColumn( B20, "B20 array", 1, colorDefault, colorBkg );
// Sort the report by the Cum(1) array descending
SetSortColumns( -3 );
(Set "ApplyTo" to the "*Current" symbol and "All quotes").
About the second issue, the best way to manage the overlapping elements in your charts is to learn how to control the Z-order for the plotting functions, the drawings tools and the low-level graphics.