Daily pivots based on the forex times and not the week days

Hi,

I use the below code to get the daily pivots and weekdays on the chart:

_SECTION_BEGIN("Pivot Point 1");
/// Standard  Pivots //
/// @link https://forum.amibroker.com/t/hw-to-plot-pivots-for-the-fresh-day-start-to-end-instead-of-candle-based/21289
GraphXSpace = 5 ;
SetChartOptions(0,chartShowArrows|chartShowDates);

Plot(C,"Close",colorBlack, styleCandle);
ppl = ParamToggle("Plot Pivot Levels","Off|On",1);
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);

dn = DateNum();
newday = dn != Ref( dn, -1);

/* This code calculates the previous days high, low and close */
Hi = TimeFrameGetPrice("H", inDaily, -1);
Lo = TimeFrameGetPrice("L", inDaily, -1);
C1 = TimeFrameGetPrice("C", inDaily, -1);

/* aily pivots calculation*/
rg = (Hi - Lo);
bp = (Hi + Lo + C1)/3; bpI = LastValue (bp,1);
r1 = (bp*2)-Lo; r1I = LastValue (r1,1);
s1 = (bp*2)-Hi; s1I = LastValue (s1,1);
r2 = bp + r1 - s1; r2I = LastValue (r2,1);
s2 = bp - r1 + s1; s2I = LastValue (s2,1);
r3 = bp + r2 - s1; r3I = LastValue (r3,1);
s3 = bp - r2 + s1; s3I = LastValue (s3,1);
r4 = bp + r2 - s2; r4I = LastValue (r4,1);
s4 = bp - r2 + s2; s4I = LastValue (s4,1);

/// @link https://forum.amibroker.com/t/hw-to-plot-pivots-for-the-fresh-day-start-to-end-instead-of-candle-based/21289/2
function SetNull(array, mode) {
	global newday;
	if ( mode )
		result = IIf(! newday, array, Null);
	else
		result = IIf(newday OR Ref(newday,-1), array, Null);
	return result;
}

if(ppl==1) {
	y_offset = 2;
	Plot(SetNull(bp, 1),"",colorBlue,styleStaircase|styleDots|styleNoRescale);
	Plot(SetNull(bp, 0),"",colorBlue,styleStaircase|styleNoRescale);
	PlotText(" Pivot = " + WriteVal(bp,fraction), BarCount+2, bpI, colorBlue, -1, y_offset);
	for (i = 1; i <= 4; i++) {
		s = VarGet("s"+i);
		Plot(SetNull(s,1),"",colorRed,styleLine|styleNoRescale);
		Plot(SetNull(s,0),"",colorRed,styleStaircase|styleNoRescale);
		PlotText(" s"+i+" = " + WriteVal(s,fraction), BarCount+2, LastValue(s), colorRed, -1, y_offset);
		r = VarGet("r"+i);
		Plot(SetNull(r,1),"",colorGreen,styleLine|styleNoRescale);
		Plot(SetNull(r,0),"",colorGreen,styleStaircase|styleNoRescale);
		PlotText(" r"+i+" = " + WriteVal(r,fraction), BarCount+2, LastValue(r), colorGreen, -1, y_offset);
	}
}
_SECTION_END();

_SECTION_BEGIN( "Days of the week with Names" );
// by Milosz Mazurkiewicz
/// @link https://forum.amibroker.com/t/how-to-visually-identify-days-of-the-week-on-intraday-charts-background-color-change-for-each-separate-day/6881/11

Color1 = ParamColor( "Ribbon color", colorLightYellow );
Color2 = ParamColor( "Text color", colorBlack );

if (Interval() >= inDaily) 
Error("This code should be used on intraday charts. Change interval.");
dn = DateNum(); 
dw = DayOfWeek(); 
bi = BarIndex();
fvbi = Status( "firstvisiblebarindex" ); 
lvbi = Status( "lastvisiblebarindex" ); 
pxchb = Status( "pxchartbottom" );
FirstBarOfDay = dn != Ref( dn, -1 );
RibbonHeight = 5; // Percent of chart height
for( i = 1; i <= 6; i++ ) 
	VarSet( "C" + i, ColorBlend( colorBlack, Color1, i / 6 ) );
RibbonColor = IIf( DW == 1, C1, IIf( dw == 2, C2, IIf( dw == 3, C3, IIf( dw == 4, C4, IIf( dw == 5, C5, IIf( dw == 6, C6, colorDefault ) ) ) ) ));

GfxSetZOrder(1); GfxSetCoordsMode(3);
GfxSetTextColor(Color2); GfxSelectFont( "Arial", 9, 700 ); // Or for example "Arial Narrow"

DaysOfWeek = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday";
for( j = fvbi; j <= Min( lvbi, BarCount - 1 ) ; j++ ) if (FirstBarOfDay[j]) GfxTextOut(StrExtract(DaysOfWeek, dw[j]), bi[j] + 1, pxchb - RibbonHeight*5);

//Plot( Close, "Close", colorDefault, styleCandle );
Plot( FirstBarOfDay, "", Color1, styleHistogram | styleOwnScale | styleNoLabel, 1, 0, 0, -1, 2 );
Plot( RibbonHeight, "", RibbonColor, styleArea | styleOwnScale | styleNoLabel, 0, 100, 0, -1 );
_SECTION_END();

So, I get the following chart:
image

If you notice, you will see that the daily pivots for Monday are tight because Sunday is not a whole day and it is a short period of time. So, now I want to calculate the daily pivots based on the 5 pm to 5 pm and not 12 am to 12 am. Could you please help me?

Note that to get the local time on the chart, I changed the time shift in the database settings:
image

If I change the time shift to 0, I will get the right daily pivots but wrong weekdays.
image

Hello @Tomasz ,

Could you possibly help me with this question?

Best regards,

hi, I'm not Tomasz but I might have found a solution,
but I'm not very advanced in afl so better double check.
I left the vertical separation lines for the days unchanged,
only the pivot lines now are based on the sessions
which can be determined with the parameters
StartTime and EndTime.

_SECTION_BEGIN( "Pivots Timely HLC" );
/// @link https://forum.amibroker.com/t/hw-to-plot-pivots-for-the-fresh-day-start-to-end-instead-of-candle-based/21289
GraphXSpace = 5 ;
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot(C,"Close",colorBlack, styleCandle);
ppl = ParamToggle("Plot Pivot Levels","Off|On",1);
fraction= IIf(StrRight(Name(),3) == "", 3.2, 3.2);
dn = DateNum();
newday = dn != Ref( dn, -1);

StartTime  = ParamTime( "Start Time", "13:00:00" );
EndTime   = ParamTime( "END Time", "15:30:00" );
tn = TimeNum();
StartBar = tn == StartTime; 
EndBar = tn == Endtime; 
YesterdayHHinRange = ValueWhen( EndBar, HighestSince( StartBar, High ), 2); 
YesterdayLLinRange = ValueWhen( EndBar, LowestSince( StartBar, High ), 2); 
YesterdayCinRange = ValueWhen( EndBar, Close, 2 );

/* This code calculates the previous time sessions high, low and close */
Hi = YesterdayHHinRange;
Lo = YesterdayLLinRange;
C1 = YesterdayCinRange;
/* Daily pivots calculation*/
rg = (Hi - Lo);
bp = (Hi + Lo + C1)/3; bpI = LastValue (bp,1);
r1 = (bp*2)-Lo; r1I = LastValue (r1,1);
s1 = (bp*2)-Hi; s1I = LastValue (s1,1);
r2 = bp + r1 - s1; r2I = LastValue (r2,1);
s2 = bp - r1 + s1; s2I = LastValue (s2,1);
r3 = bp + r2 - s1; r3I = LastValue (r3,1);
s3 = bp - r2 + s1; s3I = LastValue (s3,1);
r4 = bp + r2 - s2; r4I = LastValue (r4,1);
s4 = bp - r2 + s2; s4I = LastValue (s4,1);

/// @link https://forum.amibroker.com/t/hw-to-plot-pivots-for-the-fresh-day-start-to-end-instead-of-candle-based/21289/2
function SetNull(array, mode) {
	global newday;
	if ( mode )
		result = IIf(! newday, array, Null);
	else
		result = IIf(newday OR Ref(newday,-1), array, Null);
	return result;
}

if(ppl==1) {
	y_offset = 2;
	Plot(SetNull(bp, 1),"",colorBlue,styleStaircase|styleDots|styleNoRescale);
	Plot(SetNull(bp, 0),"",colorBlue,styleStaircase|styleNoRescale);
	PlotText(" Pivot = " + WriteVal(bp,fraction), BarCount+2, bpI, colorBlue, -1, y_offset);
	for (i = 1; i <= 4; i++) {
		s = VarGet("s"+i);
		Plot(SetNull(s,1),"",colorRed,styleLine|styleNoRescale);
		Plot(SetNull(s,0),"",colorRed,styleStaircase|styleNoRescale);
		PlotText(" s"+i+" = " + WriteVal(s,fraction), BarCount+2, LastValue(s), colorRed, -1, y_offset);
		r = VarGet("r"+i);
		Plot(SetNull(r,1),"",colorGreen,styleLine|styleNoRescale);
		Plot(SetNull(r,0),"",colorGreen,styleStaircase|styleNoRescale);
		PlotText(" r"+i+" = " + WriteVal(r,fraction), BarCount+2, LastValue(r), colorGreen, -1, y_offset);
	}
}
_SECTION_END();

_SECTION_BEGIN( "Days of the week with Names" );
// by Milosz Mazurkiewicz
/// @link https://forum.amibroker.com/t/how-to-visually-identify-days-of-the-week-on-intraday-charts-background-color-change-for-each-separate-day/6881/11
Color1 = ParamColor( "Ribbon color", colorLightYellow );
Color2 = ParamColor( "Text color", colorBlack );
if (Interval() >= inDaily) 
Error("This code should be used on intraday charts. Change interval.");
dn = DateNum(); 
dw = DayOfWeek(); 
bi = BarIndex();
fvbi = Status( "firstvisiblebarindex" ); 
lvbi = Status( "lastvisiblebarindex" ); 
pxchb = Status( "pxchartbottom" );
FirstBarOfDay = dn != Ref( dn, -1 );
RibbonHeight = 5; // Percent of chart height
for( i = 1; i <= 6; i++ ) 
VarSet( "C" + i, ColorBlend( colorBlack, Color1, i / 6 ) );
RibbonColor = IIf( DW == 1, C1, IIf( dw == 2, C2, IIf( dw == 3, C3, IIf( dw == 4, C4, IIf( dw == 5, C5, IIf( dw == 6, C6, colorDefault ) ) ) ) ));
GfxSetZOrder(1); GfxSetCoordsMode(3);
GfxSetTextColor(Color2); GfxSelectFont( "Arial", 9, 700 ); // Or for example "Arial Narrow"
DaysOfWeek = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday";
for( j = fvbi; j <= Min( lvbi, BarCount - 1 ) ; j++ ) if (FirstBarOfDay[j]) GfxTextOut(StrExtract(DaysOfWeek, dw[j]), bi[j] + 1, pxchb - RibbonHeight*5);
//Plot( Close, "Close", colorDefault, styleCandle );
Plot( FirstBarOfDay, "", Color1, styleHistogram | styleOwnScale | styleNoLabel, 1, 0, 0, -1, 2 );
Plot( RibbonHeight, "", RibbonColor, styleArea | styleOwnScale | styleNoLabel, 0, 100, 0, -1 );
_SECTION_END();
1 Like

little error which is corrected in this line ( "Low" instead of "High" )

2 Likes

@Achalendra thank you so much for your help. I deeply appreciate it.

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