# === SINGLE CANDLE ENTRY MODEL (SCE) - THINKORSWIM ===
# Developed with inspiration from DaveTeaches
declare upper;
# === USER INPUTS ===
input enableHTF1 = yes;
input htf1TimeFrame = AggregationPeriod.FOUR_HOURS;
input enableHTF2 = yes;
input htf2TimeFrame = AggregationPeriod.DAY;
input enableHTF3 = no;
input htf3TimeFrame = AggregationPeriod.WEEK;
# === REGULAR SCE DETECTION ===
def prevHigh = high[1];
def prevLow = low[1];
def isSCE = high > prevHigh and low < prevLow;
def lastSCE = isSCE within 10 bars; # Ensures labels remain visible for recent events
# === PLOT SCE LABELS ===
AddLabel(lastSCE, "SCE", if close > open then Color.GREEN else Color.RED);
Alert(isSCE, "Single Candle Entry detected!", Alert.BAR, Sound.Ring);
# === PLOT RETRACEMENT LEVELS (30%, 50%, 70%) ===
def level30 = low + (high - low) * 0.30;
def level50 = (high + low) / 2;
def level70 = low + (high - low) * 0.70;
plot Retrace30 = if isSCE then level30 else Double.NaN;
plot Retrace50 = if isSCE then level50 else Double.NaN;
plot Retrace70 = if isSCE then level70 else Double.NaN;
Retrace30.SetDefaultColor(Color.WHITE);
Retrace50.SetDefaultColor(Color.WHITE);
Retrace70.SetDefaultColor(Color.WHITE);
Retrace30.SetStyle(Curve.SHORT_DASH);
Retrace50.SetStyle(Curve.SHORT_DASH);
Retrace70.SetStyle(Curve.SHORT_DASH);
# === HTF SCE DETECTION ===
def htf1PrevHigh = high(period = htf1TimeFrame)[1];
def htf1PrevLow = low(period = htf1TimeFrame)[1];
def htf1SCE = enableHTF1 and high(period = htf1TimeFrame) > htf1PrevHigh and low(period = htf1TimeFrame) < htf1PrevLow;
def htf2PrevHigh = high(period = htf2TimeFrame)[1];
def htf2PrevLow = low(period = htf2TimeFrame)[1];
def htf2SCE = enableHTF2 and high(period = htf2TimeFrame) > htf2PrevHigh and low(period = htf2TimeFrame) < htf2PrevLow;
def htf3PrevHigh = high(period = htf3TimeFrame)[1];
def htf3PrevLow = low(period = htf3TimeFrame)[1];
def htf3SCE = enableHTF3 and high(period = htf3TimeFrame) > htf3PrevHigh and low(period = htf3TimeFrame) < htf3PrevLow;
# === PLOT HTF BOXES ===
AddCloud(if htf1SCE then htf1PrevHigh else Double.NaN, if htf1SCE then htf1PrevLow else Double.NaN, Color.YELLOW, Color.YELLOW);
AddCloud(if htf2SCE then htf2PrevHigh else Double.NaN, if htf2SCE then htf2PrevLow else Double.NaN, Color.YELLOW, Color.GREEN);
AddCloud(if htf3SCE then htf3PrevHigh else Double.NaN, if htf3SCE then htf3PrevLow else Double.NaN, Color.YELLOW, Color.ORANGE);
# === LABEL HTF SCE ===
def lastHTF1 = htf1SCE within 10 bars;
def lastHTF2 = htf2SCE within 10 bars;
def lastHTF3 = htf3SCE within 10 bars;
AddLabel(lastHTF1, "HTF1 SCE", Color.YELLOW);
AddLabel(lastHTF2, "HTF2 SCE", Color.GREEN);
AddLabel(lastHTF3, "HTF3 SCE", Color.ORANGE);