Cannot plot in Chart

I have created an AFL script to display daily pivot points. But when I select in the chart pane and do overlay, there is nothing plotted. Infact, I cannot even see the parameter inputs.
What is wrong with this?

_SECTION_BEGIN("Pivot Points");

ppColor = ParamColor("Pivot", colorDarkBlue);
rpColor = ParamColor("Resistance", colorRed);
spColor = ParamColor("Support", colorGreen);
ppLabelColor = ParamColor("Pivot Label", colorBlue);
rpLabelColor = ParamColor("Resistance Label", colorOrange);
spLabelColor = ParamColor("Support Label", colorAqua);

TimeFrameSet(inDaily);
pp = (H + L + C) / 3;
VarSet("R1", (2 * pp) - L);
VarSet("S1", (2 * pp) - H);
VarSet("R2", pp + (VarGet("R1") - VarGet("S1")));
VarSet("S2", pp - (VarGet("R1") -  VarGet("S1")));
VarSet("R3", VarGet("R1") + (H - L));
VarSet("S3", VarGet("S1") - (H - L));
TimeFrameRestore();

pp = TimeFrameExpand(pp, inDaily);
for (k = 1; k <= 3; k++) {
	VarSet("R" + k, Ref(TimeFrameExpand(VarGet("R" + k), inDaily), -1));
	VarSet("S" + k, Ref(TimeFrameExpand(VarGet("S" + k), inDaily), -1));
}

bi = BarIndex();
fvbi = FirstVisibleValue(bi);
lvbi = LastVisibleValue(bi);

dn = DateNum();
j = 0;
edges[j] = 0;
for (i = fvbi; i < lvbi; i++) {
	if (dn[i] != dn[i + 1]) 
		edges[j++] = i;
}
edges[j] = Null;

prev = fvbi;
for (j = 0; edges[j] != Null; j++) {
	xi = prev;
	yi = edges[j];
	pp_chart = LineArray(xi, pp[xi], yi, pp[yi]);
	Plot(pp_chart, "Pivot", ppColor);
	PlotText("PP", xi, yi, ppLabelColor, colorDefault, 1);
	for (k = 1; k <= 3; k++) {
		rdata = VarGet("R" + k);
		sdata = VarGet("S" + k);
		rdata_chart = LineArray(xi, rdata[xi], yi, rdata[yi]);
		sdata_chart = LineArray(xi, sdata[xi], yi, sdata[yi]);
		Plot(rdata_chart, "R" + k, rpColor);
		PlotText("R" + k, xi, yi, rpLabelColor, colorDefault, 1);
		Plot(sdata_chart, "S" + k, spColor);
		PlotText("S" + k, xi, yi, spLabelColor, colorDefault, 1);
	}
	prev = j + 1;
}


_SECTION_END();

@xterminator,

Not sure what exactly you want to achieve with the for-loop!

Here is one way to deal with daily pivots:

/*_______________________________________________________________________________________________________________________
|This code is for AmiBroker Formula Language (AFL) learning (non-commercial) purposes only. Please do not copy this code|
|(or any other version of it) and paste it over on other forums or anywhere else on the Internet or in any other form	|
|without the AmiBroker Forum owner's consent (https://forum.amibroker.com/).                                            |
_______________________________________________________________________________________________________________________*/

procedure CalcFloorPvts( _H, _L, _C ) {	 
	 VarSet( "pp", ( _H + _L + _C ) / 3 );
	 VarSet( "R1", 2 * VarGet( "pp" ) - _L );
	 VarSet( "S1", 2 * VarGet( "pp" ) - _H );
	 VarSet( "R2", VarGet( "pp" ) + ( VarGet( "R1" ) - VarGet( "S1" ) ) );
	 VarSet( "R3", VarGet( "R1" ) + ( _H - _L ) );
	 VarSet( "S2", VarGet( "pp" ) - ( VarGet( "R1" ) - VarGet( "S1" ) ) );
	 VarSet( "S3", VarGet( "S1" ) - ( _H - _L ) );
}

_SECTION_BEGIN( "Floor Pivots" );
	 SetChartBkColor( colorBlack );
	 SetChartOptions( 1, chartShowDates );
	 Plot( C, "Price", colorDefault, styleCandle );

	 ppColor = ParamColor( "Pivot Line Color", colorWhite );
	 rColor = ParamColor( "Resistance Line Color", colorRed );
	 sColor = ParamColor( "Support Line Color", colorBrightGreen );

	 ShLabels = ParamToggle( "Show pivot labels?", "No|Yes", 1 );

	 ShDevPvts = ParamToggle( "Show developing pivots?", "No|Yes", 0 );
	 ShDevPvtLabels = ParamToggle( "Show developing pivot labels?", "No|Yes", 0 );

	 bi = BarIndex();
	 DaysFrstBi = TimeFrameExpand( TimeFrameCompress( bi, inDaily, compressOpen ), inDaily, expandFirst );
	 DaysFrstBiCond = bi == DaysFrstBi;

	 PrevDayH = TimeFrameGetPrice( "H", inDaily, -1, expandFirst );
	 PrevDayL = TimeFrameGetPrice( "L", inDaily, -1, expandFirst );
	 PrevDayC = TimeFrameGetPrice( "C", inDaily, -1, expandFirst );
	 CalcFloorPvts( PrevDayH, PrevDayL, PrevDayC );

	 Plot( IIf( DaysFrstBiCond, Null, VarGet( "pp" ) ), "", ppColor, styleNoRescale | styleNoLabel, Null, Null, 0, -2 );
	 Plot( IIf( DaysFrstBiCond, Null, VarGet( "pp" ) ), "", ppColor, styleNoRescale | styleNoLabel, Null, Null, -1, -2 );
	 if( ShLabels ) PlotTextSetFont( StrFormat( "PP %1.2f", SelectedValue( pp ) ), "Courier New", 8, SelectedValue( DaysFrstBi ), SelectedValue( pp ), ppColor, colorDefault, 0 );

	 nP = 3; //Number of Res. and Supp. lines
	 for( i = 1; i <= nP; i++ ) {
		 Plot( IIf( DaysFrstBiCond, Null, VarGet( "R" + i ) ), "", rColor, styleNoRescale | styleNoLabel, Null, Null, 0, -2 );
		 Plot( IIf( DaysFrstBiCond, Null, VarGet( "R" + i ) ), "", rColor, styleNoRescale | styleNoLabel, Null, Null, -1, -2 );
 
		 Plot( IIf( DaysFrstBiCond, Null, VarGet( "S" + i ) ), "", sColor, styleNoRescale | styleNoLabel, Null, Null, 0, -2 );
		 Plot( IIf( DaysFrstBiCond, Null, VarGet( "S" + i ) ), "", sColor, styleNoRescale | styleNoLabel, Null, Null, -1, -2 );

		 if( ShLabels ) {
			 PlotTextSetFont( StrFormat( "R%0.0f %1.2f", i, SelectedValue( VarGet( "R" + i ) ) ), "Courier New", 8, SelectedValue( DaysFrstBi ), SelectedValue( VarGet( "R" + i ) ), rColor, colorDefault, 0 ); //https://forum.amibroker.com/t/cannot-plot-in-chart/15322?u=cougar
			 PlotTextSetFont( StrFormat( "S%0.0f %1.2f", i, SelectedValue( VarGet( "S" + i ) ) ), "Courier New", 8, SelectedValue( DaysFrstBi ), SelectedValue( VarGet( "S" + i ) ), sColor, colorDefault, 0 );
		 }
	 }

	 if( ShDevPvts ) {
		 shift = 20; // Number of Bars to be shifted, could use a Param if needed (read http://www.amibroker.com/kb/2015/01/14/drawing-line-extensions-on-future-bars-using-afl/)
		 lvbi = LastValue( bi );
		 LAx0 = lvbi - shift + 1;
		 LAx1 = lvbi;
	 
		 DaysH = TimeFrameGetPrice( "H", inDaily, 0, expandFirst );
		 DaysL = TimeFrameGetPrice( "L", inDaily, 0, expandFirst );
		 DaysC = TimeFrameGetPrice( "C", inDaily, 0, expandFirst );
		 CalcFloorPvts( DaysH, DaysL, DaysC );

		 Plot( LineArray( LAx0, LastValue( VarGet( "pp" ) ), LAx1, LastValue( VarGet( "pp" ) ) ), "", ppColor, styleNoRescale | styleNoLabel, Null, Null, shift );
		 if( ShDevPvtLabels ) PlotTextSetFont( StrFormat( "PP %1.2f", LastValue( pp ) ), "Courier New", 8, lvbi + shift / 2, LastValue( pp ), ppColor, colorDefault, 0 );
		 for( i = 1; i <= nP; i++ ) {
			 Plot( LineArray( LAx0, LastValue( VarGet( "R" + i ) ), LAx1, LastValue( VarGet( "R" + i ) ) ), "", rColor, styleNoRescale | styleNoLabel, Null, Null, shift );
			 Plot( LineArray( LAx0, LastValue( VarGet( "S" + i ) ), LAx1, LastValue( VarGet( "S" + i ) ) ), "", sColor, styleNoRescale | styleNoLabel, Null, Null, shift );

			 if( ShDevPvtLabels ) {
				 PlotTextSetFont( StrFormat( "R%0.0f %1.2f", i, LastValue( VarGet( "R" + i ) ) ), "Courier New", 8, lvbi + shift / 2, LastValue( VarGet( "R" + i ) ), rColor, colorDefault, 0 );
				 PlotTextSetFont( StrFormat( "S%0.0f %1.2f", i, LastValue( VarGet( "S" + i ) ) ), "Courier New", 8, lvbi + shift / 2, LastValue( VarGet( "S" + i ) ), sColor, colorDefault, 0 );
			 }
		 }
	 }
_SECTION_END();

1

3 Likes

Thanks for a solution @Cougar
I was using for-loop to identify day endpoints and call plot only for that area. And iterate through all the different days while plotting them.
Because previously, I have used Plot and it connects the line. The support and resistance lines for different days gets connected. I have referred it here:

Secondly, can you tell why are we using two calls to Plot with different zindex params?

Plot( IIf( DaysFrstBiCond, Null, VarGet( "R" + i ) ), "", rColor, styleNoRescale | styleNoLabel, Null, Null, 0, -2 );
Plot( IIf( DaysFrstBiCond, Null, VarGet( "R" + i ) ), "", rColor, styleNoRescale | styleNoLabel, Null, Null, -1, -2 );

Appreciate your help and you brought this up in a second topic. I have read what you told me.
But isn't it true that if the array given to plot is like this, which represents pivot points for different days:

1,1,1,1,5,5,5,5,10,10,10,10....

then Plot will connect the line from 1 to 5 which is exactly why I was using for-loops to batch plot for different chunks: 1,1,1,1 and 5,5,5,5 and so on. So it doesn't "connect" the lines.

I have read the understand AFL thing but these things come from some experience. So for AFL novice like me, I would appreciate if the forum members were little "less" harsh.

Regards.

No one is "harsh" on you. Sadly, you perceive so! On the flip side, in reality other(s) want you to be self-dependent.

No harm being novice, I am a beginner myself. We all ask questions here - no one is perfect!

But asking redundant questions after a solution is presented, is a clear sign of laziness. Waiting for experience to accumulate, is same as leaving for chance - realization is the key!

The problem is with not investigating, because if you did, you would have got your answers without asking.

Download and read the entire user manual slowly for 3-4 weeks, you'll see the difference!

1 Like