Watchlist column indicator for longs and shorts sollowing DMI, SMA and EMAs

Berezowsky

New member
Having the following code, I have coded to get in the watchlist column if I have an entry following my conditions:
Long: DMI must be positive, SMA20 trending positive, and the cloud for EMA9 and EMA20 should be positive.
Shorts, the opposite

I am trying to get the alarm to sound after the second bar meets the conditions.

Thank you in advanced

Code:
# Define the EMA9 and EMA20
def EMA9 = ExpAverage(close, 9);
def EMA20 = ExpAverage(close, 20);

# Define the DMI Oscillator
def DMIOsc = DMI_Oscillator(14);

# Create conditions
def isEMA9OverEMA20 = EMA9 > EMA20;
def isDMIOscPositive = DMIOsc > 0;
def isEMA20Uptrending = EMA20 > EMA20[1];
def isEMA9UnderEMA20 = EMA9 < EMA20;
def isDMIOscNegative = DMIOsc < 0;
def isEMA20Downtrending = EMA20 < EMA20[1];

# Combine conditions
def LongCondition = isEMA9OverEMA20 and isDMIOscPositive and isEMA20Uptrending;
def ShortCondition = isEMA9UnderEMA20 and isDMIOscNegative and isEMA20Downtrending;

# Check if the condition has been met for multiple bars
def longCondition2Bars = LongCondition[1] and LongCondition;
def longCondition3BarsOrMore = LongCondition[2] and LongCondition[1] and LongCondition;
def shortCondition2Bars = ShortCondition[1] and ShortCondition;
def shortCondition3BarsOrMore = ShortCondition[2] and ShortCondition[1] and ShortCondition;

# Output watchlist condition labels
def condlabel = if longCondition3BarsOrMore then 3
                else if shortCondition3BarsOrMore then -3
                else if longCondition2Bars then 2
                else if shortCondition2Bars then -2
                else if LongCondition then 1
                else if ShortCondition then -1
                else 0;
AssignBackgroundColor(
    if condlabel == 3 then Color.DARK_GREEN
    else if condlabel == -3 then Color.DARK_RED
    else if condlabel == 2 then Color.DARK_GREEN
    else if condlabel == -2 then Color.DARK_RED
    else if condlabel == 1 then Color.GREEN
    else if condlabel == -1 then Color.RED
    else Color.GRAY);
AddLabel(yes,
         if condlabel == 3 then "L3"
         else if condlabel == -3 then "S3"
         else if condlabel == 2 then "L2"
         else if condlabel == -2 then "S2"
         else if condlabel == 1 then "Long"
         else if condlabel == -1 then "Short"
         else "Wait",
         if condlabel == 3 or condlabel == -3 then Color.Black
         else if condlabel == 2 or condlabel == -2 then Color.White
         else Color.Black);

# Add Sound Alerts for when the signal changes from L to L2 or S to S2
Alert(longCondition2Bars and !longCondition2Bars[1] and LongCondition[1], "Changed from Long to L2", Alert.BAR, Sound.Ring);
Alert(ShortCondition2Bars and !shortCondition2Bars[1] and ShortCondition[1], "Changed from Short to S2", Alert.BAR, Sound.Ring);
Screenshot 2025-01-30 145713.png
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
639 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