this is an upper study to experiment with.
it calculates the slope of both averages and estimates when they might cross, after the last bar.
can pick a max number of bars in the future to look for a cross (default is 11)
if a number of 11 or less is on the last bar, a vertical line is drawn x bars in the future.
a label shows how many bars to a cross.
bubbles show the quantity of bars to a cross.
golden_cross_future
http://tos.mx/!EmwbWhBi
it calculates the slope of both averages and estimates when they might cross, after the last bar.
can pick a max number of bars in the future to look for a cross (default is 11)
if a number of 11 or less is on the last bar, a vertical line is drawn x bars in the future.
a label shows how many bars to a cross.
bubbles show the quantity of bars to a cross.
golden_cross_future
http://tos.mx/!EmwbWhBi
Code:
#golden_cross_future
# halcyonguy
# 24-08-01
#https://www.reddit.com/r/thinkorswim_scripts/comments/1eglqu7/approaching_golden_crossover/
def na = double.nan;
def bn = barnumber();
def data = close;
def lastbar = (!isnan(close) and isnan(close[-1]));
input cross_future_bars = 11;
#input avg1_type = AverageType.exponential;
input avg1_type = AverageType.Simple;
input avg1_length = 50;
def avg1 = MovingAverage(avg1_type, data, avg1_length );
input avg2_type = AverageType.Simple;
input avg2_length = 200;
def avg2 = MovingAverage(avg2_type, data, avg2_length );
input show_avg1_line = yes;
input show_avg2_line = yes;
plot zavg1 = if show_avg1_line then avg1 else na;
zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();
plot zavg2 = if show_avg2_line then avg2 else na;
zavg2.SetDefaultColor(Color.yellow);
zavg2.setlineweight(1);
zavg2.hidebubble();
#def xup = avg1 crosses above avg2;
#def xdwn = avg1 crosses below avg2;
# price change, bar to bar
def avg1chg = avg1 - avg1[1];
def avg2chg = avg2 - avg2[1];
# if barz > 0 then est of bars to a crossing
# if barz < 0 then avg lines are spreading , no future cross
def barz;
if avg1 > avg2 then {
# find when a1 < a2 . derive a formula
# start with these
#def t1 = avg1 + (bars * avg1chg)
#def t2 = avg2 + (bars * avg2chg)
# then combine and solve for bars
# (avg1 + (bars * avg1chg) < (avg2 + (bars * avg2chg))
# ((avg1-avg2) + (bars * avg1chg) < (bars * avg2chg)
# ((avg1-avg2) < (bars * avg2chg) - (bars * avg1chg)
# ((avg1-avg2) < (bars * (avg2chg - avg1chg))
# (avg1-avg2)/(avg2chg - avg1chg) < bars
barz = (avg1-avg2)/(avg2chg - avg1chg);
} else if avg1 < avg2 then {
barz = (avg2-avg1)/(avg1chg - avg2chg);
} else {
barz = 0;
}
def barz2 = if barz > 0 and barz < 1 then 1 else round(barz,0);
input test1 = yes;
addchartbubble(test1, min(avg1,avg2),
barz2 + "\n"
, color.yellow, no);
# find a future bar of possible cross. draw vert line
def future_cross;
if bn == 1 then {
future_cross = 0;
} else if (lastbar and barz2 > 0 and barz2 < cross_future_bars) then {
future_cross = barz2;
} else {
future_cross = future_cross[1];
}
def x = ((future_cross > 0) and !isnan(getvalue(close, (future_cross+1))) and isnan(getvalue(close, future_cross)));
addverticalline(x, "cross", color.cyan);
addlabel(1,
(if barz2 > 0 and barz2 < 51 then "est cross in " + barz2 + " bars"
else "no near future crossing"),
(if future_cross > 0 then color.yellow
else if barz2 > 0 and barz2 < 51 then color.gray
else color.magenta));
#