Date Math and display the date on the chart

For example :

to_date=20211008
X=10 days
Need to add 10 days to to_date and display the date on the chart;
If possible ; Even if the date is in the future
 
Solution
If I wanted to get get the date X-Trading Days in the past from today (excluding today), how can I go about doing that? I tried messing around with +/- etc, but no luck.

e.g. for today (20210910), I want to get the trading date 5 trading days ago (20210902 - due to 3 non-trading days in between), how would I achieve that?

This looks for the last trading day for the days you input the prior trading date.

Capture.jpg
Ruby:
def ymd      = GetYYYYMMDD();
def candles  = !isnan(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;

input daysback  = 5;
def today = 1;
def last_trading_day =...
Thank you for the update.

I'm trying to run the following example from jshingler.

Code:
input start = 0930; #Start of RTH for most securities
input end = 1600; #End of RTH for most securities
def toPaint = SecondsFromTime(start) >= 0 and SecondsTillTime(end) > 0;
def up = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then LowestAll(low) else Double.NaN;
AddCloud(up, down, Color.RED);

You see how it has start and end are hardcoded. I'm trying to make that dynamic and paint the cloud for ANY securities (some futures have different start/end RTH e.g. \CL). When I add the following on top "def RTHstart = GetTime() crosses above RegularTradingStart(getYYYYMMDD());" and use that in line #3 "def toPaint = SecondsFromTime(RTHstart) >= 0" it throws an error. Do you know how I can get \CL's "start" so that I can use it in SecondsFromTime?
Perhaps I was not clear, the fix was in the above code that should work. See below highlighted portion

def RTHstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD());
def RTHEnd = GetTime() crosses above RegularTradingEnd(GetYYYYMMDD());
def ExtEnd = GetTime() crosses below RegularTradingEnd(GetYYYYMMDD());
plot x = RTHstart or SecondsFromTime(0930) == 0;
x.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
AddVerticalLine(RTHstart or SecondsFromTime(0930) == 0, "RTH Hours Begin", Color.CYAN, Curve.FIRM);
AddVerticalLine(RTHEnd or SecondsFromTime(1600) == 0, "RTH Hours End", Color.CYAN, Curve.FIRM);
addVerticalLine(ExtEnd , "Extended Hours End", color.cyan, curve.firm);
 
Perhaps I was not clear, the fix was in the above code that should work. See below highlighted portion

def RTHstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD());
def RTHEnd = GetTime() crosses above RegularTradingEnd(GetYYYYMMDD());
def ExtEnd = GetTime() crosses below RegularTradingEnd(GetYYYYMMDD());
plot x = RTHstart or SecondsFromTime(0930) == 0;
x.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
AddVerticalLine(RTHstart or SecondsFromTime(0930) == 0, "RTH Hours Begin", Color.CYAN, Curve.FIRM);
AddVerticalLine(RTHEnd or SecondsFromTime(1600) == 0, "RTH Hours End", Color.CYAN, Curve.FIRM);
addVerticalLine(ExtEnd , "Extended Hours End", color.cyan, curve.firm);
Yep, I get that part and seems to work. But when i try to implement this into a cloud study by jshingler, I'm getting an error on line # 4. Assuming it's because RTHstart does not equate to "0930" for normal ticker so datatype mismatch. Is there a way to make it work so that it would use dynamically generated RTHStart??

#input start = 0930;
input end = 1030;
def RTHstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD());
def toPaint = SecondsFromTime(RTHstart) >= 0 and SecondsTillTime(end) > 0;
def up = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then LowestAll(low) else Double.NaN;
AddCloud(up, down, Color.RED);
 
Yep, I get that part and seems to work. But when i try to implement this into a cloud study by jshingler, I'm getting an error on line # 4. Assuming it's because RTHstart does not equate to "0930" for normal ticker so datatype mismatch. Is there a way to make it work so that it would use dynamically generated RTHStart??

#input start = 0930;
input end = 1030;
def RTHstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD());
def toPaint = SecondsFromTime(RTHstart) >= 0 and SecondsTillTime(end) > 0;
def up = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then LowestAll(low) else Double.NaN;
AddCloud(up, down, Color.RED);
i'm not quite sure what you are trying to end up with... automatically determine when different markets open and close? not sure if that is possible.
here is a study i made, to highlight different market sessions, even if they overlap midnight But you still have to type in the start and stop times. Maybe this will give you ideas and maybe reading other posts alongside this will help.
https://usethinkscript.com/threads/average-daily-range-indicator-for-thinkorswim.244/#post-66296
 
  • Like
Reactions: bmn
Yep, I get that part and seems to work. But when i try to implement this into a cloud study by jshingler, I'm getting an error on line # 4. Assuming it's because RTHstart does not equate to "0930" for normal ticker so datatype mismatch. Is there a way to make it work so that it would use dynamically generated RTHStart??

#input start = 0930;
input end = 1030;
def RTHstart = GetTime() crosses above RegularTradingStart(GetYYYYMMDD());
def toPaint = SecondsFromTime(RTHstart) >= 0 and SecondsTillTime(end) > 0;
def up = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then LowestAll(low) else Double.NaN;
AddCloud(up, down, Color.RED);

In the above RTHstart == 1 and did not comport with secondsfromtime syntax. I found that it is easier to do this a little different for your cloud code. Also, def down was compressing the chart so I would recommend using double.negative_infinity instead of lowestall(low).

The following picture uses 2 charts, AFL, which had the problems above, and /CL, which uses a different start time than 0930, and both work okay.

Capture.jpg

Ruby:
input end    = 1030;
def RTHstart = if GetTime() > RegularTradingStart(GetYYYYMMDD()) then BarNumber() else Double.NaN;
def rthend   = if SecondsFromTime(end) <= 0 then BarNumber() else Double.NaN;
def toPaint  = Between(BarNumber(), RTHstart, rthend);
def up   = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then Double.NEGATIVE_INFINITY else Double.NaN;
AddCloud(up, down, Color.RED);
 
  • Like
Reactions: bmn
In the above RTHstart == 1 and did not comport with secondsfromtime syntax. I found that it is easier to do this a little different for your cloud code. Also, def down was compressing the chart so I would recommend using double.negative_infinity instead of lowestall(low).

The following picture uses 2 charts, AFL, which had the problems above, and /CL, which uses a different start time than 0930, and both work okay.

This simpler version seems to also work.

Code:
input end    = 1030;
def toPaint  = GetTime() > RegularTradingStart(GetYYYYMMDD()) and SecondsfromTime(end) <= 0 ;
def up   = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then Double.negative_INFINITY else Double.NaN;
AddCloud(up, down, Color.RED);
 
  • Like
Reactions: bmn
# RTH High Timestamp Watchlist # tomsk # 12.22.2019 # Make sure you run this at 1 minute aggregation declare hide_on_daily; def Hrs = Floor(9.5 + SecondsFromTime(0930) / 60 / 60) - 1; def Min = ((9.5 + SecondsFromTime(930) / 60 / 60) % 1) * 60; def active = SecondsFromTime(0930) >= 0 and SecondsTillTime(1600) >= 0; def H = if !IsNaN(high) then high else H[1]; def RTHH = if active and !active[1] then H else if active and H > RTHH[1] then H else RTHH[1]; def HighHH = if H == RTHH then Hrs else HighHH[1]; def HighMM = if H == RTHH then Min else HighMM[1]; AddLabel(1, HighHH + ":" + HighMM, Color.Yellow); # End RTH High Timestamp Watchlist
How would I change this code for a daily chart to identify the YYYYMMDD of high and low?
 
Last edited by a moderator:
How would I change this code for a daily chart to identify the YYYYMMDD of high and low?
You cannot use that code for a daily chart as it uses intraday timeframes.

The following should provide bubbles and labels for identifying the high/low on a daily chart.

Capture.jpg
Ruby:
def hd =  GetMaxValueOffset(high, 254);
def hdate = if BarNumber() == HighestAll(BarNumber() - hd)
            then GetYYYYMMDD()
            else hdate[1];
AddLabel(yes, "H: " + AsPrice(hdate), Color.LIGHT_GREEN);
AddChartBubble(BarNumber() == HighestAll(BarNumber() - hd), high, "H: " + AsPrice(hdate), Color.LIGHT_GREEN);
def ld =  GetMinValueOffset(low, 254);
def ldate = if BarNumber() == HighestAll(BarNumber() - ld)
            then GetYYYYMMDD()
            else ldate[1];
AddChartBubble(BarNumber() == HighestAll(BarNumber() - ld), low, "L: " + AsPrice(ldate), Color.LIGHT_RED, no);
AddLabel(yes, "L: " + AsPrice(ldate), Color.LIGHT_RED);
 
@halcyonguy : I mean like, RTH Start and End of any ticker. I haven't checked from a different timezone, but are you saying that $AMD, for example, would have different RTH Start/End in UK vs US and so the RegularTradingStart/End fx wouldn't work? Btw, thanks for the link to the other study (appreciate you taking time to comment the blocks of code too).

@SleepyZ : that's PERFECT! just one other thing, say I want to end the cloud 60 mins past RTH Start (instead of specifying the end time), how would I go about doing that? the following line doesn't work and it's probably because of the reason you mentioned.

def toPaint = GetTime() => RegularTradingStart(GetYYYYMMDD()) and GetTime() <= RegularTradingStart(GetYYYYMMDD()) + (60*60*1000) ;

Edit: one more question. is there a way to prevent clouds from drawing in future/extended area on the right?
 
Last edited:
@halcyonguy : I mean like, RTH Start and End of any ticker. I haven't checked from a different timezone, but are you saying that $AMD, for example, would have different RTH Start/End in UK vs US and so the RegularTradingStart/End fx wouldn't work? Btw, thanks for the link to the other study (appreciate you taking time to comment the blocks of code too).

@SleepyZ : that's PERFECT! just one other thing, say I want to end the cloud 60 mins past RTH Start (instead of specifying the end time), how would I go about doing that? the following line doesn't work and it's probably because of the reason you mentioned.

def toPaint = GetTime() => RegularTradingStart(GetYYYYMMDD()) and GetTime() <= RegularTradingStart(GetYYYYMMDD()) + (60*60*1000) ;

Edit: one more question. is there a way to prevent clouds from drawing in future/extended area on the right?

You're welcome.

Great adaption to 'topaint'. I made a minor modification to it as follows and it seems to work. You can input the minutes you want to extend the cloud at the input minutes. Also added is the regulartradingend to prevent clouds from extending into the right expansion area.

Ruby:
input minutes = 60;

def toPaint = if GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
                 GetTime() <= RegularTradingStart(GetYYYYMMDD()) + minutes * 60 * 1000 and
                 gettime()<= regulartradingEnd(getyyyYMMDD())
              then 1 else Double.NaN;

def up   = if toPaint then Double.POSITIVE_INFINITY else Double.NaN;
def down = if toPaint then Double.NEGATIVE_INFINITY else Double.NaN;
AddCloud(up, down, Color.RED);
 
  • Like
Reactions: bmn
You're welcome.

Great adaption to 'topaint'. I made a minor modification to it as follows and it seems to work. You can input the minutes you want to extend the cloud at the input minutes. Also added is the regulartradingend to prevent clouds from extending into the right expansion area.
THANK YOU!

Very clean code and works perfectly with time duration input.

The only thing I notice is that it still paints on the right/extended area (currently draws clouds for Thu/Fri/Mon on my 3m chart). Could you please check when you've some time? Maybe adding one more condition in "if" like "and open == open" might do the trick, but that looks like a hack and not sure if it would work universally, even if you're looking at chart during offhours/days. Thanks again.

Edit: one other thing I thought of. Can the study add one more param, e.g. input coverHLOnly = false, where false = it draws the clouds from top to bottom like it's doing now and if it's true, then it draws box shaped clouds just between highest and lowest price within that range?
 
Last edited:
THANK YOU!

Very clean code and works perfectly with time duration input.

The only thing I notice is that it still paints on the right/extended area (currently draws clouds for Thu/Fri/Mon on my 3m chart). Could you please check when you've some time? Maybe adding one more condition in "if" like "and open == open" might do the trick, but that looks like a hack and not sure if it would work universally, even if you're looking at chart during offhours/days. Thanks again.

Edit: one other thing I thought of. Can the study add one more param, e.g. input coverHLOnly = false, where false = it draws the clouds from top to bottom like it's doing now and if it's true, then it draws box shaped clouds just between highest and lowest price within that range?

I was not able to see the extended cloud problem with my watchlist symbols. Please provide more specific info so I can see what is happening.

See if this is what you wanted for the cloud option

Ruby:
input coverHLOnly = no;
input minutes     = 60;

def toPaint = if GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
                 GetTime() <= RegularTradingStart(GetYYYYMMDD()) + minutes * 60 * 1000 and
                 GetTime() <= RegularTradingEnd(GetYYYYMMDD())
              then 1 else Double.NaN;

def up      = if toPaint
              then if coverHLOnly == no
                   then Double.POSITIVE_INFINITY
                   else Highestall(high)       
              else Double.NaN;
def down    = if toPaint
              then if coverHLOnly == no
                   then Double.NEGATIVE_INFINITY
                   else lowestall(low)
              else Double.NaN;
AddCloud(up, down, Color.RED);
 
I was not able to see the extended cloud problem with my watchlist symbols. Please provide more specific info so I can see what is happening.

See if this is what you wanted for the cloud option

This is another option on the high/low instead of infinity. It will plot a cloud at the high/low of each days range rather than the highestall/loweestaall, high/low of the chart.

Capture.jpg
Ruby:
input coverHLOnly = no;
input minutes     = 60;


def toPaint = if GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
                 GetTime() <= RegularTradingStart(GetYYYYMMDD()) + minutes * 60 * 1000 and
                 GetTime() <= RegularTradingEnd(GetYYYYMMDD())
              then 1 else Double.NaN;

def aftermarket =  !isnan(topaint);
def bars   = minutes;

input pricePerRowHeightMode = { AUTOMATIC, default TICKSIZE, CUSTOM};
input customRowHeight = 1.0;
input timePerProfile = {default BAR};
input onExpansion = no;
input profiles = 1000;

def period;

switch (timePerProfile) {
case BAR:
    period = BarNumber() - 1;
}


def count = CompoundValue(1, if aftermarket and period != period[1] then (count[1] + period - period[1]) % bars else count[1], 0);
def cond = count < count[1] + period - period[1];
def height;
switch (pricePerRowHeightMode) {
case AUTOMATIC:
    height = PricePerRow.AUTOMATIC;
case TICKSIZE:
    height = PricePerRow.TICKSIZE;
case CUSTOM:
    height = customRowHeight;
}

profile vol = VolumeProfile("startNewProfile" = cond, "onExpansion" = no, "numberOfProfiles" = 1000, "pricePerRow" = height, "value area percent" = 0);
def con = CompoundValue(1, onExpansion, no);

def hProfile = if aftermarket and IsNaN(vol.GetHighest()) and con then hProfile[1] else vol.GetHighest();
def lProfile = if aftermarket and IsNaN(vol.GetLowest()) and con then lProfile[1] else vol.GetLowest();
def plotsDomain = IsNaN(close) == onExpansion;
def ProfileHigh = if aftermarket and plotsDomain then hProfile else Double.NaN;
def ProfileLow = if aftermarket and plotsDomain then lProfile else Double.NaN;

def hrange = ProfileHigh;
def lrange = ProfileLow;


def up      = if toPaint
              then if coverHLOnly == no
                   then Double.POSITIVE_INFINITY
                   else hrange#Highestall(high)       
              else Double.NaN;
def down    = if toPaint
              then if coverHLOnly == no
                   then Double.NEGATIVE_INFINITY
                   else lrange#lowestall(low)
              else Double.NaN;
AddCloud(up, down, Color.RED);
 
I was not able to see the extended cloud problem with my watchlist symbols. Please provide more specific info so I can see what is happening.

See if this is what you wanted for the cloud option

Thanks for the quick update.

Please refer to the following image:
spx on 3m tf

Also, regarding the coverHLOnly, I was thinking more like, the box cloud covering H/L of those individual ranges. Like for today (with 30 mins range), 4360.39 - 4383.27 would be the range for cloud's top/bottom boundaries. for previous RTDay, the range would be 4353.72 - 4394.87. Would that be possible?
 
Thanks for the quick update.

Please refer to the following image:
spx on 3m tf

Also, regarding the coverHLOnly, I was thinking more like, the box cloud covering H/L of those individual ranges. Like for today (with 30 mins range), 4360.39 - 4383.27 would be the range for cloud's top/bottom boundaries. for previous RTDay, the range would be 4353.72 - 4394.87. Would that be possible?

I got rid of the expansion by using getday<=getlastday limiter. SPX seemed to be the only one with that happening.

As SPX does not have volume, what was posted in #52 using volume profile would not work for SPX. It worked for everything else that has volume.

Nonetheless, here is something that should work for almost everything, including SPX. I was able to have the cloud high for it to match your high figure, but not the lows. I do not know where you got the lows at this time. This code uses offsets to find the highest/lowest, highs/lows at the last bar of the minute timeframe, and offset it to the beginning of the regularhourstart.

Capture.jpg

Ruby:
input coverHLOnly = no;
input minutes     = 30;

def toPaint = if GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
                 GetTime() <= RegularTradingStart(GetYYYYMMDD()) + minutes * 60 * 1000 and
                 GetTime() <= RegularTradingEnd(GetYYYYMMDD())
              then 1 else 0;

def dayh    = if toPaint[1] == 0 and toPaint == 1
              then high
              else if toPaint == 1
              then Max(high, dayh[1])
              else 0;
def dayhi   = if toPaint[1] == 1 and toPaint == 0
              then dayh[1]
              else dayhi[1];
def dayl    = if toPaint[1] == 0 and toPaint == 1
              then low
              else if toPaint == 1
              then Min(low, dayl[1]) else 0 ;
def daylo   = if toPaint[1] == 1 and toPaint == 0
              then dayl[1]
              else daylo[1];

def up      = if GetDay() <= GetLastDay() and toPaint
              then if coverHLOnly == no
                   then Double.POSITIVE_INFINITY
                   else dayhi[-minutes / (GetAggregationPeriod() / 60000)]       
              else Double.NaN;
def down    = if GetDay() <= GetLastDay() and toPaint
              then if coverHLOnly == no
                   then Double.NEGATIVE_INFINITY
                   else daylo[-minutes / (GetAggregationPeriod() / 60000)]         
              else Double.NaN;
AddCloud(up, down, Color.RED);
 
  • Like
Reactions: bmn
I got rid of the expansion by using getday<=getlastday limiter. SPX seemed to be the only one with that happening.

As SPX does not have volume, what was posted in #52 using volume profile would not work for SPX. It worked for everything else that has volume.

Nonetheless, here is something that should work for almost everything, including SPX. I was able to have the cloud high for it to match your high figure, but not the lows. I do not know where you got the lows at this time. This code uses offsets to find the highest/lowest, highs/lows at the last bar of the minute timeframe, and offset it to the beginning of the regularhourstart.
Thanks @SleepyZ . I was looking at chart with Heikin Ashi Chart Type instead of candles. After switching to candles, it synced.

Do you think the following code is more efficient or the one you wrote for day/h/l or makes no difference?

rec dayhi= if dayhi[1] == 0 or toPaint[1] == 0 and toPaint == 1 then high else if toPaint and high > dayhi[1] then high else dayhi[1];
rec daylo= if daylo[1] == 0 or toPaint[1] == 0 and toPaint == 1 then low else if toPaint and low < daylo[1] then low else daylo[1];
 
Last edited:
Thanks @SleepyZ . I was looking at chart with Heikin Ashi Chart Type instead of candles. After switching to candles, it synced.

Do you think the following code is more efficient or the one you wrote for day/h/l or makes no difference?

rec dayhi= if dayhi[1] == 0 or toPaint[1] == 0 and toPaint == 1 then high else if toPaint and high > dayhi[1] then high else dayhi[1];
rec daylo= if daylo[1] == 0 or toPaint[1] == 0 and toPaint == 1 then low else if toPaint and low < daylo[1] then low else daylo[1];
Anytime you can eliminate code and it still works, do so. Nice!
Just def instead of rec would be better. Rec was combined into def years ago.
 
  • Like
Reactions: bmn
@SleepyZ , back with one additional query. Is it possible to plot OLHC lines (until the end of the trading day) for the bar with highest price, the bar with the lowest price, and the bar with highest volume within the duration range?
 
@SleepyZ , back with one additional query. Is it possible to plot OLHC lines (until the end of the trading day) for the bar with highest price, the bar with the lowest price, and the bar with highest volume within the duration range?

The folllowing code provides all of the above. In testing, with the highest volume code included, SPX did not plot until it was removed. You could use a separate copy of the code for SPX without the highest volume code.

Capture.jpg
Ruby:
input coverHLOnly = no;
input minutes     = 30;

def toPaint = if GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
                 GetTime() <= RegularTradingStart(GetYYYYMMDD()) + minutes * 60 * 1000 and
                 GetTime() <= RegularTradingEnd(GetYYYYMMDD())
              then 1 else 0;

def dayhi = if dayhi[1] == 0 or toPaint[1] == 0 and toPaint == 1 then high else if toPaint and high > dayhi[1] then high else dayhi[1];
def daylo = if daylo[1] == 0 or toPaint[1] == 0 and toPaint == 1 then low else if toPaint and low < daylo[1] then low else daylo[1];

def up      = if GetDay() <= GetLastDay() and toPaint
              then if coverHLOnly == no
                   then Double.POSITIVE_INFINITY
                   else dayhi[-minutes / (GetAggregationPeriod() / 60000)]       
              else Double.NaN;
def down    = if GetDay() <= GetLastDay() and toPaint
              then if coverHLOnly == no
                   then Double.NEGATIVE_INFINITY
                   else daylo[-minutes / (GetAggregationPeriod() / 60000)]         
              else Double.NaN;
AddCloud(up, down, Color.RED);

#Cloud Data at High/Low and Highest Volume Bars to RTHend------------------------

def rthend =    GetTime() >= RegularTradingStart(GetYYYYMMDD()) + minutes * 60 * 1000 and GetTime() <= RegularTradingEnd(GetYYYYMMDD());
#High Bar Data-------------------------------------------
def hbn  = if high == dayhi then BarNumber() else hbn[1];
def hblo = if BarNumber() == hbn then low else hblo[1];
def hbop = if BarNumber() == hbn then open else hbop[1];
def hbcl = if BarNumber() == hbn then close else hbcl[1];
plot hbl = if rthend == 1  then hblo else 0;
plot hbh = if rthend == 1 then dayhi else 0;
plot hbo = if rthend == 1 then hbop else 0;
plot hbc = if rthend == 1 then hbcl else 0;
hbl.setpaintingStrategy(paintingStrategy.DASHES);
hbh.setpaintingStrategy(paintingStrategy.DASHES);
hbo.setpaintingStrategy(paintingStrategy.DASHES);
hbc.setpaintingStrategy(paintingStrategy.DASHES);
hbl.setdefaultColor(color.cyan);
hbh.setdefaultColor(color.magenta);
hbo.setdefaultColor(color.white);
hbc.setdefaultColor(color.yellow);
#Low Bar Data-------------------------------------------------------
def lbn  = if low == daylo then BarNumber() else lbn[1];
def lbhi = if BarNumber() == lbn then high else lbhi[1];
def lblo = if BarNumber() == lbn then low else lblo[1];
def lbop = if BarNumber() == lbn then open else lbop[1];
def lbcl = if BarNumber() == lbn then close else lbcl[1];
plot lbl = if rthend == 1 then lblo else 0;
plot lbh = if rthend == 1 then lbhi else 0;
plot lbo = if rthend == 1 then lbop else 0;
plot lbc = if rthend == 1 then lbcl else 0;
lbl.setpaintingStrategy(paintingStrategy.DASHES);
lbh.setpaintingStrategy(paintingStrategy.DASHES);
lbo.setpaintingStrategy(paintingStrategy.DASHES);
lbc.setpaintingStrategy(paintingStrategy.DASHES);
lbl.setdefaultColor(color.cyan);
lbh.setdefaultColor(color.magenta);
lbo.setdefaultColor(color.white);
lbc.setdefaultColor(color.yellow);
#Highest Volumee Bar Data-------------------------------------------------------
def dayvol = if dayvol[1] == 0 or toPaint[1] == 0 and toPaint == 1 then volume else if toPaint and volume > dayvol[1] then volume else dayvol[1];
def vbn  = if volume == dayvol then BarNumber() else vbn[1];
def vbhi = if BarNumber() == vbn then high else vbhi[1];
def vblo = if BarNumber() == vbn then low else vblo[1];
def vbop = if BarNumber() == vbn then open else vbop[1];
def vbcl = if BarNumber() == vbn then close else vbcl[1];
plot vbl = if rthend == 1 then vblo else 0;
plot vbh = if rthend == 1 then vbhi else 0;
plot vbo = if rthend == 1 then vbop else 0;
plot vbc = if rthend == 1 then vbcl else 0;
vbl.setpaintingStrategy(paintingStrategy.DASHES);
vbh.setpaintingStrategy(paintingStrategy.DASHES);
vbo.setpaintingStrategy(paintingStrategy.DASHES);
vbc.setpaintingStrategy(paintingStrategy.DASHES);
vbl.setdefaultColor(color.cyan);
vbh.setdefaultColor(color.magenta);
vbo.setdefaultColor(color.white);
vbc.setdefaultColor(color.yellow);
 
  • Like
Reactions: bmn
This looks for the last trading day for the days you input the prior trading date.
Hi @SleepyZ! Could you help me with my code? The date of the previous day was not enough for my code and I took this part in your script. And script is working. But TOS tell me that my Script is Complex and i do not know how to fix this.

My code measure relative strength of stock in % compared to SPX.

declare lower;
input correlationWithSecurity = "SPX";

def last_trading_day = HighestAll(if !IsNaN(close) and IsNaN(close[-1]) then GetYYYYMMDD() else Double.NaN);
def referenceDate = last_trading_day -1;

def closeTwo = close(correlationWithSecurity);
def rs = if closeTwo == 0 then 0 else close / closeTwo;
def startDate = DaysFromDate(referencedate) == 0;
def sr = CompoundValue("historical data" = rs[0], "visible data" = if startDate <= 0 then sr[1] else rs[0]);
def percent_change = Round((rs - sr) * 100 / sr, 1);

AddLabel(1, " RS: " + percent_change + " %", if percent_change < 0 then Color.PINK else Color.DARK_GREEN);
al.jpg
 

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