• Happy Independence Day! Take $40 off VIP Membership with code JULY4. Sign up here.

TD Perfection Indicator for ThinkorSwim


Well-known member
I wrote this script after reading about the algorithm from a public website but added my own twist. It can be used for scalping.

Dots above and below the signal is the support and resistance levels.

Let me know what you think and how I can improve it.


# TD Perfection Indicator
# barbaros
# 04.25.2020

# v1.0 - 04.25.2020 - barbaros - Initial release of TD Perfection Indicator

input price = close;
input showbuy = yes;
input showsell = no;
input k = 0; # 0 to 8
input i = 0; # 0 to 13

# Buy Perfection
def BearishPriceTwist = price[9] > price[13] and price[8] < price[12];
def BuySystem = price[k] < price[4+k];
def BuyPerfection = Lowest(price) <= Lowest(price[3]) and Lowest(price) <= Lowest(price[2]);
def BuySupport = Lowest(low[i]);
def BuyResistance = Highest(high[i]);
def BuyProbability = ((price - BuySupport) / (BuyResistance - price)) > 0.60 and ((BuyResistance - price) / (price - BuySupport)) > 0.40;

plot buySignal = if showbuy and BearishPriceTwist and BuySystem and BuyPerfection and BuyProbability and low > BuySupport and price > BuySupport and price < BuyResistance then close else Double.NaN;

plot buySupportLine = if buySignal then BuySupport else Double.NaN;
plot buyResistanceLine = if buySignal then BuyResistance else Double.NaN;

Alert(buySignal, "Buy Perfection", Alert.BAR, Sound.Bell);

# Sell Perfection
def BullishPriceTwist = price[9] < price[13] and price[8] > price[12];
def SellSystem = price[k] > price[4+k];
def SellPerfection = Highest(price) >= Highest(price[3]) and Highest(price) >= Highest(price[2]);
def SellSupport = Highest(price[i]);
def SellResistance = Lowest(price[i]);
def SellProbability = ((SellSupport - price) / (price - SellResistance)) > 0.60 and ((price - SellResistance) / (SellSupport - price)) > 0.40;

plot sellSignal = if showsell and BullishPriceTwist and SellSystem and SellPerfection and SellProbability and high < SellSupport and price > sellResistance then close else Double.NaN;

plot sellSupportLine = if sellSignal then SellSupport else Double.NaN;
plot sellResistanceLine = if sellSignal then SellResistance else Double.NaN;

Alert(sellSignal, "Sell Perfection", Alert.BAR, Sound.Bell);
Last edited:


Well-known member
@HighBredCloud this is a study. It is possible to adopt to a strategy though. It doesn't repaint. With limited testing, I found it has good results with 2000t.
I added it as a study on a 15 min chart...I need to see how it reacts...Have you tested other time frames other than tick charts to see where this indicator works the best?


Well-known member
Forgot to mention, I use k=8 and i=13 for 2000t chart. I am not sure what parameters will work better for different time frames.


Well-known member
@tabs999 arrows indicate buy or sell. dots indicate support and resistance. you may want to set stop losses at support and take profit at or above the resistance.


Well-known member
@technicallydreaming It will repaint as it is coded right now. Backtesting is not accurate. You should take trades after the bar closes.

I am looking into having it based on one previous bar so it doesn't repaint, or use the open price instead of close. Not sure how the performance will be with open price.


If it doesn't change after the bar closes, then it doesn't repaint. Repainting means it changes AFTER the bar the signal is on closes. All indicators based on Close will not be solid until the bar finishes closing.

Similar threads