# Rectangle Price Values and the Height and No of Bars

I saw some AFL posts that enhanced the trendlines giving them the ability to measure the price difference ,no of bars etc.I want to accomplish the same for rectangles too.

When a rectangle is drawn and given a study id,i would like the price values of the top and bottom to be printed right on top of the rectangle(not the y-axis) and also the price difference and percent difference to be plotted just alongside the vertical line of the rectangle and the number of bars to be plotted just alongside the horizontal line of the rectangle.

@krisnara ok. When done, please, show us the result of your effort.

Citing an old post:

The forum is not here as a code writing service as such. Instead, Iâ€™d encourage you to have a go yourself first, and if you get stuck, then post how far youâ€™ve got with your code, and request help at that point.

Since you already located some code for similar tasks, maybe you should try to modify it to achieve your own goal.

Thanks for the motivation. I have modified the code by Panos to use the same logic for Rectangles instead of Trendlines .All is fine except the Rectangle bottom and top point values are coming out wrong .Also I would be thankful if you can guide me to print the Rectangle levels right on top of the rectangle instead of on the Y-Axis .In this code, I set the Study ID as p3 and choose "yes" in the parameters to display the values.

The modified code is given below

Blockquote
_SECTION_BEGIN("xFibo Ver 2");
// Fibonacci Retracements using Gfx* functions by Panos 05-07-2018
// EXAMPLE xFiboRetracements("P6",colorBlue ) ;

Plot( C, "Price", colorDefault, styleCandle );
GfxSetOverlayMode( 1 );
GfxSetCoordsMode( 1 ); // bar/price mode (instead of pixel)

function xFiboRetracements( StudyID, Color )
{
rectangle = Study(StudyID, GetChartID() );
bi =BarIndex();

``````X0= StartX = LastValue( ValueWhen( ExRem( rectangle, 0 ), BarIndex() ) );
X1= EndX = LastValue( ValueWhen( rectangle, BarIndex()) );			// printf("X1 StartX  = %g,\n X1 EndX = %g\n", StartX, EndX );
Y0= StartY = LastValue( ValueWhen( ExRem( rectangle, 0 ), rectangle ) );
Y1= EndY = LastValue( ValueWhen( rectangle, rectangle ) );		// printf("\nY1 StartY  = %g,\nY2  EndY = %g\n", StartY, EndY );

LineY = ( Y1 - Y0 ) ;
//y0 = Y1;
y382 = Y1 - ( LineY * 0.382 );
y50  = Y1 - ( LineY * 0.50 );
y618 = Y1 - ( LineY * 0.618 );	// 61.8%
y786 = Y1 - ( LineY * 0.786 ); 	// 78.6%
Y100 = Y0;
y112 = Y0 - ( LineY * 0.128 );	// 1.128
y127 = Y0 - ( LineY * 0.272 );	// 1.272
y161 = Y0 - ( LineY * 0.618 );	// 1.618%

GfxSelectPen( Color );
Bars=  bi[x1]-bi[x0];
if( ParamToggle( "Measuring X & Y ", "No|Yes" )) ext= Bars; else ext= 0;
GfxMoveTo( X0, y1 );	GfxLineTo( X1+ext, y1 );   // 0%
GfxMoveTo( X0, y100 );	GfxLineTo( X1, y100 );   // 100%
if( ParamToggle( "Show Fibo Retracements? ", "No|Yes" ) )
{
GfxMoveTo( X0, y382);	GfxLineTo( X1, y382 );   // 38.2%
GfxMoveTo( X0, y50 );	GfxLineTo( X1, y50 );   // 50%
GfxMoveTo( X0, y618 );	GfxLineTo( X1, y618 );   // 61.8%
GfxMoveTo( X0, y786 );	GfxLineTo( X1, y786 );   // 78.6%
GfxMoveTo( X0, y112 );	GfxLineTo( X1, y112 );  // 112.8%
GfxMoveTo( X0, y127 );	GfxLineTo( X1, y127 );  // 127%
GfxMoveTo( X0, y161 );	GfxLineTo( X1, y161 );  // 161.8%
}

if( ParamToggle( "Show Fibo number? ", "No|Yes" ) )
{
GfxSelectFont( "Tahoma", 8 );
GfxSetBkMode( colorDefault );
GfxSetTextColor( colorBrown );
GfxTextOut( "0%", X1, y1 );
GfxTextOut( "100%", X1, y100 );
GfxSetTextColor( ColorRGB( 100, 200, 100 ) );
GfxTextOut( "38.2%", X1, y382 );
GfxTextOut( "50%", X1, y50 );
GfxTextOut( "61.8%", X1, y618 );
GfxTextOut( "78.6%", X1, y786 );

GfxSetTextColor( colorBlueGrey );
GfxTextOut( "112.8%", X1, y112 );
GfxTextOut( "127%", X1, y127 );
GfxTextOut( "161.8%", X1, y161 );
}

GfxsetBkMode( colorDefault );
GfxSetTextColor( colorAqua );
if( ParamToggle( "Bars,Change,% ? ", "No|Yes" ) )
{  // rectangle Bars , Change ,Percent
InfoPosition= Y1 + ( LineY * 0.10 );
perc = NumToStr(abs(((y1-y0)/(y0+1e-9 ))*100),1.2);
InfoTxt = "Change: "+abs(LineY)+ ",("+ perc  +  " % ), Bars: "+ Bars+"";
GfxTextOut( InfoTxt, x0-5, InfoPosition );
}

if( ParamToggle( "Bars between X & Y ", "No|Yes" ))  GfxTextOut( "Bars: "+Bars  , X0+Bars/3, Y1 );

if( ParamToggle( "Show Gfx rectangle ", "No|Yes" ) )
{
GfxFillSolidRect(   BarCount +3, y112, BarCount +50, y127, color );
// GfxFillSolidRect(   BarCount -1, y112, Status("pxwidth")-80, y127, color );  // also with Pixels
// Text inside rectangle box
GfxSetTextColor( colorGold);
GfxSelectFont( "Tahoma", 10 );
GfxTextOut(  StrFormat("%g - %g",y112,y127 ), BarCount +4, y112+(y127-y112)/2);
}
``````

}
xFiboRetracements("P1",colorDarkOliveGreen ) ;
xFiboRetracements("P2",colorDarkGreen ) ;
xFiboRetracements("P3",colorDarkTeal ) ;
xFiboRetracements("P4",colorBlueGrey ) ;

_SECTION_END();

Hi

y1 and y100 is the top and bottom.

``````if( ParamToggle( "Show values(price) ", "No|Yes" ) )
{
GfxTextOut( "" + y1, X0, y1 );
GfxTextOut( "" + y100, X0, y100 );

}
``````
1 Like

Thanks a lot,Panos. This did the trick.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.