_:nb921c128ee8e4d1289cf5fd671be8111xb2
<http://schema.org/text>
"I agree the above code works...The code doesn't work when I use X_date and Y_date ;basically calculating the number of days from high to low.\ndef num_trading_days  = absValue(CountTradingDays(X_Date, Y_Date));\ndef num_calendar_days = absvalue(DaysFromDate(X_Date) - DaysFromDate(Y_Date) + 1);\n\n\nOk, I see what you were asking. Here is a revision that I think does the original and the new request. I have reorganized it to put anchor info first, then high/low. The minimum date of high/low will appear before the latest.\n\nSame minimum chart days have to at least cover the trading days, but if that does not work try covering the calendar days .\n \n\ninput anchor_date     = 20210801;\ninput to_date         = 20210831;\ninput show_label      = yes;\n\nAddLabel(show_label, \"Anchor\", Color.YELLOW);\nAddLabel(show_label, AsPrice(anchor_date) + \" to \" + AsPrice(to_date), Color.YELLOW);\n\ndef num_trading_days  = AbsValue(CountTradingDays(anchor_date, to_date));\ndef num_calendar_days = AbsValue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);\n\nAddLabel(show_label, \"TDays: \" + num_trading_days  +\n              \" | CDays: \" + num_calendar_days\n              , Color.YELLOW );\n\n#X/Y (High/Low)-----------------------------------------\nAddLabel(show_label, \"H/L\", Color.WHITE);\ndef Xamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date\n              then high(period=aggregationPeriod.DAY)\n              else if Between(GetYYYYMMDD(), anchor_date, to_date)\n              then Max(high(period=aggregationPeriod.DAY), Xamt[1])\n              else Double.NaN;\ndef X       = HighestAll(Xamt);\ndef X_Date  = HighestAll(if high(period=aggregationPeriod.DAY) == X then GetYYYYMMDD() else 0);\n\ndef Yamt    = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date\n              then low(period=aggregationPeriod.DAY)\n              else if Between(GetYYYYMMDD(), anchor_date, to_date)\n              then Min(low(period=aggregationPeriod.DAY), Yamt[1])\n              else Double.NaN;\ndef Y       = LowestAll(Yamt);\ndef Y_Date  = highestAll(if low(period=aggregationPeriod.DAY)  == Y then GetYYYYMMDD() else double.nan);\n\ndef min_date =  X_date==Min(Y_Date, X_Date);\nAddLabel(show_label, if min_date==0 then \"L: \" + AsText(Y) + \"  Y_Date: \" + AsPrice(Y_Date) else \"H: \" + AsText(X) + \"  H_Date: \" + AsPrice(X_Date), Color.WHITE);\nAddLabel(show_label, if min_date==0 then \"H: \" + AsText(X) + \"  X_Date: \" + AsPrice(X_Date) else \"L: \" + AsText(Y) + \"  L_Date: \" + AsPrice(Y_Date), Color.WHITE);\n\ndef num_trading_days1  = AbsValue(CountTradingDays(Min(Y_Date, X_Date), Max(Y_Date, X_Date)));\ndef num_calendar_days1 = AbsValue(DaysFromDate(Min(Y_Date, X_Date)) - DaysFromDate(Max(Y_Date, X_Date)) + 1);\n\nAddLabel(show_label, \"TDays: \" + num_trading_days1  +\n              \" | CDays: \" + num_calendar_days1\n              , Color.WHITE );"
<https://usethinkscript.com/threads/count-trading-days-from-the-current-bar-to-today.870/>