Combine Two Custom Studies (second only occurs if first is triggered)

cmcgivern

New member
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.


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);
 
  • I'm watching this!
Reactions: sum

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
411 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top