Correct order of moving averages for last 20 bars

Hi...I am trying to scan for correct order of moving averages (EMAs 8, 21, 50) for last 20 bars and insert a linked chart to the main chart based on the results as shown in the screen shotCOMA%20Capture

Following is the code i tried; I believe my logic is true, but i am not sure; Also i am unable to plot the shape of vertical line.

tGrn1 = EMA(Close,8) > EMA(Close,21);
Grn2  = EMA(Close,21) > EMA(Close,50);
Grn = Grn1 AND Grn2;

Rdd1 = EMA(Close,8) < EMA(Close,21);
Rdd2  = EMA(Close,21) < EMA(Close,50);
Rdd = Rdd1 AND Rdd2;

days = 20;

for (k = 0; k < BarCount; k++)
		sumG[k] = 0;
		sumR[k] = 0;

for (j = 21; j < BarCount; j++)
		sumG[j] = 0;
		sumR[j] = 0;
		for(i = j-days; i < j; i++)
				sumG[j] = sumG[j] + Grn[i];
				sumR[j] = sumR[j] + Rdd[i];
PlotShapes(IIf(sumG==20,shapeUpArrow,shapeNone), IIf(sumG==20,colorWhite,colorBlack));
PlotShapes(IIf(sumR==20,shapeDownArrow,shapeNone), IIf(sumR==20,colorWhite,colorBlack));ype or paste code here

I am new to Amibroker - can you pls help?

Thank You,

@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 );


1 Like

Thank you.

Sorry i wasn't clear on explaining what i wanted the logic to do - let me try again:

I want the green line to be plotted when UpTrend (correct order of moving averages) exists for all the last 20 bars and want the red line to be plotted when DownTrend exists for all the last 20 bars

So, i guess the following code could work:

if  (Sum(UpTrend,20) == 20)
    plot greenline
else if (Sum(DownTrend,20) ==20)
   plot redline

Small changes to achieve that,

Up20 = Sum( UpTrend, 20 ) == 20;
Down20 = Sum( DownTrend, 20 ) == 20;

DynamicColor = IIf( Up20, colorGreen, IIf( Down20, colorRed, colorBlack ) );

Plot( Up20 or Down20, "Trend", DynamicColor, styleHistogram | styleThick );

And some people prefer the "Ribbon" chart for a trend indicator. Sometimes a code like this,

Plot( 1, "", DynamicColor, styleArea |  styleOwnScale | styleNoLabel, 0, 25 );
PlotOHLC( O, H, L, C, "", colorDefault, styleCandle );

And of course you adjust the height of the Ribbon, or the colors (instead of Black try something else)



Thank you very much - you saved me lot of time!!

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:

30bar = barindex() - 30


First get the desired value, say,

B30C = Ref(C, -30);
// in current TF OR 
B30C = TimeFrameGetPrice("C",inDaily,-30);

then plot that line

Plot(B30C,"",colorYellow,styleDashed | styleNoRescale);

I didnt want the price - i want a vertical line as indicator for the 30th prior bar in the above plot

@uvdsatish search the forum as there are several examples that should help you get started towards a solution.

1 Like

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() ??


Try this

B30 = Cum( 1 ) == LastValue( Cum( 1 ) ) - 30;
Plot( B30, "", colorLightBlue,styleHistogram | styleThick | styleOwnScale);

Above code will draw a single thick vertical line 30 bars ago.

Unnecessarily complicating by mixing different things.
You want a ribbon or full vertical bar?
You also mention bars, then say one thick bar 30 bars prior to current bar.

If you're not sure, do post your code atleast with what you tried. Only your first post has Plotshapes in it.


quick? what does B30 = Cum( 1 ) == LastValue( Cum( 1 ) ) - 30; this actually do? Cum and LastValue - read thru those,but couldnt thoroughly understand

Also,One plot is overlaying the other plot in linked chart - is there any way i can have all the three plots (bars displayed) even when there is overlap?

UpTrend =  EMA(Close,14) > EMA(Close,21)  AND EMA(Close,21) > EMA(Close,50);
DownTrend = EMA(Close,14) < EMA(Close,21) AND EMA(Close,21) < EMA(Close,50);

Up30 = Sum( UpTrend, 30 ) == 30;
Down30 = Sum( DownTrend, 30 ) == 30;

DynamicColor = IIf( Up30, colorGreen, IIf( Down30, colorRed, colorWhite ) );

Plot( Up30 or Down30, "COMA30", DynamicColor, styleHistogram | styleThick );

//Plot( 1, "", DynamicColor, styleArea |  styleOwnScale | styleNoLabel, 0, 25 );

B30 = Cum(1) == LastValue(Cum(1)) - 30;
Plot( B30, "", colorGold,styleHistogram | styleThick | styleOwnScale);

B20 = Cum(1) == LastValue(Cum(1)) - 20;
Plot( B20, "", colorPaleTurquoise,styleHistogram | styleThick | styleOwnScale);

@uvdsatish the use of explorations is a great way to examine and understand the array values used in your formula and learn more about the used functions.

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.


Another nice way of "looking" inside the array is to append variables directly to

_N(Title = StrFormat("{{DATE}}ā€¦ā€¦.{{VALUES}}  %g %.2f %s  ", ā€¦., var1, var2, var3));

This way, all variables will display that Value at selected bar. Very easy to troubleshoot visually :smiley:
Can easily scroll or navigate bar-by-bar.

@uvdsatish as you have had some good help from @beppe and @travick you may have already solved your problem. There are probably a dozen different ways to change your display.

But here are a couple of simple methods that may help.

First you could try to change the z-order,

Plot( B20, "", colorPaleTurquoise,styleHistogram | styleThick | styleOwnScale, Null, Null, Null, 1 );


A second possibility is to assign a different number (you currently are scoring from zero to one), so for example without changing the z-order

Plot( IIf(B30, 1.5, 0), "", colorGold,styleHistogram | styleThick );


Or change both,

Plot( IIf(B30, 1.5, 0), "", colorGold, styleHistogram | styleThick, Null, Null, Null, 1 );