# cycles_periods_lines_01
#https://usethinkscript.com/threads/thinkscript-for-cycle-brackets.4891/#post-119704
def na = double.nan;
def bn = barnumber();
# valid price ? price enable
def pr_en = !isnan(close);
def d = getday();
def w = getweek();
def m = getmonth();
def daycurrent = d == getlastday();
def daynew = d != d[1];
def weeknew = w != w[1];
def monthnew = m != m[1];
# period counts, used by % formulas
def daycnt = if bn == 1 then 1
else if daynew then daycnt[1] + 1
else daycnt[1];
def weekcnt = if bn == 1 then 1
else if weeknew then weekcnt[1] + 1
else weekcnt[1];
def monthcnt = if bn == 1 then 1
else if monthnew then monthcnt[1] + 1
else monthcnt[1];
#-------------------------
input period = {default Day, Week, Month };
input qty = 3;
#def charttime = GetAggregationPeriod();
#def en = if period >= charttime then 1 else 0;
#----------------------------
# pick color
# https://usethinkscript.com/threads/format-watchlist-text-in-thinkorswim.4045/page-3#post-67014
# this doesn't assign the color in quotes.
# the location of the picked color in the series, is changed to a number
# ex. pink is the 3rd number. 3 is saved in color1_choice
input Color1_choice = {default "magenta", "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "yellow", "white"};
#AddLabel(yes, "test1 " + Color1_choice , GetColor(Color1_Choice));
#---------------------------
input line_type = {default LINE, dashes, POINTS};
addlabel(1, " ", color.black);
addlabel(1, qty, color.yellow);
addlabel(1, period, color.yellow);
addlabel(1, " ", color.black);
# getday is a day # that includes weekends... so doesn't work for this
#def xd = (d % qty) == 0;
#def xd = if qty == 1 then 1 else ((daycnt % qty) == 0);
def xd = ((daycnt % qty) == 0);
def xw = ((weekcnt % qty) == 0);
def xm = ((monthcnt % qty) == 0);
def xd2 = if qty == 1 then daynew else (!xd[1] and xd);
def xw2 = if qty == 1 then weeknew else (!xw[1] and xw);
def xm2 = if qty == 1 then monthnew else (!xm[1] and xm);
def x = if period == period.day then xd2
else if period == period.week then xw2
else if period == period.month then xm2
else 0;
def p = pr_en and x;
AddVerticalLine(p, "-", GetColor(Color1_Choice),
(if line_type == line_type.POINTS then PaintingStrategy.POINTS
else if line_type == line_type.dashes then PaintingStrategy.dashes
else PaintingStrategy.LINE));
#--------------------------
# test stuff
input test1 = no;
addchartbubble(test1, low,
daycnt + "\n" +
xd + "\n" +
weekcnt + "\n" +
xw + "\n" +
monthcnt + "\n" +
xm + "\n"
, color.yellow, no);
# change plot type , kind of works
#input line_type = {default LINE, dashes, POINTS};
#input test3 = no;
#addverticalline(test3 and daycurrent, "z", color.white,
#(if line_type == line_type.POINTS then PaintingStrategy.POINTS
# else if line_type == line_type.dashes then PaintingStrategy.dashes
# else PaintingStrategy.LINE));
# https://usethinkscript.com/threads/create-a-horizontal-drawing-then-remove-it-once-price-hits-it.5257/#post-75706
# comments at end of study, regarding choosing a painting type
#
# paint a line , choose type
#input lines = {default LINE, POINTS};
#def paint = if lines == lines.POINTS then PaintingStrategy.POINTS else PaintingStrategy.LINE;
# plot x1 =
#x1.SetPaintingStrategy(paint);
#