I've had this script for years,...and began to explore it's code.
# __ Bull flag indicator for Thinkorswim by Steve from thestudentloanranger.wordpress.com
# Input and basic definitions
input pChg = 1.50; # Daily minimum percent increase during expansion phase (def=1.50)
def C = close;
def O = open;
def H = high;
def L = low;
def V = volume;
# isHigh is to stop a plotting issue inertia code, don't touch it!
def isHigh = C > 20000;
# Expansion range price and volume
def XRANGE = C[3] - O[5];
def XRANGEH = H[3] - O[5];
def XVOL = V[5] + V[4] + V[3];
# Define average true ranges and amount above
def atr = Average(TrueRange(high, C, low), 1.0);
def EXPANDER = atr > (Average(atr) * 1.10);
def EXPNDR = if EXPANDER[5] && EXPANDER[4] && EXPANDER[3] then 1 else 0;
# Percentage change characteristics
def PercentChg = Round(100 * (C / C[1] - 1), 3);
def PCCOUNT = if (PercentChg[5] > pChg && PercentChg[4] > pChg && PercentChg[3] > pChg)
or Round(100 * (C[3] / C[5] - 1), 3) > 10.0 then 1 else 0;
# Higher than average V characteristics
def Volavg = if (V[5] + V[4] + V[3]) / 3 > Average(V) * 1.10 then 1 else 0;
# Price action characteristics
def CandleAction = (C[5] > O[5] && C[4] > O[4] && C[3] > O[3]);
def Signal = if EXPNDR == 1 && Volavg == 1 && CandleAction && PCCOUNT then 1 else 0;
# 3-day consolidation settings
def CRANGE = Highest(H, 3) - Lowest(L, 3);
def UPDRIFT = Highest(H, 3) < (H[3] + (XRANGEH * 0.25));
def CONVOL = V[0] + V[1] + V[2];
# Ratio calculations
def XCRATIO = XRANGE / CRANGE;
def XVRATIO = XVOL / CONVOL;
# Confirmations and plot formatting
plot BULL = if XCRATIO > 2 && XVRATIO > 1.20 && UPDRIFT && Signal == 1 then 1 else 0;
BULL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BULL.SetDefaultColor(Color.LIME);
BULL.SetLineWeight(2);
AssignPriceColor(if BULL then Color.BLUE else Color.CURRENT);
plot STPPRICE = If(BULL and C > 0, (C[3] - (XRANGE * 0.40)), If(C < 0 and !BULL , (C[0] + (XRANGE * 0.95)), Double.NaN));
STPPRICE.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
STPPRICE.SetDefaultColor(Color.RED);
STPPRICE.SetLineWeight(2);
plot TGTPRICE = If(BULL and C > 0, (C[0] + (XRANGE * 0.90)), If(C < 0 and !BULL , (C[0] - (XRANGE * 0.40)), Double.NaN));
TGTPRICE.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TGTPRICE.SetDefaultColor(Color.GREEN);
TGTPRICE.SetLineWeight(2);