Need help troubleshooting pullback entry code


New member
Hi everyone,

New to the forum (and thinkscript for that matter). I am starting to understand this language a little better and wrote the code that follows below. The intent of this code is to identify a swing high (peak), and plot an arrow for that peak. Next, the code should track the pullback from the uptrend by checking that the high of every candle going back to the peak is lower than the high of the previous candle (essentially i want to see that starting with the peak, the high of every candle in the pullback should get lower with every successive candle). Finally, when a candle breaks the pullback, and the high is greater than the high of the previous candle, plot an arrow as the entry point.

It seems that the "peakArrow" plots no problem. After troubleshooting a little, i found that the issue I am having is with "endPullback". I used addChartBubble to show the value of "pullback" at each candle, and it all looks good. However, once i add "and pullback == 1", the plot does not appear (and the "peakArrow" plot disappears as well). After staring at the screen for hours, i cannot figure this out, although i think it may be a simpler answer than i expect. Hopefully someone can get me on the right track. Thanks everyone!

#define peak
def peak = high > highest(high[1], 10) and high > highest(high[-1],1);

#calculate slope of trend to ensure in an uptrend
input length = 15;
def avg = Average(close, length);
def height = avg - avg[length];
def slope = ATan(height/length) * 180 / Double.Pi;

#plot peak arrow if in an uptrend
plot peakArrow = if slope > 0 then peak else double.NaN;

#Count number of candles since last peak
def pullbackCount = if peak then 0 else pullbackCount[1] + 1;

#track pullback by ensuring high of candle decreases with each successive candle
#if high of all candles in pullback are lower than previous after completion of iterations, pullback returns true
def pullback = fold x = 0 to pullbackCount[1] with value = 1 while value == 1 do if getValue(high,x,0) <= getValue(high,x+1,0) then 1 else 0;

#identify last candle of pullback
def endPullback = if high[-1] > high and pullback == 1 then 1 else 0;
#plot arrow on candle after the end of pullback
plot entry = if endPullback[1] == 1 then 1 else 0;

Similar threads