Get Date/Time of last Swing High/Low

Cyph3r

New member
I've tried a lot of things and nothing is working for getting the date/time of the most recent swing high/low. This is to pass to InertiaAll/StDevAll for auto plotting a linear regression channel from that startdate and starttime (parameters) to the end of the chart. Seems only constants can be passed for those parameters.

Help please :)
 
Solution
I've tried a lot of things and nothing is working for getting the date/time of the most recent swing high/low. This is to pass to InertiaAll/StDevAll for auto plotting a linear regression channel from that startdate and starttime (parameters) to the end of the chart. Seems only constants can be passed for those parameters.

Help please :)

This may help by using an InertiaAll workaround for startdate and starttime by using price, constrained by those criteria.

There are two options, manual and auto, to create the regression channel.
Manual will use the date and time input.
Auto will use the last swing based upon the length input

Code:
#Get_Date/Time_of_Swing_High/Low_to_Plot_InertiaAll_using_Workaround

input length = 20...
Enjoy!


Code:
declare upper;

input strength = 20;

# --- Swing Detection ---
def swingHigh =
    high[strength] == Highest(high, strength * 2 + 1);

def swingLow =
    low[strength] == Lowest(low, strength * 2 + 1);

# --- Time Components ---
def sec = SecondsFromTime(0)[strength];
def hh = Floor(sec / 3600);
def mm = Floor((sec % 3600) / 60);

# --- Swing High Bubble ---
AddChartBubble(
    swingHigh,
    high,
    AsText(GetYYYYMMDD()[strength]) + " " +
    (if hh < 10 then "0" else "") + AsText(hh) + ":" +
    (if mm < 10 then "0" else "") + AsText(mm),
    Color.RED,
    yes
);

# --- Swing Low Bubble ---
AddChartBubble(
    swingLow,
    low,
    AsText(GetYYYYMMDD()[strength]) + " " +
    (if hh < 10 then "0" else "") + AsText(hh) + ":" +
    (if mm < 10 then "0" else "") + AsText(mm),
    Color.GREEN,
    no
);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I've tried a lot of things and nothing is working for getting the date/time of the most recent swing high/low. This is to pass to InertiaAll/StDevAll for auto plotting a linear regression channel from that startdate and starttime (parameters) to the end of the chart. Seems only constants can be passed for those parameters.

Help please :)

This may help by using an InertiaAll workaround for startdate and starttime by using price, constrained by those criteria.

There are two options, manual and auto, to create the regression channel.
Manual will use the date and time input.
Auto will use the last swing based upon the length input

Code:
#Get_Date/Time_of_Swing_High/Low_to_Plot_InertiaAll_using_Workaround

input length = 20;
def n  = length;#hint n: Lookback/Forward periods for finding swing highs, lows.
def bn = BarNumber();
def na = Double.NaN;

def h = high;
def l = low;

#Added >= or <= instead of just > or < in forward looking swings to create more swings
#Swings can help identify Manual inputs for Regression Channel
def swinghigh = if h >= Highest(h, n)[-n] and h > Highest(h, n)[1] then 1 else 0;
def swinglow  = if l <= Lowest(l, n)[-n] and l < Lowest(l, n)[1] then 1 else 0;

plot ph = if swinghigh then h else 0;
ph.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
plot pl = if swinglow then l else 0;
pl.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#-----------------------------------------------------------------------------------------------

#******************************************
#LinearRegression_Workaround using derived swing high/low date/time or manually input date/time # *******************************************

input Swing_method  = {default Auto, Manual};
input manual_date = 20260107;
input manual_time = 945;
def ymd        = GetYYYYMMDD(); # Today's Date
def swing_date = HighestAll(if ph or pl then GetYYYYMMDD() else Double.NaN);
def swing_time = HighestAll(if ph or pl then GetTime() else Double.NaN);

def date;
def time;
def bar;
switch (Swing_method) {
case Manual:
    date = manual_date;
    time = manual_time;
    bar = if ymd == date and SecondsFromTime(time)[1] < 0 and SecondsFromTime(time) >= 0 then bn else Double.NaN;
case Auto:
    date = swing_date;
    time = swing_time;
    bar = if ymd == date and GetTime() == time then bn else Double.NaN;
}

input distance1        = .5;
input distance2        = 1;
input show_label       = yes;
input show_vertical    = yes;
input test_data        = yes;

plot price  = if bn < HighestAll(bar)
             then 0 else close; # Limit Linear Regression to Close > 0

# Formula Based Linear Regression
plot x = if bn < HighestAll(bar) then 0 else InertiaAll(data = price, extendToRight = yes);

def dist1 = HighestAll(AbsValue(x - price)) * distance1;
plot UpperLR = x + dist1;
plot LowerLR = x - dist1;
UpperLR.SetDefaultColor(Color.RED);
LowerLR.SetDefaultColor(Color.GREEN);

def dist2 = HighestAll(AbsValue(x - price)) * distance2;
plot UpperLR2 = x + dist2;
plot LowerLR2 = x - dist2;
UpperLR2.SetDefaultColor(Color.RED);
LowerLR2.SetDefaultColor(Color.GREEN);

#************Display of Date & Time of Swing Used *****************
def hr = HighestAll(if bn == HighestAll(bar) then Floor(((9 * 60 + 30) + (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000) / 60) else Double.NaN);
def min = HighestAll(if bn == HighestAll(bar) then (GetTime() - RegularTradingStart(GetYYYYMMDD())) / 60000 - ((hr - 9) * 60 + 30) + 60 else Double.NaN);
def direction = if x > x[1] then 1 else 0;

AddLabel(show_label, Swing_method + " Regression, Direction " + (if direction == 1 then " Up" else " Down ") + "   " + AsPrice(date) + "     " +  hr + ":" +    (if min < 10 then "0" else "") + min , Color.WHITE);
AddVerticalLine(show_vertical and bn == HighestAll(bar), "           " + AsPrice(date) + "     " + hr + ":" +    (if min < 10 then "0" else "") + min, Color.WHITE);

# End Code
Screenshot 2026-01-08 061018.png
 
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1005 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