Session as GFX candle

I needed an AFL showing session from 10:00 to 14:00 OHLC as a candle, which I tried to obtain from 2TF candle formula using low level graphic function, but I only able to get OHLC of first candle of the session instead of the whole session. Please help the following code.

_SECTION_BEGIN("Session Timeframe candle");
H_Bar		= ParamToggle("Session Bars", "Off, On", 1);
HbarFill	= ParamToggle("Session Fill", "Off, On", 0);
H_Upbarcolor=ParamColor("H_Upbarcolor",colorDarkOliveGreen);
H_Dnbarcolor=ParamColor("H_Dnbarcolor",colorDarkRed);
H_Uplinecolor=ParamColor("H_Uplinecolor",colorTurquoise);
H_Dnlinecolor=ParamColor("H_Dnlinecolor",colorOrange);
H_UpWickcolor=ParamColor("H_UpWickclr",colorTurquoise);
H_DnWickcolor=ParamColor("H_DnWickclr",colorOrange);
H_BarLum 	= Param("H_Bar Clr Intensity", 0.1, 0, 1, 0.01);
H_WickLum 	= Param("H_Wick Clr Intensity", 0.1, 0, 1, 0.01);
H_Line 		= Param("H_Bar Line Thickness", 2, 0, 10, 1);
H_Wick 		= Param("H_Wick Thickness", 4, 0, 20, 1);

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// FUNCTIONS:
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

function GetVisibleBarCount() 
{ 
 lvb = Status("lastvisiblebar"); 
 fvb = Status("firstvisiblebar"); 
 return Min( Lvb - fvb, BarCount - fvb ); 
} 

function GfxConvertBarToPixelX( bar ) 
{ 
 lvb = Status("lastvisiblebar"); 
 fvb = Status("firstvisiblebar"); 
 pxchartleft = Status("pxchartleft"); 
 pxchartwidth = Status("pxchartwidth"); 
 return pxchartleft + bar  * pxchartwidth / ( Lvb - fvb + 1 ); 
} 

function GfxConvertValueToPixelY( Value ) 
{ 
 local Miny, Maxy, pxchartbottom, pxchartheight; 
 Miny = Status("axisminy"); 
 Maxy = Status("axismaxy"); 
 pxchartbottom = Status("pxchartbottom"); 
 pxchartheight = Status("pxchartheight");
 return pxchartbottom - floor( 0.5 + ( Value - Miny ) * pxchartheight/(Maxy-Miny+1e-9) ); 
} 

// Function to identify session bars (10:00 to 14:00)
function IsSessionTime()
{
    hours = Hour();
    minutes = Minute();
    time = hours + minutes/100;
    
    // Session from 10:00 to 14:00
    return time >= 10.00 AND time < 14.00;
}

// Function to get session boundaries
function GetSessionBars()
{
    session = IsSessionTime();
    sessionStart = session AND !Ref(session, -1);
    sessionEnd = !session AND Ref(session, -1);
    
    return sessionStart OR sessionEnd;
}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// MAIN PROGRAM:
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function PlotSessionBars(BarLum, Style)
{
if(H_Bar==1)
{
// Bar Colors For The Session candlestick bars:
TFUpBarColor	= ColorBlend(H_Upbarcolor, colorWhite, BarLum);
TFDnBarColor	= ColorBlend(H_Dnbarcolor, colorWhite, BarLum);
TFUpWicColor    = ColorBlend(H_UpWickcolor, colorWhite, BarLum);
TFDnWicColor    = ColorBlend(H_DnWickcolor, colorWhite, BarLum);

// Calculate session OHLC
sessionStart = IsSessionTime() AND !Ref(IsSessionTime(), -1);
sessionMarker = Cum(sessionStart);

TFOpen = ValueWhen(sessionStart, O);
TFHigh = HighestSince(sessionStart, H);
TFLow = LowestSince(sessionStart, L);
TFClose = IIf(IsSessionTime(), C, ValueWhen(IsSessionTime() AND !Ref(IsSessionTime(), 1), C));

// Get session bar index for positioning
TFBarIndex = ValueWhen(sessionStart, BarIndex());
TFLastBarIndex = LastValue(BarIndex());

CandleTop = Max(TFOpen, TFClose); 
CandleBottom = Min(TFOpen, TFClose); 

//============================================================================
// GFX LOW-LEVEL GRAPHICS SECTION.
// DRAWING THE SESSION CANDLESTICK BARS:
//============================================================================
GfxSetOverlayMode(1); 

AllVisibleBars = GetVisibleBarCount(); 
fvb = Status("firstvisiblebar"); 
ChartWidth = GfxConvertBarToPixelX(AllVisibleBars);
PixBar = ChartWidth / AllVisibleBars;
Adjust = PixBar * 0.35;

// Identify session bars for drawing
NewTFBar = sessionStart;
SessionActive = IsSessionTime();

// DRAW BAR HISTORY AND THE CURRENT BAR:
for(i = 0; i < AllVisibleBars; i++) 
{
    currentBar = i + fvb;
    
    if(NewTFBar[currentBar] == 1)
    {
        // Find session end
        sessionEndIndex = currentBar;
        for(n = currentBar + 1; n < BarCount AND SessionActive[n]; n++)
        {
            sessionEndIndex = n;
        }
        
        x1 = GfxConvertBarToPixelX(i) - Adjust;
        x2 = GfxConvertBarToPixelX(i + (sessionEndIndex - currentBar)) + Adjust;
        
        y1 = GfxConvertValueToPixelY(CandleTop[currentBar]); 
        y2 = GfxConvertValueToPixelY(CandleBottom[currentBar]); 
        yH = GfxConvertValueToPixelY(TFHigh[currentBar]);
        yL = GfxConvertValueToPixelY(TFLow[currentBar]);
        
        // Candle Body:
        LineColor = IIf(TFOpen[currentBar] < TFClose[currentBar], H_Uplinecolor, H_Dnlinecolor);
        FillColor = IIf(TFOpen[currentBar] < TFClose[currentBar], TFUpBarColor, TFDnBarColor);
        Wickcolor = IIf(TFOpen[currentBar] < TFClose[currentBar], TFUpWicColor, TFDnWicColor);        
        
        GfxSetZOrder(-3);
        GfxSelectPen(LineColor, H_Line);
        
        if(HbarFill == 1)
        {
            GfxSelectSolidBrush(FillColor);
        }
        else
        {
            GfxSelectStockObject(5);
        }
        
        if(y1 == y2)
        {
            y1 = y1 - Adjust; 
            y2 = y2 + Adjust;
            GfxSelectSolidBrush(FillColor);
        }
        
        if(x1 > 0)
        {
            GfxRectangle(x1, y1, x2, y2); 

            // Candle High and Low:
            GfxSetZOrder(-3);
            GfxSelectPen(ColorBlend(WickColor, colorWhite, H_WickLum), H_Wick);
            GfxMoveTo(x2 + (x1 - x2)/2, y1);
            GfxLineTo(x2 + (x1 - x2)/2, yH);
            GfxMoveTo(x2 + (x1 - x2)/2, y2);
            GfxLineTo(x2 + (x1 - x2)/2, yL);
            RequestTimedRefresh(0); 
        }
    }
}
}
}

PlotSessionBars(H_BarLum, "Fill");	

_SECTION_END();

As the above image, it is only showing OHCL of opening candle of 10:00 and not whole session from 10:00 to 14:00.

Your code is extremely lengthy to study and make sense.

Instead, why dont you just do a light pass over the visible bars and put value to matrix,
then iterate over the matrix and draw the boxes.

// OpenBI, Obox,  HighBI, HWick, LowBi, LWick, CloseBI, Cbox
mx = Matrix( 5, 8, 0 );	// write your own code for max number of rows(boxes)

row = startBar = lastBar = 0;

curBoxOpenBI = 0;

fvbi = Status("firstvisiblebarindex");
lvbi = Status("lastvisiblebarindex");
tn = TimeNum();

for( i = fvbi; i < lvbi; ++i) {

	if( (startBar==0 AND i==fvbi AND tn[i]>100000) OR
		(startBar==0 AND tn[i]==100000 ) ) {
		
		startBar = 1;
		curBoxOpenBI = i;
		
		mx[row][0] = i;	mx[row][1] = O[i];
		mx[row][2] = i;	mx[row][3] = H[i];
		mx[row][4] = i;	mx[row][5] = L[i];
		mx[row][6] = i;	mx[row][7] = C[i];
	}
	
	if( startBar AND tn[i]<=140000 AND i > curBoxOpenBI) {
		
		if( H[i] > mx[row][3] ) {
			mx[row][2] = i;	mx[row][3] = H[i];
		}
		if( L[i] < mx[row][5] ) {
			mx[row][4] = i;	mx[row][5] = L[i];
		}
		mx[row][6] = i;	mx[row][7] = C[i];	
	}
	
	if( tn[i]==140000 OR (i+1 == lvbi ) ) {
		startBar = 0;	// end current box
		row += 1;		// new box
	}
}

once your matrix is ready, write your looping code to draw boxes.

this is a manual example


GfxSetCoordsMode( 1 );	// (bar,price)

GfxSelectPen( colorAqua, 1 );
GfxSelectStockObject( 5 );

printf("%s", MxToString(mx) );

GfxRectangle( mx[1][0], mx[1][3], mx[1][6], mx[1][5] ); // OHLC

This considers open and close. the above uses H and L of range.

GfxRectangle( mx[1][0], mx[1][1], mx[1][6], mx[1][7] ); // open-close

And if you really want to draw a proper candle,
the matrix has ALL OHLC value, and you can use open-close box and draw your own WICKS

mxtostring() output

{
 { 21558, 128.25, 21558, 128.29, 21588, 126.56, 21606, 126.9 },
 { 21636, 126.2, 21637, 126.45, 21665, 124.6, 21684, 125.43 },
 { 21714, 126.24, 21757, 127.98, 21714, 126.24, 21762, 127.86 },
 { 21792, 129, 21792, 129.15, 21827, 127.51, 21840, 127.94 },
 { 21870, 126.02, 21884, 127.54, 21913, 124.6, 21918, 125.07 }
}

2 Likes

ok, i will try it