Display shading only during pre-market


Category:
0
0

Hi Pete,

I have a script that paints a shaded region for 30-50% up from the Previous Close. I would like to limit this line to pre-market hours only. Is this something that can easily be done? Thank you!

input percentThresholdTop = 50.0;
input percentThresholdBottom = 30.0;
input timeFrame = AggregationPeriod.DAY;
DefineGlobalColor(“shading”, Color.GREEN);
def Close = close(period = “DAY”)[1];
plot rangeTop = Close + (Close * percentThresholdTop * 0.01);
rangeTop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rangeTop.AssignValueColor(GlobalColor(“shading”));
plot rangeBottom = Close + (Close * percentThresholdBottom * 0.01);
rangeBottom.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rangeBottom.AssignValueColor(GlobalColor(“shading”));
AddCloud(rangeTop, rangeBottom, GlobalColor(“shading”), GlobalColor(“shading”));

 

-Davide

Marked as spam
Posted by (Questions: 7, Answers: 12)
Asked on April 12, 2022 7:25 pm
39 views
0
Private answer

This topic has already been covered previously in the forum. I have updated the title of your question to make this post easier to locate using the search function.

Here is a link to that previous post:

https://www.hahn-tech.com/ans/how-do-i-change-the-background-color-based-on-time-of-day/

Here is the core section of code which makes this work. I have adjusted the time inputs to fit the specific times you requested, when the chart is set to Eastern timezone.

input alertPeriodStart = 400;
input alertPeriodEnd = 930;
def startCounter = SecondsFromTime(alertPeriodStart);
def endCounter = SecondsTillTime(alertPeriodEnd);
def alertPeriod = if startCounter >= 0 and endCounter >= 0
then 1 else 0;

And here is how we fold that section of code into your existing solution:

input percentThresholdTop = 50.0;
input percentThresholdBottom = 30.0;
input alertPeriodStart = 400;
input alertPeriodEnd = 930;
def startCounter = SecondsFromTime(alertPeriodStart);
def endCounter = SecondsTillTime(alertPeriodEnd);
def alertPeriod = if startCounter >= 0 and endCounter >= 0
then 1 else 0;
DefineGlobalColor(“shading”, Color.GREEN);
def previousDailyClose = close(period = “DAY”)[1];
plot rangeTop = previousDailyClose + (previousDailyClose * percentThresholdTop * 0.01);
rangeTop.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rangeTop.AssignValueColor(GlobalColor(“shading”));
plot rangeBottom = previousDailyClose + (previousDailyClose * percentThresholdBottom * 0.01);
rangeBottom.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
rangeBottom.AssignValueColor(GlobalColor(“shading”));
AddCloud(if alertPeriod then rangeTop else Double.NaN, if alertPeriod then rangeBottom else Double.NaN, GlobalColor(“shading”), GlobalColor(“shading”));

Note that I have updated the variable name for the previous daily close. You had assigned it to a variable named "close" which is bad form because "close" is a key word. When writing code we must be very careful to avoid using key words and function names as variable names.

This solution impacts the shaded area, but it does not change the horizontal lines that plot on the top and bottom of your custom range. You can work out that part, given the example I have applied to the AddCloud() statement.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 13, 2022 9:04 am
0
Hi Pete, Thank you for this! This gets me half way there. There are still two lines that continue into market hours however. I tried to remove these lines, but didn't succeed. I also tried to apply the IF statement to the lines, but keep getting errors in my script. Sorry, I am not good at programming, so I don't know how to do the last step. I would prefer to remove the lines altogether if possible, as they are not really needed. Thank you, Davide
( at April 13, 2022 10:15 am)
0
To remove the lines but retain their function you have two very simple options. First is to open the Edit Studies window and turn off those two plots. The second is to edit the code, changing the plot to def and removing the style plots from the remaining code. The first option is the easiest.
( at April 13, 2022 11:52 am)