input anchor_date = 20210801;
input to_date = 20210831;
input show_label = yes;
AddLabel(show_label, "Anchor", Color.YELLOW);
AddLabel(show_label, AsPrice(anchor_date) + " to " + AsPrice(to_date), Color.YELLOW);
def num_trading_days = AbsValue(CountTradingDays(anchor_date, to_date));
def num_calendar_days = AbsValue(DaysFromDate(anchor_date) - DaysFromDate(to_date) + 1);
AddLabel(show_label, "TDays: " + num_trading_days +
" | CDays: " + num_calendar_days
, Color.YELLOW );
#X/Y (High/Low)-----------------------------------------
AddLabel(show_label, "H/L", Color.WHITE);
def Xamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then high(period = AggregationPeriod.DAY)
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Max(high(period = AggregationPeriod.DAY), Xamt[1])
else Double.NaN;
def X = HighestAll(Xamt);
def X_Date = HighestAll(if high(period = AggregationPeriod.DAY) == X then GetYYYYMMDD() else 0);
def Yamt = if GetYYYYMMDD()[1] < anchor_date and GetYYYYMMDD() >= anchor_date
then low(period = AggregationPeriod.DAY)
else if Between(GetYYYYMMDD(), anchor_date, to_date)
then Min(low(period = AggregationPeriod.DAY), Yamt[1])
else Double.NaN;
def Y = LowestAll(Yamt);
def Y_Date = HighestAll(if low(period = AggregationPeriod.DAY) == Y then GetYYYYMMDD() else Double.NaN);
def min_date = X_Date == Min(Y_Date, X_Date);
AddLabel(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);
AddLabel(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);
def num_trading_days1 = AbsValue(CountTradingDays(Min(Y_Date, X_Date), Max(Y_Date, X_Date)));
def num_calendar_days1 = AbsValue(DaysFromDate(Min(Y_Date, X_Date)) - DaysFromDate(Max(Y_Date, X_Date)) + 1);
AddLabel(show_label, "TDays: " + num_trading_days1 +
" | CDays: " + num_calendar_days1
, Color.WHITE );
# Calendar Days From Date Input----------------------------------
input days = 6;
def date = X_Date;
def yymmdd = date;
def year = Round(date / 10000, 0);
def Month = AbsValue(year - Round(date / 100, 0) / 100) * 100;
def Day = GetDayOfMonth(date);
def daysinmonth = if Month == 2
then 28
else if Month == 4 or Month == 6 or Month == 9 or Month == 11
then 30
else 31;
def newdays = if Month then (Day + days - 1) else newdays[1];
def month_ = if Between(newdays, 1, daysinmonth)
then Month
else if Between(newdays, 1, daysinmonth * 2)
then Month + 1
else if Between(newdays, 1, daysinmonth * 3)
then Month + 2
else if Between(newdays, 1, daysinmonth * 4)
then Month + 3
else month_[1];
def newmonth = if month_ <= 13
then month_
else if month_ <= 25
then month_ - 12
else if month_ > 25
then month_ - 24
else newmonth[1];
def daysinmonth_ = if month_ == 2
then 28
else if month_ == 4 or month_ == 6 or month_ == 9 or month_ == 11
then 30
else 31;
def newyear = if month_ < 13
then year
else if month_ < 24
then year + 1
else if month_ > 24
then year + 2
else newyear[1];
def newday = if (newdays - daysinmonth) == 1
then 1
else if (newdays - daysinmonth) <= 0
then newdays
else if (newdays - daysinmonth * 2) <= 0
then newdays - (daysinmonth * 1)
else if (newdays - daysinmonth * 3) <= 0
then newdays - (daysinmonth * 2)
else if (newdays - daysinmonth * 4) <= 0
then newdays - (daysinmonth * 3)
else newday[1];
AddLabel(1, "Date Input: " + Month + "/" + Day + "/" + AsPrice(year), Color.YELLOW);
AddLabel(1, "Calendar Days Added: " + days, Color.WHITE);
AddLabel(1, "New Calendar Date: " + (if daysinmonth_ == 2 and newday[1] == 29
then 31 else newmonth) + "/"
+ newday + "/"
+ AsPrice(newyear), Color.YELLOW);
input debug = no;
AddLabel(debug, " " + month_ + " " + newdays + " " + daysinmonth + " " + newyear, Color.WHITE);
#New Calendar Date or Nearest Trading Date to it ------------------------------------
def count = if count[1] == 0 and GetYear() == newyear and GetMonth() == month_
then 1
else if GetMonth() == month_ and count[1] >= 1
then count[1] + 1 else 0;
def cdbegin = HighestAll(if count == 1 then GetYYYYMMDD() else 0) ;
def cdday = getdayofmonth(cdbegin);
def newcd = cdbegin + newday - cdday;
input showarrow_new_calendar_date = yes;
plot newcdarrow = if !showarrow_new_calendar_date
then double.nan
else GetYYYYMMDD()[1] < newcd and getyyyYMMDD()>=newcd;
newcdarrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
newcdarrow.setdefaultColor(color.cyan);
newcdarrow.setlineWeight(3);
input showvertical = yes;
addverticalLine(showvertical and newcdarrow," New Calendar Date or Closest Trading Date " + asprice(newcd), color = Color.YELLOW, stroke = Curve.FIRM);
addlabel(debug, asprice(cdbegin + newday - cdday) ,color.white);