plot MA1 = moving average 1;
plot MA2 = moving average 2;
def CX = if MA1...
plot MA1 = moving average 1;
plot MA2 = moving average 2;
def CX = if MA1 crosses MA2 then 1 else 0;
def value_at_cx = if CX == 1 then (MA1 + MA1[1]) / 2 else value_at_cx[1];
AddLabel(yes, " value at last cx: " + value_at_cx + " ", color.blue);
plot MA1 = moving average 1;
plot MA2 = moving average 2;
def CX = if MA1 crosses MA2 then 1 else 0;
def value_at_cx = if CX == 1 then (((MA1 + MA1[1]) / 2) + ((MA2 + MA2[1]) / 2)) / 2 else value_at_cx[1];
AddLabel(yes, " value at last cx: " + value_at_cx + " ", color.blue);
As shown in the image above, I'm looking to pull the exact price where these two lines intersect. Is this possible to do?
As of right now, I'm stuck using the next day's midpoint value and I need the exact intersection for more accurate analysis.
As shown in the image above, I'm looking to pull the exact price where these two lines intersect. Is this possible to do?
# crossing_price_01
# find price where 2 lines cross
# use the slopes of 2 lines, between 2 bars
# crossing is signaled after it happens, on the next bar
def na = double.nan;
def bn = barnumber();
#def lastbar = !isnan(close[0]) and isnan(close[-1]);
# test data -------------------------------------
def price = close;
input MA1_len = 7;
input MA1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input MA2_len = 15;
input MA2_type = AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
input plot_signal_lines = yes;
plot z1 = ma1;
plot z2 = ma2;
z1.SetDefaultColor(Color.cyan);
z2.SetDefaultColor(Color.yellow);
z1.SetHiding(!plot_signal_lines);
z2.SetHiding(!plot_signal_lines);
# ===============================
# assign signals to generic names
def sig1 = ma1;
def sig2 = ma2;
# ===============================
def x = sig1 crosses sig2;
input draw_triangle_on_crossings = yes;
plot zx = if x and draw_triangle_on_crossings then low*0.998 else na;
zx.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
zx.SetDefaultColor(Color.cyan);
zx.setlineweight(3);
zx.hidebubble();
# price levels
input draw_crossing_before_after_levels = yes;
plot sig1_before = if x[-1] then sig1[0] else if x then sig1[1] else na;
plot sig1_after = if x[-1] then sig1[-1] else if x then sig1[0] else na;
plot sig2_before = if x[-1] then sig2[0] else if x then sig2[1] else na;
plot sig2_after = if x[-1] then sig2[-1] else if x then sig2[0] else na;
sig1_before.SetDefaultColor(Color.cyan);
sig1_after.SetDefaultColor(Color.cyan);
sig2_before.SetDefaultColor(Color.yellow);
sig2_after.SetDefaultColor(Color.yellow);
sig1_before.SetHiding(!draw_crossing_before_after_levels);
sig1_after.SetHiding(!draw_crossing_before_after_levels);
sig2_before.SetHiding(!draw_crossing_before_after_levels);
sig2_after.SetHiding(!draw_crossing_before_after_levels);
#---------------------------------------------------------------------------
# find x , some % of a slope, between 2 bars,
# for which the same x, produces the same $$ in both signals
# sig1 start + (slope1 * x) = sig2 start + ( slope2 * x)
# (sig1-sig2) = (slope2 * x) - (slope1 * x)
# (sig1-sig2) = (slope2 - slope1) * x
# (sig1 start - sig2 start) / (slope2 - slope1) = x = slope_factor
def slope1 = if x[0] then sig1[0] - sig1[1] else na;
def slope2 = if x[0] then sig2[0] - sig2[1] else na;
def slope_factor = (sig1[1] - sig2[1]) / (slope2 - slope1);
def fac_pr = (slope1 * slope_factor);
def xprice = sig1[1] + fac_pr;
def recent_cross_lvl = if x then xprice else recent_cross_lvl[1];
addlabel(1, " ", color.black);
addlabel(1, "recent crossing price " + round(recent_cross_lvl,2) , color.yellow);
# plot line at cross level
input show_white_line_at_crossing = yes;
plot xxx = if x[-1] then xprice[-1] else if x[0] then xprice[0] else na;
xxx.SetDefaultColor(Color.white);
xxx.SetHiding(!show_white_line_at_crossing);
input crossing_price_bubbles = yes;
addchartbubble(crossing_price_bubbles and x, xprice,
round(xprice, 2)
, color.yellow, no);
# --------------------------------
input test1 = no;
addchartbubble(test1 and x[0], max(sig1,sig2)*0.999,
ma1[1] + " 1\n" +
slope1 + " s1\n" +
slope_factor + " fac\n" +
fac_pr + " fac$\n" +
xprice + "\n" +
ma2[1] + " 2\n" +
slope2 + " s2\n"
, color.yellow, no);
input test2 = no;
addchartbubble(test2 and x[0], max(sig1,sig2)*0.999,
sig1_before + "\n" +
sig1_after + "\n" +
"--" + "\n" +
sig2_before + "\n" +
sig2_after
, color.yellow, no);
#
Thank you so much for this! It's exactly what I was looking for. And very easy to toggle on/off by the way. Nicely done.this will find the price where 2 lines cross.
it uses the slopes of 2 lines, between 2 adjacent bars.
it draws several things. all can be turned off,
. a white line at the crossing level.
. displays a bubble with the price.
. a label with the price of the recent crossing.
. a cyan triangle below the bar, after a crossing.
. shows 4 horizontal lines, for the start and stop price levels, of the 2 slope lines.
there are comments in the code describing how i came up with the formula.
this example uses 2 averages for signals.
you can copy this to the end of your study.
then look for the below code, and replace ma1 and ma2 , with your variables.
# ===============================
# assign signals to generic names
def sig1 = ma1;
def sig2 = ma2;
# ===============================
Code:# crossing_price_01 # find price where 2 lines cross # use the slopes of 2 lines, between 2 bars # crossing is signaled after it happens, on the next bar def na = double.nan; def bn = barnumber(); #def lastbar = !isnan(close[0]) and isnan(close[-1]); # test data ------------------------------------- def price = close; input MA1_len = 7; input MA1_type = AverageType.EXPONENTIAL; def ma1 = MovingAverage(ma1_type, price, ma1_len); input MA2_len = 15; input MA2_type = AverageType.EXPONENTIAL; def ma2 = MovingAverage(ma2_type, price, ma2_len); input plot_signal_lines = yes; plot z1 = ma1; plot z2 = ma2; z1.SetDefaultColor(Color.cyan); z2.SetDefaultColor(Color.yellow); z1.SetHiding(!plot_signal_lines); z2.SetHiding(!plot_signal_lines); # =============================== # assign signals to generic names def sig1 = ma1; def sig2 = ma2; # =============================== def x = sig1 crosses sig2; input draw_triangle_on_crossings = yes; plot zx = if x and draw_triangle_on_crossings then low*0.998 else na; zx.SetPaintingStrategy(PaintingStrategy.TRIANGLES); zx.SetDefaultColor(Color.cyan); zx.setlineweight(3); zx.hidebubble(); # price levels input draw_crossing_before_after_levels = yes; plot sig1_before = if x[-1] then sig1[0] else if x then sig1[1] else na; plot sig1_after = if x[-1] then sig1[-1] else if x then sig1[0] else na; plot sig2_before = if x[-1] then sig2[0] else if x then sig2[1] else na; plot sig2_after = if x[-1] then sig2[-1] else if x then sig2[0] else na; sig1_before.SetDefaultColor(Color.cyan); sig1_after.SetDefaultColor(Color.cyan); sig2_before.SetDefaultColor(Color.yellow); sig2_after.SetDefaultColor(Color.yellow); sig1_before.SetHiding(!draw_crossing_before_after_levels); sig1_after.SetHiding(!draw_crossing_before_after_levels); sig2_before.SetHiding(!draw_crossing_before_after_levels); sig2_after.SetHiding(!draw_crossing_before_after_levels); #--------------------------------------------------------------------------- # find x , some % of a slope, between 2 bars, # for which the same x, produces the same $$ in both signals # sig1 start + (slope1 * x) = sig2 start + ( slope2 * x) # (sig1-sig2) = (slope2 * x) - (slope1 * x) # (sig1-sig2) = (slope2 - slope1) * x # (sig1 start - sig2 start) / (slope2 - slope1) = x = slope_factor def slope1 = if x[0] then sig1[0] - sig1[1] else na; def slope2 = if x[0] then sig2[0] - sig2[1] else na; def slope_factor = (sig1[1] - sig2[1]) / (slope2 - slope1); def fac_pr = (slope1 * slope_factor); def xprice = sig1[1] + fac_pr; def recent_cross_lvl = if x then xprice else recent_cross_lvl[1]; addlabel(1, " ", color.black); addlabel(1, "recent crossing price " + round(recent_cross_lvl,2) , color.yellow); # plot line at cross level input show_white_line_at_crossing = yes; plot xxx = if x[-1] then xprice[-1] else if x[0] then xprice[0] else na; xxx.SetDefaultColor(Color.white); xxx.SetHiding(!show_white_line_at_crossing); input crossing_price_bubbles = yes; addchartbubble(crossing_price_bubbles and x, xprice, round(xprice, 2) , color.yellow, no); # -------------------------------- input test1 = no; addchartbubble(test1 and x[0], max(sig1,sig2)*0.999, ma1[1] + " 1\n" + slope1 + " s1\n" + slope_factor + " fac\n" + fac_pr + " fac$\n" + xprice + "\n" + ma2[1] + " 2\n" + slope2 + " s2\n" , color.yellow, no); input test2 = no; addchartbubble(test2 and x[0], max(sig1,sig2)*0.999, sig1_before + "\n" + sig1_after + "\n" + "--" + "\n" + sig2_before + "\n" + sig2_after , color.yellow, no); #
KHC day chart 8/16
hal_cross
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Start a new thread and receive assistance from our community.
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.
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.