Sunday 20 October 2024

Pine Script Feature Update alert() • Pine Script V4 Tutorial

hey pinescripters welcome back to
another video today's video is going to
be covering
a recent update that the trading view
team
made to the pinescript language on the
25th of january
they changed how alerts work or they
added a new alert functionality to our
scripts
so this new alert function works in both
strategies and studies and it allows a
fully dynamic message to be generated
when the alert triggers so when we get
to the pine editor i'll explain exactly
what that means
but this is a pretty cool feature the
new alerts work using a similar model
to the recent strategy alerts where only
one alert created in the chart user
interface can aggregate all the triggers
generated by any number of alerts
basically what this means is that in
strategy scripts you can only trigger
one alert if you set an alert on a
strategy script
any of those order management function
calls will trigger an alert if you've
set one
on that strategy script and the alert
condition function does not work
on strategy scripts which just means you
don't have as much control over alerts
in a strategy script as you do in a
study script
but that's changed now with this new
alert functionality
basically instead of using the alert
condition function we can now use this
just alert function you can use it as
many times in your script as you want
and whenever one of these trigger
if you've set an alert on the training
view platform referencing these alert
function calls
then that will trigger an alert and i'm
going to go to the pine editor and show
you exactly what that means in a second
but let's finish reading this
so to create these new alerts you can
include as many alert calls in your
script as you wish
enclosing each one in an if block
defining the triggering conditions
so in an alert condition function you
normally have to pass
your boolean value or your alert trigger
condition
into that function with these new alert
calls
you can use if statements to trigger
them
so normally an alert condition function
cannot be
anywhere except for the global scope
meaning you can't have it in a for loop
or in an if statement
well these alert condition calls behave
differently the only thing that hasn't
changed is how you actually set the
alert so you use the create alert dialog
box
and you select the alert type called
alert function call
so that means when you're creating
strategy scripts you can
create alerts that trigger only on an
alert event
or on order fill events or on both so
that's not something i want to cover in
today's video because it's a bit
complex and advanced or require a fair
bit of time to get into that
that's something i'd cover in the pine
script mentorship program if you want to
learn more about that
head to panscriptmastery.com but stick
around because i'm going to jump into
the pine editor in a second and show you
the basic rundown of this new alert
functionality
the final really cool thing about this
new alert function
is that you can input dynamic alert
messages
so that you don't need to use
placeholders anymore as long as your
message is in string format you can
pretty much
pass any data you want to these new
alert functions
so before we jump into the pine editor
here is the documentation
the official technical write-up for this
alert function
you can find this information on the
pinescript reference
manual or you can just hold ctrl and
click on the alert function in your pine
editor and this will pop up so here's a
script
example of how to use this new alert
function i'm just going to copy and
paste this into the pine editor and
modify it slightly
and we're going to go over this new
alert function here
but the main differences are this alert
function does not take a title it only
takes a message
and a frequency parameter and we'll go
over those in the pine editor
the final thing to go over before we go
there is these remarks
so contrary to alert condition alert
calls do not
count as an additional plot this is only
relevant
to people who use third-party apis and
reference their plots
in their alert condition messages if you
don't know what that means don't worry
it's not important to you
function calls can be located in both
global and local scopes so this is a big
change
normally the alert condition function
can only exist in your overall script
it cannot exist in a function or an if
statement or a for loop
or anything like that basically that
means it just can't be tabbed or
indented
these function calls do not display
anything on the chart
and the frequency argument or parameter
only affects the triggering frequency of
the function call where it is used
so you can have different frequencies on
different alert function calls in your
script
so that's the technical jargon out of
the way let's get into the pine editor
and show a practical example of this
info so here i am in the pine editor
i've copy and pasted
the trading views official pine script
reference
code into my editor and i've changed a
couple things all i've done is add
another cross
under variable so that i can show you a
couple of variations of this alert
function
so everything in the top half of the
script is
business as usual nothing is different
about it still using the study
annotation function
we're still declaring our alert
conditions as you normally would the
only difference is
right here so if i scroll to the bottom
of my script and let me
make some more space here this is how
you would normally do what these two
functions are doing you'd write an alert
condition in the global scope so that
means there's no tab you can see that
these are tabbed underneath this if
statement
the alert condition function cannot be
placed here
if i were to copy this and paste it here
and save the script we'll get an alert
an error sorry an alert we'll get an
error cannot use alert condition in
local scope
so that's what that means to save the
script it will now compile without any
errors because you are allowed to put
these
new alert function calls within a local
scope that's what these if statements
are
so basically if you wanted to do what
these alert function calls
are doing in the script before they
added this feature
you would need to set your alerts up
like this you need to use an alert
condition
function call and you need to pass your
two alert
variables or alert condition variables
into the alert condition function itself
you would then need to title the alert
condition and your message needs to be
static which means
it's a constant so it doesn't change
based on whatever's happening on your
price chart
the only exception for this is using the
placeholders so see how these two alert
functions they pass in the closing price
and the moving average price of the
current bar that triggered the alert
you need to convert that data into a
string
to make this work if i get rid of two
string here and save the script we'll
get a compile error
because you cannot pass an integer or a
float
or any other data value except for
string into this alert
message parameter if we were to try to
do the same thing here if i were to
add that on to the end of our alert
condition message and save the script
we'll get an error as well
reason for that is the alert condition
function requires
a static or constant string const string
which means you can't use function calls
within your message parameter
because that's no longer static whatever
this function does will be different
depending on
which bar triggers the alert so we need
we can't use that on here if you wanted
to do that same thing
if you wanted to add the closing price
to your alert condition message you
would need to plot the closing price on
your chart like this
this would count as plot number 0 and
then you would need to change this to
say
plot underscore in two curly brackets
so this would achieve the same thing as
this
but obviously it's a little more
convoluted you've got to add another
plot to your chart which will draw onto
your chart uh and so these new alert
functions
make it so that that is no longer
necessary so let's go over how they
actually work
so these alert functions trigger the
actual alert so it doesn't matter what
you put in here
if this alert function is called or
triggered
an alert will be generated if you've set
one up here which we'll go over in a
second so if you were to move this
into the global scope and save the
script
now if i was to set an alert on this
script using this
any alert function call condition then
the script will just generate random
alerts for no reason because it's not
taking into consideration
our boolean variable so by putting this
alert function within the scope of this
if statement what that's doing is it's
telling pinescript
the pinescript engine not to trigger
this alert
unless x up is true and x up is only
true
if the current closing price has crossed
over
the moving average on our chart which is
just a simple moving average
so this condition could be anything um
this is just an example
to show you how this actually works so
if the closing price
uh plots a green arrow like you see here
that means that the closing price of the
current bar crossed
up and over the moving average when that
happens an alert will be generated
if you've set one in the alert dialog it
will include the current closing price
in that message it will include the
current moving average price in that
message
and it will trigger once per bar
even if the current bar hasn't closed
yet our x down
our cross down boolean variable here
behaves
a little bit differently with this alert
so we're doing exactly the same thing
we're passing our closing price and our
moving average price into this alert
message the only difference is
this alert frequency is set to once per
bar close
so the current bar must be confirmed it
must close above the sma
before this alert would be triggered and
if you want to see what
options you have here if you just press
ctrl space after writing
alert and a full stop it will list all
of the alert frequency
options there are only really two or you
can combine the two
so you can either trigger it once per
real time bar
so a bar that hasn't closed yet or once
per a confirmed bar close
otherwise you can just set it to
frequency underscore all which i believe
is pretty much the same thing as setting
it to frequency once per bar
so that's basically it for these new
alert functions
it's not particularly complex it's just
a little bit different to how we're used
to working with alerts and it's quite a
cool feature
before we finish the video i should show
you how to set these alerts
you come up to your alert create alert
dialog window
click on that or press the hotkey alt
plus a
or i think it's command a on a mac and
you need to come up to your condition
box
select alert example or your script name
in my case this is my script name so
click on that and then you have your
alert conditions here
this is a script this is the condition
if i click on this drop down box
we now have two alert options here other
than all the built-in ones
and that is any alert function call
which are these guys here
or we can select our alert condition
function call
so if i click on that now this alert
will behave as traditional alerts
do but if i select this
option any alert function call and i
create an alert
now an alert will be triggered if any of
these alert
functions are called so that's it for
today's lesson i'll leave a link in the
video description
to the reference manual and the blog
post about this new feature it's
probably a good idea for you to go
over into the pine editor and play
around with it yourself to get familiar
with it
i'm not going to show you how to use it
in a strategy function because it's
exactly the same
behaves exactly the same way in a
strategy so you can pass any information
to your strategy alerts now using this
alert
new alert function call which is pretty
cool there are a lot of use cases for
that particularly with automating
trading
through a third party so that's it for
today's lesson i hope you found that
interesting and valuable
if you did please hit the like and
subscribe button i'll be back soon with
plenty more free content
and if you want to learn more advanced
concepts with pine script or you want to
dive into this
new alert function in greater detail
head over to panscriptmastery.com and
check out my courses there there's a
free course there on the basics of
planscript
if all of this was a bit too advanced
for you then you might want to check
that free course out because it goes
into great detail about the foundational
concepts of pine script and it's helped
many beginner traders
get a grasp on this language so thanks
for watching and i'll see you in the
next video good luck with your trading
guys and girls
take care be good speak soon

No comments:

Post a Comment

PineConnector TradingView Automation MetaTrader 4 Setup Guide

what's up Traders I'm Kevin Hart and in today's video I'm going to be showing you how to install Pine connecto...