compute starting date, 1 year from the current date

tron

New member
VIP
@SleepyZ always appreciate your support on this site. I'm trying to dynamically compute a starting date that is 1 year from the current date (as opposed to having the start date be a statically defined input) and pass it into InertiaAll but I'm having no luck. Any help would be greatly appreciated.

input startdate = 20231014;
plot MiddleLR = if GetYYYYMMDD() > enddate then Double.NaN else InertiaAll(Fundamental(price, period = agg), startDate = startdate, startTime = starttime, length = length);
 
Solution
@SleepyZ always appreciate your support on this site. I'm trying to dynamically compute a starting date that is 1 year from the current date (as opposed to having the start date be a statically defined input) and pass it into InertiaAll but I'm having no luck. Any help would be greatly appreciated.

input startdate = 20231014;
plot MiddleLR = if GetYYYYMMDD() > enddate then Double.NaN else InertiaAll(Fundamental(price, period = agg), startDate = startdate, startTime = starttime, length = length);

This is a workaround of the need for a 'constant' limitation in the linear regression channel (LRC).
This uses the 'def price' to limit the data by your 1 year date requirement without having to manually input the date from a...
@SleepyZ always appreciate your support on this site. I'm trying to dynamically compute a starting date that is 1 year from the current date (as opposed to having the start date be a statically defined input) and pass it into InertiaAll but I'm having no luck. Any help would be greatly appreciated.

input startdate = 20231014;
plot MiddleLR = if GetYYYYMMDD() > enddate then Double.NaN else InertiaAll(Fundamental(price, period = agg), startDate = startdate, startTime = starttime, length = length);

This is a workaround of the need for a 'constant' limitation in the linear regression channel (LRC).
This uses the 'def price' to limit the data by your 1 year date requirement without having to manually input the date from a year ago in the LRC's inertiaall function.
The year ago date needs to be visible on the chart to properly draw the LRC
A label displays the year ago date used in the 'def price'.
The test lines and data will help confirm the alignment of the auto and manual lines as well as the difference between the standard LRC, unmodified.

As today's date 10/14/2024 was not a trading day in 2023, the image shows the overlapping auto and manual LRC lines drawn based upon the next trading day, 10/16/2023, as computed by the auto formula. The dashed line is the standard, unmodified LRC, which draws and computes from the 1st bar on the chart.

Screenshot 2024-10-14 122135.jpg
Code:
#LinearRegression_Workaround_Auto_1_YearAgo

def ymd     = GetYYYYMMDD(); # Today's Date
def py      = if IsNaN(close[-1]) and !IsNaN(close) then ymd - 10000 else Double.NaN; # Date 1 Year Ago
def bn      = BarNumber();

input startdate        = 20231016;
input show_label       = yes;
input test_lines       = yes;
input test_data        = no;
input show_LR_Auto     = yes;
input show_LR_Manual   = yes;
input show_LR_Standard = yes;

plot price  = if ymd < HighestAll(py)
              then 0 else close; # Limit Linear Regression to Close > 0
price.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
price.SetHiding(!test_data);

# Formula Based Linear Regression
plot x = InertiaAll(price, extendToRight = yes);
x.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
x.SetHiding(!show_LR_Auto);

#Label
def pbar   = if ymd[1] <= HighestAll(py) and ymd > HighestAll(py) then bn else 0;
def pdate  = if bn == HighestAll(pbar) then ymd else 0;

AddLabel(show_label ,
if HighestAll(pdate) == 0
then "1 Year Date Ago not Visible on Chart "
else "1 Year Ago == " + AsPrice(HighestAll(pdate)), Color.WHITE);

#Test Lines and Data ################################################

# Manual Based Linear Regression
plot y = InertiaAll(close, startDate = startdate);
# Standard Based Linear Regression
plot z = InertiaAll(close);

y.SetPaintingStrategy(PaintingStrategy.POINTS);
z.SetStyle(Curve.LONG_DASH);
y.SetHiding(!test_lines);
z.SetHiding(!test_lines);

AddLabel(test_data, HighestAll(pbar));
AddVerticalLine(test_data and bn == HighestAll(pbar), HighestAll(pbar), Color.WHITE);

# End Code
 
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
310 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