Background Color for a time slot

Hello Team,
I wish to have a background color for a timestot, the highest and the lowest levels only should be colored.Background_Color

Currently the system colors the complete timeslot with a background Complete_Color

I wish to color only the zone between high and lows of that timeslots to be colored.

Could you please guide me what can be done to achieve this.

Hello all,
I am creating a background color based on a time slot (example 09:30 to 10:15) I want to have a background box with the height = H of this period and Low the time period.

Based on suggestions I am able to create a horizontal strip but need assistance to manage the width of the background box.

I wish to have the box stretch from time 9:30 to 10:15 only

PlotOHLC( tn>091500, 355, 365, tn<101500, "", ColorBlue, styleCloud + styleNoLabel,091500,101500 );

The image below shows the strip, the requirement is between the timeslots with height of the box being H and L of the period 091500,101500

image

You can refer example here
http://www.amibroker.com/kb/2016/01/22/how-to-fill-background-between-hand-drawn-trend-lines/

You cannot use a condition in O,C fields, refer to the example post and see how you can use a function to create a price Array of Low and High and only set values for the required time period.

The rest of the bars outside the time will be set to NULL, that can be achieved with Iif()

1 Like

I'm using arbitrary values for H and L, that's left to you.

boxH = 72;
boxL = 71;

tn = TimeNum();
dn = DateNum();
bH = IIf( tn >= 130000 && tn < 140000 && dn == LastValue( dn), boxH, Null);
bL = IIf( tn >= 130000 && tn < 140000 && dn == LastValue( dn), boxL, Null);

PlotOHLC( bH, bH, bL, bL, "",
		IIf(O > C, colorDarkBlue, colorDarkBlue),
		styleCloud | styleNoRescale | styleNoLabel, Null, Null, 0, -5, 1 );

It is important to note a few things here:

  1. See how OHLC values are passed to PlotOHLC(), it needs prices not conditions
  2. See how an Array is used for Color of the Box.
  3. You are using Min, Max and substitung them with time which is incorrect. Refer manual to what they do.
  4. You code doesn't have Zorder, without that i'm just surprised how much of the manual and help KB you checked.

Anyway, the code works and should draw boxes like this
image

2 Likes

Thank you so much for your help.

Want to check the method to get the H value during a time period.

boxH = HighestSince(dn!=Ref(dn,-1), H ); Not working
boxH = ValueWhen( tn >= 110000 && tn < 140000, HighestSince(dn!=Ref(dn,-1), H )); Not working

What should be the apt way to get the value during the timeslot

Found the solution in this thread HHV and LLV of Specific Time range in Intraday

You should use code tags and properly format your code.

try this

boxH = LastValue( ValueWhen(tn == 140000,  HighestSince( tn == 130000, H)));
boxL = LastValue( ValueWhen(tn == 140000,   LowestSince( tn == 130000, L)));
2 Likes

And here is another solution using GFX

PB 	  = ParamTime("Period Begin","09:30",0);
PE 	  = ParamTime("Period End","10:30",0);
Color = ParamColor("Box Color",colorLightBlue);

Condition = TimeNum() >= PB AND TimeNum() <= PE;


PB = Period_Beginning = Ref(Condition,0) > Ref(Condition,-1);
PE = Period_Ending    = Ref(Condition,0) > Ref(Condition,1);
PD = Period_Duration  = ValueWhen(PE,BarsSince(!Condition),0)-1;

HB = Higher_Boundry   = ValueWhen(PB,Ref(HighestSince(!Condition,High),PD));
LB = Lowest_Boundry   = ValueWhen(PB,Ref(LowestSince(!Condition,Low),PD));

FBi = First_Bar		  = ValueWhen(PB,BarIndex());
LBi = Last_Bar		  = ValueWhen(PB,FBi+PD);

for ( i = 0; i <= BarCount-1; i++ )
{
	if (PB[i])
	{
		GfxSetZOrder(-1);
		GfxSetCoordsMode(1);
		GfxFillSolidRect(FBi[i],LB[i],LBi[i],HB[i],Color);
	}
}


Plot(Close,"Condition",colorWhite,styleBar);

2 Likes

Hello @travick
I tried this code

Plot( H, "Close", 43|StyleLine );

tn = TimeNum();
dn = DateNum();

// define start/end hours in TimeNum format
StartTime = 93000;
Endtime = 113000;

// these conditions are true when TimeNum of the bar equals startime/endtime
StartBar = tn == StartTime;
EndBar = tn == Endtime;

BoxH = ValueWhen( EndBar, HighestSince( StartBar, High ) );
BoxL = ValueWhen( EndBar, LowestSince( StartBar, Low ) );
BoxH=490;
BoxL=485;
Plot( BoxH, "BoxH", 43|StyleLine );

bH = IIf( tn >= 130000 && tn < 140000 && dn == LastValue( dn), boxH, Null);
bL = IIf( tn >= 130000 && tn < 140000 && dn == LastValue( dn), boxL, Null);

PlotOHLC( bH, bH, bL, bL, "",
		IIf(O > C, colorDarkBlue, colorDarkBlue),
		styleCloud | styleNoRescale | styleNoLabel, Null, Null, 0, -5, 1 );

The value of BoxH and BoxL works fine if given static value like BoxH=490

If I use

BoxH = ValueWhen( EndBar, HighestSince( StartBar, High ) );
BoxL = ValueWhen( EndBar, LowestSince( StartBar, Low ) );

Then the values for BoxH and BoxL is Null throughout

image

Thank you @Sebastian for a modular code suggestion where we can control the variables while loading the indicator and customize the colors.

I tried the code and get the following message
image

Was checking https://www.amibroker.com/guide/afl/gfxfillsolidrect.html if it's supported in 5.90, seems that is not the problem.

The code being tested is

_SECTION_BEGIN("Todel");
Plot( H, "Close", 43|StyleLine );
PB 	  = ParamTime("Period Begin","09:30",0);
PE 	  = ParamTime("Period End","10:30",0);
Color = ParamColor("Box Color",colorLightBlue);

Condition = TimeNum() >= PB AND TimeNum() <= PE;


PB = Period_Beginning = Ref(Condition,0) > Ref(Condition,-1);
PE = Period_Ending    = Ref(Condition,0) > Ref(Condition,1);
PD = Period_Duration  = ValueWhen(PE,BarsSince(!Condition),0)-1;

HB = Higher_Boundry   = ValueWhen(PB,Ref(HighestSince(!Condition,High),PD));
LB = Lowest_Boundry   = ValueWhen(PB,Ref(LowestSince(!Condition,Low),PD));

FBi = First_Bar		  = ValueWhen(PB,BarIndex());
LBi = Last_Bar		  = ValueWhen(PB,FBi+PD);

for ( i = 0; i <= BarCount-1; i++ )
{
	if (PB[i])
	{
		GfxSetZOrder(-1);
		GfxSetCoordsMode(1);
		GfxFillSolidRect(FBi[i],LB[i],LBi[i],HB[i],Color);
	}
}


Plot(Close,"Condition",colorWhite,styleBar);
_SECTION_END();

I think somethink is wrong with the interpretation of this function, GfxFillSolidRect doesn't get blue out as a known function like other functions
image

In post #5 I have already put the code for time values. Why are you mixing two different codes ?

Also, I suggest you "study" AFL better, rather than looking for short-term quick code.
Then you will know how to arrange them.

GfxFillSolidRect () is a native built-in function , such function cannot be defined , your version is absolutely ( which is consider as obsolete ) does not support that function as you said the function's name had not been highlighted which mean that AFL editor does not recognize it so it is not supported in the version you are using and the problem definitely is not within the code itself ...

Here's another adjusted version without using GFX function

TB 	  = ParamTime("Period Begin","09:30",0);
TE 	  = ParamTime("Period End","10:30",0);
Color = ParamColor("Box Color",colorLightBlue);

Condition = TimeNum() >= TB AND TimeNum() <= TE;

PB  = Period_Beginning = Ref(Condition,0) > Ref(Condition,-1);
PE  = Period_Ending    = Ref(Condition,0) > Ref(Condition, 1) OR (LastValue(TimeNum()) < TE AND LastValue(BarIndex()) == BarIndex());
PD  = Period_Duration  = ValueWhen(PE,BarsSince(!Condition),0)-1;

HB  = Higher_Boundry   = IIf(Condition,ValueWhen(PB,Ref(HighestSince(!Condition,High),PD)),Null);
LB  = Lowest_Boundry   = IIf(Condition,ValueWhen(PB,Ref(LowestSince(!Condition,Low),PD)),Null);

FBi = First_Bar_Index  = IIf(Condition,ValueWhen(PB,BarIndex()),Null);
LBi = Last_Bar_Index   = IIf(Condition,ValueWhen(PB,FBi+PD),Null);


Plot(Close,"Close",colorDefault,styleBar);
PlotOHLC(HB,HB,LB,LB,"",Color,styleNoRescale|styleNoLabel|styleCloud,Null,Null,0,-1);
3 Likes

Thanks @Sebastian
The code is just perfect.

I use the following variant and the almost there, just need support to adjust the value of bH and bL, since it now takes the highest and the lowest of the day and not of the time range.

Could you guide me what should be done.

starttime2=093500;
endtime2=114500;

starttime3=114500;
endtime3=121500;

starttime4=121500;
endtime4=134500;
function PanchangTimeSlot( starttime, endtime) 
{ 
tn = TimeNum();
dn = DateNum();

boxH= HighestSince(Cross(TimeNum(),starttime),H,1);
boxL= LowestSince(Cross(TimeNum(),starttime),L,1);

bH = IIf( tn >= starttime && tn < endtime && dn == LastValue( dn), LastValue(boxH), Null);
bL = IIf( tn >= starttime && tn < endtime && dn == LastValue( dn), LastValue(boxL), Null);

PlotOHLC( bH, bH, bL, bL, "",
		IIf(O > C, colorDarkBlue, colorDarkBlue),
		styleCloud | styleNoRescale | styleNoLabel, Null, Null, 0, -5, 1 );

}

I want the yellow box for box2 and box3

image

I did not check your code but as you used mine and it works fine , so replacing ParamTime() with ParamDate() would satisfy your need as the same idea can be also applied on EOD charts

1 Like

I had not used Param functions but just straight values
I did not get a revert on this which simply speaking works

boxH = LastValue( ValueWhen(tn == 140000,  HighestSince( tn == 130000, H)));
boxL = LastValue( ValueWhen(tn == 140000,   LowestSince( tn == 130000, L)));

Its clear OP hasn't tried the two pieces of code together, as seen from his earlier own code examples.

Edit:
This is what happens when one wants copy-paste solutions.
@Sebastian even your code is straight forward, but in wrong copy-paste ppl will say bad code for no reason.

2 Likes

I agree with you except that i am not likely to haste in judging others :slight_smile: , Since all of us are prone to encounter some difficulties in programming , So all people deserve to get help ONLY if they are serious
You provide a very good example and i think that krishnakhanna will learn from it , he just may need some time to figure it out :wink:

2 Likes

But see the lack of due credit from OP in post 12,
he says

Then he says

From that post #13, bH,bL and PlotOHLC are all lines from my code.

He has used boxH and boxL from your code and then requesting more support. For what?
Probably mixing two codes wrongly.

If he added my code to find H and L of range from post 6, there's no problem.

This is the whole bit of code,

tn = TimeNum();
dn = DateNum();

boxH = LastValue( ValueWhen(tn == 140000,  HighestSince( tn == 130000, H)));
boxL = LastValue( ValueWhen(tn == 140000,   LowestSince( tn == 130000, L)));

bH = IIf( tn >= 130000 && tn < 140000 && dn == LastValue( dn), boxH, Null);
bL = IIf( tn >= 130000 && tn < 140000 && dn == LastValue( dn), boxL, Null);

PlotOHLC( bH, bH, bL, bL, "",
		IIf(O > C, colorDarkBlue, colorDarkBlue),
		styleCloud | styleNoRescale | styleNoLabel, Null, Null, 0, -5, 1 );

And if he wants multiple runs, then put a bit of code in function and run it.

starttime2=093500;
endtime2=114500;

starttime3=114500;
endtime3=121500;

starttime4=121500;
endtime4=134500;

tn = TimeNum();
dn = DateNum();

function PanchangTimeSlot( starttime, endtime) 
{
boxH = LastValue( ValueWhen(tn == endtime,  HighestSince( tn == starttime, H)));
boxL = LastValue( ValueWhen(tn == endtime,   LowestSince( tn == starttime, L)));

bH = IIf( tn >= starttime && tn < endtime && dn == LastValue( dn), boxH, Null);
bL = IIf( tn >= starttime && tn < endtime && dn == LastValue( dn), boxL, Null);

PlotOHLC( bH, bH, bL, bL, "",
		IIf(O > C, colorDarkBlue, colorDarkBlue),
		styleCloud | styleNoRescale | styleNoLabel, Null, Null, 0, -5, 1 );
}

But instead no credit to my code and calling his bit a variant when the whole thing is functional.

Anyway, i'll put my time to better use helping others :slight_smile:

2 Likes

Hello @travick and @Sebastian
I really appreciate your support on an open forum.

Both of you have helped me reach a direction I wanted to be guided to.

Thank you so very much!

2 Likes