Use GetValue() to reference Barnumber based on Date/Time?

RustyKayaker

New member
VIP
For a multi-day, intra-day chart, such as 5-90 day, 5-min to 4-hour chart, I want to start a trendline at a certain Date/Time (associated with a bar start time) and end at another Date/Time.

I’ve done trendlines like this for a single day, such as a 5-min chart. I can control which day (out of the last X days) and which time the plot begins and ends. By referencing the Day and the Session Start time, I convert times to a bar count and count bars based on GetAggregationPeriod(). So, this works whatever bar time increment (aggregation) I choose. The attached chart has a curving trendline on 6/18/26.

I’ve also written code like this for daily charts to increment* the trendline based on calendar days, starting on a particular date, but that results in a big jump on Mondays (and even more on 3-day weekends). This is not good. I am aware of CountTradingDays(fromDate, toDate), but I had trouble calculating a toDate (an input toDate worked, but not a calculated value, even using GetYYYYMMDD()). But, this is not necessary if barnumbers are used.

(*The values are actually a ratio times the beginning value.)

For multi-day, intra-day charts, it seems the best way is to use barnumbers. Then I can calculate an incremental* change per bar and I won’t have to worry about holidays either. I’ve been able to plot simple barnumber() on a lower chart. But, I need to set the start of the trendline to a barnumber to equate to zero for calculation purposes. Conceptually this is easily done by identifying the barnumber of the start Date/Time. But, how to do that?

I have found an example of using GetValue(barnumber(), offset) in
https://usethinkscript.com/threads/xstream-regime-bands-for-thinkorswim.22196/
and an example of GetValue(len[gth]), i) and GetValue(GetTime(), i) in
https://usethinkscript.com/threads/...-relative-volume-indicator.20284/#post-149345

I’m not sure how to create an offset (to find the barnumber) based on an input Date/Time for starting and ending the trendline. So, I need help here. I appreciate anything you can provide. I don’t need the whole code, just the snippet of how to reference the barnumber for a certain date/time similar to those referenced above.
And, this needs to work if I change the chart interval from 90 days to 30 days, which will cause the barnumbers to change, but not the begin and end date/time.

In the attached chart, my end-of-day time is 1400 because I’m in the Mountain Time zone.
TOS_CHART, NDX 5min, 260618.png
 
Solution
Are you just drawing it for the sake of drawing it, or is this a segment of a more complicated script that will have to access the line for additional purposes? That changes the approach. This will cover the former. For the later, we can get into that deeper if need be, but I'd rather not write it unnecessarily.

Ruby:
@Date input StartDate =
            20260617;
      input StartTime =
            1000;
@Date input EndDate =
            20260618;
      input EndTime =
            1135;
      input DataPoint =
            Low;
      input RightExpansion =
            Yes;
      def   Date =
            GetYYYYMMDD();
      def   StartFound =
            Date == StartDate &&
           (!SecondsFromTime(StartTime)
            or (...
Are you just drawing it for the sake of drawing it, or is this a segment of a more complicated script that will have to access the line for additional purposes? That changes the approach. This will cover the former. For the later, we can get into that deeper if need be, but I'd rather not write it unnecessarily.

Ruby:
@Date input StartDate =
            20260617;
      input StartTime =
            1000;
@Date input EndDate =
            20260618;
      input EndTime =
            1135;
      input DataPoint =
            Low;
      input RightExpansion =
            Yes;
      def   Date =
            GetYYYYMMDD();
      def   StartFound =
            Date == StartDate &&
           (!SecondsFromTime(StartTime)
            or (
                 SecondsFromTime(StartTime) < 0
                 && SecondsFromTime(StartTime)[-1] > 0
               )
            );
      def   EndFound =
            Date == EndDate &&
           (!SecondsFromTime(EndTime)
            or (
                 SecondsFromTime(EndTime) < 0
                 && SecondsFromTime(EndTime)[-1] > 0
               )
            );
      def   SanStart =
            if (!SanStart[1], StartFound, SanStart[1]);
      def   SanEnd =
            if (!SanEnd[1], EndFound, SanEnd[1]);
      def   sP =
            if (StartFound, DataPoint, sP[1]);
      def   sC =
            if (StartFound, 1, sC[1] + 1);
      def   eP =
            if (EndFound, DataPoint, eP[1]);
      def   Slope =
            if (EndFound, (ep - sp) / sc, Slope[1]);
      def   RightEx =
            if !RightEx[1] and EndFound[1] then DataPoint[1] + slope
            else if RightEx[1] <> 0 then RightEx[1] + slope
            else 0;
      plot  TrendLine =
            If StartFound or EndFound
            then DataPoint
            else if RightExpansion && RightEx then RightEx
            else Double.NaN;
            TrendLine.EnableApproximation();
            TrendLine.setdefaultColor(color.cyan);
            TrendLine.SetlineWeight(5);
 
            AddLabel(!SanStart, "start point not found",color.orange);
            AddLabel(!SanEnd, "end point not found",color.orange);

On daily, you can set it right from the chart by right-clicking bars. They haven't added a similar capability for time though. You can still set the date this way intra-day. The time must be manually entered.

d8SMmeA.png
 
Last edited:
Solution
Joshua,

I am still going through the code you sent to understand it all. But, the key piece was connecting a specific Date/Time to a starting bar count of zero. This was in:

@Date
Code:
input StartDate =
20260617;
input StartTime =
1200;
** which set the Start Date/Time

def StartFound =
Date == StartDate &&
(!SecondsFromTime(StartTime)
or (
SecondsFromTime(StartTime) < 0
&& SecondsFromTime(StartTime)[-1] > 0
)
);

** which connects the Start Date/Time to the corresponding bar, even if the Time input is not an increment of the aggregation period.

def sC =
If (StartFound, 0, sC[1] + 1);
** which starts and continues the bar counter. (I changed the starting value from 1 to 0.)

This is the missing piece I needed. I can take it from here. Yes, I will use this in a script that uses the starting point and then calculates a curving trendline, so I won’t need the End Point for the calculations. (Yet I need the End Date/Time for the purpose of stopping the plot for visual reasons.)

The calculated values are going to be used to compare with a smoothed price to trigger orders under Conditional Orders.

Also, I realized that this will apply to any time scale (aggregation period) without modification because it simply tracks by bar number and that will not change the input Start Date/Time.

Thank you so much! I would have not figured this out on my own. You are amazing!

Sincerely,
Paul
 
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
819 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