#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