The
end point of the perpendicular "tine" lines is a bit abstract. It
is the "slope" of the line that is important, so the end point could be
just a few bars away, or it could be at the last bar of the chart, or
it could be off the chart, as long as the rise/run remains equal to the
needed slope.
Load this code into the qscript
editor and carefully review the last section. Be sure to set 2
hotspots in the script properties.
My use of a
variable named "span" shows how changing the end point of the
perpendicular line can be helpful, all the while maintaining the
correct line slope. The tricky part was simply solving the slope
equation to give the new "Y" value of the end point.
Let me know what questions you might have, and then try to do the code to draw the second tine, from hotspot 2.
# Todd example
# 2 hot spots for initial swing
# hot spot price is snapped to the bar H or L, whichever is nearer
# trend line is drawn from hot spot 1 to hot spot 2
# then draw lines from each hotspot, perpendicular to the trendline
input Lines(".5"),MainColor(red),span(150);
If (barnum==barsback)
{
# step 1 draw the initial trend line from hot spot A to hot spot B
# get chart price and bar values of hotspots
barA = hotspot_to_bar(1)-1; # set the starting bar for trendline
barback=barnum_to_offset(barA);
High1=High[barback];
Low1=Low[barback];
price1 = hotspot_to_price(1);
if( (high1-price1 < price1-low1)) { priceA=high1; } # snap to bar H/L price
if( (high1-price1 > price1-low1)) { priceA=low1; }
trefA=text(barA-2,priceA,priceA,MainColor,tx_right,10); # display hot spot price
barB = hotspot_to_bar(2)-1; # set the ending bar for trendline
barback=barnum_to_offset(barB);
High2=High[barback];
Low2=Low[barback];
price2 = hotspot_to_price(2);
if( (high2-price2 < price2-low2)) { priceB=high2; } # snap to bar H/L price
if( (high2-price2 > price2-low2)) { priceB=low2; }
trefB=text(barB-2,priceB,priceB,MainColor,tx_right,10); # display hot spot price
refAB=trendline(barA,priceA,barB,priceB,MainColor); # draw swing trend line
# prepare to draw additional lines using "canvas" plots, for geometric accuracy
#get pixel values of first hotspot / snap spot
x1=bar_to_pixel(barA+1);
y1=price_to_pixel(priceA);
#get pixel values of the second hotspot / snap spot
x2=bar_to_pixel(barB+1);
y2=price_to_pixel(priceB);
rise=y1-y2; # get slope of primary trendline, note that the delta y values are inverse to price,
# prices
go UP from bottom of chart to top of chart, while "y" values go DOWN
# as the
computer screen coordinates are laid out upper left to lower right
increments
run=x2-x1;
#print " rise/run = ", rise/run, " ",;
degree=atan2(rise,run);
#print " at angle ",degree;
# the perpendicular of m1 slope is m2 = -
1/m1 this is the slope needed to draw lines
perpendicular to AB
newSlope= -(1/(rise/run));
#print "Perpendicular line slope is ",newslope;
# next step --
draw right angle trend lines from hot spots A and B (and midpoint)
to end of chart
# use the
perpendicular slope to draw lines from the A & B points --
slope= rise/run from points A & B
# y2 - y1 = m * (x2 - x1) using the
perp.slope as m, and known x1 & y1 from points A & B
# so
calculate the needed rise to make the perp. slope correct from
run point
# y2 = y1 + m(x2-x1) to get the y2 intercept point for the perp. line
# e.g. if perp slope = -1.25 then y2= y1 + -1.25(x2-x1)
# to draw Perp. line from X1, think of the "run", (x2-x1), as any "span"
# so y2 = y1 + m(span)
canvas_pencolor(MainColor) ;
canvas_movepen(x1,y1); # set the start point at snap 1
############################################################################
xRun= x1+span; # any value for the
run may be used-- larger is better to display lines so try input span
500
# to extend run to end of chart, the
end pixel value of chart's last plot bar would be needed
yRise= to_int(y1 - newslope*(span));
# subtract not add since y values are inverted
canvas_lineto(xRun,yRise); #print "End point of Perp. Line from point A is ",xRun," , ",yRise;
############################################################################
}