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:

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



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.

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


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).


Thank you for the help.
I used

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

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.


1 Like

It seems not putting the text

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


Can you please have a look.

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


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.