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
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);
Last edited: