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 shot
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.
@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 );
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
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() ??
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?
@uvdsatishthe 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.
@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.