Future Averages' Crosses For ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
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

Gnm3HVs.jpeg


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));
#
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
203 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top