I have two custom studies that I would like to create as a pair. The first would be entry criteria for a trade and the second would be exit criteria. I currently have them separately but the second study is basic enough that it shows separate from the entry criteria. I would like for it to only show if the entry criteria is present. Here's a basic summary of the criteria (I'll post the code at the end):
ENTRY STUDY: Looks for 3 consecutive green candles, followed by a big red candle, then price breaks below red candle's low. Must happen when SMA60 is flat.
EXIT STUDY: Looks for 4 bars making lower lows, followed by 2 bars making higher lows.
Is it possible to have the exit study only trigger if the entry criteria is met? I understand that I cannot make reference to custom studies in ToS but the things I've tried when connecting them just isn't working. They do work separately.
ENTRY STUDY: Looks for 3 consecutive green candles, followed by a big red candle, then price breaks below red candle's low. Must happen when SMA60 is flat.
EXIT STUDY: Looks for 4 bars making lower lows, followed by 2 bars making higher lows.
Is it possible to have the exit study only trigger if the entry criteria is met? I understand that I cannot make reference to custom studies in ToS but the things I've tried when connecting them just isn't working. They do work separately.
Code:
# Sharp Reversal After Uptrend Pattern - Improved Slope Detection
declare upper;
def o = open;
def h = high;
def l = low;
def c = close;
# Moving Averages
def EMA9 = ExpAverage(close, 9);
def SMA60 = SimpleMovingAvg(close, 60);
# Improved Slope Calculation
input lookBack = 5; # Number of bars to look back
input minSlope = 10; # Slope threshold CHANGED TO 10
# Calculate slope using difference method
def maDiff = 10 * (SMA60 - SMA60[lookBack]);
def maSlope = AbsValue(Atan(maDiff / lookBack) * 180 / Double.Pi);
# Flatness Check
def isFlat = maSlope <= minSlope;
# [Rest of the original code remains the same]
def bullishCandles = c[4] > o[4] and # First green candle
c[3] > o[3] and # Second green candle
c[2] > o[2]; # Third green candle
# EMA conditions
def emaFilter = c[3] > EMA9[3] and # Two bars ago close above 9 EMA
c[2] > EMA9[2]; # Last green candle close above 9 EMA
# Uptrend close comparisons
def closeCompare1 = c[2] > c[3]; # Last green candle higher close than previous
def closeCompare2 = c[3] > c[4]; # Second green higher than first green
# Uptrend low comparisons
def lowCompare1 = l[2] > l[3]; # Last green candle higher low than previous
def lowCompare2 = l[3] > l[4]; # Second green higher low than first green
# Red reversal candle conditions
def bodySize = AbsValue(c[1] - o[1]); # Red candle body size
def avgBodySize = (AbsValue(c[2] - o[2]) + AbsValue(c[3] - o[3]) + AbsValue(c[4] - o[4])) / 3;
def isFullBody = bodySize >= avgBodySize;
def redReversal = l[1] <= l[3] and # Red candle low lower than second green candle
c[1] < o[1] and # Must be red
isFullBody and # Must be full-bodied
c[1] < o[3]; # Red close lower than second green open
# New candle breakdown
def newBreakdown = l < l[1]; # Current candle breaks below red candle's low
# Complete pattern
def pattern = bullishCandles and
emaFilter and
closeCompare1 and
closeCompare2 and
lowCompare1 and
lowCompare2 and
redReversal and
newBreakdown and
isFlat;
# Plot signal
plot signal = if pattern then h else double.nan;
signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
signal.SetLineWeight(3);
signal.SetDefaultColor(Color.MAGENTA);
signal.HideBubble();
signal.HideTitle();
# Add debug labels for testing
AddLabel(yes, "Sharp Reversal", Color.MAGENTA);
AddLabel(yes, "Historical SMA60 Slope: " + maSlope, Color.YELLOW);
Code:
declare upper;
def o = open;
def h = high;
def l = low;
def c = close;
# 5-Candle Downtrend Verification (Before the 2 confirming candles)
def downTrendCandles =
l[6] > l[5] and
l[5] > l[4] and
l[4] > l[3] and
l[3] > l[2];
# 2-Candle Higher Low Confirmation (Last two candles must make higher lows)
def breakDownTrend =
l[1] > l[2] and
l[0] > l[1];
# Exit Signal with Downtrend and Break Conditions
def exitSignal =
downTrendCandles and
breakDownTrend;
# Plot Signal Only When Exit Conditions Are Met
plot signal = if exitSignal then h else Double.NaN;
signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
signal.SetLineWeight(3);
signal.SetDefaultColor(Color.GREEN);
signal.HideBubble();
signal.HideTitle();
# Optional Debug Labels
AddLabel(exitSignal, "Exit Strategy Triggered", Color.GREEN);