Previous Days High, Low, Open, Close, and High/Low of defined timeframe For ThinkOrSwim

@Jerseystranger
Added input to hide chart bubbles.
Ruby:
#Shows previous days High, Low, Open, Close, and High/Low of defined timeframe defaulted to premarket hours.
#Code by Svanoy

input Show_High = Yes;
input Show_Low = Yes;
input Show_Open = Yes;
input Show_Close = Yes;
input Show_Premarket_High_and_Low = Yes;
input Show_Chart_Bubbles = No;
input sPeriod = {default DAY};

def offset = 1;

def Today = if GetDay()==GetLastDay() then 1 else 0;
def varhigh = high(period = sPeriod)[offset];
def varlow = low(period = sPeriod)[offset];
def varopen = open(period = sPeriod)[offset];
def varclose = close(period = sPeriod)[offset];
def periodstart = 0400;
def periodend = 0930;
def activeperiod = If secondsFromTime(periodstart)>=0 and secondsTillTime(periodend)>0 then yes else Double.NaN;
def acriveperiodstartbar = if secondsFromTime(periodstart)==0 then barnumber() else acriveperiodstartbar[1];
def activeperiodendbar = fold i=0 to AbsValue(BarNumber()) while !IsNaN(GetValue(activeperiod,-i)) do GetValue(BarNumber(),-i);
def activeperiodlength = 1+(activeperiodendbar-acriveperiodstartbar);

def activeperiodhigh;
if secondsFromTime(periodstart)==0 {
activeperiodhigh = high;
}else if !IsNaN(activeperiod) and high>=activeperiodhigh[1]{
activeperiodhigh = high;
}else{
activeperiodhigh = activeperiodhigh[1];}

def activeperiodlow;
if secondsFromTime(periodstart)==0 {
activeperiodlow = low;
}else if !IsNaN(activeperiod) and low<=activeperiodlow[1]{
activeperiodlow = low;
}else{
activeperiodlow = activeperiodlow[1];}

def activephigh = fold iah=0 to AbsValue(activeperiodlength) while !IsNaN(activeperiod) do GetValue(activeperiodhigh,-iah);
def activeplow = fold ial=0 to AbsValue(activeperiodlength) while !IsNaN(activeperiod) do GetValue(activeperiodlow,-ial);
def aph = fold iaph = 0 to 1 while !IsNaN(close[10]) do activeperiodhigh;
def apl = fold iapl = 0 to 1 while !IsNaN(close[10]) do activeperiodlow;
def h = fold ih = 0 to 1 while !IsNaN(close[10]) do varhigh;
def l = fold il = 0 to 1 while !IsNaN(close[10]) do varlow;
def o = fold io = 0 to 1 while !IsNaN(close[10]) do varopen;
def c = fold ic = 0 to 1 while !IsNaN(close[10]) do varclose;

plot PreHigh = if !IsNaN(activeperiod) and activephigh>0 then activephigh else Double.NaN;
PreHigh.SetPaintingStrategy(PaintingStrategy.LINE);
PreHigh.SetDefaultColor(Color.LIME);
PreHigh.SetHiding(!Show_Premarket_High_and_Low);

plot PreLow = if !IsNaN(activeperiod) and activeplow>0 then activeplow else Double.NaN;
PreLow.SetPaintingStrategy(PaintingStrategy.LINE);
PreLow.SetDefaultColor(Color.PINK);
PreLow.SetHiding(!Show_Premarket_High_and_Low);

plot PMHigh = if IsNaN(activeperiod) and aph>0 then aph else Double.NaN;
PMHigh.SetPaintingStrategy(PaintingStrategy.DASHES);
PMHigh.SetDefaultColor(Color.LIME);
PMHigh.SetHiding(!Show_Premarket_High_and_Low);

plot PMLow = if IsNaN(activeperiod) and apl>0 then apl else Double.NaN;
PMLow.SetPaintingStrategy(PaintingStrategy.DASHES);
PMLow.SetDefaultColor(Color.PINK);
PreLow.SetHiding(!Show_Premarket_High_and_Low);

plot DayHigh = if h > 1 then h else Double.NaN;
DayHigh.SetPaintingStrategy(PaintingStrategy.DASHES);
DayHigh.SetDefaultColor(Color.LIGHT_GREEN);
DayHigh.SetHiding(!Show_High);

plot DayLow = if l > 1 then l else Double.NaN;
DayLow.SetPaintingStrategy(PaintingStrategy.DASHES);
DayLow.SetDefaultColor(Color.LIGHT_RED);
DayLow.SetHiding(!Show_Low);

plot DayOpen = if o > 1 then o else Double.NaN;
DayOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DayOpen.AssignValueColor(if varopen >= varclose then Color.GREEN else Color.RED);
DayOpen.SetHiding(!Show_Open);

plot DayClose = if c > 1 then c else Double.NaN;
DayClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DayClose.AssignValueColor(if varopen >= varclose then Color.RED else Color.GREEN);
DayClose.SetHiding(!Show_Close);

AddChartBubble(IsNaN(PreHigh[1]) and !IsNaN(PreHigh) and Show_Premarket_High_and_Low and Show_Chart_Bubbles,PreHigh,"PreMarket High",Color.WHITE,yes);
AddChartBubble(IsNaN(PreLow[1]) and !IsNaN(PreLow) and Show_Premarket_High_and_Low and Show_Chart_Bubbles,PreLow,"PreMarketLow",Color.WHITE,no);
AddChartBubble(DayHigh!=DayHigh[1] and !IsNaN(close) and Show_High and Show_Chart_Bubbles,h,"PrevDayHigh",Color.WHITE,yes);
AddChartBubble(DayLow!=DayLow[1] and !IsNaN(close)and Show_Low and Show_Chart_Bubbles,l,"PrevDayLow",Color.WHITE,no);
AddChartBubble(DayOpen!=DayOpen[1] and !IsNaN(close) and Show_Open and Show_Chart_Bubbles,o,"PrevDayOpen",Color.WHITE,if o>=c then yes else no);
AddChartBubble(DayClose!=DayClose[1] and !IsNaN(close) and Show_Close and Show_Chart_Bubbles,c,"PrevDayClose",Color.WHITE, if o>=c then no else yes);

AddCloud(if Show_Premarket_High_and_Low then PreHigh else Double.NEGATIVE_INFINITY,if Show_Premarket_High_and_Low then PreLow else Double.NEGATIVE_INFINITY,color.LIGHT_GRAY,color.LIGHT_GRAY);
 

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

@Jerseystranger
Added input to hide chart bubbles.
Ruby:
#Shows previous days High, Low, Open, Close, and High/Low of defined timeframe defaulted to premarket hours.
#Code by Svanoy

input Show_High = Yes;
input Show_Low = Yes;
input Show_Open = Yes;
input Show_Close = Yes;
input Show_Premarket_High_and_Low = Yes;
input Show_Chart_Bubbles = No;
input sPeriod = {default DAY};

def offset = 1;

def Today = if GetDay()==GetLastDay() then 1 else 0;
def varhigh = high(period = sPeriod)[offset];
def varlow = low(period = sPeriod)[offset];
def varopen = open(period = sPeriod)[offset];
def varclose = close(period = sPeriod)[offset];
def periodstart = 0400;
def periodend = 0930;
def activeperiod = If secondsFromTime(periodstart)>=0 and secondsTillTime(periodend)>0 then yes else Double.NaN;
def acriveperiodstartbar = if secondsFromTime(periodstart)==0 then barnumber() else acriveperiodstartbar[1];
def activeperiodendbar = fold i=0 to AbsValue(BarNumber()) while !IsNaN(GetValue(activeperiod,-i)) do GetValue(BarNumber(),-i);
def activeperiodlength = 1+(activeperiodendbar-acriveperiodstartbar);

def activeperiodhigh;
if secondsFromTime(periodstart)==0 {
activeperiodhigh = high;
}else if !IsNaN(activeperiod) and high>=activeperiodhigh[1]{
activeperiodhigh = high;
}else{
activeperiodhigh = activeperiodhigh[1];}

def activeperiodlow;
if secondsFromTime(periodstart)==0 {
activeperiodlow = low;
}else if !IsNaN(activeperiod) and low<=activeperiodlow[1]{
activeperiodlow = low;
}else{
activeperiodlow = activeperiodlow[1];}

def activephigh = fold iah=0 to AbsValue(activeperiodlength) while !IsNaN(activeperiod) do GetValue(activeperiodhigh,-iah);
def activeplow = fold ial=0 to AbsValue(activeperiodlength) while !IsNaN(activeperiod) do GetValue(activeperiodlow,-ial);
def aph = fold iaph = 0 to 1 while !IsNaN(close[10]) do activeperiodhigh;
def apl = fold iapl = 0 to 1 while !IsNaN(close[10]) do activeperiodlow;
def h = fold ih = 0 to 1 while !IsNaN(close[10]) do varhigh;
def l = fold il = 0 to 1 while !IsNaN(close[10]) do varlow;
def o = fold io = 0 to 1 while !IsNaN(close[10]) do varopen;
def c = fold ic = 0 to 1 while !IsNaN(close[10]) do varclose;

plot PreHigh = if !IsNaN(activeperiod) and activephigh>0 then activephigh else Double.NaN;
PreHigh.SetPaintingStrategy(PaintingStrategy.LINE);
PreHigh.SetDefaultColor(Color.LIME);
PreHigh.SetHiding(!Show_Premarket_High_and_Low);

plot PreLow = if !IsNaN(activeperiod) and activeplow>0 then activeplow else Double.NaN;
PreLow.SetPaintingStrategy(PaintingStrategy.LINE);
PreLow.SetDefaultColor(Color.PINK);
PreLow.SetHiding(!Show_Premarket_High_and_Low);

plot PMHigh = if IsNaN(activeperiod) and aph>0 then aph else Double.NaN;
PMHigh.SetPaintingStrategy(PaintingStrategy.DASHES);
PMHigh.SetDefaultColor(Color.LIME);
PMHigh.SetHiding(!Show_Premarket_High_and_Low);

plot PMLow = if IsNaN(activeperiod) and apl>0 then apl else Double.NaN;
PMLow.SetPaintingStrategy(PaintingStrategy.DASHES);
PMLow.SetDefaultColor(Color.PINK);
PreLow.SetHiding(!Show_Premarket_High_and_Low);

plot DayHigh = if h > 1 then h else Double.NaN;
DayHigh.SetPaintingStrategy(PaintingStrategy.DASHES);
DayHigh.SetDefaultColor(Color.LIGHT_GREEN);
DayHigh.SetHiding(!Show_High);

plot DayLow = if l > 1 then l else Double.NaN;
DayLow.SetPaintingStrategy(PaintingStrategy.DASHES);
DayLow.SetDefaultColor(Color.LIGHT_RED);
DayLow.SetHiding(!Show_Low);

plot DayOpen = if o > 1 then o else Double.NaN;
DayOpen.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DayOpen.AssignValueColor(if varopen >= varclose then Color.GREEN else Color.RED);
DayOpen.SetHiding(!Show_Open);

plot DayClose = if c > 1 then c else Double.NaN;
DayClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
DayClose.AssignValueColor(if varopen >= varclose then Color.RED else Color.GREEN);
DayClose.SetHiding(!Show_Close);

AddChartBubble(IsNaN(PreHigh[1]) and !IsNaN(PreHigh) and Show_Premarket_High_and_Low and Show_Chart_Bubbles,PreHigh,"PreMarket High",Color.WHITE,yes);
AddChartBubble(IsNaN(PreLow[1]) and !IsNaN(PreLow) and Show_Premarket_High_and_Low and Show_Chart_Bubbles,PreLow,"PreMarketLow",Color.WHITE,no);
AddChartBubble(DayHigh!=DayHigh[1] and !IsNaN(close) and Show_High and Show_Chart_Bubbles,h,"PrevDayHigh",Color.WHITE,yes);
AddChartBubble(DayLow!=DayLow[1] and !IsNaN(close)and Show_Low and Show_Chart_Bubbles,l,"PrevDayLow",Color.WHITE,no);
AddChartBubble(DayOpen!=DayOpen[1] and !IsNaN(close) and Show_Open and Show_Chart_Bubbles,o,"PrevDayOpen",Color.WHITE,if o>=c then yes else no);
AddChartBubble(DayClose!=DayClose[1] and !IsNaN(close) and Show_Close and Show_Chart_Bubbles,c,"PrevDayClose",Color.WHITE, if o>=c then no else yes);

AddCloud(if Show_Premarket_High_and_Low then PreHigh else Double.NEGATIVE_INFINITY,if Show_Premarket_High_and_Low then PreLow else Double.NEGATIVE_INFINITY,color.LIGHT_GRAY,color.LIGHT_GRAY);
Really awesome presentation here @Svanoy thank you. Would love to see the current rth open plot at the open and extend as the day moves on.
Also, based on this
Post in thread 'Average Daily Range Indicator for ThinkorSwim'
https://usethinkscript.com/threads/average-daily-range-indicator-for-thinkorswim.244/post-66296
Asia begins at 21:30. This script can only go back to midnight 0000 to plot pre market low/ highs. Ideally, would like to plot from 18:00 to 9:29 , if possible. Thanks!
 
Really awesome presentation here @Svanoy thank you. Would love to see the current rth open plot at the open and extend as the day moves on.
Also, based on this
Post in thread 'Average Daily Range Indicator for ThinkorSwim'
https://usethinkscript.com/threads/average-daily-range-indicator-for-thinkorswim.244/post-66296
Asia begins at 21:30. This script can only go back to midnight 0000 to plot pre market low/ highs. Ideally, would like to plot from 18:00 to 9:29 , if possible. Thanks!
@Svanoy

Here is an example where being able to adjust the start time for the pre-market range is necessary.

Love the script, hoping you or anyone can adjust the code so that I can go back and start the pre-market range from 18:00 and end at 9:29am.

Also, if possible have the ability to select how many days back to show data? In my case, only showing 2-5 days max would be sufficient, but others may want more or less.

If you can buy/sell a futures contract after 18:00 that time-range should be counted, imho.

Thanks for the awesome script and for your time.

H

1713533585438.png
 
Last edited:
Anyone know how to adjust the Previous Day Close to 4 PM?
For the futures like ES and NQ, the PDC is always showing 5 pm, which is not correct.
Cannot see any study that has the correct PDC for futures.
 
Anyone know how to adjust the Previous Day Close to 4 PM?
For the futures like ES and NQ, the PDC is always showing 5 pm, which is not correct.
Cannot see any study that has the correct PDC for futures.

This should plot a prior day's close at 4 PM for input back number of days. An optional label is included.

Screenshot 2024-06-22 111932.png

Code:
input back  = 1;
input label = yes;
def pdc = if getday()==getlastday() - back and SecondsFromTime(1600) == 0 then close else pdc[1];
plot pdc_futures = pdc;
pdc_futures.setpaintingStrategy(paintingStrategy.DASHES);

addlabel(label, "PD_Close " + back + " days ago: " + asdollars(pdc_futures), color.yellow);
#
 
Last edited:
This should plot a prior day's close at 4 PM for input back number of days. An optional label is included.
Thank you @SleepyZ. I have a study that gives me the same exact results.
The issue I think is that it calculates at 5 PM when the futures market closes as previous day and not the 4 PM market close. The TOS previous close and Net Change/% Change doesn't show the same... but I think it's close enough.

EDIT: Looks like putting the number of days back to 0, does show the prior day close.
Thanks again.
 
Last edited:
Ruby:
def activeperiodstartbar = If (!SecondsFromTime(0400), BarNumber(), activeperiodstartbar[1]);
def activeperiodendbar = If (!IsNaN(GetValue(activeperiod, 40)), BarNumber(), activeperiodendbar[1]);
def activeperiodlength = 1 + (activeperiodendbar - activeperiodstartbar);

def activeperiodhigh;
if !SecondsFromTime(0400) {
    activeperiodhigh = open;
} else if !IsNaN(activeperiod) && open >= activeperiodhigh[1] {
    activeperiodhigh = open;
} else {
    activeperiodhigh = activeperiodhigh[1];
}

def activeperiodlow;
if SecondsFromTime(0400) == 0 {
    activeperiodlow = close;
} else if !IsNaN(activeperiod) && close <= activeperiodlow[1] {
    activeperiodlow = close;
} else {
    activeperiodlow = activeperiodlow[1];
}

def activephigh = If (!IsNaN(activeperiod), GetValue(activeperiodhigh, -80), activephigh[1]);
def activeplow = If (!IsNaN(activeperiod), GetValue(activeperiodlow, -80), activeplow[1]);

The current implementation does not meet the intended behavior and results in incorrect plotting on timeframes exceeding 5 minutes. While Rec adjusts with each bar, it fails to maintain horizontal lines from pre-market start to end as effectively as the original fold loop. Despite attempts to use workarounds, including Rec, the functionality remains inconsistent on higher timeframes. Any assistance is greatly appreciated in resolving the issue without using the fold loop. Thank you.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
232 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