#digital_root
#------------------------
# halcyonguy
# 24-01-10
#------------------------
#https://usethinkscript.com/threads/digital-root-equals-time-and-price.20226/
#Digital Root Equals Time and Price
#abundant1 1/10
# write a code that,
# marks when time and price digital roots are equal.
#------------------------
declare lower;
def na = double.nan;
def bn = barnumber();
def base = 10;
# restrict price data to dollars
def cls_n = Round(close, 0);
#------------------------
# input a number and base, calc digital root
script digroot{
input n = 0;
input base = 10;
def r1 = ((n % power(base,1)) - (n % power(base,0)))/power(base,0);
def r2 = ((n % power(base,2)) - (n % power(base,1)))/power(base,1);
def r3 = ((n % power(base,3)) - (n % power(base,2)))/power(base,2);
def r4 = ((n % power(base,4)) - (n % power(base,3)))/power(base,3);
def r5 = ((n % power(base,5)) - (n % power(base,4)))/power(base,4);
def r6 = ((n % power(base,6)) - (n % power(base,5)))/power(base,5);
def r7 = ((n % power(base,7)) - (n % power(base,6)))/power(base,6);
def r8 = ((n % power(base,8)) - (n % power(base,7)))/power(base,7);
def rttl = r1+r2+r3+r4+r5+r6+r7+r8;
plot z = rttl;
}
#------------------------
# price root
def price_root1 = digroot(cls_n);
#plot z1 = price_root1;
def price_root2 = if price_root1 < 10 then price_root1 else digroot(price_root1);
plot z2 = price_root2;
z2.SetDefaultColor(Color.yellow);
#------------------------
# time root
# hours_minutes HHMM
# calc HHMM from elapsed minutes
# adjust for time zone
#https://usethinkscript.com/threads/vertical-line-with-time.15159/#post-124472
#sleepyz
input timezone = {default "ET", "CT", "MT", "PT"};
def hour_offset = (if timezone == timezone."ET" then 0
else if timezone == timezone."CT" then -1
else if timezone == timezone."MT" then -2
else -3);
input time_data = { default hours_minutes , minutes };
def start = 0000;
def tmin1 = SecondsFromTime(start)/60;
def thour1 = tmin1/60;
def thour2 = floor(thour1);
def tmin2 = (thour1 - thour2)*60;
def thour3 = (thour2 + hour_offset);
def thour4 = if thour3 > 0 then thour3 else thour3 + 11;
def time_hhmm = (thour4*100)+tmin2;
#---------------------
# time - HHMM
def time_hhmm_root1 = digroot(time_hhmm);
def time_hhmm_root2 = if time_hhmm_root1 < 10 then time_hhmm_root1 else digroot(time_hhmm_root1);
#--------------------
# time - minutes
def time_min_root1 = digroot(tmin1);
def time_min_root2 = if time_min_root1 < 10 then time_min_root1 else digroot(time_min_root1);
#--------------------
def time_rootx;
def t;
switch(time_data) {
case hours_minutes:
time_rootx = time_hhmm_root2;
t = 1;
case minutes:
time_rootx = time_min_root2;
t = 2;
}
#--------------------
plot z3 = time_rootx;
z3.SetDefaultColor(Color.cyan);
#--------------------
def root_match = (price_root2 == time_rootx);
#root_match
plot zm = if root_match then price_root2*0.92 else na;
zm.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zm.SetDefaultColor(Color.cyan);
zm.setlineweight(3);
zm.hidebubble();
#--------------------
addlabel(1, "price root " + price_root2 + " ", z2.takevaluecolor());
addlabel(1, "time root " + time_rootx + " ", z3.takevaluecolor());
addlabel(1, (if t == 2 then "minutes" else "HHMM"), z3.takevaluecolor());
#--------------------
input test_bubbles1 = no;
addchartbubble(test_bubbles1, 0,
"price\n" +
cls_n + "\n" +
price_root2
, z2.takevaluecolor(), no);
addchartbubble(test_bubbles1 and t == 1, 0,
"time HHMM\n" +
thour4 + ":" + tmin2 + "\n" +
time_hhmm + " hm\n" +
time_hhmm_root1 + " r1\n" +
time_hhmm_root2 + " r2"
, z3.takevaluecolor(), no);
addchartbubble(test_bubbles1 and t == 2, 0,
"time minutes\n" +
tmin1 + " min\n" +
time_min_root1 + " r1\n" +
time_min_root2 + " r2\n"
, z3.takevaluecolor(), no);
#------------------------
# ref
#https://en.wikipedia.org/wiki/Digital_root#:~:text=The%20digital%20root%20(also%20repeated,to%20compute%20a%20digit%20sum.
#