Yesterday High, Low, Close for Intraday

jonesjb

New member
VIP
I stumbled across code months ago, and I've tried to identify the source, but haven't been able to do so... So please let me know if you are the creator, and I will apply credit. I did not write this (I did shorten it though).

What it does an why: The code allows us to plot yesterday's high, low and close on an intraday chart, and on mobile. It doesn't use the aggregation period reference, so It can also be incorporated into a 5 minute (or intraday) watchlist script or scan.

My ask: Can someone please shorten the below code, so it's even more lean/streamlined? I'm trying to use these references in a 5 min watchlist column as part of a larger study, and once I do, the study becomes too complex. If I can get this somewhat shorter, it will work.

Is there was a way to simplify this without having the odd and even day references. Thank you!

Ruby:
def PremarketTradingHours = RegularTradingStart(GetYYYYMMDD()) > GetTime();
def RegularTradingHours = RegularTradingStart(GetYYYYMMDD()) <= GetTime() and GetTime() < RegularTradingEnd(GetYYYYMMDD());
def PostmarketTradingHours = GetTime() >= RegularTradingEnd(GetYYYYMMDD());

def RegularSessionStart = PremarketTradingHours[1] and RegularTradingHours;
def REgularSessionEnd = RegularTradingHours and PostmarketTradingHours[-1];

# ---- Alternates between 1 and 0 based on whether the day number is even or odd ----
def days = CompoundValue(1,
if GetDay() != GetDay()[1] and SecondsFromTime(1000) <=0
then days[1]+1
else days[1],1);

def EvenOrOdd = if days % 2 == 0 then 1 else 0;

# ---- Formulas that calculate the lines ----
def PriorDayHigh = CompoundValue(1,
if RegularSessionStart and EvenOrOdd == 1
then high
else if RegularTradingHours and EvenOrOdd == 1
then max(high,if PriorDayHigh[1] == 0 then high else PriorDayHigh[1])
else PriorDayHigh[1]
,high);

def PriorDayHigh2 = CompoundValue(1,
if RegularSessionStart and EvenOrOdd == 0
then high
else if RegularTradingHours and EvenOrOdd == 0
then max(high,if PriorDayHigh2[1] == 0 then high else PriorDayHigh2[1])
else PriorDayHigh2[1]
,high);

def PriorDayLow = CompoundValue(1,
if RegularSessionStart and EvenOrOdd == 1
then low
else if RegularTradingHours and EvenOrOdd == 1
then min(low,if PriorDayLow[1] == 0 then low else PriorDayLow[1])
else PriorDayLow[1]
,low);

def PriorDayLow2 = CompoundValue(1,
if RegularSessionStart and EvenOrOdd == 0
then low
else if RegularTradingHours and EvenOrOdd == 0
then min(low,if PriorDayLow2[1] == 0 then low else PriorDayLow2[1])
else PriorDayLow2[1]
,low);

def PriorDayClose = CompoundValue(1,
if RegularSessionEnd and EvenOrOdd == 1
then close
else PriorDayClose[1]
,close);

def PriorDayClose2 = CompoundValue(1,
if RegularSessionEnd and EvenOrOdd == 0
then close
else PriorDayClose2[1]
,close);

# ---- Here we are declaring when to show the line, and the color and style of the lines ----

def PDHigh = if (EvenOrOdd == 0) and days <> 1
then if PremarketTradingHours or
RegularTradingHours or
PostmarketTradingHours
then PriorDayHigh
else Double.NaN
else Double.NaN;

def PDHigh2 = if (EvenOrOdd == 1) and days <> 1
then if PremarketTradingHours or
RegularTradingHours or
PostmarketTradingHours
then PriorDayHigh2
else Double.NaN
else Double.NaN;

def PDLow = if (EvenOrOdd == 0) and days <> 1
then if PremarketTradingHours or
RegularTradingHours or
PostmarketTradingHours
then PriorDayLow
else Double.NaN
else Double.NaN;

def PDLow2 = if (EvenOrOdd == 1) and days <> 1
then if PremarketTradingHours or
RegularTradingHours or
PostmarketTradingHours
then PriorDayLow2
else Double.NaN
else Double.NaN;

def PDClose = if (EvenOrOdd == 0) and days <> 1
then if PremarketTradingHours or
RegularTradingHours or
PostmarketTradingHours
then PriorDayClose
else Double.NaN
else Double.NaN;

def PDClose2 = if (EvenOrOdd == 1) and days <> 1
then if PremarketTradingHours or
RegularTradingHours or
PostmarketTradingHours
then PriorDayClose2
else Double.NaN
else Double.NaN;

plot DailyHigh = if isNan(PDHigh) then PDHigh2 else PDHigh;
plot DailyLow = if isNan(PDLow) then PDLow2 else PDLow;
plot DailyClose = if isNan(PDClose) then PDClose2 else PDClose;
 
Last edited:
Solution
But don't watchlist scripts have a limit to the amount of text/lines of code? Even if it's just slight, could removing the odd and even free up some additional text? I hope you don't mind my asking. Thanks for your help though.

In ToS, the number of lines of code limitation only applies conditional orders.
Charts, Scans, Watchlists can have a unlimited number of lines, without adding complexity.

You can have 2000 lines of code in a watchlist script. Without any complexity.
In your script, what creates complexity is calls to functions.
Function calls can be identified as code that contains parentheses, such as CompoundValue()
While it appears as a simple statement to you. The ToS functions can be extremely complex.

Other...
My ask: Can someone please shorten the below code, so it's even more lean/streamlined? I'm trying to use these references in a 5 min watchlist column as part of a larger study, and once I do, the study becomes too complex. If I can get this somewhat shorter, it will work. Thank you!

Unfortunately no.
The compound value statements cannot be made "more lean/streamlined".
They are complex by definition.
When added to a larger study, it will become too complex for watchlists, scans, and conditional orders.

Do the odd and even day references add complexity?

No, the even/odd references do not add any complexity to the code.
 
Unfortunately no.
The compound value statements cannot be made "more lean/streamlined".
They are complex by definition.
When added to a larger study, it will become too complex for watchlists, scans, and conditional orders.



No, the even/odd references do not add any complexity to the code.
But don't watchlist scripts have a limit to the amount of text/lines of code? Even if it's just slight, could removing the odd and even free up some additional text? I hope you don't mind my asking. Thanks for your help though.

Oddly I’ve seen a script which was on the cusp of getting the too “complex” error go from complex to okay by deleting a # (hashtag statement) or going from a custom color to a predetermined color.
 
But don't watchlist scripts have a limit to the amount of text/lines of code? Even if it's just slight, could removing the odd and even free up some additional text? I hope you don't mind my asking. Thanks for your help though.

In ToS, the number of lines of code limitation only applies conditional orders.
Charts, Scans, Watchlists can have a unlimited number of lines, without adding complexity.

You can have 2000 lines of code in a watchlist script. Without any complexity.
In your script, what creates complexity is calls to functions.
Function calls can be identified as code that contains parentheses, such as CompoundValue()
While it appears as a simple statement to you. The ToS functions can be extremely complex.

Other types of routines that add complexity are recursions and fold statements.
Some script{} subroutines could become complex.

Oddly I’ve seen a script which was on the cusp of getting the too “complex” error go from complex to okay by deleting a # (hashtag statement) or going from a custom color to a predetermined color.

Agreed. PLOT statements and their associated formatting conditions also can push a script that is bordering on complex into too complex.
 
Last edited:
Solution

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
435 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