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!
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: