How can I have an alert go off when the signal is 0 or 5 but only if it changed between the two values?

TRexing

New member
So signal is a plot value between 0 and 5. How can I make it so the alert only goes off once if it changes between 0 and 5 but not when it changes back to the same number?

Currently if the signal value goes from 5 -> 4 -> 5 it'll ding twice. I'd like it to only ding when it changes between 0 and 5 but not if it goes from 5 -> 4 -> 5 again or 0 -> 1 -> 0, etc.

Code:
def condition = signal == 0 or signal == 5;
Alert( condition, "Alert Test!", Alert.ONCE, Sound.Ding );
 
Solution
1. do you have a variable with values of 0 to 5?

Yes

2. are you saying you want to look for alternating extreme numbers, only the highest number and the lowest number ?
after a low of 0, only the first bar with a 5 will cause a trigger?
then once a 5 happens, after that only the first bar with a 0 will cause a trigger ?

Yeah, that's what I'm going for.

i shouldn't give time estimates on my responses, things come up.....

i forgot to ask for your study, so i had to create test data to test this.


this sets a variable, state, to be true, when signal is 0 or 5 , but only after the opposite level was the previous state value.

test data
an average, that is normalized to -2.5 to 7.5 . levels above and below 0 and 5, to cause...
Do you think this would work? (The market isn't open so there's no way to test at the moment.)

Code:
signal alert
def currentSignal = if signal == 0 then 0
               else if signal == 5 then 5
               else currentSignal; # keep value
def lastSignal;
Alert( currentSignal != lastSignal, "Alert!", Alert.ONCE, Sound.Ding );
lastSignal = if signal == 0 then 0
               else if signal == 5 then 5
               else lastSignal; # keep value
 
Do you think this would work? (The market isn't open so there's no way to test at the moment.)

Code:
signal alert
def currentSignal = if signal == 0 then 0
               else if signal == 5 then 5
               else currentSignal; # keep value
def lastSignal;
Alert( currentSignal != lastSignal, "Alert!", Alert.ONCE, Sound.Ding );
lastSignal = if signal == 0 then 0
               else if signal == 5 then 5
               else lastSignal; # keep value

you don't need to wait till the market opens, to test a study.
for testing, make the study a chart study , if it isn't. then draw objects when conditions are true.
bubbles and can be added to display values. arrows can be drawn when a condition becomes true.

Code:
addchartbubble(1, low, 
barnumber() +"\n" +
signal +"\n" +
currentsignal
,color.yellow, no);

work ? no, not as you want.
..it isn't reading previous values , with a [1] , to see what the value was before. So you don't know if it just changed to a new number or if it's been the same number for a while.

if you want to do something the first time a condition is true, =5, and ignore all the other times it happens,

.. if prev value was not 5 and current value is 5 , then set a variable to 1 and keep it equal to 1.

on my cell. will edit this post in a bit
 
Last edited:
So signal is a plot value between 0 and 5. How can I make it so the alert only goes off once if it changes between 0 and 5 but not when it changes back to the same number?

Currently if the signal value goes from 5 -> 4 -> 5 it'll ding twice. I'd like it to only ding when it changes between 0 and 5 but not if it goes from 5 -> 4 -> 5 again or 0 -> 1 -> 0, etc.

Code:
def condition = signal == 0 or signal == 5;
Alert( condition, "Alert Test!", Alert.ONCE, Sound.Ding );
your rules are confusing. i've read your post several times.. lets see if i got it...
1. do you have a variable with values of 0 to 5?
2. are you saying you want to look for alternating extreme numbers, only the highest number and the lowest number ?
after a low of 0, only the first bar with a 5 will cause a trigger?
then once a 5 happens, after that only the first bar with a 0 will cause a trigger ?
 
1. do you have a variable with values of 0 to 5?

Yes

2. are you saying you want to look for alternating extreme numbers, only the highest number and the lowest number ?
after a low of 0, only the first bar with a 5 will cause a trigger?
then once a 5 happens, after that only the first bar with a 0 will cause a trigger ?

Yeah, that's what I'm going for.
 
1. do you have a variable with values of 0 to 5?

Yes

2. are you saying you want to look for alternating extreme numbers, only the highest number and the lowest number ?
after a low of 0, only the first bar with a 5 will cause a trigger?
then once a 5 happens, after that only the first bar with a 0 will cause a trigger ?

Yeah, that's what I'm going for.

i shouldn't give time estimates on my responses, things come up.....

i forgot to ask for your study, so i had to create test data to test this.


this sets a variable, state, to be true, when signal is 0 or 5 , but only after the opposite level was the previous state value.

test data
an average, that is normalized to -2.5 to 7.5 . levels above and below 0 and 5, to cause several crossings, for testing.


variables,
state - will equal 0 or 5 . (except the first few bars until after signal = 0 or 5. )
new_extreme_signal - is true on the first bar of a different value, of 0 or 5


rtge5LG.jpg



Ruby:
# signals_0or5

# find when signal data goes from one limit to the other (0,5)
# create experimantal data , 0 to 5, for testing

# -------------------------------------------
declare lower;
def bn = barnumber();

input lowest_number = 0;
input highest_number = 5;

# ===========================================
# test data  \/ \/ \/ \/
# ===========================================

input avg1_len = 5;
input avg1_type =  AverageType.exponential;
def ma1 = MovingAverage(avg1_type, close, avg1_len);

# -------------------------------------------

# normalize_std_00
#https://usethinkscript.com/threads/how-to-compare-stock-charts-in-thinkorswim.1249/page-2#post-53385
#post 35
script normalizePlot {
  input data = close;
  input newRngMin = 0;
  input newRngMax = 100;
  def HHData = HighestAll( data );
  def LLData = LowestAll( data );
  plot nr = ((( newRngMax - newRngMin ) * ( data - LLData )) / ( HHData - LLData )) + newRngMin;
}

# test factor, is a number to extend values to go outside of 0 to 5 range.
input factor = 2.5;
def norm = normalizePlot(ma1, lowest_number-factor, highest_number+factor);

# -------------------------------------------

# round normalized test data, to be integers
def rnd_norm = round(norm, 0);

plot u = rnd_norm;
u.SetDefaultColor(Color.yellow);
u.setlineweight(1);
u.hidebubble();

def signal = rnd_norm;

# ===========================================
# test data  /\ /\ /\ /\
# ===========================================

# signal = .... 0 to 5

def state;
if bn == 1 then {
 # init value#
  state = lowest_number - 1;

} else if signal == lowest_number then {
#  current is low# , did it change from highest or from init# ?
  state = if ( state[1] == highest_number or state[1] < lowest_number ) then lowest_number else state[1];

} else if signal == highest_number then {
#  current is high# , did it change from lowest or from init# ?
  state = if ( state[1] == lowest_number or state[1] < lowest_number ) then highest_number else state[1];

} else {
  state = state[1];
}

# -------------------------------------------

# first time on different state value,  lowest or highest
def new_extreme_signal = if bn == 1 then 0 else if ( state[1] <> state[0] ) then 1 else 0;

# ===========================================
# test data  \/ \/ \/ \/
# ===========================================

addchartbubble(new_extreme_signal, state ,
bn + " BN\n" +
state + "  State\n"
 , color.yellow ,
( if state == lowest_number then 0 else 1) );

# -------------------------------------------

plot w1 = lowest_number;
plot w2 = highest_number;
w1.SetDefaultColor(Color.gray);
#w1.hidebubble();
w2.SetDefaultColor(Color.gray);
#w2.hidebubble();
#
 
Last edited:
Solution

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
511 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