How do I use previous day high and low in premarket?

123MBB

New member
Plus
I have an indicator that show where the day's open is compared to the previous day's range.
An example, if the previous day's high (PDH) 101.0 and the previous day's low (PDL) was 100.0 and today's open was 100.5 then the value would be 50%, if open was 100.1 then 110%, if open was 99.7 the value would be -30%.
1712165699329.png

The code below works well after the market opens. But I would like to see were the price is trading relative to the previous day's range in the premarket. I need help on getting it to work in the premarket. After the open the value does not change. I would be fine using the premarket close rather than the market open, but I cannot get the PDH and PDL in the premarket. Again, once the market opens it provides works as I want it. How can I make it work in the premarket?

input showLabel = yes;

plot DailyHigh = high(period=”DAY”)[1];
plot DailyLow = low(period="Day")[1];

# Label for r/g
def price = high;
def prev_high = high(period=”DAY”)[1];
def prev_low = low(period="DAY")[1];
def openprice = open(period = ”day”)[0];
def pd_percent_open = ((openprice-prev_low)/(prev_high-prev_low));

def bullish = pd_percent_open >= .90;
def bearish = pd_percent_open <= .10;
def neutral = pd_percent_open < .90 and pd_percent_open >.10;


AddLabel(showLabel, aspercent(pd_percent_open), if bullish then Color.GREEN else if bearish then Color.RED else Color.white);
 
Solution
I have an indicator that show where the day's open is compared to the previous day's range.
An example, if the previous day's high (PDH) 101.0 and the previous day's low (PDL) was 100.0 and today's open was 100.5 then the value would be 50%, if open was 100.1 then 110%, if open was 99.7 the value would be -30%.

The code below works well after the market opens. But I would like to see were the price is trading relative to the previous day's range in the premarket. I need help on getting it to work in the premarket. After the open the value does not change. I would be fine using the premarket close rather than the market open, but I cannot get the PDH and PDL in the premarket. Again, once the market opens it provides works as I...
I have an indicator that show where the day's open is compared to the previous day's range.
An example, if the previous day's high (PDH) 101.0 and the previous day's low (PDL) was 100.0 and today's open was 100.5 then the value would be 50%, if open was 100.1 then 110%, if open was 99.7 the value would be -30%.

The code below works well after the market opens. But I would like to see were the price is trading relative to the previous day's range in the premarket. I need help on getting it to work in the premarket. After the open the value does not change. I would be fine using the premarket close rather than the market open, but I cannot get the PDH and PDL in the premarket. Again, once the market opens it provides works as I want it. How can I make it work in the premarket?

input showLabel = yes;

plot DailyHigh = high(period=”DAY”)[1];
plot DailyLow = low(period="Day")[1];

# Label for r/g
def price = high;
def prev_high = high(period=”DAY”)[1];
def prev_low = low(period="DAY")[1];
def openprice = open(period = ”day”)[0];
def pd_percent_open = ((openprice-prev_low)/(prev_high-prev_low));

def bullish = pd_percent_open >= .90;
def bearish = pd_percent_open <= .10;
def neutral = pd_percent_open < .90 and pd_percent_open >.10;


AddLabel(showLabel, aspercent(pd_percent_open), if bullish then Color.GREEN else if bearish then Color.RED else Color.white); hal_pre

here is an upper chart code to experiment with
default premarket is 4am to 9:30
draws purple lines for the previous day high/low range of pre market
shows a % of day open compared to the previous day premarket range
added a 2nd label that shows the % of close to the range


Code:
#pre_market_compare_rng

#https://usethinkscript.com/threads/how-do-i-use-previous-day-high-and-low-in-premarket.18357/
#How do I use previous day high and low in premarket?
#123MBB  4/3

#I have an indicator that show where the day's open is compared to the previous day's range.
#An example, if the previous day's high (PDH) 101.0 and the previous day's low (PDL) was 100.0 and today's open was 100.5 then the value would be 50%, if open was 100.1 then 110%, if open was 99.7 the value would be -30%.

#The code below works well after the market opens. But I would like to see were the price is trading relative to the previous day's range in the premarket. I need help on getting it to work in the premarket. After the open the value does not change. I would be fine using the premarket close rather than the market open, but I cannot get the PDH and PDL in the premarket. Again, once the market opens it provides works as I want it. How can I make it work in the premarket?



def na = double.nan;
def bn = barnumber();
def big = 99999;

def d = GetDay();
def day_first = (d[1] != d);

# normal trading time
input day_start = 0930;
input day_end = 1600;
def day_period = (secondsfromTime(day_start) >= 0 and secondstillTime(day_end) > 0);
# pre market
input pre_start = 0400;
input pre_end = 0930;
def pre_period = (secondsfromTime(pre_start) >= 0 and secondstillTime(pre_end) > 0);
#addverticalline(pre_period,"-");
#addverticalline(startbar,"-");

def start_bar = (secondsfromTime(day_start)[1] < 0 and secondsfromTime(day_start) >= 0);
def start_open = if start_bar then open else start_open[1];

def hi = if bn == 1 or isnan(close) then 0
 else if !pre_period[1] and pre_period then high
 else if pre_period then max(hi[1],high)
 else hi[1];
def lo = if bn == 1 or isnan(close) then 0
 else if !pre_period[1] and pre_period then low
 else if pre_period then min(lo[1],low)
 else lo[1];

# if a doji candle, then make the hi and lo 0.01 apart, not 0
def y = 0.005;

def prev_hi = if bn == 1 or isnan(close) then 0
 else if day_first and hi[1] > 0 and hi[1] == lo[1] then hi[1] + y
 else if day_first then hi[1]
 else prev_hi[1];

def prev_lo = if bn == 1 or isnan(close) then 0
 else if day_first and hi[1] > 0 and hi[1] == lo[1] then lo[1] - y
 else if day_first then lo[1]
 else prev_lo[1];

def per_open = round(100*((start_open - prev_lo) / (prev_hi - prev_lo)),1);
def per_close = round(100*((close - prev_lo) / (prev_hi - prev_lo)),1);
def perhi = 90;
def perlo = 10;

#addlabel(1, " ", color.black);
addlabel(1, "Open " + per_open + "%", (if per_open > perhi then color.green else if per_open < perlo then color.red else color.gray));

#--------------------------------
# remove everything after for a watchlist code
addlabel(1, " ", color.black);
addlabel(1, "Close " + per_close + "%", (if per_close > perhi then color.green else if per_close < perlo then color.red else color.gray));
addlabel(1, " ", color.black);

#-------------------------------
# test data

input test_lines1_current = no;
plot z1 = if test_lines1_current and hi > 0 then hi else na;;
plot z2 = if test_lines1_current and lo > 0 then lo else na;
z1.SetDefaultColor(Color.yellow);
z2.SetDefaultColor(Color.yellow);
addlabel(test_lines1_current , "today levels", color.yellow);

input test_lines2_prev = yes;
plot z3 = if test_lines2_prev and prev_hi > 0 then prev_hi else na;
plot z4 = if test_lines2_prev and prev_lo > 0 then prev_lo else na;
z3.SetDefaultColor(Color.magenta);
z4.SetDefaultColor(Color.magenta);
addlabel(test_lines2_prev , "previous day levels", color.magenta);

input test_labels1 = no;
addlabel(test_labels1, "O " + start_open, color.yellow);
addlabel(test_labels1, "Hi " + prev_hi, color.yellow);
addlabel(test_labels1, "Lo " + prev_lo, color.yellow);
addlabel(test_labels1,(start_open - prev_lo), color.yellow);
addlabel(test_labels1, (prev_hi - prev_lo), color.yellow);

input start_bar_triangle = no;
plot zs = if start_bar and start_bar_triangle then low*0.999 else na;
zs.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
#x.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zs.SetDefaultColor(Color.cyan);
zs.setlineweight(4);

addchartbubble(0, prev_hi,
prev_hi
, color.yellow, no);

#
 

Attachments

  • 00b-1amzn-5.JPG
    00b-1amzn-5.JPG
    110.1 KB · Views: 51
Last edited:
Solution
Thank you. This is helpful.
I was able to get this to work.



def na = Double.NaN;
def bn = BarNumber();
def big = 99999;

def d = GetDay();
def day_first = (d[1] != d);

# normal trading time
input day_start = 0930;
input day_end = 1600;
def day_period = (SecondsFromTime(day_start) >= 0 and SecondsTillTime(day_end) > 0);
# pre market
input pre_start = 0400;
input pre_end = 0930;
def pre_period = (SecondsFromTime(pre_start) >= 0 and SecondsTillTime(pre_end) > 0);
#addverticalline(pre_period,"-");
#addverticalline(startbar,"-");

def start_bar = (SecondsFromTime(day_start)[1] < 0 and SecondsFromTime(day_start) >= 0);
def start_open = if start_bar then open else start_open[1];

def hi = if bn == 1 or IsNaN(close) then 0
else if !pre_period[1] and pre_period then high
else if pre_period then Max(hi[1], high)
else hi[1];
def lo = if bn == 1 or IsNaN(close) then 0
else if !pre_period[1] and pre_period then low
else if pre_period then Min(lo[1], low)
else lo[1];
def pmc = if bn == 1 or IsNaN(close) then 0
else if !pre_period[1] and pre_period then close
else if pre_period then close
else open(period = ”day”)[0];

#plot pmcl = pmc;
#plot pmhi = hi;
#plot pmlo = lo;

def y = 0.005;
def prev_hi = if bn == 1 or isnan(close) then 0
else if day_first and hi[1] > 0 and hi[1] == lo[1] then hi[1] + y
else if day_first then hi[1]
else prev_hi[1];

def prev_lo = if bn == 1 or isnan(close) then 0
else if day_first and hi[1] > 0 and hi[1] == lo[1] then lo[1] - y
else if day_first then lo[1]
else prev_lo[1];

#plot DailyHigh = high(period=”DAY”)[1];
#plot DailyLow = low(period="Day")[1];

# Label for r/g
def price = high;
def prev_high = high(period=”DAY”)[1];
def prev_low = low(period="DAY")[1];

def pd_percent_open = ((pmc-prev_low)/(prev_high-prev_low));

input showLabel = yes;

def bullish = pd_percent_open >= .85;
def bearish = pd_percent_open <= .15;
def neutral = pd_percent_open < .85 and pd_percent_open >.15;

AddLabel(showLabel, aspercent(pd_percent_open), if bullish then Color.GREEN else if bearish then Color.RED else Color.white);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
395 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top