How to plot for past Real Trading Days based on input?

bmn

Member
Hello, I want to write a script that allows for final plot to calculate/display only for number of Real Trading Days in the past based on an user input (+ current day if it's a Real Trading Day).

e.g.
input howManyDaysInPast = 5;

Chart output: plot high/low of each of the 5 Real Trading Days in the past + Today (if it's a Real Trading Day. If you're running this on a non Real Trading Day, only show most recent 5 Real Trading Days)

So, if you run this on a Monday, it would show H/L for Past F + Past Th + Past W + Past T + Past M + Current M
So, if you run this on a Thursday, it would show H/L for Past W + Past T + Past M + Past F + Past Th + Current Th
If you run this on a Saturday/Sunday, it would show H/L for for Past F + Past Th + Past W + Past T + Past M

p.s. if the current chart in the above example is set to 3D 1h, I hope it's still possible render plot just for the days shown and not get an error. Also, hope there's a way to make sure it works if there're multiple years involved based on most recent Real Trading Day - howManyDaysInPast calculation.
 
Last edited:
Solution
This is WONDERFUL! The use of function is pretty neat. Curious, is it possible to make how many days you want to plot based on a user input (e.g. input daysBack = 2 to plot it for only 2/3 Real Trading Days )?

You can now control the number displayed at the input days_displayed for up to 10 plots.

Capture.jpg
Ruby:
declare upper;

script x {
    input aggregationPeriod = AggregationPeriod.DAY;
    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) ;
    def bar = BarNumber();
    def h  = high(period =...
i'm confused on what you are asking for.
if 5 is picked, do you only want to see candles for the current day and 4 previous days? and somehow hide the previous candles?
the color of the previous candles could be changed, to be the same as the background, so it appears as though they are gone.
or all candles can be turned off and just the most recent 5 days worth are redrawn, with addchart.

are you looking at items that trade on the weekend? the market is closed on the weekend for most items, so there won't be any data on the chart for those 2 days to worry about.
 
i'm confused on what you are asking for.
if 5 is picked, do you only want to see candles for the current day and 4 previous days? and somehow hide the previous candles?
the color of the previous candles could be changed, to be the same as the background, so it appears as though they are gone.
or all candles can be turned off and just the most recent 5 days worth are redrawn, with addchart.

are you looking at items that trade on the weekend? the market is closed on the weekend for most items, so there won't be any data on the chart for those 2 days to worry about.
I want the candles to stay as is. To make it simpler, let's say I want a date label of the X Real Trading Day in the past (for $SPY as an example). If I choose 5 today, the label would display 20210903 (it would exclude weekends and Sept 6th for example).
 
I want the candles to stay as is. To make it simpler, let's say I want a date label of the X Real Trading Day in the past (for $SPY as an example). If I choose 5 today, the label would display 20210903 (it would exclude weekends and Sept 6th for example).

@haIcyonguy, not trying to step in, but I just posted something similar, so hopefully this helps.

There are two charts with the high/lows for today and 4 previous trading days, one with extended and the other not extended , based upon user input selection.

Capture.jpg
Ruby:
declare upper;

script x {
    input aggregationPeriod = AggregationPeriod.DAY;
    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) ;
    def bar = BarNumber();
    def h  = high(period = aggregationPeriod);
    def l  = low(period = aggregationPeriod);

    input count  = 0;
    input extend = yes;

    def hext = if isnan(close) or thisDay < count then hext[1] else h;
    def lext = if isnan(close) or thisDay < count then lext[1] else l;

    plot h_ = if thisDay > count then Double.NaN else if extend == yes then hext else h;
    plot l_ = if thisDay > count then Double.NaN else if extend == yes then lext else l;
}
input extend     = no;
input lineweight = 2;

plot h0 = x(count = 0, extend = extend);
plot h1 = x(count = 1, extend = extend);
plot h2 = x(count = 2, extend = extend);
plot h3 = x(count = 3, extend = extend);
plot h4 = x(count = 4, extend = extend);

plot l0 = x(count = 0, extend = extend).l_;
plot l1 = x(count = 1, extend = extend).l_;
plot l2 = x(count = 2, extend = extend).l_;
plot l3 = x(count = 3, extend = extend).l_;
plot l4 = x(count = 4, extend = extend).l_;

h0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

h0.setdefaultColor(color.cyan);
h1.setdefaultColor(color.cyan);
h2.setdefaultColor(color.cyan);
h3.setdefaultColor(color.cyan);
h4.setdefaultColor(color.cyan);

l0.setdefaultColor(color.magenTA);
l1.setdefaultColor(color.magenTA);
l2.setdefaultColor(color.magenTA);
l3.setdefaultColor(color.magenTA);
l4.setdefaultColor(color.magenTA);

h0.setlineWeight(lineweight);
h1.setlineWeight(lineweight);
h2.setlineWeight(lineweight);
h3.setlineWeight(lineweight);
h4.setlineWeight(lineweight);
l0.setlineWeight(lineweight);
l1.setlineWeight(lineweight);
l2.setlineWeight(lineweight);
l3.setlineWeight(lineweight);
l4.setlineWeight(lineweight);
 
@haIcyonguy, not trying to step in, but I just posted something similar, so hopefully this helps.

There are two charts with the high/lows for today and 4 previous trading days, one with extended and the other not extended , based upon user input selection.
@SleepyZ , your ideas and those from others are always welcome. people approach problems differently and all of us can learn something from anyone.
 
Last edited:
@haIcyonguy, not trying to step in, but I just posted something similar, so hopefully this helps.

There are two charts with the high/lows for today and 4 previous trading days, one with extended and the other not extended , based upon user input selection.
This is WONDERFUL! The use of function is pretty neat. Curious, is it possible to make how many days you want to plot based on a user input (e.g. input daysBack = 2 to plot it for only 2/3 Real Trading Days )?
 
This is WONDERFUL! The use of function is pretty neat. Curious, is it possible to make how many days you want to plot based on a user input (e.g. input daysBack = 2 to plot it for only 2/3 Real Trading Days )?

You can now control the number displayed at the input days_displayed for up to 10 plots.

Capture.jpg
Ruby:
declare upper;

script x {
    input aggregationPeriod = AggregationPeriod.DAY;
    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) ;
    def bar = BarNumber();
    def h  = high(period = aggregationPeriod);
    def l  = low(period = aggregationPeriod);

    input days_displayed = 1;
    input count  = 0;
    input extend = yes;

    def hext = if thisday > days_displayed - 1 or isnan(close) or thisDay < count then hext[1] else h;
    def lext = if thisday > days_displayed - 1 or isnan(close) or thisDay < count then lext[1] else l;

    plot h_ = if thisday> days_displayed - 1 or thisDay > count then Double.NaN else if extend == yes then hext else h;
    plot l_ = if thisday> days_displayed - 1 or thisDay > count then Double.NaN else if extend == yes then lext else l;
}
input days_displayed = 0;
input extend         = yes;
input lineweight     = 2;



plot h0 = x(days_displayed = days_displayed, count = 0, extend = extend).h_;
plot h1 = x(days_displayed = days_displayed, count = 1, extend = extend).h_;
plot h2 = x(days_displayed = days_displayed, count = 2, extend = extend).h_;
plot h3 = x(days_displayed = days_displayed, count = 3, extend = extend).h_;
plot h4 = x(days_displayed = days_displayed, count = 4, extend = extend).h_;
plot h5 = x(days_displayed = days_displayed, count = 5, extend = extend).h_;
plot h6 = x(days_displayed = days_displayed, count = 6, extend = extend).h_;
plot h7 = x(days_displayed = days_displayed, count = 7, extend = extend).h_;
plot h8 = x(days_displayed = days_displayed, count = 8, extend = extend).h_;
plot h9 = x(days_displayed = days_displayed, count = 9, extend = extend).h_;

plot l0 = x(days_displayed = days_displayed, count = 0, extend = extend).l_;
plot l1 = x(days_displayed = days_displayed, count = 1, extend = extend).l_;
plot l2 = x(days_displayed = days_displayed, count = 2, extend = extend).l_;
plot l3 = x(days_displayed = days_displayed, count = 3, extend = extend).l_;
plot l4 = x(days_displayed = days_displayed, count = 4, extend = extend).l_;
plot l5 = x(days_displayed = days_displayed, count = 5, extend = extend).l_;
plot l6 = x(days_displayed = days_displayed, count = 6, extend = extend).l_;
plot l7 = x(days_displayed = days_displayed, count = 7, extend = extend).l_;
plot l8 = x(days_displayed = days_displayed, count = 8, extend = extend).l_;
plot l9 = x(days_displayed = days_displayed, count = 9, extend = extend).l_;

h0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l0.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


h0.setdefaultColor(color.cyan);
h1.setdefaultColor(color.cyan);
h2.setdefaultColor(color.cyan);
h3.setdefaultColor(color.cyan);
h4.setdefaultColor(color.cyan);
h5.setdefaultColor(color.cyan);
h6.setdefaultColor(color.cyan);
h7.setdefaultColor(color.cyan);
h8.setdefaultColor(color.cyan);
h9.setdefaultColor(color.cyan);

l0.setdefaultColor(color.magenTA);
l1.setdefaultColor(color.magenTA);
l2.setdefaultColor(color.magenTA);
l3.setdefaultColor(color.magenTA);
l4.setdefaultColor(color.magenTA);
l5.setdefaultColor(color.magenTA);
l6.setdefaultColor(color.magenTA);
l7.setdefaultColor(color.magenTA);
l8.setdefaultColor(color.magenTA);
l9.setdefaultColor(color.magenTA);

h0.setlineWeight(lineweight);
h1.setlineWeight(lineweight);
h2.setlineWeight(lineweight);
h3.setlineWeight(lineweight);
h4.setlineWeight(lineweight);
h5.setlineWeight(lineweight);
h6.setlineWeight(lineweight);
h7.setlineWeight(lineweight);
h8.setlineWeight(lineweight);
h9.setlineWeight(lineweight);
l0.setlineWeight(lineweight);
l1.setlineWeight(lineweight);
l2.setlineWeight(lineweight);
l3.setlineWeight(lineweight);
l4.setlineWeight(lineweight);
l5.setlineWeight(lineweight);
l6.setlineWeight(lineweight);
l7.setlineWeight(lineweight);
l8.setlineWeight(lineweight);
l9.setlineWeight(lineweight);
 
Solution
Hello,

I am trying to display, via bubbles, the close price of the last three HOD candles.

Can someone please share their expertise and help me with this?
I have the High of the last three HOD candles, but I can't figure out how to display the close price via bubbles.

Thanks!


input ShowTodayOnly = yes;

def today = if !ShowTodayOnly then 1 else GetDay() == GetLastDay();

def h3 = high > Highest(high[-3], 3);
def highLevel = if GetDay() != GetDay()[1] then high else if h3 and high > highLevel[1] then high else highLevel[1];

def l3 = low < Lowest(low[-3], 3);
def lowLevel = if GetDay() != GetDay()[1] then low else if l3 and low < lowLevel[1] then low else lowLevel[1];

plot HL = if !today then Double.NaN else highLevel;
HL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HL.SetDefaultColor(Color.UPTICK);


AddChartBubble(Hl, HL, "H" + HL, Color.pink);
 
Last edited:
Hello,

I am trying to display, via bubbles, the close price of the last three HOD candles.

Can someone please share their expertise and help me with this?
I have the High of the last three HOD candles, but I can't figure out how to display the close price via bubbles.

Thanks!


input ShowTodayOnly = yes;

def today = if !ShowTodayOnly then 1 else GetDay() == GetLastDay();

def h3 = high > Highest(high[-3], 3);
def highLevel = if GetDay() != GetDay()[1] then high else if h3 and high > highLevel[1] then high else highLevel[1];

def l3 = low < Lowest(low[-3], 3);
def lowLevel = if GetDay() != GetDay()[1] then low else if l3 and low < lowLevel[1] then low else lowLevel[1];

plot HL = if !today then Double.NaN else highLevel;
HL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HL.SetDefaultColor(Color.UPTICK);


AddChartBubble(Hl, HL, "H" + HL, Color.pink);
@KevinSammy
add this to the end of your code, to show a bubble with 3 prices in it.

Code:
# assumes chart is day
# ref highLevel so it will work when today only.
# try not to reference plot variables that might be disabled.
def hl9 = highLevel;
AddChartBubble(Hl, HL, "H0:" + round(HL9[0],2) + "\n" + "H1:" + round(HL9[1],2) + "\n" + "H2:" + round(HL9[2],2), Color.pink, yes);

your formulas for finding the high of day are wrong. example,.. on a 5min chart, there are more than 1 high lines on some days.

take a look at this code, that finds the high and low, of several days, and draws lines from them.
high low lines for past 10 days
https://usethinkscript.com/threads/...-trading-days-based-on-input.7904/#post-75740
How to plot for past Real Trading Days based on input?
 
Last edited by a moderator:
add this to the end of your code, to show a bubble with 3 prices in it.

Code:
# assumes chart is day
# ref highLevel so it will work when today only.
# try not to reference plot variables that might be disabled.
def hl9 = highLevel;
AddChartBubble(Hl, HL, "H0:" + round(HL9[0],2) + "\n" + "H1:" + round(HL9[1],2) + "\n" + "H2:" + round(HL9[2],2), Color.pink, yes);

your formulas for finding the high of day are wrong. example,.. on a 5min chart, there are more than 1 high lines on some days.

take a look at this code, that finds the high and low, of several days, and draws lines from them.
high low lines for past 10 days
https://usethinkscript.com/threads/...-trading-days-based-on-input.7904/#post-75740
How to plot for past Real Trading Days based on input?

Thanks, @halcyonguy.

The piece that I’m struggling with is for the last three HOD candles, how to show a bubble that displays the low price for each of these candles.

Then I will try to create a cloud function to draw a cloud from the high to low of these last 3 HOD candles.
 
Last edited by a moderator:

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