Using PlotGrid() function in a simple & short AFL: No line is plotted

I am able to draw shapes wherever I want. But I want horizontal lines to be drawn on the last value when condition was satisfied.
Here is the code:

SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

pk1=Ref(H,-2)>Ref(H,-3) AND Ref(H,-2)>Ref(H,-1) AND Ref(L,-2)<Ref(L,-3) AND Ref(L,-2)<Ref(L,-1);
o1=Ref(o,-2);

c1=Ref(c,-2);
h1=(o1+c1)/2;
hh=HHV(H,3);
ll=LLV(L,3);
longt=hh+(hh-ll)*1;
shortt=ll-(hh-ll)*1;

lon=IIf(pk1,longt,Null);
longtt=LastValue(lon,true);


PlotShapes(IIf(pk1, shapeCircle, shapeNone),coloryellow, 0,longt,yoffset=0,XShift=-2);
PlotShapes(IIf(pk1, shapeCircle, shapeNone),coloryellow, 0,shortt,yoffset=0,XShift=-2);
PlotGrid(longtt, colorRed, 10, 2, False ); // solid line 2 pixels thick, no label 

Screenshot is attached.
image

@snbarma, please, make an effort to learn how to debug your formulas.

This thread provides links to learn how to use explorations, _TRACE() and the debugger to inspect the variables and arrays values used by your formulae.

PlotGrid() works as expected when you invoke it with a level value in the range of the main Plot() highest and lowest values.
If you pass it a zero value (hint) in a price chart then "No line is plotted" (as expected!)

2 Likes

Thanks for your response.

I have one query on the syntax part, can you please look into:

hpk = ValueWhen(pk1,shortt,1 );
PlotShapes(IIf(pk1, shapeCircle, shapeNone),coloryellow, 0,shortt,yoffset=0,XShift=-2);
PlotGrid(hpk, colorRed, 10, 2, False ); // solid line 2 pixels thick, no label 

In the above case, hpk sometime will have value, sometime it will hold NULL. If pk1==true, hpk will have value.
In plotshape(), we have a syntax to handle this.
Can we handle such case with PLOTGRID() function..

@snbarma in the PlotGrid documentation comments section it is clearly stated that:

Instead of a number you can also use expression but it must
be a NUMERIC expression, not an ARRAY.
Use LastValue() to convert:

your_expression = ...
PlotGrid( LastValue( your_expression ) );

To verify it, apply this code snippet to a blank chart:

hpk_null = Null;
Plot(C, "Close", colorYellow);
PlotGrid( LastValue(hpk_null), colorRed, 1, 1, True);
PlotGrid( LastValue(Close), colorGreen, 1, 1, True );

It should run with no error.
Obviously, in this example, there will be no red line to see.
So you can safely use the LastValue() function to convert any of your arrays to a numeric value to be used as the 1st argument (level).

2 Likes

Thank you for the help.
I used

hpk = LastValue(ValueWhen(pk1,shortt,1));
PlotGrid(hpk,colorgreen);

It is drawing lines as desired.

I want the AFL program to write the price on the horizontal line same as when we draw horizontal line manually.
Would you please suggest.

@snbarma if you use True as the last parameter when you invoke PlotGrid (as in my previous example) you'll see the "level" (price) as a label in the right axis scale.

immagine

1 Like

It seems not putting the text
Code

SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
PlotGrid(170,colorRed,1,1,True);

image.

Can you please have a look.

@snbarma, actually my previous image was misleading. I apologize.

immagine

The "label" when using PlotGrid() is displayed only as an extra number (note the 167 value position without decimals) in the right axis scale, using the same color of the other numbers of the scale.

(In this example, I invoked it with a 167.27 value).

When other (plot) labels are near/over it may not be visible.

So, if you prefer to see a full label (with the same color of the line) you can revert to using Plot instead of PlotGrid:

Plot( 160, "", colorRed, styleLine | styleThick ); 

In this case too, pass a NUMERIC as the first parameter to get a horizontal line.

As an additional alternative, take a look also to the PlotText() function.

By the way, a recent thread showed how to use it.
A search in the forum will provide additional examples.

It worked very well. Thank you for your help.