ORB draw horizontal line without loop and

Hi,

I have slightly modified the code from Amibroker KB to draw the horizontal line in realtime as price make new high. I am facing two problems

  1. Only the last ORB level are plotted
  2. Is it possible to avoid the for loop

I am out of ideas and not sure how to proceed from here, any help will be much appreciated.

Thank you

ORB

// Adopted from http://www.amibroker.com/kb/2015/02/02/high-low-of-certain-hours-of-the-day/

SetChartOptions( 0, chartShowArrows | chartShowDates );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );



_SECTION_BEGIN( "ORB" );
{
    //LookBackPeriod = 500;
    //SetBarsRequired( LookBackPeriod );


    ORBstart = ParamTime( "ORB Start", "03:00", 0 );
    ORBend =   ParamTime( "ORB End  ", "03:55", 0 );


    newDay = Day() !=  Ref( Day(), -1 ); //check for the start of the day
    tn = TimeNum();
    bi = BarIndex();
    bc = BarCount;

    Bars_so_far_today = 1 + BarsSince( newDay );
    StartBar = tn == ORBstart;
    EndBar = tn >= ORBstart AND tn <= ORBend;

    StartBar_bi = ValueWhen( StartBar, bi );
    EndBar_bi = ValueWhen( EndBar, bi );

    ORBcheck = tn == ORBend;

    ORBplot_condition = tn >= ORBstart AND tn <= ORBend;

    ORBendBarCount = ValueWhen( ORBcheck, bc );
    ORBlow = ValueWhen( EndBar, LowestSince( StartBar, Low ) );
    ORBhigh = ValueWhen( EndBar, HighestSince( StartBar, High ) );


    StartBar_bi_lv = LastValue( StartBar_bi );
    EndBar_bi_lv = LastValue( EndBar_bi );

    ORBhigh_plot_value = Null;

    for( x = StartBar_bi_lv; x <= EndBar_bi_lv; x++ )
    {
        if( EndBar[x] )
        {
            ORBhigh_plot_value[x] = LastValue( ORBhigh );
        }
    }

    plot( iif( ORBplot_condition, ORBhigh_plot_value, Null ), "ORB High", colorRed, styleLine, 0,  0, 0, 0, -10 );
}
RequestTimedRefresh( 1 );

Hi,
Have you investigated LineArray() function?

Thank you JohnHT. I will read up on LineArray() function and see if it can be used.

Instead of LineArray and Plot, it is more efficient and easier to use GfxMoveTo/GfxLineTo to draw line segments

GfxSetCoordsMode( 1 ); // use bar / price co-ordinates
GfxMoveTo( start_bar, start_price );
GfxLineTo( end_bar, end_price );
4 Likes

Thank you Tomasz.

I modified using GfxMoveTo/GfxLineTo and achieved the below but there are two problems which I am not able to figure out.

  1. Latest box is drawn correctly but previous boxes extend until the currect box, not sure why.
  2. Similarly the horizontal lines stop at 9:30 instead of extending until 14:00 only for the lastest lines. Previous lines extend beyond 14:00 and it extends until the next line starts.

Any pointers to what I am doing wrong?

SetChartOptions( 0, chartShowArrows | chartShowDates );

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

function drawHorizontalLine(i, lineValue, lineColor, lineShift, lineEnd, pxchr)
{
	GfxSetZOrder( -1 );
	GfxSelectFont( "Arial Narrow", 9, 700, False );
	
	// X = barindex Y = price
	GfxSetCoordsMode( 1 ); 
	GfxSelectPen( lineColor, 2);
	GfxSelectSolidBrush( lineColor );
	GfxMoveTo( i-lineShift, lineValue ); 
	GfxLineTo( lineEnd[i] , lineValue );
	// X = pixel Y = price
	GfxSetCoordsMode( 2 );
	GfxSetTextColor( lineColor );
	GfxTextOut("" + lineValue, pxchr, lineValue );
}

function drawRectangle(left_bi, left_price, right_bi, right_price, box_color, box_fill_color)
{
	// X barindex and Y price
	GfxSetCoordsMode( 1 );
	GfxSelectPen( box_color, 2);
	GfxSelectSolidBrush( box_fill_color );
	GfxRectangle(left_bi, left_price, right_bi, right_price);
	
}

_SECTION_BEGIN( "ORB" );

ORBstart = ParamTime( "ORB Start", "08:30", 0 );
ORBend =   ParamTime( "ORB End  ", "09:25", 0 );
ORBplotEnd = ParamTime( "ORB End  ", "14:00", 0 );


newDay = Day() !=  Ref( Day(), -1 ); //check for the start of the day
tn = TimeNum();
dn = DateNum();
bi = BarIndex();
bc = BarCount;

Bars_so_far_today = 1 + BarsSince( newDay );
StartBar = tn == ORBstart;

EndBar = tn == ORBend;
PlotEndBar = tn == ORBplotEnd;

StartBar_bi = ValueWhen( StartBar, bi );
EndBar_bi = ValueWhen( EndBar, bi );
ORBplotEnd_bi = ValueWhen( PlotEndBar, bi );

ORBplot_HL_condition = tn >= ORBstart AND tn <= ORBplotEnd;
ORBplot_BOX_condition = tn >= ORBstart AND tn <= ORBend;

ORBlow = ValueWhen( EndBar, LowestSince( StartBar, Low ) );
ORBhigh = ValueWhen( EndBar, HighestSince( StartBar, High ) );

ORB_Close_high = ValueWhen( EndBar, HighestSince( StartBar, Close ) );
ORB_Open_high = ValueWhen( EndBar, HighestSince( StartBar, Open ) );

ORB_Close_low = ValueWhen( EndBar, LowestSince( StartBar, Close ) );
ORB_Open_low = ValueWhen( EndBar, LowestSince( StartBar, Open ) );

ORB_BODY_high = Max(ORB_Close_high,ORB_Open_high);
ORB_BODY_low = Min(ORB_Close_low,ORB_Open_low);

pxchr = Status( "pxchartright" );

Start = 0;
End = BarCount - 1;

for( i = Start; i <= End ; i++ )
{
	if( ORBplot_HL_condition[i] )
	{
		// Draw ORB high
		drawHorizontalLine(i, ORBhigh[i], colorBlue, 11, ORBplotEnd_bi[i], pxchr);
		// Draw ORB Low
		drawHorizontalLine(i, ORBlow[i], colorBlue, 11, ORBplotEnd_bi[i], pxchr);
		
		//PlotText("BI " + EndBar_bi[i], bi[i], H[i] + 20 ,colorBlack, colorWhite);
	}
	
	if( ORBplot_BOX_condition[i] )
	{
		// Draw box body high to body low
		drawRectangle(StartBar_bi[i], ORB_BODY_low[i], EndBar_bi[i], ORB_BODY_high[i], colorBlack, colorYellow);
	}
}

//RequestTimedRefresh( 1 );

ORB_short_box

This is what I am trying to achieve

ORB_correct_box

I was able to use explorer to figure out what I was doing wrong and was able to fix it, below is the fixed code.

SetChartOptions( 0, chartShowArrows | chartShowDates );
SetBarsRequired(500);
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

function drawHorizontalLine(i, lineValue, lineColor, lineShift, lineStart, lineEnd, pxchr, loc)
{
	GfxSetZOrder( -1 );
	GfxSelectFont( "Arial Narrow", 9, 700, False );
	if(loc =="TOP")
	{
		GfxSetTextAlign( 6 | 24 | 0 );
	}
	else
	{
		GfxSetTextAlign( 6 | 0 | 0 );
	}
	
	//textLoc = IIf(loc == "TOP",lineValue + 2, lineValue);
	// X = barindex Y = price
	GfxSetCoordsMode( 1 ); 
	GfxSelectPen( lineColor, 2);
	GfxSelectSolidBrush( lineColor );
	GfxMoveTo( i-lineShift, lineValue ); 
	GfxLineTo( lineEnd[i] , lineValue );
	GfxTextOut("" + lineValue, lineStart, lineValue );
	// X = pixel Y = price
	GfxSetCoordsMode( 2 );
	GfxSetTextColor( lineColor );
	
	GfxTextOut("" + lineValue, pxchr, lineValue );
	
}

function drawRectangle(left_bi, left_price, right_bi, right_price, box_color, box_fill_color)
{
	GfxSetZOrder( -1 );
	// X barindex and Y price
	GfxSetCoordsMode( 1 );
	GfxSelectPen( box_color, 2);
	GfxSelectSolidBrush( box_fill_color );
	GfxRectangle(left_bi, left_price, right_bi, right_price);
	
}

_SECTION_BEGIN( "ORB" );

ORBstart   = ParamTime( "ORB Start",    "08:30", 0 );
ORBend     = ParamTime( "ORB End",      "09:25", 0 );
ORBplotEnd = ParamTime( "ORB Plot End", "18:55", 0 );
ORBplotEnd_bi_input = Param( "Number of bars to plot ORB lines ", 20, 12, 50,1,0 );
ORBlineColor = ParamColor("ORB Line Color",colorBlue);
boxBorder = ParamColor("Box Border Color",colorBlack);
boxFillColor = ParamColor("Box Fill Color",colorYellow);
PlotSessionDivider = ParamToggle("Plot Session Divider", "Yes|No", 0);



newDay = Day() !=  Ref( Day(), -1 ); //check for the start of the day
tn = TimeNum();
dn = DateNum();
bi = BarIndex();
bc = BarCount;


ORBStartBar = tn == ORBstart;

ORBEndBar = tn == ORBend;
ORBPlotEndBar = tn == ORBplotEnd;

ORBStartBar_bi = ValueWhen( ORBStartBar, bi );
ORBEndBar_bi = ValueWhen( ORBEndBar, bi );

numberOfORBBars = ORBEndBar_bi - ORBStartBar_bi;

ORBplotEnd_bi = ORBStartBar_bi + ORBplotEnd_bi_input;
//ORBplotEnd_bi = ValueWhen( ORBPlotEndBar, bi );


ORBplot_HL_condition = tn >= ORBend AND tn <= ORBplotEnd;
ORBplot_BOX_condition = tn >= ORBend AND tn <= ORBend;


ORBlow = ValueWhen( ORBEndBar, LowestSince( ORBStartBar, Low ) );
ORBhigh = ValueWhen( ORBEndBar, HighestSince( ORBStartBar, High ) );

ORB_Close_high = ValueWhen( ORBEndBar, HighestSince( ORBStartBar, Close ) );
ORB_Open_high = ValueWhen( ORBEndBar, HighestSince( ORBStartBar, Open ) );

ORB_Close_low = ValueWhen( ORBEndBar, LowestSince( ORBStartBar, Close ) );
ORB_Open_low = ValueWhen( ORBEndBar, LowestSince( ORBStartBar, Open ) );

ORB_BODY_high = Max(ORB_Close_high,ORB_Open_high);
ORB_BODY_low = Min(ORB_Close_low,ORB_Open_low);

pxchr = Status( "pxchartright" );



Start = 0;
End = BarCount - 1;

for( i = Start; i <= End ; i++ )
{
	if( ORBplot_HL_condition[i] )
	{
		// Draw ORB high
		drawHorizontalLine(i, ORBhigh[i], ORBlineColor, numberOfORBBars[i], ORBStartBar_bi[i], ORBplotEnd_bi[i], pxchr, "TOP");
		//drawHorizontalLine( i, ORBhigh[i], ORBlineColor, numberOfORBBars[i], ORBStartBar_bi[i], ORBplotEnd_bi[i], pxchr, "TOP" );
		// Draw ORB Low
		drawHorizontalLine( i, ORBlow[i], ORBlineColor, numberOfORBBars[i], ORBStartBar_bi[i], ORBplotEnd_bi[i], pxchr, "BOTTOM" );
	}
	
	if( ORBplot_BOX_condition[i] )
	{
		// Draw box body high to body low
		drawRectangle(ORBStartBar_bi[i], ORB_BODY_high[i], ORBEndBar_bi[i], ORB_BODY_low[i], boxBorder, boxFillColor);
	}
}

RequestTimedRefresh( 1 );

// Exploration output


Filter = 1;

AddColumn(ORBStartBar, "ORBStartBar");
AddColumn(ORBEndBar,"ORBEndBar");
AddColumn(ORBStartBar_bi,"ORBStartBar_bi");
AddColumn(ORBEndBar_bi,"ORBEndBar_bi");
AddColumn(ORBplotEnd_bi,"ORBplotEnd_bi");
AddColumn(ORBplot_HL_condition,"ORBplot_HL_condition");
AddColumn(ORBplot_BOX_condition,"ORBplot_BOX_condition");
AddColumn(ORBlow,"ORBlow");
AddColumn(ORBhigh,"ORBhigh");
AddColumn(ORB_BODY_high,"ORB_BODY_high");
AddColumn(ORB_BODY_low,"ORB_BODY_low");

ORB_fully_working

Thanks you
Deepak

1 Like

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