I need some help with Alert Bar coding

Mart

New member
VIP
What I am trying to do is have an alarm after the last maximum point is plotted.

The def Signal line is not correct can some one explain, Thanks.


def sma1 = SimpleMovingAvg(close, 10);
def Signal = Alert.BAR, Sound.Ring;

Alert(Signal == 1, "NewHighIsDone", Alert.BAR, Sound.Ring);

plot sma1higher = if sma1 > sma1[1] then sma1 else Signal and Double.NaN;
sma1higher.SetLineWeight (1);

================================================

I also tried to use some code from halcyonguy
that is not working for me either.

#up
alert(a, "up", alert.bar, sound.ding);

#down
alert(b, "down", alert.bar, sound.bell);

input barsback = 8;
# check if current high is higher than prev x bars
# these will be 1 or 0 , true or false
def ishigher = ( high > highest( high[1], barsback) );
def islower = ( low < lowest[1], barsback) );

then use ishigher as the trigger variable in 1 alert function, and use islower in the other alert.
 
Last edited:
Solution
What I am trying to do is have an alarm after the last maximum point is plotted.

The def Signal line is not correct can some one explain, Thanks.


def sma1 = SimpleMovingAvg(close, 10);
def Signal = Alert.BAR, Sound.Ring;

Alert(Signal == 1, "NewHighIsDone", Alert.BAR, Sound.Ring);

plot sma1higher = if sma1 > sma1[1] then sma1 else Signal and Double.NaN;
sma1higher.SetLineWeight (1);

================================================

I also tried to use some code from halcyonguy
that is not working for me either.

#up
alert(a, "up", alert.bar, sound.ding);

#down
alert(b, "down", alert.bar, sound.bell);

input barsback = 8;
# check if current high is higher than prev x bars
# these will be 1 or 0 , true or...

SleepyZ

Well-known member
VIP
Lifetime
What I am trying to do is have an alarm after the last maximum point is plotted.

The def Signal line is not correct can some one explain, Thanks.


def sma1 = SimpleMovingAvg(close, 10);
def Signal = Alert.BAR, Sound.Ring;

Alert(Signal == 1, "NewHighIsDone", Alert.BAR, Sound.Ring);

plot sma1higher = if sma1 > sma1[1] then sma1 else Signal and Double.NaN;
sma1higher.SetLineWeight (1);

================================================

I also tried to use some code from halcyonguy
that is not working for me either.

#up
alert(a, "up", alert.bar, sound.ding);

#down
alert(b, "down", alert.bar, sound.bell);

input barsback = 8;
# check if current high is higher than prev x bars
# these will be 1 or 0 , true or false
def ishigher = ( high > highest( high[1], barsback) );
def islower = ( low < lowest[1], barsback) );

then use ishigher as the trigger variable in 1 alert function, and use islower in the other alert.

The def Signal is not a boolean (yes/no, 1/0, true/false) signal needed to trigger an alert. Your code was just attributes of the Alert function and not useable to trigger an alert.

The following code triggers the alert when the current bar is a double.nan (IsNaN(sma1higher)) and the prior bar is a number and not a double.nan (!IsNaN(sma1higher[1]).

The alert may make false triggers, as does most alerts using the current bar as part of the trigger, if the current developing bar changes from a true/false condition on that bar. In your case, the current bar may be a continuation of the SMA and change to not being a continuation, creating multiple alerts on the same bar, some being false ultimately.

To visually alert you, the code below has a chart bubble option to display at the low of the bar that triggers your alert, the message "NewHighIsDone".

Capture.jpg
Ruby:
def sma1 = SimpleMovingAvg(close, 10);
#def Signal = Alert.BAR, Sound.Ring;
#Alert(Signal == 1, "NewHighIsDone", Alert.BAR, Sound.Ring);

plot sma1higher = if sma1 > sma1[1] then sma1 else Double.NaN;
sma1higher.SetLineWeight (5);

input usealert = yes;
Alert(usealert and IsNaN(sma1higher) and !IsNaN(sma1higher[1]), "NewHighIsDone", Alert.BAR, Sound.Ring);
input showbubbles = yes;
AddChartBubble(showbubbles and IsNaN(sma1higher) and !IsNaN(sma1higher[1]), low, "New\nHigh\nIs\nDone", Color.RED, no);
 
Solution

Mart

New member
VIP
Many Thanks, your help is greatly appreciated.
It looks like you added 2 Alerts, sound and a bubble.
I will try them out after I get some sleep.
HAPPY HOLIDAYS
 

Mart

New member
VIP
The audio alert works great. I hope someday to as good as you, Many thanks.
I am using declare lower and
when I activate the showbubbles line of code the text plots at 4695 on the subgraph
and the subgraph of the data is a horizontal line.
can the position of the showbubbles text be lowered?
 

SleepyZ

Well-known member
VIP
Lifetime
The audio alert works great. I hope someday to as good as you, Many thanks.
I am using declare lower and
when I activate the showbubbles line of code the text plots at 4695 on the subgraph
and the subgraph of the data is a horizontal line.
can the position of the showbubbles text be lowered?

Yes, the bubbles can be moved up or down in the following code at the input bubbles_updn = 0; By increasing this number it will move the bubbles down and decreasing moving them up. It is then multiplied by the ticksize() of the chart you are viewing. The default is zero. In the image below it is set to 20 to move the bubbles down.

Also, included is an option to shorten the text in the bubble.

Added is a Verticalline option to use as a visual

Capture.jpg
Ruby:
input length = 10;
def sma1 = SimpleMovingAvg(close, length);

plot sma1higher = if sma1 > sma1[1] then sma1 else Double.NaN;
sma1higher.SetLineWeight (5);

input usealert = yes;
Alert(usealert and IsNaN(sma1higher) and !IsNaN(sma1higher[1]), "NewHighIsDone", Alert.BAR, Sound.Ring);

input showbubbles  = yes;
input bubbles_updn = 0;
AddChartBubble(showbubbles and IsNaN(sma1higher) and !IsNaN(sma1higher[1]),
               low - ticksize() * bubbles_updn,
               "High\nDone", #"New\nHigh\nIs\nDone",
               Color.RED, no);

input showvertical = yes;
addverticalLine(showvertical and IsNaN(sma1higher) and !IsNaN(sma1higher[1]),
                "High\nDone", #"New\nHigh\nIs\nDone",
                Color.RED, curve.firm);
 

wchillman

New member
Yes, I could use some help on this too. It’s been 50 years since I had a basic coding course (Fortran) most of which has been long forgotten.

Here’s my situation: I have an Alert on each stock in my portfolio. I would like to create a custom column script that would plot the percentage that the Alert is below the current price.

What is the code that I could use to call out the value of the Alert? I think I could do the rest. Thanks.
 

halcyonguy

Well-known member
VIP
Lifetime
Yes, I could use some help on this too. It’s been 50 years since I had a basic coding course (Fortran) most of which has been long forgotten.

Here’s my situation: I have an Alert on each stock in my portfolio. I would like to create a custom column script that would plot the percentage that the Alert is below the current price.

What is the code that I could use to call out the value of the Alert? I think I could do the rest. Thanks.

to post 6
just came across this post,


an alert doesn't have a value.
do you mean to,
compare the close of the bar when the alert triggered,
to the current close?

maybe this will be close to what you are looking for.

this uses the crossing of 2 averages as a trigger.
replace the trigger formula with yours.

watchlist column study

Ruby:
# zalert_per_0

# https://usethinkscript.com/threads/i-need-some-help-with-alert-bar-coding.9418/#post-86406
# i need some help with Alert Bar coding
# wchillman  Jan 2, 2022
# I have an Alert on each stock in my portfolio. 
# I would like to create a custom column script that would plot the percentage that the Alert is below the current price.

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


def bn = barnumber();
def na = double.nan;

#-------------------------------------
# replace this with your trigger code

# test trigger
input avg1_len = 10;
input avg1_type =  AverageType.simple;
def ma1 = MovingAverage(avg1_type, close, avg1_len);

input avg2_len = 80;
input avg2_type =  AverageType.simple;
def ma2 = MovingAverage(avg2_type, close, avg2_len);

def xup = if ma1 crosses above ma2 then 1 else 0;

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


#def trigger = your code for alert trigger
def trigger = xup;

def trigger_close =
  if barnumber() == 1 then 0
  else if trigger then close
  else trigger_close[1];

# percent of change of close , from alert close
def per = round(100 * (( trigger_close - close ) / close ), 1);

plot z = per;
z.setdefaultcolor(color.black);

assignbackgroundcolor(if per > 0 then color.green else if per < 0 then color.red else color.gray);

# if you want to display text, disable the plot and use this addlabel 
# addlabel(1, per, color.black);
#
 

Similar threads

Top