Sunday 20 October 2024

Pine Script ULTIMATE BEGINNER'S GUIDE! đź’ą [2024]

hey what's up traders! welcomeĂ‚ 
to my pine script basics course!
the material in this video is taken from myĂ‚ 
website over at pine scriptmastery.com thisĂ‚ Ă‚ 
is basically a stripped down version of my freeĂ‚ 
pine script basics course the official versionĂ‚ Ă‚ 
on my website goes into a lot more detail it'sĂ‚ 
more geared towards complete beginners who haveĂ‚ Ă‚ 
never written a line of code in their life soĂ‚ 
if that sounds like you you might want to goĂ‚ Ă‚ 
through the official material as there's a lot ofĂ‚ 
presentations and information about programming inĂ‚ Ă‚ 
general like how to get started what programmingĂ‚ 
is all the technical jargon i explain all of thatĂ‚ Ă‚ 
stuff in the official course i've stripped all ofĂ‚ 
that out for this version because there's four orĂ‚ Ă‚ 
five hours of content in my official basics courseĂ‚ 
on my website and i wanted to keep this video asĂ‚ Ă‚ 
close to two hours or less as possible i thinkĂ‚ 
it's going to be a little bit over two hoursĂ‚ Ă‚ 
but it's going to be worth going throughĂ‚ 
because the material you are about to watchĂ‚ Ă‚ 
will teach you everything you need to know inĂ‚ 
order to get started with pine scripts and startĂ‚ Ă‚ 
writing your own scripts i'm going to teachĂ‚ 
you the core fundamentals of pine how to useĂ‚ Ă‚ 
some of the more common basic functions likeĂ‚ 
drawing to the chart getting user input generatingĂ‚ Ă‚ 
alerts and referencing inbuilt indicators so iĂ‚ 
hope you find the course material interestingĂ‚ Ă‚ 
if you do make sure to hit the subscribe buttonĂ‚ 
and like button and all that youtube stuffĂ‚ Ă‚ 
and uh i hope you enjoy it i really doĂ‚ 
i'm really passionate about trading andĂ‚ Ă‚ 
especially pinescript and i hope that passionĂ‚ 
rubs off on you throughout this material withĂ‚ Ă‚ 
all that said i'll get my ugly mug out of yourĂ‚ 
face and let you get started with the content uhĂ‚ Ă‚ 
have fun best of luck with your trading and i'llĂ‚ 
speak with you in the lessons to come take care
hi traders in this video i'm going to be showingĂ‚ 
you a basic overview and breakdown of the pineĂ‚ Ă‚ 
script editor so the pinescript editor is inbuiltĂ‚ 
directly into the tradingview platform and you canĂ‚ Ă‚ 
open it and use it directly through your browserĂ‚ 
so here i am on the tradingview platform to openĂ‚ Ă‚ 
up the pine editor we just click on this tab hereĂ‚ 
so we click on that that will bring up the pineĂ‚ Ă‚ 
editor so this is what the editor looks like whenĂ‚ 
you first open it up with a blank script there areĂ‚ Ă‚ 
a few things to go over here first of all you canĂ‚ 
open existing scripts by clicking on this buttonĂ‚ Ă‚ 
here if i open that you can open an existingĂ‚ 
script from your own script library you canĂ‚ Ă‚ 
create a new default built-in script so this willĂ‚ 
create a new script based on an inbuilt indicatorĂ‚ Ă‚ 
so if i scroll down to let's find the let's goĂ‚ 
with a just a moving average if i click on thatĂ‚ Ă‚ 
this will create a new script with the sourceĂ‚ 
code for the inbuilt moving average indicatorĂ‚ Ă‚ 
and if we come up and open a new blank indicatorĂ‚ 
script that will open a new blank indicatorĂ‚ Ă‚ 
template and if we open a strategy this is theĂ‚ 
template for a new blank strategy then we haveĂ‚ Ă‚ 
our new blank library script and this will createĂ‚ 
a new blank library now don't worry we'll go overĂ‚ Ă‚ 
what each of these different types are there'sĂ‚ 
really only three we've got an indicator script aĂ‚ Ă‚ 
strategy script and a library script we'll go overĂ‚ 
all of these throughout the course next up we haveĂ‚ Ă‚ 
save so if i click save on this blank scriptĂ‚ 
i can name this script it doesn't matter whatĂ‚ Ă‚ 
you name it but i would recommend getting into aĂ‚ 
habit of some sort of consistent organized namingĂ‚ Ă‚ 
system so for me for example i like to put 2021 orĂ‚ 
the current year 2022 whatever the current year isĂ‚ Ă‚ 
in square brackets in front of my scriptĂ‚ 
or something like that just to organizeĂ‚ Ă‚ 
my scripts if i open up my scripts here if iĂ‚ 
go open my script you can see that i have aĂ‚ Ă‚ 
significant amount of scripts here and a lot ofĂ‚ 
them aren't organized but i have recently begunĂ‚ Ă‚ 
organizing them by a year and also by subject soĂ‚ 
you can see i have a bunch of psmc scripts hereĂ‚ Ă‚ 
this is a part of script mastery course thisĂ‚ 
is from the previous version of this courseĂ‚ Ă‚ 
from version 4 of pine script from when pineĂ‚ 
script was version four it's now updated toĂ‚ Ă‚ 
version five so i'm re-recording this entireĂ‚ 
course so whenever i create a new script forĂ‚ Ă‚ 
this course i will probably call it p psmc versionĂ‚ 
five dash and then the name or something like thisĂ‚ Ă‚ 
that way when i open up my scripts i can just typeĂ‚ 
in psmc and i get a list of all my psmc scriptsĂ‚ Ă‚ 
you don't need to do this but this is just a goodĂ‚ 
habit to get into early if you plan on writingĂ‚ Ă‚ 
a lot of scripts so let me open up a blankĂ‚ 
indicator here and save it and i'll call thisĂ‚ Ă‚ 
psmc v5 dash pine editor let me save that scriptĂ‚ 
and now we'll move on to add to chart so if iĂ‚ Ă‚ 
click on add to chart that will add the script toĂ‚ 
my chart and now we have our script here and thenĂ‚ Ă‚ 
next we have publish script this will publish yourĂ‚ 
script to the trading view library we'll go overĂ‚ Ă‚ 
this in more detail in a future lesson but this isĂ‚ 
where you can publish your script you can give itĂ‚ Ă‚ 
a description as detailed as you want it to be umĂ‚ 
you have your privacy settings so you could makeĂ‚ Ă‚ 
it a public or private script a private scriptĂ‚ 
means it's only visible to those with the linkĂ‚ Ă‚ 
a public script is visible to all uh tradersĂ‚ 
on the tradingview platform but you can protectĂ‚ Ă‚ 
your source code so that only you can see it butĂ‚ 
people can still use your script or you can makeĂ‚ Ă‚ 
it an invite-only script so that only you can seeĂ‚ 
the source code and you choose who can use it thisĂ‚ Ă‚ 
is particularly valuable or useful for creatingĂ‚ 
premium scripts that you can charge traders moneyĂ‚ Ă‚ 
to use such as my ultimate pullback indicatorĂ‚ 
which is a good example of using this invite-onlyĂ‚ Ă‚ 
version of a public script but let's cancelĂ‚ 
out of this publish script dialogue and moveĂ‚ Ă‚ 
on to this little button here if we click onĂ‚ 
this we have a whole bunch of resources hereĂ‚ Ă‚ 
so we can make a copy of our current scriptĂ‚ 
that we have open we can view the pine scriptĂ‚ Ă‚ 
official documentation we can view the plan scriptĂ‚ 
official reference manual this is the same thingĂ‚ Ă‚ 
but as a pop-up so if i click on this we have theĂ‚ 
reference manual pop up within our browser this isĂ‚ Ă‚ 
really useful when you're learning pan scriptĂ‚ 
if there's something that doesn't quite makeĂ‚ Ă‚ 
sense to you or something i haven't covered in theĂ‚ 
course material you can search through the searchĂ‚ Ă‚ 
box here for whatever that is so for exampleĂ‚ 
if we're looking at an rsi lesson and i don'tĂ‚ Ă‚ 
cover something in enough detail and you want toĂ‚ 
learn more then you can just click on this ta.rsiĂ‚ Ă‚ 
at the top of this list and that will give youĂ‚ 
the reference manual for this function and it willĂ‚ Ă‚ 
tell you in detail exactly how it works and howĂ‚ 
to use this function but let's close out of thatĂ‚ Ă‚ 
the next thing we have is our pine editor keyboardĂ‚ 
shortcuts this is a useful thing to look overĂ‚ Ă‚ 
and try to memorize some of these keyĂ‚ 
shortcuts if i click on this drop downĂ‚ Ă‚ 
box under pine editor we have a whole bunch ofĂ‚ 
keyboard shortcuts for use in the pine editorĂ‚ Ă‚ 
so for example you can save your script by justĂ‚ 
pressing ctrl s which is useful and then we haveĂ‚ Ă‚ 
a bunch of lesson resources here so the pinecodersĂ‚ 
and codify are kind of tutorials and examplesĂ‚ Ă‚ 
of how to use pine they're kind of just writtenĂ‚ 
guides one other thing to mention in regards toĂ‚ Ă‚ 
the pine editor keyboard shortcuts is that whenĂ‚ 
you're writing out your code pressing controlĂ‚ Ă‚ 
space will list all of the available functionsĂ‚ 
and inbuilt variables we have to work withĂ‚ Ă‚ 
so for example if i wanted to see what technicalĂ‚ 
analysis functions we have i can type t a dot andĂ‚ Ă‚ 
then control space will list all of the functionsĂ‚ 
we have that deal with technical analysis so hereĂ‚ Ă‚ 
we have the atr if i click on that we now have theĂ‚ 
atr so if i put in a period length here like taĂ‚ Ă‚ 
dot atr 14 if i wanted to plot this to myĂ‚ 
chart i could just cut this ctrl x and paste itĂ‚ Ă‚ 
into the plot function save the script andĂ‚ 
now we're plotting the atr onto my chartĂ‚ Ă‚ 
so this is the power of pinescript just how easyĂ‚ 
it is to get certain information and plot it ontoĂ‚ Ă‚ 
our chart but that's something to keep in mind theĂ‚ 
control space feature of pine script is extremelyĂ‚ Ă‚ 
valuable you'll be using that keyboard shortcutĂ‚ 
a lot in your coding and i believe it's commandĂ‚ Ă‚ 
space on a mac next up we have this littleĂ‚ 
button here which just renames your scriptĂ‚ Ă‚ 
then we have this favorite buttonĂ‚ 
if you click on this this will addĂ‚ Ă‚ 
your script to your favorites menu so when youĂ‚ 
click on the indicators and strategies buttonĂ‚ Ă‚ 
your favorite scripts will be at the top of theĂ‚ 
list up here mine are taking a while to loadĂ‚ Ă‚ 
because my internet sucks but yours should loadĂ‚ 
a lot quicker than that hopefully and you can seeĂ‚ Ă‚ 
here that these are all my favorite scripts someĂ‚ 
of them are other people's scripts and some ofĂ‚ Ă‚ 
them are my scripts then we have the search buttonĂ‚ 
here this is useful for finding and replacingĂ‚ Ă‚ 
things in your script so you won't be usingĂ‚ 
this very often on smaller or simpler scriptsĂ‚ Ă‚ 
but on longer and complex scripts this is a reallyĂ‚ 
useful tool especially for refactoring or changingĂ‚ Ă‚ 
parts of your code if you click on this littleĂ‚ 
button here that will drop down the replace textĂ‚ Ă‚ 
box and in here we could write something likeĂ‚ 
atr let's replace atr with rsi to do that you canĂ‚ Ă‚ 
click on this button which will replace the firstĂ‚ 
instance of this atr so the very first time thisĂ‚ Ă‚ 
tool finds atr in your script it will replaceĂ‚ 
it with rsi if we click on the second buttonĂ‚ Ă‚ 
it will replace all mentions of atr with rsiĂ‚ 
and if you want to add more filters to thisĂ‚ Ă‚ 
find and replace tool you can click on this buttonĂ‚ 
here and we can match case we can match the wholeĂ‚ Ă‚ 
word or you can use regular expressions which isĂ‚ 
basically patterns in text you don't need to worryĂ‚ Ă‚ 
about this unless you're an advanced programmerĂ‚ 
now the final thing worth mentioning here is thisĂ‚ Ă‚ 
button here this is your revision or scriptĂ‚ 
version button if i click on 1.0 that will loadĂ‚ Ă‚ 
into the pine editor the very first revisionĂ‚ 
of the script we have loaded into this editorĂ‚ Ă‚ 
so this will load the script before i made thisĂ‚ 
change of plotting the atr so let me click on 1.0Ă‚ Ă‚ 
and now we're back to our blank script andĂ‚ 
if i want to load the latest version of theĂ‚ Ă‚ 
script i can click on 2.0 and that will loadĂ‚ 
back the latest version as far as i can tellĂ‚ Ă‚ 
there is no limit to how many revisions every timeĂ‚ 
you save your script and make a change to yourĂ‚ Ă‚ 
script there will be a new revision added to thisĂ‚ 
list so i have scripts with hundreds of revisionsĂ‚ Ă‚ 
and i can drop back to any prior version of myĂ‚ 
script this is particularly useful for debuggingĂ‚ Ă‚ 
scripts so let's say you're trying to add a newĂ‚ 
feature to a script and after several changes toĂ‚ Ă‚ 
your script you realize you've broken somethingĂ‚ 
in the script and you can't work out what you didĂ‚ Ă‚ 
using this as a as a sort of undo button canĂ‚ 
undo any changes you've made to your scriptĂ‚ Ă‚ 
and reload a previous version so it's basicallyĂ‚ 
like an inbuilt backup tool in the pine editorĂ‚ Ă‚ 
anyway that's about all you need toĂ‚ 
know about the pine editor itselfĂ‚ Ă‚ 
in the next few lessons we'll cover someĂ‚ 
practical coding examples i'll see you there
now before we get into actually writing code it'sĂ‚ 
probably a good idea for me to go over some ofĂ‚ Ă‚ 
the basic things you will need to know about theĂ‚ 
compiler for pinescript so the compiler is whatĂ‚ Ă‚ 
compiles our code converts it into informationĂ‚ 
that trading views servers can understand so allĂ‚ Ă‚ 
of this english written code will turn intoĂ‚ 
computer code that the computer can run andĂ‚ Ă‚ 
execute our commands the pinescript compiler isĂ‚ 
responsible for doing that so when i click saveĂ‚ Ă‚ 
here i'm just going to delete this script after soĂ‚ 
i'll just name this delete my script just compiledĂ‚ Ă‚ 
and i know it compiled because i didn't getĂ‚ 
any errors if however we do something likeĂ‚ Ă‚ 
this let me get rid of one of these parenthesesĂ‚ 
here and save the script now we have a problemĂ‚ Ă‚ 
we have a syntax error this red line here now theĂ‚ 
wonderful thing about most programming languagesĂ‚ Ă‚ 
and compilers is that they will usually tell youĂ‚ 
where the error happened so we get the exact lineĂ‚ Ă‚ 
that this error occurred on it occurred on lineĂ‚ 
six this line here so that makes it really easyĂ‚ Ă‚ 
for us to you can even click on this um it'sĂ‚ 
a link that will take you to that line of codeĂ‚ Ă‚ 
in your script if i click on that it takes meĂ‚ 
straight there now every error message will beĂ‚ Ă‚ 
different depending on what you messed up in yourĂ‚ 
script a lot of these error messages are quiteĂ‚ Ă‚ 
cryptic so end of line without line continuationĂ‚ 
even i don't know what that really means um it'sĂ‚ Ă‚ 
kind of it's amusing to me some of the errorĂ‚ 
messages you get in some programming languagesĂ‚ Ă‚ 
sometimes they're really helpful sometimes you'reĂ‚ 
just like wow thanks compiler that was reallyĂ‚ Ă‚ 
helpful so now if i close this off and save myĂ‚ 
scripts we don't have any errors the compilerĂ‚ Ă‚ 
compiled our code without any problems let'sĂ‚ 
try getting rid of one of these quotation marksĂ‚ Ă‚ 
save the script now we get a different errorĂ‚ 
mismatched character this character here means weĂ‚ Ă‚ 
have a new line starting so pan script got toĂ‚ 
the end of this line and then a new line startedĂ‚ Ă‚ 
but it was expecting this quotationĂ‚ 
mark to close off this string of textĂ‚ Ă‚ 
and so it through an error on line five so most ofĂ‚ 
the problems in your script are going to be prettyĂ‚ Ă‚ 
obvious and intuitive when you see an error popĂ‚ 
up usually it means you didn't close something offĂ‚ Ă‚ 
you missed a comma you missed a bracket somethingĂ‚ 
like that and within just a few seconds of readingĂ‚ Ă‚ 
your code back you should be able to spot theĂ‚ 
problem now some errors will be a little moreĂ‚ Ă‚ 
difficult to solve but in part scriptĂ‚ 
it's very rare that you run into an errorĂ‚ Ă‚ 
that can't be solved very quickly because usuallyĂ‚ 
it's a result of a typo of some kind and you don'tĂ‚ Ă‚ 
even need to know what the error message meansĂ‚ 
you can just click on that line of code and seeĂ‚ Ă‚ 
at a glance that you missed something relatingĂ‚ 
to the syntax i will cover errors in your code inĂ‚ Ă‚ 
much more detail in its own section of the courseĂ‚ 
towards the end of the course so if you run intoĂ‚ Ă‚ 
any problems maybe jump ahead and check out thatĂ‚ 
section but the last thing i'm going to show youĂ‚ Ă‚ 
here is what happens if we get rid of the compilerĂ‚ 
directive if i get rid of that and save the scriptĂ‚ Ă‚ 
now we're going to get all kinds of weird problemsĂ‚ 
and errors with our code the reason for that isĂ‚ Ă‚ 
that the compiler now doesn't know what version ofĂ‚ 
panel script we're using so right now we are usingĂ‚ Ă‚ 
the original version of pinescript the versionlessĂ‚ 
version of pinescript the first iteration of thisĂ‚ Ă‚ 
language i have no idea what the syntax is forĂ‚ 
that language i wasn't around when that was aĂ‚ Ă‚ 
thing i started using pine script when it was inĂ‚ 
version three and so recently version five hasĂ‚ Ă‚ 
released so i've had to learn the new syntax ofĂ‚ 
pine script three times now i started with versionĂ‚ Ă‚ 
three so i learned version three syntax then itĂ‚ 
updated a version four and i learned version fourĂ‚ Ă‚ 
syntax now we're on version 5. the good newsĂ‚ 
is it's not that much different to version 4.Ă‚ Ă‚ 
if i set the version to 4 say the script we'llĂ‚ 
still get the same error because in version 4 ofĂ‚ Ă‚ 
pinescript we don't use the indicator annotationĂ‚ 
function we use the study annotation function toĂ‚ Ă‚ 
create an indicator so now my script just compiledĂ‚ 
fine and so it's very important that you keepĂ‚ Ă‚ 
this compiler directive in and that you get yourĂ‚ 
syntax correct in your code you don't need theseĂ‚ Ă‚ 
comments if you don't want them there this willĂ‚ 
compile just fine as well the reason why we haveĂ‚ Ă‚ 
this compiler directive that allows us to targetĂ‚ 
older versions of pine script is for backwardsĂ‚ Ă‚ 
compatibility so there are hundreds if notĂ‚ 
thousands of scripts that have been written fromĂ‚ Ă‚ 
the beginning of trading views existence that areĂ‚ 
still very useful even though they were writtenĂ‚ Ă‚ 
in an old version of pinescript and rather thanĂ‚ 
force all of those coders to update their codeĂ‚ Ă‚ 
to version 5 of pinescript tradingviews simplyĂ‚ 
allow you to target old versions of pinescriptĂ‚ Ă‚ 
so that you don't need to update your scripts forĂ‚ 
them to work the only reason you would want toĂ‚ Ă‚ 
update your script to version 5 of pinescriptĂ‚ 
is to take advantage of the new featuresĂ‚ Ă‚ 
of that version so i have dozens of indicatorsĂ‚ 
written in version 4. i'm not going to convertĂ‚ Ă‚ 
all of them into version 5 of pinescript exceptĂ‚ 
for the ones that i use regularly now one lastĂ‚ Ă‚ 
thing i want to mention about the compilerĂ‚ 
is that it is not particularly picky aboutĂ‚ Ă‚ 
where you put some of your syntax for exampleĂ‚ 
if you have a plot function that has a lot ofĂ‚ Ă‚ 
parameters which we'll cover in future lessons youĂ‚ 
can separate them across multiple lines now thereĂ‚ Ă‚ 
are some rules with this that takes some gettingĂ‚ 
used to but let me just quickly demonstrate what iĂ‚ Ă‚ 
mean by this so for example we could put a spaceĂ‚ 
between our plot and our parentheses save theĂ‚ Ă‚ 
script and this will work just fine no problems noĂ‚ 
compiler errors we can also split our parametersĂ‚ Ă‚ 
across multiple lines so if i press enter hereĂ‚ 
and i tab indent this line of code and i save theĂ‚ Ă‚ 
script we'll get an error in pinescript if youĂ‚ 
want to indent your parameters across multipleĂ‚ Ă‚ 
lines you need to put a space after your tab soĂ‚ 
now if i say the script this compiles just fineĂ‚ Ă‚ 
so the purpose of being able to do thisĂ‚ 
is maybe you have a bunch of parametersĂ‚ Ă‚ 
in your plot function that stretch way off yourĂ‚ 
screen you can separate these onto multiple linesĂ‚ Ă‚ 
to make it easier to read your code so forĂ‚ 
example if i put a comma here and then enterĂ‚ Ă‚ 
i could say offset equals one comma titleĂ‚ 
equals plot close comma a line width equals twoĂ‚ Ă‚ 
comma and let's set the color to color dot blackĂ‚ 
and then let's move this parenthesis down oneĂ‚ Ă‚ 
line now if i save the script this compiles justĂ‚ 
fine so we'll cover this sort of thing in futureĂ‚ Ă‚ 
lessons i just wanted to make you aware of theĂ‚ 
fact that the compiler is quite flexible when itĂ‚ Ă‚ 
comes to your syntax just so long as everythingĂ‚ 
is in the right order and you respect some ofĂ‚ Ă‚ 
the indentation rules which we'll cover in futureĂ‚ 
lessons as well but anyway i just wanted to makeĂ‚ Ă‚ 
you aware of the fact that we can do this sortĂ‚ 
of thing in pinescript the compiler will allow usĂ‚ Ă‚ 
to separate our code across multiple lines underĂ‚ 
certain circumstances as long as we respectĂ‚ Ă‚ 
certain syntax rules i'll leave it there becauseĂ‚ 
i don't want to overwhelm you guys with too muchĂ‚ Ă‚ 
information before we actually cover what all ofĂ‚ 
these parameters do but now at least you know thatĂ‚ Ă‚ 
when we get into future advanced lessons that thisĂ‚ 
sort of thing is possible if you need to separateĂ‚ Ă‚ 
your code across multiple lines anyway that aboutĂ‚ 
sums it up for the compiler i'm sure we will runĂ‚ Ă‚ 
into other issues that i haven't covered hereĂ‚ 
that we'll address in future lessons especiallyĂ‚ Ă‚ 
the error section of the course so i'll wrapĂ‚ 
this up here and i'll see you in the next lesson
all right traders welcome back i was thinkingĂ‚ 
while recording this material about my earlyĂ‚ Ă‚ 
days as a programmer and i thought this wouldĂ‚ 
be a fun lesson in this lesson we're just goingĂ‚ Ă‚ 
to plot the text hello world onto our chart nowĂ‚ 
technically we're already doing that you can seeĂ‚ Ă‚ 
i've got an indicator here indicator function withĂ‚ 
the title hello world and that's drawing under myĂ‚ Ă‚ 
chart but we're going to do something slightlyĂ‚ 
different we're going to plot a shape onto ourĂ‚ Ă‚ 
chart with the text hello world above it butĂ‚ 
before we do that i want to sort of reminisce onĂ‚ Ă‚ 
my early days as a programmer i first discoveredĂ‚ 
programming when i was in high school i wasĂ‚ Ă‚ 
playing a computer game called runescape it wasĂ‚ 
an online computer game and in that game youĂ‚ Ă‚ 
had to do a bunch of repetitive clicking tasks itĂ‚ 
was like an old school game kind of like pokemonĂ‚ Ă‚ 
where you had a little character that would runĂ‚ 
around the screen and you'd have to click onĂ‚ Ă‚ 
things to make him walk there or mine a rock orĂ‚ 
do some fishing or whatever now i can't rememberĂ‚ Ă‚ 
where i found out about this but at some point iĂ‚ 
discovered that some people were writing scriptsĂ‚ Ă‚ 
that were playing the game for them and performingĂ‚ 
some of the more repetitive tasks that weren't funĂ‚ Ă‚ 
so there were elements of the game that were a lotĂ‚ 
of fun and there were elements of the game thatĂ‚ Ă‚ 
were extremely monotonous and boring a lot likeĂ‚ 
trading actually i'm getting ahead of myself butĂ‚ Ă‚ 
that's kind of why i got into coding and tradingĂ‚ 
as well is i wanted to automate some of the moreĂ‚ Ă‚ 
boring tasks and hopefully this course will helpĂ‚ 
you do the same in your own trading but anywayĂ‚ Ă‚ 
back when i was a teenager i was around 14 or 15Ă‚ 
i discovered the world of coding in the context ofĂ‚ Ă‚ 
a computer game and so i was writing scriptsĂ‚ 
to perform tasks in this computer game thatĂ‚ Ă‚ 
i didn't want to do in real life because my timeĂ‚ 
was limited i had to be at school all day i'd getĂ‚ Ă‚ 
home from school and only have a couple of hoursĂ‚ 
to play and i didn't have time to do everythingĂ‚ Ă‚ 
i wanted to do and so i wrote these scriptsĂ‚ 
that would play the game while i was at schoolĂ‚ Ă‚ 
and it would level up my character and do allĂ‚ 
these things technically it was cheating and i didĂ‚ Ă‚ 
get banned for it in the end luckily in tradingĂ‚ 
automating your processes is completely legalĂ‚ Ă‚ 
we're allowed to do that in trading but anywayĂ‚ 
back when i was learning to code this was beforeĂ‚ Ă‚ 
the wealth of information on the internetĂ‚ 
you couldn't just google how to learn codingĂ‚ Ă‚ 
and be met with thousands of examples and guidesĂ‚ 
and i mean youtube was barely a thing when iĂ‚ Ă‚ 
first started learning to code and there wasĂ‚ 
not that much educational content on youtubeĂ‚ Ă‚ 
so i had to learn all of my coding fromĂ‚ 
books i had to buy books rent booksĂ‚ Ă‚ 
from the library and it was a really monotonousĂ‚ 
job learning how to code but i remember when iĂ‚ Ă‚ 
got my first hands on a coding book to learn javaĂ‚ 
and one of the first lessons in that book showedĂ‚ Ă‚ 
me how to write the words hello world into theĂ‚ 
command prompt on windows and i still remember theĂ‚ Ă‚ 
feeling of getting that short sentence to appearĂ‚ 
on my screen it was absolutely exhilarating to seeĂ‚ Ă‚ 
my own work my own hands telling the computer toĂ‚ 
output something nowadays that's kind of normalĂ‚ Ă‚ 
i mean everyone has some experience in scriptingĂ‚ 
or excel or just generally telling computers toĂ‚ Ă‚ 
do something on your behalf but back then it wasĂ‚ 
pretty revolutionary to me and so i hope that thisĂ‚ Ă‚ 
course can inspire that kind of feeling in youĂ‚ 
as we go through it i hope that you get excitedĂ‚ Ă‚ 
about what is possible with coding especiallyĂ‚ 
in pine script as you learn to translate yourĂ‚ Ă‚ 
trading ideas into code i hope you get excitedĂ‚ 
over the possibilities because they truly areĂ‚ Ă‚ 
almost infinite almost endless there areĂ‚ 
some limitations you'll run into as youĂ‚ Ă‚ 
learn to code but for the most part most basicĂ‚ 
processes can be done in code and you can automateĂ‚ Ă‚ 
a significant part or semi-automate at least aĂ‚ 
significant part of your trading process yourĂ‚ Ă‚ 
analysis process setup detection indicator signalsĂ‚ 
all of that stuff but anyway for this lessonĂ‚ Ă‚ 
let's just quickly write the words helloĂ‚ 
world onto our chart to do that i'm goingĂ‚ Ă‚ 
to use the plot shape function now thisĂ‚ 
plot shape function takes a booleanĂ‚ Ă‚ 
series meaning true or false so i'm just going toĂ‚ 
pass true in here and in fact that's all we needĂ‚ Ă‚ 
if i save the script we'll be having a cross drawnĂ‚ 
onto our chart you can see all these crosses hereĂ‚ Ă‚ 
drawing onto our chart um let's first of allĂ‚ 
set overlay to true so this is actually drawingĂ‚ Ă‚ 
onto price action so let me save the scriptĂ‚ 
remove that and add it back to my chartĂ‚ Ă‚ 
now let's add some text to this shape to do thatĂ‚ 
we can use the text argument or parameter so textĂ‚ Ă‚ 
equals and now let's write hello world and whenĂ‚ 
i save the script we're going to be getting helloĂ‚ Ă‚ 
world drawn all over our chart on every bar hereĂ‚ 
so let me save the script and you can see it'sĂ‚ Ă‚ 
pretty unreadable we have way too many shapesĂ‚ 
drawing onto our chart now so let's changeĂ‚ Ă‚ 
that really quickly let's just add on a booleanĂ‚ 
condition here let's say bar state dot is last soĂ‚ Ă‚ 
now this will only draw our shape on the last barĂ‚ 
on our chart so let me save the script there we goĂ‚ Ă‚ 
we are plotting the words hello world onto ourĂ‚ 
chart as simple as that literally one line ofĂ‚ Ă‚ 
code we have our indicator function you need thisĂ‚ 
line of code in every script same with this butĂ‚ Ă‚ 
if we get rid of this and i save it technicallyĂ‚ 
three lines of code and we're drawing text ontoĂ‚ Ă‚ 
our chart that is incredible it's amazing howĂ‚ 
far the world has come in technology back whenĂ‚ Ă‚ 
i was learning how to code it took over a dozenĂ‚ 
lines and a heck of a lot of times setting up theĂ‚ Ă‚ 
programming language java the runtime environmentĂ‚ 
finding a word editor a code editing they callĂ‚ Ă‚ 
them integrated development environments theyĂ‚ 
weren't even really a thing back when i wasĂ‚ Ă‚ 
learning how to code in java or at least theyĂ‚ 
weren't available to everyone for free and nowĂ‚ Ă‚ 
in 2021 as i'm recording this we can do allĂ‚ 
of this in our browser right through theĂ‚ Ă‚ 
tradingview platform in a handful of lines ofĂ‚ 
code it's just it's absolutely phenomenal it'sĂ‚ Ă‚ 
so exciting the world we live in and what isĂ‚ 
possible for everyday people like you and meĂ‚ Ă‚ 
so this is just the beginning of your journeyĂ‚ 
as a coder and the beginning of your journeyĂ‚ Ă‚ 
taking your trading to the next level so i hopeĂ‚ 
you're as excited as i am to teach you this stuffĂ‚ Ă‚ 
and as excited as i was to learn this stuff inĂ‚ 
the beginning believe it or not this right hereĂ‚ Ă‚ 
is the basic building blocks for every scriptĂ‚ 
you're ever going to write in pine scriptĂ‚ Ă‚ 
and it really is this simple you can obviouslyĂ‚ 
make this as complex as you want you can changeĂ‚ Ă‚ 
the shape we're drawing you can change theĂ‚ 
color you can change all kinds of things theĂ‚ Ă‚ 
location of the shape all sorts of things butĂ‚ 
it's not difficult it's not complex all it isĂ‚ Ă‚ 
is learning new habits and new rules codingĂ‚ 
is still in english this is all english wordsĂ‚ Ă‚ 
but the rules in which we express the commands weĂ‚ 
want to give pinescript are important but they areĂ‚ Ă‚ 
very simple so you will learn them throughout theĂ‚ 
course don't be afraid if this is the first timeĂ‚ Ă‚ 
you've ever learned any coding if you followĂ‚ 
the lessons step by step one after anotherĂ‚ Ă‚ 
you will be able to write your own scripts byĂ‚ 
the end especially if you take the whole masteryĂ‚ Ă‚ 
course so strap in we're going to cover someĂ‚ 
really exciting stuff in the lessons to comeĂ‚ Ă‚ 
i can't wait good luck with your tradingĂ‚ 
and good luck with your coding speak soon
all right traders so in this lesson i'm goingĂ‚ 
to be breaking down how comments work in pineĂ‚ Ă‚ 
scripts so comments are basically a wayĂ‚ 
of leaving notes in your code for yourselfĂ‚ Ă‚ 
or for other traders or coders who readĂ‚ 
your script so right here i have a blankĂ‚ Ă‚ 
indicator script using the defaultĂ‚ 
template that tradingview have createdĂ‚ Ă‚ 
and you can see two comments at the top here theseĂ‚ 
are automatically added to all scripts that youĂ‚ Ă‚ 
create through the pine editor this top commentĂ‚ 
here is saying that our source code is subjectĂ‚ Ă‚ 
to the terms of the mozilla public license andĂ‚ 
here's a link to that license if you want to readĂ‚ Ă‚ 
what this means here's the license here andĂ‚ 
just really quickly while we're here i figuredĂ‚ Ă‚ 
we might as well go over this as i'm sure someĂ‚ 
of you are interested in what what this meansĂ‚ Ă‚ 
if you haven't seen it before but basicallyĂ‚ 
this just limits your liability as a coderĂ‚ Ă‚ 
so it just says the covered software which is ourĂ‚ 
scripts is provided under this license on an as-isĂ‚ Ă‚ 
basis without warranty of any kind the entire riskĂ‚ 
as to the quality and performance of the coveredĂ‚ Ă‚ 
software is with you and you being the script userĂ‚ 
or anyone who uses your script basically is usingĂ‚ Ă‚ 
your script at their own risk most open sourceĂ‚ 
software will operate under a license like thisĂ‚ Ă‚ 
and then next up we have our copyright so this isĂ‚ 
a copyright and then it will automatically insertĂ‚ Ă‚ 
your trading view username here now both of theseĂ‚ 
comments are not read by the pinescript compilerĂ‚ Ă‚ 
what that means is when the engine that runs yourĂ‚ 
script through the tradingview platform does itsĂ‚ Ă‚ 
thing it ignores these lines it's like theyĂ‚ 
don't exist and this is all the compiler willĂ‚ Ă‚ 
see in fact it will really only see this so theseĂ‚ 
comments are really just for your eyes only andĂ‚ Ă‚ 
the way you write them is with two forward slashesĂ‚ 
you can put them anywhere they don't need to beĂ‚ Ă‚ 
at the start of the line they could be after theĂ‚ 
line so i could say here i declare my indicatorĂ‚ Ă‚ 
here we could say plot the close and you can evenĂ‚ 
use them to comment out code so let's say we wantĂ‚ Ă‚ 
to get the rsi here 14 period rsi on the closingĂ‚ 
price let's say there's a problem with our codeĂ‚ Ă‚ 
or there's something we want to test and we wantĂ‚ 
to temporarily remove this code without deletingĂ‚ Ă‚ 
it we can just add two comments in front of itĂ‚ 
and now this code is ignored by the compilerĂ‚ Ă‚ 
so that is the purpose of comments to leaveĂ‚ 
notes to yourself one other purpose whichĂ‚ Ă‚ 
we'll cover in future lessons is what's calledĂ‚ 
annotation comments so this right here is aĂ‚ Ă‚ 
annotation comment it's a special comment thatĂ‚ 
is not ignored by the compiler but technicallyĂ‚ Ă‚ 
isn't code you could think of this as kind ofĂ‚ 
like metadata if you're familiar with metadataĂ‚ Ă‚ 
so this is just telling pinescript that we want toĂ‚ 
work with version 5 of pinescript if we get rid ofĂ‚ Ă‚ 
this and compile our script we're now dealingĂ‚ 
with the original version of pinescript whichĂ‚ Ă‚ 
had no version and we're going to get all kindsĂ‚ 
of errors because of that because the syntax ofĂ‚ Ă‚ 
version five of pine script is very differentĂ‚ 
to prior versions and by syntax i just meanĂ‚ Ă‚ 
the grammar of the language the way we express ourĂ‚ 
code is different in version five than any otherĂ‚ Ă‚ 
version if i bring back that annotation commentĂ‚ 
and save the script now it compiles just fineĂ‚ Ă‚ 
so that's the purpose of comments we'll be usingĂ‚ 
them a lot in our scripts i highly encourage youĂ‚ Ă‚ 
to comment your scripts heavily it's always betterĂ‚ 
to have too many comments and not enough commentsĂ‚ Ă‚ 
the reason for that is there will be times whenĂ‚ 
you write a script and you'll come back to itĂ‚ Ă‚ 
weeks maybe even months or years later and youĂ‚ 
might not remember why you did things a certainĂ‚ Ă‚ 
way or why your code was written the way it wasĂ‚ 
having comments can help remind you why you didĂ‚ Ă‚ 
what you did and it can help other traders knowĂ‚ 
what you're trying to do with your code as wellĂ‚ Ă‚ 
so if you're writing public open source scripts iĂ‚ 
definitely encourage you to comment them and it'sĂ‚ Ă‚ 
just a good idea in general to get into a habitĂ‚ 
of commenting your code regularly and extensivelyĂ‚ Ă‚ 
so that you know what you're doing and it's kindĂ‚ 
of like um if you remember in school when youĂ‚ Ă‚ 
did math stuff you always had to show your workĂ‚ 
you needed to explain how you got to your answerĂ‚ Ă‚ 
comments are basically the sameĂ‚ 
thing using comments can explainĂ‚ Ă‚ 
how you got to where you got to in terms of yourĂ‚ 
solution in your code if i open up a script i'veĂ‚ Ă‚ 
been working on recently which is my zen libraryĂ‚ 
you can see that this script has i think moreĂ‚ Ă‚ 
comments in it than actual lines of code in factĂ‚ 
it certainly does have more comments in it thanĂ‚ Ă‚ 
actual lines of code now that's not a normal thingĂ‚ 
you wouldn't normally see that in most scripts butĂ‚ Ă‚ 
because this is a library and we'll get intoĂ‚ 
libraries in future lessons i don't want toĂ‚ Ă‚ 
overwhelm you with too much information but eachĂ‚ 
comment here explains what each block of code doesĂ‚ Ă‚ 
in great detail the reason for this is that thisĂ‚ 
library is designed so that i could give it to aĂ‚ Ă‚ 
complete stranger who knows nothing about me whoĂ‚ 
knows nothing about how i like to code and theyĂ‚ Ă‚ 
can work out exactly exactly to a t what everyĂ‚ 
single part of my code does if i got rid of allĂ‚ Ă‚ 
of these comments the code becomes significantlyĂ‚ 
harder to read because there is no explanationĂ‚ Ă‚ 
on um what is happening if we get rid of allĂ‚ 
of these comments and we're just left with thisĂ‚ Ă‚ 
this is not useful to most traders mostĂ‚ 
traders will see this and they'll justĂ‚ Ă‚ 
move on to a different script becauseĂ‚ 
no one has the time to reverse engineerĂ‚ Ă‚ 
and deconstruct exactly what this code doesĂ‚ 
but by having all these comments suddenly thisĂ‚ Ă‚ 
script is much much easier to read for anyoneĂ‚ 
who um is unfamiliar with what the code doesĂ‚ Ă‚ 
so that's the purpose of comments we'llĂ‚ 
be using them a lot throughout the courseĂ‚ Ă‚ 
so as i said before it's a good idea to get intoĂ‚ 
a habit of using them but don't worry you'll seeĂ‚ Ă‚ 
lots of examples of how to use comments in yourĂ‚ 
code in future lessons so this doesn't quiteĂ‚ Ă‚ 
make sense to you don't worry it will by theĂ‚ 
end of the course see you in the next lesson
all right hey traders welcome back in thisĂ‚ 
lesson we're going to be covering the indicatorĂ‚ Ă‚ 
annotation function now annotation function couldĂ‚ 
be also thought of as like a directive functionĂ‚ Ă‚ 
it's a special function that tells pan scriptĂ‚ 
important information about the general natureĂ‚ Ă‚ 
of your script so this comment here of at versionĂ‚ 
equals five is what's called a compiler directiveĂ‚ Ă‚ 
so this is directing the compiler to version 5Ă‚ 
of pinescript it's telling pinescript the engineĂ‚ Ă‚ 
that runs pinescript to treat the code of thisĂ‚ 
script by the rules of version 5 of pinescriptĂ‚ Ă‚ 
the indicator annotation function does the exactĂ‚ 
same thing except that it tells pine script toĂ‚ Ă‚ 
treat the code of this script as an indicator theĂ‚ 
reason we need to use this is because there are aĂ‚ Ă‚ 
couple of different variations of script typesĂ‚ 
so we have strategy scripts so if i call thisĂ‚ Ă‚ 
strategy that changes the rules of this scriptĂ‚ 
if i say library that changes the rules againĂ‚ Ă‚ 
so we only have three to work with indicatorĂ‚ 
strategy and library but each of those haveĂ‚ Ă‚ 
different rules that govern your script for thisĂ‚ 
lesson we're going to break down the indicatorĂ‚ Ă‚ 
function which is probably the script type thatĂ‚ 
you'll be working with the most especially in theĂ‚ Ă‚ 
beginning of your pine script journey so let'sĂ‚ 
break down the parameters of this function so byĂ‚ Ă‚ 
calling this an indicator function that givesĂ‚ 
the script access to a different subset ofĂ‚ Ă‚ 
inbuilt functions and commands to work with so forĂ‚ 
example let's try and use a strategy function hereĂ‚ Ă‚ 
let's say strategy.close and we need to pass an idĂ‚ 
here let's just say close our buy trade if i saveĂ‚ Ă‚ 
this script we'll get an error here that says youĂ‚ 
cannot use strategy functions in indicator scriptsĂ‚ Ă‚ 
please replace indicator with strategy so that'sĂ‚ 
why we need to use these annotation functions ifĂ‚ Ă‚ 
i change this to strategy suddenly the script isĂ‚ 
treated differently by the pine script compiler itĂ‚ Ă‚ 
has different functionality and features but let'sĂ‚ 
stick with indicator for this lesson and let'sĂ‚ Ă‚ 
break down what we can do with this indicatorĂ‚ 
function so if i hover my mouse over indicator andĂ‚ Ă‚ 
hold down control or command on a mac and clickĂ‚ 
on this function on the name of this functionĂ‚ Ă‚ 
we'll get the pinescript language referenceĂ‚ 
manual popping up here and this will tell usĂ‚ Ă‚ 
all of the various arguments we have to work withĂ‚ 
here it'll give us a description of each one i'mĂ‚ Ă‚ 
going to break down the most common ones here thatĂ‚ 
you will most likely use in most of your scriptsĂ‚ Ă‚ 
so let's get rid of our title for a moment hereĂ‚ 
and hover my mouse over this function name andĂ‚ Ă‚ 
we'll get a list of arguments so the first oneĂ‚ 
we have is title so this is your long titleĂ‚ Ă‚ 
so let's say i want to call this my super fancyĂ‚ 
script and if i save this and move this down aĂ‚ Ă‚ 
bit you can see that my script name has changedĂ‚ 
there and if i were to publish this script to theĂ‚ Ă‚ 
trading view library this is what it would beĂ‚ 
called on the trading view library this is itsĂ‚ Ă‚ 
long title this is its more descriptive title butĂ‚ 
you can see that it takes up quite a lot of spaceĂ‚ Ă‚ 
on my chart and if we had a lot of plotsĂ‚ 
here a lot of numbers plotting out hereĂ‚ Ă‚ 
having a long indicator title name could be aĂ‚ 
problem so let's say we want to abbreviate this toĂ‚ Ă‚ 
msfs to do that we just add in a comma here allĂ‚ 
of our arguments in any function are separated byĂ‚ Ă‚ 
commas so to add another argument or parameterĂ‚ 
to this function we need to put a comma thereĂ‚ Ă‚ 
and then i need to pass in short title and thenĂ‚ 
this is a string as well so we open up withĂ‚ Ă‚ 
two quotations and let's say we want to call thisĂ‚ 
msfs now if i save the script watch our title nameĂ‚ Ă‚ 
here it's now abbreviated to msfs but our officialĂ‚ 
title is still my super fancy script and if i wereĂ‚ Ă‚ 
to publish this to the training view library thatĂ‚ 
is still what this script would be called but whenĂ‚ Ă‚ 
it's added to our chart this is what it will sayĂ‚ 
in the indicator box so that's how our titles workĂ‚ Ă‚ 
next up we have overlay so by default overlayĂ‚ 
is set to false so if i save the script havingĂ‚ Ă‚ 
it set to false draws into its own box you can seeĂ‚ 
we're in our own oscillator box here like the rsiĂ‚ Ă‚ 
or stochastics macd those sorts of indicators theyĂ‚ 
are drawn into their own indicator box they're notĂ‚ Ă‚ 
drawn directly on the price action like a movingĂ‚ 
average would for example but if you want to drawĂ‚ Ă‚ 
overprice action say you're working with a movingĂ‚ 
average script for example you want overlay to beĂ‚ Ă‚ 
set to true overlay just means it overlays overĂ‚ 
your price action so if we set this to true andĂ‚ Ă‚ 
save the script first nothing will happen becauseĂ‚ 
we've already added the script to our chart so toĂ‚ Ă‚ 
update this particular parameter we need to removeĂ‚ 
our script by clicking on this remove button hereĂ‚ Ă‚ 
and then re-add it to our chart so now whenĂ‚ 
i click add to chart overlay will be updatedĂ‚ Ă‚ 
and it will be drawing over price action so if iĂ‚ 
zoom in here you can see that this script is justĂ‚ Ă‚ 
plotting the closing price so it's connecting theĂ‚ 
closing price of all of these candles on my chartĂ‚ Ă‚ 
directly over price action so that's what overlayĂ‚ 
does next up we have format so let's set overlayĂ‚ Ă‚ 
back to false and remove the script save it addĂ‚ 
it back to my chart and let's say we want to plotĂ‚ Ă‚ 
volume instead of the closing price so now let'sĂ‚ 
save the script and it will update whoops we'llĂ‚ Ă‚ 
update and now it's plotting the volume of eachĂ‚ 
bar on my chart let me go to the daily so thatĂ‚ Ă‚ 
we get a higher number here so notice this numberĂ‚ 
down here is an exact number of the volume printedĂ‚ Ă‚ 
um it's not that readable we don't really needĂ‚ 
to know the exact quantity of shares traded onĂ‚ Ă‚ 
on a stock or in the crypto market how many of aĂ‚ 
particular crypto asset was traded in this caseĂ‚ Ă‚ 
we're dealing with ticks price ticks on theĂ‚ 
forex market tick volume we don't need to knowĂ‚ Ă‚ 
the precise number so if you are dealing thisĂ‚ 
is mostly useful for volume this i don't reallyĂ‚ Ă‚ 
know of any other use cases where you'd wantĂ‚ 
to do this but this format parameter you canĂ‚ Ă‚ 
see down there above the blue underline format canĂ‚ 
be set to format dot and if i press control spaceĂ‚ Ă‚ 
or command space on a mac you get a list of theĂ‚ 
various formatting options we have to work withĂ‚ Ă‚ 
here i'm not going to go over all of these youĂ‚ 
can play around with these in your own time toĂ‚ Ă‚ 
see what they do it's not often you will need toĂ‚ 
change the format of your script anyway but inĂ‚ Ă‚ 
today's example we're dealing with volume rightĂ‚ 
now so let's click on volume and save the scriptĂ‚ Ă‚ 
now keep an eye on this number here it's nowĂ‚ 
changed to a smaller number with a k after itĂ‚ Ă‚ 
so if i hover over the last bar on myĂ‚ 
chart that says 6.037 k so roughly 6 000Ă‚ Ă‚ 
volume on this latest bar so that's the purposeĂ‚ 
of the format parameter in the indicator functionĂ‚ Ă‚ 
next up we have precision this is your decimalĂ‚ 
precision so let me get rid of this formatĂ‚ Ă‚ 
for now and let me get rid of this reallyĂ‚ 
quickly and save the script to update itĂ‚ Ă‚ 
um the precision argument changesĂ‚ 
your decimal precision of theĂ‚ Ă‚ 
numbers that draw onto the chart in your scriptĂ‚ 
so by default the precision is going to be set toĂ‚ Ă‚ 
the decimal precision of your price so right nowĂ‚ 
we have five decimals here on euro dollar if weĂ‚ Ă‚ 
were to go to a different market like uh bitcoinĂ‚ 
for example now we have a two decimal precisionĂ‚ Ă‚ 
and you can see after the decimal place hereĂ‚ 
we have two digits but if we wanted to overrideĂ‚ Ă‚ 
that to a higher precision or a lower precisionĂ‚ 
we can do that using the precision argument inĂ‚ Ă‚ 
this function so let me set this to 5 and save theĂ‚ 
script now you can see 5 decimal places after theĂ‚ Ă‚ 
decimal place on this script now in thisĂ‚ 
particular case we're just plotting theĂ‚ Ă‚ 
closing price so that's not that useful seeingĂ‚ 
five extra or three extra decimal placesĂ‚ Ă‚ 
on this price value but what if we wereĂ‚ 
plotting other values other indicator valuesĂ‚ Ă‚ 
where we wanted to see those extra decimal placesĂ‚ 
but we couldn't see them because of the numberĂ‚ Ă‚ 
of decimals on our price so for example whatĂ‚ 
about a relative volume indicator let's say weĂ‚ Ă‚ 
want to compare today's volume or this bar'sĂ‚ 
volume to the average over the past 10 daysĂ‚ Ă‚ 
and we want to know that number to a three decimalĂ‚ 
precision instead of just two that's where thisĂ‚ Ă‚ 
argument comes into handy you can changeĂ‚ 
the precision override the precision ofĂ‚ Ă‚ 
your indicator plot values with this argument weĂ‚ 
will be using this in future lessons but for nowĂ‚ Ă‚ 
we can move on i'll show you practical examples ofĂ‚ 
this later on in the course next up we have scaleĂ‚ Ă‚ 
so this is the axis the scale axis you wantĂ‚ 
to plot your values onto so by default yourĂ‚ Ă‚ 
indicator is going to plot onto whatever priceĂ‚ 
access you have on your chart so in this case weĂ‚ Ă‚ 
have price plotting onto our right axis but if weĂ‚ 
wanted this to plot onto our left axis for exampleĂ‚ Ă‚ 
you can use the scale argument here so we can setĂ‚ 
scale to scale dot control space let's set it toĂ‚ Ă‚ 
left and save the script and we need to remove theĂ‚ 
script to update this and add it back to our chartĂ‚ Ă‚ 
now you can see that our our values here are beingĂ‚ 
plotted onto the left scale not the right scaleĂ‚ Ă‚ 
so again not often you will need to do this butĂ‚ 
there is the option to if you need to let's getĂ‚ Ă‚ 
rid of that for now save the script remove it addĂ‚ 
it back to our chart so whenever you're dealingĂ‚ Ă‚ 
with a visual update like scale or overlay youĂ‚ 
need to remove the script and re-add it back toĂ‚ Ă‚ 
your chart so if you make a change to your scriptĂ‚ 
and it doesn't update a general good rule of thumbĂ‚ Ă‚ 
is to try removing it had it back on that willĂ‚ 
force tradingview to update your script entirelyĂ‚ Ă‚ 
now the next parameter we have to work with is maxĂ‚ 
bars back this is not something you will often useĂ‚ Ă‚ 
in your scripts i've never used it in my scriptsĂ‚ 
to be honest but it's here if you need to use itĂ‚ Ă‚ 
now this tells the script how many bars back itĂ‚ 
can reference from whichever current bar it'sĂ‚ Ă‚ 
calculating on so if we're looking at the finalĂ‚ 
bar on my chart and we set max bars back to 10Ă‚ Ă‚ 
for example and i save the script whatever codeĂ‚ 
is running on this last script this last bar on myĂ‚ Ă‚ 
chart the script can only reference the past 1 2 3Ă‚ 
4 5 6 7 8 9 10 bars so only this price action hereĂ‚ Ă‚ 
now personally in my own coding i've writtenĂ‚ 
dozens of scripts over the years i've neverĂ‚ Ă‚ 
used this feature i've never used this max barsĂ‚ 
back i've never had to i've never had a reason toĂ‚ Ă‚ 
but if you do run into a situation where you needĂ‚ 
to use this function this feature this is how youĂ‚ Ă‚ 
would do it you can set your max bars back toĂ‚ 
10 just keep in mind that this is in referenceĂ‚ Ă‚ 
to the current bar so when your script isĂ‚ 
running over historical price action eachĂ‚ Ă‚ 
bar on your chart can look back 10 bars and thenĂ‚ 
this bar can look back 10 bars and then this oneĂ‚ Ă‚ 
etc so that's how that argument works but let'sĂ‚ 
get rid of it for now since we would rarely useĂ‚ Ă‚ 
that the final parameter i'm going to cover hereĂ‚ 
that is commonly used is time frame so time frameĂ‚ Ă‚ 
specifies what time frame your script can runĂ‚ 
on so for example let's drop down to the fourĂ‚ Ă‚ 
hour chart and change this to d short for dailyĂ‚ 
and save the script now my script is plottingĂ‚ Ă‚ 
the daily time frame closing price onto my chartĂ‚ 
instead of the four hour so if i zoom in hereĂ‚ Ă‚ 
you can see that each time this updates weĂ‚ 
get a new value down here so on this bar hereĂ‚ Ă‚ 
a new day began and the previous day's closingĂ‚ 
price was five four six nine zero and then on theĂ‚ Ă‚ 
bars in between we get n a because these fourĂ‚ 
hour bars are plotting in between the next dayĂ‚ Ă‚ 
and then on that next day's beginning we get theĂ‚ 
previous day's closing price which was five sevenĂ‚ Ă‚ 
four eight seven and so on so that's one wayĂ‚ 
to hard code your script to only reference aĂ‚ Ă‚ 
particular time frame i wouldn't recommend doingĂ‚ 
this there are better ways to do this depending onĂ‚ Ă‚ 
the script if you have a script that you only wantĂ‚ 
to reference a particular time frame you mightĂ‚ Ă‚ 
want to hard code it this way but if you have aĂ‚ 
script that you want to be able to use on multipleĂ‚ Ă‚ 
time frames you're better off using user inputsĂ‚ 
to specify the time frame of your script and weĂ‚ Ă‚ 
will cover that in future lessons i don't want toĂ‚ 
overload you with too much information so we'llĂ‚ Ă‚ 
leave this lesson here if you want to see whatĂ‚ 
these other parameters do again just hold ctrlĂ‚ Ă‚ 
and click on this function name and you'll get aĂ‚ 
list of the arguments we have here with detailedĂ‚ Ă‚ 
descriptions on how they work but anyway that'llĂ‚ 
do it for this lesson i'll see you in the next one
hey traders in this short lesson i will introduceĂ‚ 
you to the concept of namespaces so namespacesĂ‚ Ă‚ 
in pine script are what we call the keywordsĂ‚ 
that contain certain functions or variables soĂ‚ Ă‚ 
that might sound a bit confusing but when i showĂ‚ 
you a visual example it should make perfect senseĂ‚ Ă‚ 
there are several namespaces that we have accessĂ‚ 
to in our pine script code the first one i'm goingĂ‚ Ă‚ 
to show you is the technical analysis namespace soĂ‚ 
for example let's say we want to get the rsi valueĂ‚ Ă‚ 
to do that i can create a new variable calledĂ‚ 
rsi and we can assign this variable or initializeĂ‚ Ă‚ 
this variable using the equal sign so rsi isĂ‚ 
set to and then we need to reference the taĂ‚ Ă‚ 
or technical analysis namespace so if iĂ‚ 
write ta here then a full stop and thenĂ‚ Ă‚ 
control space or command space on a mac we get aĂ‚ 
list of all of our technical analysis functionsĂ‚ Ă‚ 
so if i start writing rsi here is our rsiĂ‚ 
function and you can see here that it isĂ‚ Ă‚ 
underneath the ta or technical analysis namespaceĂ‚ 
so we'll click on that we pass in as price sourceĂ‚ Ă‚ 
close and 14 default period there we go we haveĂ‚ 
an rsi value and if i cut this plot down hereĂ‚ Ă‚ 
and change the plot from the closing price to theĂ‚ 
rsi value we just retrieved let's save the scriptĂ‚ Ă‚ 
we're now plotting the rsi so pinescript makesĂ‚ 
it really easy to access all of the most popularĂ‚ Ă‚ 
indicator tools that traders typically use andĂ‚ 
you'll find all of your indicator functionsĂ‚ Ă‚ 
under the ta namespace so that's really itĂ‚ 
for namespaces that's all you really need toĂ‚ Ă‚ 
know you can also create your own librariesĂ‚ 
um we'll cover that later on in the courseĂ‚ Ă‚ 
but when you create your own custom library itĂ‚ 
behaves pretty much identically to these inbuiltĂ‚ Ă‚ 
name spaces so for example if i type in import zenĂ‚ 
and the art of trading control space here is oneĂ‚ Ă‚ 
of my libraries i've created if i import this asĂ‚ 
zen i can now reference my zen library namespaceĂ‚ Ă‚ 
the same way we did the technical analysisĂ‚ 
namespace so for example if i write here zen dotĂ‚ Ă‚ 
control space command space we get a list ofĂ‚ 
all of my custom functions these are functionsĂ‚ Ă‚ 
that i created that i wrote in my own codeĂ‚ 
in the zen library script and i publishedĂ‚ Ă‚ 
this to the trading view platform i can nowĂ‚ 
reference all of these functions the same wayĂ‚ Ă‚ 
we can reference the inbuilt functions thatĂ‚ 
tradingview offers what pinescript offersĂ‚ Ă‚ 
but as i said we'll cover libraries in a futureĂ‚ 
lesson because it is quite an advanced concept soĂ‚ Ă‚ 
i just wanted to make you aware of that featureĂ‚ 
and so you can basically think of a namespace asĂ‚ Ă‚ 
a collection of functions or tools that you'veĂ‚ 
written either you've written or the tradingĂ‚ Ă‚ 
view pan script developers have created so beforeĂ‚ 
we end this lesson um we'll go over just a coupleĂ‚ Ă‚ 
of other namespaces for example if we're dealingĂ‚ 
with strings so text this is a string right hereĂ‚ Ă‚ 
if i create a variable called my stringĂ‚ 
and we just call this a string ofĂ‚ Ă‚ 
text i can now use the string name space so strĂ‚ 
is short for string if i put in a dot and thenĂ‚ Ă‚ 
control space or command space we get a list ofĂ‚ 
all of the different functions we can use relatingĂ‚ Ă‚ 
to strings so for example um if i set this toĂ‚ 
the text 10 and then i use the two two numberĂ‚ Ă‚ 
function and we pass in my string if i cut thatĂ‚ 
and paste that into our plot and save the scriptĂ‚ Ă‚ 
we are now plotting the number 10 after usingĂ‚ 
the string namespace to convert this textĂ‚ Ă‚ 
into a number and there are a whole bunch ofĂ‚ 
other namespaces we have the strategy namespaceĂ‚ Ă‚ 
when you're dealing with strategy scripts so youĂ‚ 
have your trade commands and various variablesĂ‚ Ă‚ 
that you can reference such as your currentĂ‚ 
account balance and things like thatĂ‚ Ă‚ 
we also have the request namespace which we'llĂ‚ 
cover in future lessons this is where you retrieveĂ‚ Ă‚ 
or request financial data about stocks you canĂ‚ 
reference quandl which is a third-party resourceĂ‚ Ă‚ 
uh that offers all sorts of things like umĂ‚ 
sentiment interest rate information all kindsĂ‚ Ă‚ 
of stuff we'll cover quandl in a future lesson andĂ‚ 
then we also have the security function here whichĂ‚ Ă‚ 
we use to reference other symbols and other timeĂ‚ 
frames so you use the request.security functionĂ‚ Ă‚ 
to request for example we're on euro dollarĂ‚ 
one hour chart here maybe i want to checkĂ‚ Ă‚ 
what the daily high and low is and then plotĂ‚ 
that information over this one hour intradayĂ‚ Ă‚ 
time frame to do that we would use theĂ‚ 
request.security function and so that isĂ‚ Ă‚ 
a quick crash course on pine scriptsĂ‚ 
namespaces and we'll be using these frequentlyĂ‚ Ă‚ 
in our scripts so you'll get used to usingĂ‚ 
these and you'll soon memorize them all veryĂ‚ Ă‚ 
quickly anyway that's it for this lessonĂ‚ 
i will speak with you in the next one
all right traders welcome to the price seriesĂ‚ 
lesson in this lesson i'm going to be showingĂ‚ Ă‚ 
you how to reference candlestick price data soĂ‚ 
you're open high low and close this is obviouslyĂ‚ Ă‚ 
going to make up the backbone of most of yourĂ‚ 
scripts you're going to need to reference thisĂ‚ Ă‚ 
data very frequently in your scripts since what isĂ‚ 
price analysis without referencing price so it'sĂ‚ Ă‚ 
quite simple to access this data obviously theĂ‚ 
challenge comes with what you do with this dataĂ‚ Ă‚ 
first of all let me get rid of precision oneĂ‚ 
that's from a previous lesson so here i have aĂ‚ Ă‚ 
blank script let me save the script to show youĂ‚ 
so we're just plotting the closing price ontoĂ‚ Ă‚ 
our chart so by default this is the defaultĂ‚ 
template for any new script we already haveĂ‚ Ă‚ 
our closing data being referenced and to referenceĂ‚ 
the other price points on your chart is as simpleĂ‚ Ă‚ 
as you might think so let's say we want to getĂ‚ 
our candle open price i'm going to declare a newĂ‚ Ă‚ 
variable here called candle open and it's goingĂ‚ 
to be set to the open price value next up we'llĂ‚ Ă‚ 
get our high so candle high is set to high then weĂ‚ 
can get our candle low is set to the low and thenĂ‚ Ă‚ 
candle close is set to the close now you don'tĂ‚ 
normally need to do this you can just directlyĂ‚ Ă‚ 
reference open high low and close in your scriptĂ‚ 
but for today's example i thought this might beĂ‚ Ă‚ 
a little more intuitive to see what's going onĂ‚ 
here so now let's save the script and let's drawĂ‚ Ă‚ 
these values onto our chart so i'm going to use aĂ‚ 
comment here say draw candle data onto our chartĂ‚ Ă‚ 
i'm going to paste candle open in there copy thisĂ‚ 
and i'll paste it four times and i'll just pasteĂ‚ Ă‚ 
in each variable here and let's change the colorĂ‚ 
of each plot because if i save the script nowĂ‚ Ă‚ 
we're going to get four blue lines drawingĂ‚ 
on our chart and we can't tell what's whatĂ‚ Ă‚ 
so let's set the color of the open to color dotĂ‚ 
blue and now i'm going to copy this line of codeĂ‚ Ă‚ 
and paste it in and let's change the color ofĂ‚ 
each data value here let's change the high to redĂ‚ Ă‚ 
we'll change the low to green and let's changeĂ‚ 
the clothes to purple and save the script weĂ‚ Ă‚ 
should get four different colors plotting onto ourĂ‚ 
chart here now and each color corresponds to eachĂ‚ Ă‚ 
candle price so it's as simple as that to getĂ‚ 
our price data which is what we're doing hereĂ‚ Ă‚ 
so now let's say we want to do something usefulĂ‚ 
with this data let's say we want to get theĂ‚ Ă‚ 
current bar's size in points so we want toĂ‚ 
subtract the high from the low so now let'sĂ‚ Ă‚ 
create a new section of code here and i'll sayĂ‚ 
analyze price data and for this we're going toĂ‚ Ă‚ 
create a couple of variables the first variableĂ‚ 
we create is going to be called candle size andĂ‚ Ă‚ 
to calculate the candle size is really easy allĂ‚ 
we need to do is subtract the low from the highĂ‚ Ă‚ 
so high minus low will give us our candle sizeĂ‚ 
in points and let's plot this onto our chartĂ‚ Ă‚ 
as well so we'll call this candle size we'll setĂ‚ 
this to color dot orange and in fact let's commentĂ‚ Ă‚ 
out all of our open high low close because weĂ‚ 
don't really need to see that data on our chartĂ‚ Ă‚ 
save the script we should be getting an orangeĂ‚ 
line drawing here now which is showing the candleĂ‚ Ă‚ 
size in points or pips in this case so right nowĂ‚ 
as i hover over the last bar on my chart hereĂ‚ Ă‚ 
our candle size is 6.9 pips so as you can see it'sĂ‚ 
really easy to analyze price data obviously itĂ‚ Ă‚ 
gets more complex the more complex analysis you'reĂ‚ 
doing but it's really simple to access this dataĂ‚ Ă‚ 
and to analyze it let's try something a littleĂ‚ 
bit more complicated to demonstrate the historicalĂ‚ Ă‚ 
operator so let's say we want to referenceĂ‚ 
a historical bar on our chart this willĂ‚ Ă‚ 
reference the current bar and this will run onĂ‚ 
every bar in our chart so as i hover my mouseĂ‚ Ă‚ 
let's hover over this bar here which is quiteĂ‚ 
large if i hover my mouse over that you canĂ‚ Ă‚ 
see down here this number says 65.2 pips soĂ‚ 
this script is running on each historical barĂ‚ Ă‚ 
on our chart and so every bar on our chartĂ‚ 
will reference its own open high low and closeĂ‚ Ă‚ 
but what if we wanted to reference let's sayĂ‚ 
10 bars back so we want to reference 10 barsĂ‚ Ă‚ 
in the past let's go back to our current bar andĂ‚ 
let's say we want to compare the current bar'sĂ‚ Ă‚ 
candle size to the candle size of a bar 10 barsĂ‚ 
ago to do that is really simple let's just createĂ‚ Ă‚ 
a new variable here called candle size 10. andĂ‚ 
we'll set that to the high of the bar 10 bars agoĂ‚ Ă‚ 
minus the low of the bar 10 bars ago so toĂ‚ 
reference historical data we use square bracketsĂ‚ Ă‚ 
and then we pass in a number an integer number aĂ‚ 
whole number this whole number is your look backĂ‚ Ă‚ 
count so this will get the candle sizeĂ‚ 
of the bar 10 bars ago so let's compareĂ‚ Ă‚ 
these two values to do that we'll say candle sizeĂ‚ 
ratio and we will divide our current candle sizeĂ‚ Ă‚ 
by the candle size 10 bars ago and let's plotĂ‚ 
that onto our chart as well candle size ratioĂ‚ Ă‚ 
with the color of purple so when i save theĂ‚ 
script we'll be getting another line drawingĂ‚ Ă‚ 
onto our chart that is comparing the current bar'sĂ‚ 
size to the bar size 10 bars ago save the scriptĂ‚ Ă‚ 
and there we go so the current bar is 130 percentĂ‚ 
the size of the bar from 10 bars ago or 1.3 timesĂ‚ Ă‚ 
and it's that simple that's how we referenceĂ‚ 
historical price data very easy and thisĂ‚ Ă‚ 
historical operator works on any series of dataĂ‚ 
that we can reference in pine script so thisĂ‚ Ă‚ 
applies to indicator values as well it appliesĂ‚ 
to basically anything that has a series of dataĂ‚ Ă‚ 
that we can reference so for example let's sayĂ‚ 
we wanted to get the atr value of the current barĂ‚ Ă‚ 
to do that we could just create a new variableĂ‚ 
here called atr is set to ta.atr and let's justĂ‚ Ă‚ 
pass in the default 14 length periodĂ‚ 
so this will get the current atr valueĂ‚ Ă‚ 
and let's say we wanted to plot the atrĂ‚ 
from 10 bars ago we can do so just like thatĂ‚ Ă‚ 
so this plot function now will reference theĂ‚ 
atr value from 10 bars ago save the script andĂ‚ Ă‚ 
now our white number here is drawing theĂ‚ 
atr of 10 bars ago really really simpleĂ‚ Ă‚ 
to reference historical data and this appliesĂ‚ 
to anything as i said we could reference theĂ‚ Ă‚ 
volume from 100 bars ago if we wanted to let's sayĂ‚ 
234 bars ago save the script now our white lineĂ‚ Ă‚ 
is the volume from 234 bars ago there is a limitĂ‚ 
to how far back you can look so if i set this toĂ‚ Ă‚ 
99999 and save the script we'll get a error youĂ‚ 
can see this little warning sign pops up hereĂ‚ Ă‚ 
if i click on that it says too large max totalĂ‚ 
bars back and then we have our large number hereĂ‚ Ă‚ 
the maximum is 300 000 bars which is still quite aĂ‚ 
lot of bars so let's set this back to let's say 10Ă‚ Ă‚ 
save the script and there we go so that's how youĂ‚ 
reference open high load close data that's how youĂ‚ Ă‚ 
reference historical data using the historicalĂ‚ 
series operator and this technique applies toĂ‚ Ă‚ 
any value indicator values as well so if we wantedĂ‚ 
to compare the atr from 10 bars ago the same wayĂ‚ Ă‚ 
we did here we could just say atr 10 and thenĂ‚ 
atr ratio is atr divided by hr10 this will workĂ‚ Ă‚ 
just fine the same way this did now obviouslyĂ‚ 
this is a pretty useless example of how to useĂ‚ Ă‚ 
price series and indicator seriesĂ‚ 
in our scripts but in future lessonsĂ‚ Ă‚ 
we'll be covering more complex more practicalĂ‚ 
demonstrations of analyzing price action usingĂ‚ Ă‚ 
this information and that brings me to theĂ‚ 
end of this lesson i'll see in the next one
in this lesson we're going to be covering theĂ‚ 
fundamental commonly used data types that youĂ‚ Ă‚ 
will be using in most of your scripts to performĂ‚ 
your calculations and analysis the first data typeĂ‚ Ă‚ 
you're going to be working with in all of yourĂ‚ 
scripts for the most part will be number typesĂ‚ Ă‚ 
so there are two types of numbers inĂ‚ 
pinescript we have integer type this isĂ‚ Ă‚ 
whole numbers so for example 1 would be aĂ‚ 
integer this could be 1 10 100 20 whateverĂ‚ Ă‚ 
just as long as it's a whole number and there'sĂ‚ 
no decimal in it that will be called an integerĂ‚ Ă‚ 
next up we have float type this is like anĂ‚ 
integer except it has a decimal place after itĂ‚ Ă‚ 
so any number with a decimal place is consideredĂ‚ 
a float the reason this is called a float type isĂ‚ Ă‚ 
because there is no fixed number of digits beforeĂ‚ 
and after the decimal place so the decimal placeĂ‚ Ă‚ 
can float around and so that's why they call itĂ‚ 
a float programming can be weird like that don'tĂ‚ Ă‚ 
ask me why that became a thing but you'll get usedĂ‚ 
to it so there's a lot of technical jargon you'llĂ‚ Ă‚ 
need to learn in coding integers just mean wholeĂ‚ 
numbers there is more nuance to this for exampleĂ‚ Ă‚ 
there is a maximum number that an integer can beĂ‚ 
but you don't need to know about that because it'sĂ‚ Ă‚ 
extremely unlikely you're going to run into thatĂ‚ 
issue in pine script i certainly never have in theĂ‚ Ă‚ 
three plus years i've been working on all kinds ofĂ‚ 
scripts but anyway moving on we have our floatingĂ‚ Ă‚ 
point numbers float type numbers um for exampleĂ‚ 
a closing price is a float because there's aĂ‚ Ă‚ 
decimal number in it you can see here on bitcoinĂ‚ 
we have two digits after the decimal place thatĂ‚ Ă‚ 
means that this price here is a floating pointĂ‚ 
data type next up we have it's not another dataĂ‚ Ă‚ 
type it's just a different way of using floatingĂ‚ 
points so float type e we'll call this and if iĂ‚ Ă‚ 
were to say 0.1 and then use the mathematicalĂ‚ 
operator e and then put 2 on the end hereĂ‚ Ă‚ 
what this is saying is return the number orĂ‚ 
the result of this equation which is 0.1 timesĂ‚ Ă‚ 
10 to the power of 2. now it's not often you'llĂ‚ 
need to use this if ever but the option is thereĂ‚ Ă‚ 
if you need to or if you're mathematicallyĂ‚ 
inclined for me personally i'm terrible at mathĂ‚ Ă‚ 
which is interesting considering i became a traderĂ‚ 
and programmer but that's a beauty of modernĂ‚ Ă‚ 
programming you don't need to be good at math toĂ‚ 
be a programmer but if you are talented at mathĂ‚ Ă‚ 
this is a tool your disposal the next dataĂ‚ 
type we have to work with is what's calledĂ‚ Ă‚ 
boolean values so a boolean data type isĂ‚ 
simply yes or no true or false one or zeroĂ‚ Ă‚ 
in pine script it's represented by the word trueĂ‚ 
and the word false so a bull type or boolean typeĂ‚ Ă‚ 
would look something like this true for true falseĂ‚ 
for false so let me leave that as true this isĂ‚ Ă‚ 
quite simple to understand you'll use this dataĂ‚ 
type quite a lot in your scripts in fact theseĂ‚ Ă‚ 
first three data types will make up the majorityĂ‚ 
of your scripts in terms of how you calculateĂ‚ Ă‚ 
stuff the next data type we have that will beĂ‚ 
commonly used is color types so color typesĂ‚ Ă‚ 
in pine script are their own data type and toĂ‚ 
reference color data types you can use the inbuiltĂ‚ Ă‚ 
data types that panes could provide if i type outĂ‚ 
color and then a full stop and hit control spaceĂ‚ Ă‚ 
or command space on a mac and i scroll down youĂ‚ 
can see a list of inbuilt colors here so if iĂ‚ Ă‚ 
say color.red that will give the pine scriptĂ‚ 
inbuilt red color as a data type and this can beĂ‚ Ă‚ 
passed into any of my drawing tools so for exampleĂ‚ 
we've got a plot here that is plotting our floatĂ‚ Ă‚ 
type e if i change this to say color equals colorĂ‚ 
type whoops and i save the script our line willĂ‚ Ă‚ 
turn red so you can see there so that's whatĂ‚ 
color data types are like you can also use umĂ‚ Ă‚ 
hexadecimal colors so if i wanted to color thisĂ‚ 
red in hexadecimal that would look like ff000Ă‚ Ă‚ 
i save that we get bright red we'll coverĂ‚ 
hexadecimal colors in a future lessonĂ‚ Ă‚ 
you don't need to know them in order to to useĂ‚ 
colors in your script but if you are familiarĂ‚ Ă‚ 
with hexadecimals from web development primarilyĂ‚ 
these are used in web development so if you'reĂ‚ Ă‚ 
familiar with working on websites you'll knowĂ‚ 
how hexadecimal colors work or you can use aĂ‚ Ă‚ 
hexadecimal color picker just look up hex colorĂ‚ 
picker on google and this can be an easy way toĂ‚ Ă‚ 
fill out custom colors in your scripts one otherĂ‚ 
option we have to work with in pan script is theĂ‚ Ă‚ 
color dot rgb function this we can pass in threeĂ‚ 
values or four values actually red green blueĂ‚ Ă‚ 
and transparency so for example if i wanted fullĂ‚ 
red i could type in two five five comma zero greenĂ‚ Ă‚ 
zero blue and zero transparencyĂ‚ 
now the transparency is optionalĂ‚ Ă‚ 
if i just put in rgb as two five five zeroĂ‚ 
zero save the script this will stay redĂ‚ Ă‚ 
actually let's change this color so that weĂ‚ 
can actually see the difference so this willĂ‚ Ă‚ 
now be bright green there we go because we've setĂ‚ 
green to full so rgb colors range from 0 to 255Ă‚ Ă‚ 
0 being none and 255 being the maximum andĂ‚ 
if we wanted to change the transparency let'sĂ‚ Ă‚ 
say we wanted to make the line completelyĂ‚ 
transparent we could set transparency to 100Ă‚ Ă‚ 
save the script and now the line will disappearĂ‚ 
from our chart but it's still plotting up here soĂ‚ Ă‚ 
that's a one way of plotting values onto yourĂ‚ 
chart without the user seeing the actual lineĂ‚ Ă‚ 
being drawn i use this quite a lot in my scriptsĂ‚ 
to draw information onto the chart that i wantĂ‚ Ă‚ 
to reference in my indicator values butĂ‚ 
that i don't want to see on the actualĂ‚ Ă‚ 
chart such as stop loss sizes and things likeĂ‚ 
that don't worry if this was a bit confusingĂ‚ Ă‚ 
we'll cover color types in future lessons inĂ‚ 
great detail and in more practical examplesĂ‚ Ă‚ 
the next data type we have to work with thatĂ‚ 
you will commonly use in your scripts not allĂ‚ Ă‚ 
scripts but some scripts will be the stringĂ‚ 
data type so this will be string type equalsĂ‚ Ă‚ 
text so string is just what programmers callĂ‚ 
text and in order to define it we need to putĂ‚ Ă‚ 
two quotation marks on either side of our text theĂ‚ 
reason why we do that is if we just say text hereĂ‚ Ă‚ 
then pinescript doesn't know the differenceĂ‚ 
between this and an actual function or variableĂ‚ Ă‚ 
in our script if i save the script we'll getĂ‚ 
an error here it'll say undeclared identifierĂ‚ Ă‚ 
text if we put quotation marks around it andĂ‚ 
save the script will get no error becauseĂ‚ Ă‚ 
now pine script knows that this is text nowĂ‚ 
you can use quotation marks or you can useĂ‚ Ă‚ 
single quotation marks as well that will workĂ‚ 
just the same if i say the script this isĂ‚ Ă‚ 
treated exactly the same as if we'd used quotationĂ‚ 
marks so it's really just a matter of preferenceĂ‚ Ă‚ 
which one you use i prefer to use the doubleĂ‚ 
quotes personally just because i'm used to thatĂ‚ Ă‚ 
from other programming languages but if you'reĂ‚ 
experienced in a different language and you'reĂ‚ Ă‚ 
used to using single quotes you can use them inĂ‚ 
your scripts if you want to and just as a sideĂ‚ Ă‚ 
note while we're here if you want to includeĂ‚ 
a single quote in your text you can't just addĂ‚ Ă‚ 
another single quote because now that's treatingĂ‚ 
this as a string and then we just have a singleĂ‚ Ă‚ 
quote on its own if i save the script we'llĂ‚ 
get an error because we didn't close offĂ‚ Ă‚ 
this string here if we add a backslash in frontĂ‚ 
of this text pinescript will now know that we wantĂ‚ Ă‚ 
to include this quote within our string then thisĂ‚ 
backslash is telling pinescript we want to includeĂ‚ Ă‚ 
this single quote within our text soĂ‚ 
for example let's change this to it sĂ‚ Ă‚ 
bullish now if we were to draw this onto ourĂ‚ 
chart using a label or any other text drawingĂ‚ Ă‚ 
function a table or something like thatĂ‚ 
this text would display on our chart as thisĂ‚ Ă‚ 
so this quotation would be included in theĂ‚ 
text and the backslash would be removedĂ‚ Ă‚ 
so that's the purpose of the backslash you canĂ‚ 
also do things like backslash n and that wouldĂ‚ Ă‚ 
move bullish onto a new line and so if we were toĂ‚ 
draw this onto our chart it would look like thisĂ‚ Ă‚ 
it's bullish on a new line there are severalĂ‚ 
escape characters you can use in your stringsĂ‚ Ă‚ 
we won't cover that in today's lesson becauseĂ‚ 
it's outside the scope of this lesson i'm justĂ‚ Ă‚ 
introducing you to these concepts i don't wantĂ‚ 
to overwhelm you with too much informationĂ‚ Ă‚ 
and so let's just get rid of all this and goĂ‚ 
back to text and we'll cover escape charactersĂ‚ Ă‚ 
and stuff like that in future lessons so beforeĂ‚ 
we wrap up this lesson let's plot all of theseĂ‚ Ă‚ 
types to our chart and we'll just have a quickĂ‚ 
look at what they look like so we have integerĂ‚ Ă‚ 
type float type flow type e and bull type weĂ‚ 
can't plot a boolean directly onto our chartĂ‚ Ă‚ 
so we can only plot these numbers at the momentĂ‚ 
directly onto our chart so let me change this toĂ‚ Ă‚ 
color dot white and i'll change this toĂ‚ 
color.white as well and we'll save theĂ‚ Ă‚ 
script so now you can see that we're plotting ourĂ‚ 
integer type as 20 it's just a whole number andĂ‚ Ă‚ 
then our float type let me change my floatĂ‚ 
type to [Music] 33.3 and save the script
now here's our float type 33.30 now remember in aĂ‚ 
previous lesson i talked about precision of yourĂ‚ Ă‚ 
script because we have two decimal placesĂ‚ 
after our market price we have two decimalĂ‚ Ă‚ 
places on our indicator values so even thoughĂ‚ 
i set this number to 33.3 it displays as 33.30Ă‚ Ă‚ 
if i didn't want that i could change my precisionĂ‚ 
to one save the script and now it's certainlyĂ‚ Ă‚ 
plotting to one decimal place and so here we'reĂ‚ 
plotting our integer number as a whole numberĂ‚ Ă‚ 
our float number and then our float typeĂ‚ 
e number and in our integer plot we areĂ‚ Ă‚ 
passing in our color type now let's use ourĂ‚ 
bull type here let's say if bull type is trueĂ‚ Ă‚ 
then let's overwrite our color type to color dotĂ‚ 
red so now when i save the script this color hereĂ‚ Ă‚ 
this plot will turn to red because bull type isĂ‚ 
set to true and we just said if bull type is trueĂ‚ Ă‚ 
override color type with the color red and toĂ‚ 
override something we need to use the colon equalsĂ‚ Ă‚ 
symbol we can't just use equals because that'sĂ‚ 
declaring a new variable we want to overwrite thisĂ‚ Ă‚ 
variable with this new one and don't worry we'llĂ‚ 
cover this in future lessons i'm just showing thisĂ‚ Ă‚ 
to you before we wrap this lesson up how ballĂ‚ 
types work so now if i save the script thisĂ‚ Ă‚ 
color will turn red because bull type is true andĂ‚ 
we've overwritten our color with red if i changeĂ‚ Ă‚ 
this to false the color will go back to greenĂ‚ 
because bull type is no longer true say the scriptĂ‚ Ă‚ 
there we go so that's it for this lesson thereĂ‚ 
are several data types here that we'll workĂ‚ Ă‚ 
with these are the common data types there areĂ‚ 
other data types too like tables labels drawingĂ‚ Ă‚ 
objects things like that but we'll cover thoseĂ‚ 
in future lessons once you become more familiarĂ‚ Ă‚ 
with the basics of pine script so don't worryĂ‚ 
if you're overwhelmed by any of this lessonĂ‚ Ă‚ 
throughout the course we'll be using all of theseĂ‚ 
data types quite regularly in practical scenariosĂ‚ Ă‚ 
and over time you'll learn how they all workĂ‚ 
and how you can use them in your own scriptsĂ‚ Ă‚ 
so that'll do it for this lesson iĂ‚ 
hope you're enjoying the course so farĂ‚ Ă‚ 
and if you are stick around becauseĂ‚ 
it's about to get a lot more interesting
hi welcome back to another partnership lessonĂ‚ 
in this lesson we are going to be covering howĂ‚ Ă‚ 
to appropriately uh or correctly declare yourĂ‚ 
variables in your scripts so variables are usedĂ‚ Ă‚ 
in all programming languages we've already coveredĂ‚ 
a few examples of them in this lesson we're justĂ‚ Ă‚ 
going to keep things simple and cover the basicĂ‚ 
fundamentals of variable declaration in order toĂ‚ Ă‚ 
declare a variable we need to basically give it aĂ‚ 
name and then that name is like a key so we have aĂ‚ Ă‚ 
key and a value so in today's example we're goingĂ‚ 
to be counting candles in our chart so i want toĂ‚ Ă‚ 
count how many green candles are on my chart soĂ‚ 
i'm going to say green candle count is set to zeroĂ‚ Ă‚ 
so right here we have declared a variableĂ‚ 
we've declared a variable with the name or keyĂ‚ Ă‚ 
green candle count and we've given it theĂ‚ 
value 0. now pi script will automaticallyĂ‚ Ă‚ 
determine what data type your variable is basedĂ‚ 
on what value you give it so pinescript knowsĂ‚ Ă‚ 
that 0 without any decimal places is an integerĂ‚ 
value a whole number value so whatever we doĂ‚ Ă‚ 
with this variable will be treated as an integerĂ‚ 
value if we were to add a decimal place like 0.1Ă‚ Ă‚ 
suddenly this becomes a float and now whateverĂ‚ 
we do to this variable needs to be governed byĂ‚ Ă‚ 
the rules of floating point numbers and thereĂ‚ 
are certain things you can and can't do withĂ‚ Ă‚ 
data types so it's important that you understandĂ‚ 
how the data types work the good news is you'llĂ‚ Ă‚ 
pick this up throughout the course it's notĂ‚ 
particularly complex but it is important thatĂ‚ Ă‚ 
you understand how data types work now you canĂ‚ 
explicitly tell pinescript what type of variableĂ‚ Ă‚ 
you want for example we wanted to tell pinescriptĂ‚ 
that this green candle count needs to be a floatĂ‚ Ă‚ 
we can just add the word float inĂ‚ 
front of it and save the scriptĂ‚ Ă‚ 
and now pinescript knows that even though there'sĂ‚ 
no decimal place after this whole number valueĂ‚ Ă‚ 
this essentially becomes 0.0 pinescript isĂ‚ 
now treating this as a floating point numberĂ‚ Ă‚ 
even though we gave it an integer value now if weĂ‚ 
were to change this to text for example a stringĂ‚ Ă‚ 
this won't work if i save the script pinescriptĂ‚ 
says declared typefloat is not compatible withĂ‚ Ă‚ 
the assigned type string so whatever data typeĂ‚ 
you give it you need to make sure it's accurateĂ‚ Ă‚ 
but the option to explicitly define what data typeĂ‚ 
your variable is is possible using these data typeĂ‚ Ă‚ 
operators so this can be useful in certainĂ‚ 
situations for example if i get rid of thisĂ‚ Ă‚ 
and i set green candle counter n a or not a numberĂ‚ 
this is basically null nothing i've set greenĂ‚ Ă‚ 
candle count to literally nothing it's not a dataĂ‚ 
type of any kind if i then want to draw that ontoĂ‚ Ă‚ 
my chart so let me paste the green can accountĂ‚ 
into the plot save the script you can see thatĂ‚ Ă‚ 
it says down here line seven value with n a typeĂ‚ 
cannot be assigned to a variable that was definedĂ‚ Ă‚ 
without a type keyword so we cannot declare greenĂ‚ 
candle count as n a but if we add float on the endĂ‚ Ă‚ 
and save the script this will now compileĂ‚ 
and we are now plotting n a onto the chartĂ‚ Ă‚ 
so that is one useful way of declaring a data typeĂ‚ 
with nothing as its initial value so we don't wantĂ‚ Ă‚ 
this to be zero we want this to be nothing theĂ‚ 
only way to do that is to explicitly give thisĂ‚ Ă‚ 
variable a type keyword but we don't usually needĂ‚ 
to do this so i'm going to get rid of that andĂ‚ Ă‚ 
set this back to 0 and save my script so now we'reĂ‚ 
plotting just zero onto our chart on every barĂ‚ Ă‚ 
now let's say that we want to count every greenĂ‚ 
bar on our chart to do that we need to check ifĂ‚ Ă‚ 
the closing price is greater than the openingĂ‚ 
price then set green candle count to greenĂ‚ Ă‚ 
candle count plus one now notice that i used theĂ‚ 
colon equals operator here and i didn't just setĂ‚ Ă‚ 
equals now the reason for this is because once aĂ‚ 
variable is already declared in order to overwriteĂ‚ Ă‚ 
the value that we've given this variable weĂ‚ 
need to use the equals the colon equals operatorĂ‚ Ă‚ 
to reassign this variable to a new value so forĂ‚ 
example if i set this to 50 and i save the scriptĂ‚ Ă‚ 
we will still be plotting zero here and we get anĂ‚ 
error or a warning this orange writing down hereĂ‚ Ă‚ 
is a warning not an error it's just bringingĂ‚ 
to our attention that maybe we made a mistakeĂ‚ Ă‚ 
it says here that shadowing variableĂ‚ 
green candle count exists in the parentĂ‚ Ă‚ 
scope did you want to use the assign operatorĂ‚ 
instead of the initialize operator or equalsĂ‚ Ă‚ 
so when you're first declaring a variable you needĂ‚ 
to use the equals that initializes the variableĂ‚ Ă‚ 
you can't use the assigned variable until theĂ‚ 
variable already exists so if i save the scriptĂ‚ Ă‚ 
now we'll get an error that green candle countĂ‚ 
is an undeclared variable so the equal sign isĂ‚ Ă‚ 
for declaring a new variable and the colon equalsĂ‚ 
is for reassigning an existing variable and newĂ‚ Ă‚ 
value so now if i change this to colon equalsĂ‚ 
instead of plotting 0 onto our chart like we doĂ‚ Ă‚ 
here if i use the colon equals we will now beĂ‚ 
plotting 50 because our green candle count isĂ‚ Ă‚ 
being overwritten with the number 50 but onlyĂ‚ 
on bullish bars so let's say we want to countĂ‚ Ă‚ 
all the green bars on our chart to do that we needĂ‚ 
to set green candle count two green candle countĂ‚ Ă‚ 
plus one so that will increment this variable byĂ‚ 
one it will get whatever its current value is andĂ‚ Ă‚ 
add one to it so let me save the script hereĂ‚ 
and you'll notice that on each bullish bar inĂ‚ Ă‚ 
our chart we're only drawing one onto the chartĂ‚ 
and on bearish bars we draw zero the reason forĂ‚ Ă‚ 
this is that our variables are re-declared onĂ‚ 
every new bar on our chart so every time a newĂ‚ Ă‚ 
bar historical or real time begins drawing ontoĂ‚ 
our chart our script code is run all over againĂ‚ Ă‚ 
it just keeps looping our code so every time ourĂ‚ 
script runs green candle count is set to zeroĂ‚ Ă‚ 
and so it never gets past one because on everyĂ‚ 
new bar this code is run again green candleĂ‚ Ă‚ 
count is reset back to zero and then incrementedĂ‚ 
by one on bullish bars if we want this variableĂ‚ Ă‚ 
to not update on new bars but to persist acrossĂ‚ 
all of the bars on our chart we need to use theĂ‚ Ă‚ 
var operator so var converts this green candleĂ‚ 
count variable into what is called a persistentĂ‚ Ă‚ 
variable so this variable will now persist acrossĂ‚ 
all of the bars on our chart and so wheneverĂ‚ Ă‚ 
we detect a green bar the green candle countĂ‚ 
will increment by one whenever we encounter aĂ‚ Ă‚ 
red or bearish bar nothing will happenĂ‚ 
because our little if statement hereĂ‚ Ă‚ 
will not be true don't worry we'll cover ifĂ‚ 
statements in more detail in future lessonsĂ‚ Ă‚ 
but for now if i save this script we will nowĂ‚ 
be counting every green bar over historicalĂ‚ Ă‚ 
price data so if i save the script there youĂ‚ 
can see that our line is incrementing slowlyĂ‚ Ă‚ 
from bottom left to top right if i reset myĂ‚ 
chart and hover over this last bar on my chartĂ‚ Ă‚ 
we have 11 771 bullish bars on our chart fromĂ‚ 
whenever our historical price action beganĂ‚ Ă‚ 
so that is the purpose of the var variable now theĂ‚ 
reason um not all variables of va are variablesĂ‚ Ă‚ 
are because let's say we want to draw a shapeĂ‚ 
whenever we detect a bullish bar if i change drawĂ‚ Ă‚ 
shape to true and then we use the plot shapeĂ‚ 
function and we pass in our draw shape variableĂ‚ Ă‚ 
if i save the script now you can seeĂ‚ 
an x plotting in our box whenever aĂ‚ Ă‚ 
bar is bullish so for every green bar here we haveĂ‚ 
an x if i change this draw shape to a var variableĂ‚ Ă‚ 
and i click save all of these bars will haveĂ‚ 
an x above them because the very first timeĂ‚ Ă‚ 
we detect a bullish bar draw shape will be setĂ‚ 
to true and it will never reset back to falseĂ‚ Ă‚ 
because we're using a var variable so let meĂ‚ 
save the script here to show you what i meanĂ‚ Ă‚ 
you can see we have all these x's if i go backĂ‚ 
to the very first bar on my chart using the alt gĂ‚ Ă‚ 
shortcut here you can see that the very first barĂ‚ 
in our chart is bullish and so draw shape is setĂ‚ Ă‚ 
to true and it is never set back to false becauseĂ‚ 
this is a persistent variable and so we draw xs onĂ‚ Ă‚ 
every single bar if i get rid of var and save theĂ‚ 
script now draw shape is reset on every new bar onĂ‚ Ă‚ 
our chart and we want that to happen in this caseĂ‚ 
for this particular variable because we want it toĂ‚ Ă‚ 
be reset on every new bar we want each new bar toĂ‚ 
be considered as a brand new calculation so thatĂ‚ Ă‚ 
is why not all variables are persistent with thisĂ‚ 
var tag and we need to explicitly tell pine scriptĂ‚ Ă‚ 
when we want a variable to be saved across all ofĂ‚ 
the bars on our chart this var can be useful forĂ‚ Ă‚ 
all kinds of things such as saving your stopĂ‚ 
loss and target until one is hit and drawingĂ‚ Ă‚ 
that onto the chart it can be useful for keepingĂ‚ 
track of certain market conditions so for exampleĂ‚ Ă‚ 
maybe you want to check when the rsi goesĂ‚ 
overbought so you would set your var variableĂ‚ Ă‚ 
to true when the rsi goes overbought and then youĂ‚ 
want to wait for a bearish candle and once thatĂ‚ Ă‚ 
happens maybe you enter short for example andĂ‚ 
then maybe you reset your rsi flag or your rsiĂ‚ Ă‚ 
overbought monitoring variable thoseĂ‚ 
are some practical use cases for thisĂ‚ Ă‚ 
variable type let's get rid of our draw shapeĂ‚ 
variable here for a moment and let's just countĂ‚ Ă‚ 
every single bar on our chart to do that we canĂ‚ 
rename our variable to total bars and get ridĂ‚ Ă‚ 
of our if statement and get rid of this and justĂ‚ 
plot total bars down here now before we continueĂ‚ Ă‚ 
i should probably quickly go over the namingĂ‚ 
conventions for variables the most importantĂ‚ Ă‚ 
thing you need to know is that variables cannotĂ‚ 
start with a number so if we change this to oneĂ‚ Ă‚ 
total bars and save the script we'll get an errorĂ‚ 
down here because you cannot start a variableĂ‚ Ă‚ 
name with a number you can have it after text soĂ‚ 
we could say t1 odual bars and save the scriptĂ‚ Ă‚ 
that will work just fine you can have any numberĂ‚ 
on the end of your variable name and that willĂ‚ Ă‚ 
work just fine you can have an underscore inĂ‚ 
front of your name your variable name that'llĂ‚ Ă‚ 
be okay you can't use any other symbol soĂ‚ 
we couldn't use a dollar sign for exampleĂ‚ Ă‚ 
that will give us a compile errorĂ‚ 
so you can only use underscoresĂ‚ Ă‚ 
and text to begin your variable name andĂ‚ 
you can have numbers and underscores inĂ‚ Ă‚ 
your variable names but that's as creative asĂ‚ 
you can get with your variable names so that'sĂ‚ Ă‚ 
important to keep in mind but anyway let's wrapĂ‚ 
this lesson up by counting how many bars in totalĂ‚ Ă‚ 
have drawn onto our chart to do that all we needĂ‚ 
to do is increment our total bars by 1 wheneverĂ‚ Ă‚ 
our script runs on a new bar so let's save theĂ‚ 
script and see how many bars drew onto our chartĂ‚ Ă‚ 
there you can see a diagonal line allĂ‚ 
the way from bottom left to top rightĂ‚ Ă‚ 
you can see that we've had 23 553 bars plottedĂ‚ 
onto our chart in total you could also get thisĂ‚ Ă‚ 
number by using the bar underscore index so if iĂ‚ 
plot that onto our chart you can see we get theĂ‚ Ă‚ 
same number minus one the reason for that is thatĂ‚ 
everything in programming starts from zero andĂ‚ Ă‚ 
so the very first bar on our chart isĂ‚ 
considered by our index zero and our scriptĂ‚ Ă‚ 
would have incremented our total bars to one soĂ‚ 
in our scripts case let's plot total bars with theĂ‚ Ă‚ 
color of color dot purple and save the script nowĂ‚ 
we'll get two lines drawing um offset by one so ifĂ‚ Ă‚ 
i hover my mouse over this very first bar you canĂ‚ 
see that our script says that this bar number oneĂ‚ Ă‚ 
whereas bar index says it's bar number zero theĂ‚ 
reason for that is that our total bars number isĂ‚ Ă‚ 
incremented on the first bar whereas bar indexĂ‚ 
is not it's only incremented after that bar isĂ‚ Ă‚ 
finished drawing so it starts from zero one twoĂ‚ 
three so on all the way to our final bar and ourĂ‚ Ă‚ 
total bars starts from 1 and counts all the wayĂ‚ 
up to the last bar so that about does it for ourĂ‚ Ă‚ 
variable declaration rules and fundamentals we'llĂ‚ 
be covering variable declaration in quite someĂ‚ Ă‚ 
detail in the lessons to come and in much moreĂ‚ 
practical use cases so again if this is all brandĂ‚ Ă‚ 
new to you and you're a little bit overwhelmedĂ‚ 
don't worry just keep going through this contentĂ‚ Ă‚ 
and eventually it will start to click you canĂ‚ 
always re-watch the lessons or send an emailĂ‚ Ă‚ 
to support the artoftrading.com if somethingĂ‚ 
doesn't make sense to you and i can help guideĂ‚ Ă‚ 
you to understand these concepts better but withĂ‚ 
that said that brings me to the end of this lessonĂ‚ Ă‚ 
i'll see you in the next one where we will coverĂ‚ 
getting user input in your scripts see you there
okay so in the official pinescript basics courseĂ‚ 
over on my website at pinescriptmastery.comĂ‚ Ă‚ 
this is where i would get into how toĂ‚ 
get user input from the settings menuĂ‚ Ă‚ 
in your scripts now the problem i have hereĂ‚ 
is that this video alone is 40 minutes longĂ‚ Ă‚ 
almost and would make this youtube video threeĂ‚ 
hours long and i wanted to try and keep thisĂ‚ Ă‚ 
at two hours or less so instead of copying thisĂ‚ 
lesson from the official course into this youtubeĂ‚ Ă‚ 
series i'm just going to briefly go over theĂ‚ 
basics of user input so in the official lessonĂ‚ Ă‚ 
i go into great detail about each input typeĂ‚ 
and all of the parameters we have to work withĂ‚ Ă‚ 
when getting user input but for this lessonĂ‚ 
i'm going to keep things extremely simple we'reĂ‚ Ă‚ 
just going to get a couple of user inputsĂ‚ 
without bothering with all of these extraĂ‚ Ă‚ 
optional parameters and if you do want to learnĂ‚ 
the specifics of all of the various advancedĂ‚ Ă‚ 
user input functionality and features we have toĂ‚ 
work with in pine then it's definitely going to beĂ‚ Ă‚ 
worth signing up to the official pine scriptĂ‚ 
basics course and going through the materialĂ‚ Ă‚ 
so let's jump into the pine editor and go overĂ‚ 
some basic user input so that you can at leastĂ‚ Ă‚ 
get started with adding options to your scriptsĂ‚ 
so here i am with a blank script all i'm doingĂ‚ Ă‚ 
is plotting an a to the chart so that my scriptĂ‚ 
will compile and let's get started with gettingĂ‚ Ă‚ 
some user input so the first type of input iĂ‚ 
want to show you guys is a boolean input and toĂ‚ Ă‚ 
get a boolean input which is just a true or falseĂ‚ 
value we use the input dot bool function and thisĂ‚ Ă‚ 
function has several parameters we can work withĂ‚ 
i'm just going to show you how to use the titleĂ‚ Ă‚ 
and the default value so i can title this someĂ‚ 
true false setting and set the default valueĂ‚ Ă‚ 
to let's set it to true save my code and nowĂ‚ 
i've opened up the little settings menu hereĂ‚ Ă‚ 
we have our some true false setting that we canĂ‚ 
turn on or off and we can reference this in ourĂ‚ Ă‚ 
script like we would any other variable the nextĂ‚ 
two inputs i want to show you are number inputsĂ‚ Ă‚ 
so the first one is going to be an integer inputĂ‚ 
that's just a whole number so input.int will getĂ‚ Ă‚ 
you a whole number input again this has severalĂ‚ 
parameters we're only going to cover a coupleĂ‚ Ă‚ 
of parameters here the first one is going toĂ‚ 
be title we'll set this to an integer inputĂ‚ Ă‚ 
we'll set the default value to 33 and we will setĂ‚ 
the minimum value to zero and the max val to 40.Ă‚ Ă‚ 
and let's also set the step to two so nowĂ‚ 
when i save my code i'll explain what eachĂ‚ Ă‚ 
parameter here does open up the settings menuĂ‚ 
we now have a number input the step parameterĂ‚ Ă‚ 
tells the interface how many numbers to incrementĂ‚ 
or decrement when we click the up and down arrowsĂ‚ Ă‚ 
here so if i click up you can see it's going upĂ‚ 
in steps of two and now that we've reached 39Ă‚ Ă‚ 
we cannot go any higher because our maximum valueĂ‚ 
is 40 and because this up arrow is trying toĂ‚ Ă‚ 
increment this to 41 it's not working you'd haveĂ‚ 
to type in manually 40. and that will work if youĂ‚ Ă‚ 
type in anything higher if we type in 50 it willĂ‚ 
do what i just did there just cut off the zeroĂ‚ Ă‚ 
let's try something different let's try 77. let'sĂ‚ 
try 99. yeah you can see it's just cutting offĂ‚ Ă‚ 
the second number there and rounding down to theĂ‚ 
first number um and same with our minimum vowel ifĂ‚ Ă‚ 
we go down to zero because we're incrementing inĂ‚ 
steps of two we're trying to go to minus one butĂ‚ Ă‚ 
we can't here because we've set the minimum valueĂ‚ 
to zero if i set this to negative ten save my codeĂ‚ Ă‚ 
open the settings now we can go down intoĂ‚ 
the negative numbers so that's how weĂ‚ Ă‚ 
get integer inputs from the user let's have aĂ‚ 
look at float inputs or decimal numbers to do thatĂ‚ Ă‚ 
we just use the input.float title this decimalĂ‚ 
input we'll give it a default value of 11.1 andĂ‚ Ă‚ 
if i hover over this function name you can seeĂ‚ 
it has a bunch of other input function parametersĂ‚ Ă‚ 
now i won't bother with min val maxval and stepĂ‚ 
i just showed you how to do that with integersĂ‚ Ă‚ 
it's exactly the same with floats but whatĂ‚ 
i will show you how to do is use the confirmĂ‚ Ă‚ 
parameter so if i set confirm to true save myĂ‚ 
code for this to work i need to remove the scriptĂ‚ Ă‚ 
from my chart and when i add the script to myĂ‚ 
chart now that i've set confirm equals trueĂ‚ Ă‚ 
on this input variable watch what happens whenĂ‚ 
i click add to chart i now have to confirmĂ‚ Ă‚ 
this input so this confirm dialog popped upĂ‚ 
anything you set confirm equals true to will popĂ‚ Ă‚ 
up when you first add the script to your chart andĂ‚ 
now you need to set this to whatever this settingĂ‚ Ă‚ 
is supposed to be then you click apply now whenĂ‚ 
you open up settings menu everything is uh back toĂ‚ Ă‚ 
normal so that's a float input very simple thereĂ‚ 
pretty much the same as an integer one slightlyĂ‚ Ă‚ 
different input i'll show you how to use is theĂ‚ 
price input so price input is essentially a floatĂ‚ Ă‚ 
input except that it's treated as a price value soĂ‚ 
if i set price input let's set the default valueĂ‚ Ă‚ 
to zero and set confirm equals true watch whatĂ‚ 
happens now when i click uh save and add theĂ‚ Ă‚ 
script to my chart actually let me get rid of thisĂ‚ 
confirming true since i already showed you howĂ‚ Ă‚ 
that works this one is slightly different a priceĂ‚ 
input is slightly different to a float input andĂ‚ Ă‚ 
i'll show you what i mean if i click add to chartĂ‚ 
we now have to set the price input price for ourĂ‚ Ă‚ 
basic user input script and you can see we haveĂ‚ 
this horizontal line that tracks with my mouseĂ‚ Ă‚ 
this is making me forcing me to select a priceĂ‚ 
value with my mouse so for example if you'reĂ‚ Ă‚ 
writing a script maybe that detects candlestickĂ‚ 
patterns around a support and resistance levelĂ‚ Ă‚ 
and alerts you so that you don't need to babysitĂ‚ 
your charts all day you could use this type ofĂ‚ Ă‚ 
input to place this line at a support level likeĂ‚ 
i see here if i click right here and i open up myĂ‚ Ă‚ 
settings menu my price input has now been set toĂ‚ 
the price value that i just clicked on so that'sĂ‚ Ă‚ 
a really cool feature in pine you can also do thisĂ‚ 
with time inputs so if i quickly show you a timeĂ‚ Ă‚ 
input input dot time and i forgot to mention ifĂ‚ 
you press control space after you write inputĂ‚ Ă‚ 
here is a list of all the inputĂ‚ 
functions we have to work withĂ‚ Ă‚ 
i'm not going to cover all of them because that'sĂ‚ 
why the other video is 40 minutes long in theĂ‚ Ă‚ 
main transcript basics course on my websiteĂ‚ 
but i will quickly show you how to use a timeĂ‚ Ă‚ 
input the same way we just uh used the priceĂ‚ 
input so let's just set the default value to zeroĂ‚ Ă‚ 
and set confirm equals true and watch what happensĂ‚ 
now if i get rid of the confirm equals true in ourĂ‚ Ă‚ 
price input and i save my code and i remove theĂ‚ 
script and add it back to my chart now i haveĂ‚ Ă‚ 
to select the time input for this variable andĂ‚ 
we now have a vertical line tracking my mouseĂ‚ Ă‚ 
which is showing the time that i'm hovering overĂ‚ 
this is perfect for something like an atr trailingĂ‚ Ă‚ 
stop there is a video on my youtube channel ifĂ‚ 
you search my channel for atr trailing stop i didĂ‚ Ă‚ 
release a video lesson explaining how to create anĂ‚ 
atr based training stop where you use this type ofĂ‚ Ă‚ 
input to select where to begin trailing from andĂ‚ 
once you select the bar the script begins trailingĂ‚ Ă‚ 
below price action until price comes down andĂ‚ 
hits it so that's a really cool input thereĂ‚ Ă‚ 
now i think i'll wrap this lesson up here to keepĂ‚ 
it short hopefully you get the idea by now anywayĂ‚ Ă‚ 
that will do it for this particular part of theĂ‚ 
basics lesson video here on youtube if you wantĂ‚ Ă‚ 
to go into more detail about some of these otherĂ‚ 
parameters or some of the inputs i haven't coveredĂ‚ Ă‚ 
here make sure to go to pine scriptmastery.comĂ‚ 
and sign up for the basics course it's completelyĂ‚ Ă‚ 
free but there is a lot more informationĂ‚ 
there there's at least a couple of hoursĂ‚ Ă‚ 
of extra content anyway with that said i will seeĂ‚ 
you in the next lesson coming up right about now
hi traders in this lesson i'm going to be showingĂ‚ 
you the fundamental concepts that you need to knowĂ‚ Ă‚ 
regarding alerts in pine script so there areĂ‚ 
two ways to trigger alerts in pine script bothĂ‚ Ă‚ 
ways depend on boolean triggers so true or falseĂ‚ 
triggers if the trigger is true an alert is firedĂ‚ Ă‚ 
if it is false no alert is triggeredĂ‚ 
so for today's example we're going toĂ‚ Ă‚ 
keep it extremely simple i will cover practicalĂ‚ 
alerts for things like setup detection indicatorĂ‚ Ă‚ 
conditions that sort of thing in future lessonsĂ‚ 
for now i'm just going to give you an overviewĂ‚ Ă‚ 
of the basic functionality of alerts so to doĂ‚ 
that we're going to create a really simple uhĂ‚ Ă‚ 
alert condition for this lesson we're going toĂ‚ 
detect higher closers and lower closers so hcĂ‚ Ă‚ 
is going to stand for higher close and that'sĂ‚ 
just going to check does the current bar closeĂ‚ Ă‚ 
higher than the previous bar's high and thenĂ‚ 
lower close is the same but the oppositeĂ‚ Ă‚ 
does the current bar close lower thanĂ‚ 
the previous low and this is the firstĂ‚ Ă‚ 
time you're seeing the greater than and less thanĂ‚ 
operators in a future lesson in the next sectionĂ‚ Ă‚ 
we'll cover all of the different math operatorsĂ‚ 
and pine script operators we have to work withĂ‚ Ă‚ 
don't worry about that for now basically todayĂ‚ 
we're just dealing with two boolean variablesĂ‚ Ă‚ 
high close lower close if the current bar closesĂ‚ 
higher than the previous high higher close is setĂ‚ Ă‚ 
to true otherwise it's set to false and the sameĂ‚ 
for lower close but the opposite it needs to closeĂ‚ Ă‚ 
lower than the previous bar's low and so now weĂ‚ 
have two different ways we can trigger alertsĂ‚ Ă‚ 
with these variables the first way is to use theĂ‚ 
alert function the second way is to use the alertĂ‚ Ă‚ 
condition function and i'll go over theĂ‚ 
differences now so first of all let's useĂ‚ Ă‚ 
the alert function the way this works is an alertĂ‚ 
function triggers an alert as soon as it is calledĂ‚ Ă‚ 
so for example if we check if hc is equal to trueĂ‚ 
fire our alert and this alert function takes twoĂ‚ Ă‚ 
parameters message which is a string and frequencyĂ‚ 
which is an inbuilt variable or built-in variableĂ‚ Ă‚ 
so we have three options here once per bar onceĂ‚ 
per bar close and both of these options basicallyĂ‚ Ă‚ 
so if we set this to once per bar close thisĂ‚ 
alert will only fire if the current bar closesĂ‚ Ă‚ 
as a higher close bar and the message can beĂ‚ 
anything so we could make our string anythingĂ‚ Ă‚ 
i'm going to say here that this bar closedĂ‚ 
higher than previous bar and then i'm goingĂ‚ Ă‚ 
to add a colon on the end and then i'm going toĂ‚ 
add on the current closing price of the bar to ourĂ‚ Ă‚ 
alert so to do that we need to use the tostringĂ‚ 
function because you cannot add a number directlyĂ‚ Ă‚ 
to a string and we need to use the str name spaceĂ‚ 
here we'll go over namespaces in a future lessonĂ‚ Ă‚ 
but we need to say str.2 string and then passĂ‚ 
in our number and this will convert the currentĂ‚ Ă‚ 
closing price into text into a string and so whenĂ‚ 
we get a higher close bar that closes so our alertĂ‚ Ă‚ 
frequency is set to once per bar close if we get aĂ‚ 
higher close bar an alert will be fired with thisĂ‚ Ă‚ 
text so that's one way we can triggerĂ‚ 
alerts it's quite simple really and nowĂ‚ Ă‚ 
you don't need to specify these parametersĂ‚ 
here the function will assume that the firstĂ‚ Ă‚ 
parameter you give it is a string so we could getĂ‚ 
rid of message equals i mean it will also assumeĂ‚ Ă‚ 
that the next parameter will be a alert frequencyĂ‚ 
so we can get rid of that so we have our alertĂ‚ Ă‚ 
function triggering with a string argument andĂ‚ 
a frequency argument only on high close bars weĂ‚ Ă‚ 
could do the same for if lower close i could copyĂ‚ 
this string paste it in there change that to lowerĂ‚ Ă‚ 
and now we've got an alert firing on aĂ‚ 
lower close candle and just to demonstrateĂ‚ Ă‚ 
that we can do it let's change this to frequencyĂ‚ 
once per bar so this will not wait for the bar toĂ‚ Ă‚ 
be confirmed it won't wait for the bar to closeĂ‚ 
so that's one way to fire alerts and it's myĂ‚ Ă‚ 
preferred way to trigger alerts in my scripts butĂ‚ 
there is also another option here and that is theĂ‚ Ă‚ 
alert condition function so this alert conditionĂ‚ 
function cannot be placed in an if statementĂ‚ Ă‚ 
it needs to be in your main script scope so itĂ‚ 
can't be in an if statement it needs to be inĂ‚ Ă‚ 
your main scripts sort of body of text so it needsĂ‚ 
to be checked on every single bar on your chartĂ‚ Ă‚ 
and this function takes three parameters insteadĂ‚ 
of two the first is the condition the booleanĂ‚ Ă‚ 
condition to trigger the function so in this caseĂ‚ 
if we wanted to trigger an alert for higher closesĂ‚ Ă‚ 
we would pass in h c comma that's our conditionĂ‚ 
if hc is true then the alert will fire and thenĂ‚ Ă‚ 
we have our title so here i'm going to say umĂ‚ 
hire close alert and then we have our messageĂ‚ Ă‚ 
so for this i'm just going to say the same thingĂ‚ 
here this bar closed higher than the previous barĂ‚ Ă‚ 
let's save the script and make sure thatĂ‚ 
compiles and there we go no problems thereĂ‚ Ă‚ 
now how this will work is on our real-time bar onĂ‚ 
the current bar in our chart this alert conditionĂ‚ Ă‚ 
function will check is hc true if it is thenĂ‚ 
fire an alert with the title higher close alertĂ‚ Ă‚ 
and the message this bar closed higher thanĂ‚ 
the previous bar now there are a couple ofĂ‚ Ă‚ 
differences between these functions the firstĂ‚ 
important one is that we cannot just add on ourĂ‚ Ă‚ 
closing price as a string here the same way we canĂ‚ 
with this alert function if i save this script nowĂ‚ Ă‚ 
we will get an error cannot call alert conditionĂ‚ 
with argument message equals and then our seriesĂ‚ Ă‚ 
string so a series string just means a stringĂ‚ 
that can change based on certain conditions soĂ‚ Ă‚ 
the closing price can obviously change at any timeĂ‚ 
the alert condition function does not accept anyĂ‚ Ă‚ 
parameter that can change at any time it needsĂ‚ 
a constant string meaning a string that neverĂ‚ Ă‚ 
changes such as this right here the onlyĂ‚ 
exception with this is that we can use placeholderĂ‚ Ă‚ 
tags so if i come up to the alert button here andĂ‚ 
click on that and i scroll down to the bottom hereĂ‚ Ă‚ 
you can see this text here you can use specialĂ‚ 
placeholders such as close time plot zero etcĂ‚ Ă‚ 
if you click on this little question mark buttonĂ‚ 
here you can see all of the different placeholdersĂ‚ Ă‚ 
we can use in our alerts and there are quite aĂ‚ 
few of them when we are using the alert conditionĂ‚ Ă‚ 
function the placeholder tags are the only wayĂ‚ 
to put in variables that change so for exampleĂ‚ Ă‚ 
if we wanted to put in the closing price we wouldĂ‚ 
need to do two curly brackets and then right closeĂ‚ Ă‚ 
and then to close curly brackets this placeholderĂ‚ 
would be replaced by trading view when the alertĂ‚ Ă‚ 
fires with the current closing price so now ifĂ‚ 
i save the script and we come up to the alertĂ‚ Ă‚ 
button and i select our script alerts if i clickĂ‚ 
on this little drop down box here you'll seeĂ‚ Ă‚ 
that we have our any alert function call as anĂ‚ 
option that is this function here so we couldĂ‚ Ă‚ 
set up an alert so that either of these get firedĂ‚ 
or we can select our higher close alert which isĂ‚ Ă‚ 
our alert condition so if i click on that andĂ‚ 
i say once per bar and turn off my web hookĂ‚ Ă‚ 
uh we'll cover web hooks in a futureĂ‚ 
lesson don't worry about them for nowĂ‚ Ă‚ 
but if i just set now an alert as thisĂ‚ 
is right now i've set it to once per barĂ‚ Ă‚ 
because the current bar's price is greater thanĂ‚ 
the previous bar's high this alert should fireĂ‚ Ă‚ 
immediately so let me click create we'll wait forĂ‚ 
price action to move as soon as price action movesĂ‚ Ă‚ 
there we go our alert is fired so you can seeĂ‚ 
when i hover over that alert it says this barĂ‚ Ă‚ 
closed higher than the previous bar and then itĂ‚ 
has the closing price so our little placeholderĂ‚ Ă‚ 
here was replaced with the closing price youĂ‚ 
could also put in the ticker if you wanted toĂ‚ Ă‚ 
so now if i save the script and set a newĂ‚ 
alert and select hire close alert and clickĂ‚ Ă‚ 
create now we have a new alert that fired and youĂ‚ 
can see that it now says the market the ticker idĂ‚ Ă‚ 
of the market that fired this alert so that's howĂ‚ 
you pass in dynamic information into your alertĂ‚ Ă‚ 
condition functions the alert function on theĂ‚ 
other hand is a lot easier to pass in informationĂ‚ Ă‚ 
so this particular function is definitely theĂ‚ 
preferred method for dealing with third-party apisĂ‚ Ă‚ 
for example for trade automation using tradingviewĂ‚ 
alerts you would be better off using thisĂ‚ Ă‚ 
alert function if you can this alert condition onĂ‚ 
the other hand is great for manual alerts becauseĂ‚ Ă‚ 
you can give each alert a title so for exampleĂ‚ 
i could copy this paste it down here changeĂ‚ Ă‚ 
this to lower close and change the condition toĂ‚ 
our lower close variable change that to lowerĂ‚ Ă‚ 
so here's our lower close alert and then let'sĂ‚ 
say we want to merge both of these so we want toĂ‚ Ă‚ 
say if we have a higher close or we have a lowerĂ‚ 
close then we have a hclc alert and then we couldĂ‚ Ă‚ 
just say this bar closed higher or lower than theĂ‚ 
previous bar now if i save the script and we comeĂ‚ Ă‚ 
up to the alerts dialog and select our scriptĂ‚ 
when i click on this drop down box we have threeĂ‚ Ă‚ 
different alerts to choose from here we have aĂ‚ 
higher close level close alert high close alertĂ‚ Ă‚ 
and lower close alert with their own titles soĂ‚ 
this is really useful for setting manual alertsĂ‚ Ă‚ 
in your scripts say you have a script thatĂ‚ 
is designed to help traders trade your scriptĂ‚ Ă‚ 
manually and you want to detect things likeĂ‚ 
a certain candlestick patterns or certainĂ‚ Ă‚ 
indicator conditions that sort of thing this isĂ‚ 
a great way to separate your alerts with a titleĂ‚ Ă‚ 
to make it more intuitive for the user whereasĂ‚ 
this any alert function call will literally fireĂ‚ Ă‚ 
for any alert function call that we put in ourĂ‚ 
script now if i were to create this alert nothingĂ‚ Ă‚ 
would happen because i've set my alert frequencyĂ‚ 
to once per bar closed so we would have to waitĂ‚ Ă‚ 
for the bar to close higher than the previousĂ‚ 
bar for this to fire let me get rid of thisĂ‚ Ă‚ 
close out of our alert frequency so this shouldĂ‚ 
now fire immediately as soon as i set an alertĂ‚ Ă‚ 
on this any alert function call so let meĂ‚ 
first click show pop-up and then click createĂ‚ Ă‚ 
we'll see what happens there we go an alert justĂ‚ 
fired on euro dollar it fired based on the anyĂ‚ Ă‚ 
alert function call condition and we get our textĂ‚ 
here that we wrote out here so this bar closedĂ‚ Ă‚ 
higher than the previous bar and then we haveĂ‚ 
our bar price our closing price so for exampleĂ‚ Ă‚ 
let's say we wanted to pass the current rsi valueĂ‚ 
to our alert as well we could replace um this barĂ‚ Ă‚ 
close higher than the previous bar and then setĂ‚ 
the rsi value in there so now it will tell usĂ‚ Ă‚ 
the current rsi value when we set our alert so letĂ‚ 
me select alerts leave that as it is click createĂ‚ Ă‚ 
there we have it now you can see our rsi value nowĂ‚ 
there's a lot of decimal places here um i'll showĂ‚ Ă‚ 
you in a future lesson how you can truncateĂ‚ 
or cut excess decimal places off your alertsĂ‚ Ă‚ 
but for now that's not important you can seeĂ‚ 
the the current rsi value over the 14 periodĂ‚ Ă‚ 
is 68.62 or 63 if we round it up and that'sĂ‚ 
really all you need to know for alerts weĂ‚ Ă‚ 
have these two methods for setting alerts bothĂ‚ 
have their pros and cons the pros for the alertĂ‚ Ă‚ 
function is that we can easily pass in values soĂ‚ 
price values indicator values third-party syntaxĂ‚ Ă‚ 
for sending your alerts to a service thatĂ‚ 
automates your trades based on your alertsĂ‚ Ă‚ 
if you're using a service like that they willĂ‚ 
require a certain syntax for your alerts so forĂ‚ Ă‚ 
example you might need to say um set my stop lossĂ‚ 
to and then in here you could set tostring yourĂ‚ Ă‚ 
stop loss price and then you might need to put inĂ‚ 
a comma take profit equals and then put in yourĂ‚ Ă‚ 
take profit price etc and then you could sendĂ‚ 
this information to a third-party api using aĂ‚ Ă‚ 
web hook using this so for example i've beenĂ‚ 
using pine connector recently to automateĂ‚ Ă‚ 
some of my trading strategy scripts if i sendĂ‚ 
the right syntax in an alert message to thisĂ‚ Ă‚ 
url using the trainingview alert functionalityĂ‚ 
pi connector will automatically manage my tradesĂ‚ Ă‚ 
for me based on the commands i give it in myĂ‚ 
alert message and i can pass in my stop-loss priceĂ‚ Ă‚ 
breakeven distance trailing stops limit orderĂ‚ 
entry prices all that sort of thing just usingĂ‚ Ă‚ 
this technique that you see right here so that'llĂ‚ 
do it for this particular lesson on alerts thisĂ‚ Ă‚ 
is the basic core functionality of alerts we'llĂ‚ 
cover more detailed practical applications of thisĂ‚ Ă‚ 
information in future lessons especially when weĂ‚ 
get into setup detection and indicator conditionsĂ‚ Ă‚ 
that sort of thing so i hope you found this lessonĂ‚ 
interesting and i'll see you in the next one
hi traders welcome back to another lesson in thisĂ‚ 
lesson we're going to be breaking down the plotĂ‚ Ă‚ 
function and i'll be demonstrating how you canĂ‚ 
use it to plot data onto your chart so the plotĂ‚ Ă‚ 
function is used to plot numbers onto the chartĂ‚ 
and numbers only we can't plot any other data typeĂ‚ Ă‚ 
other than numbers using this particular functionĂ‚ 
so here we are just plotting the closing priceĂ‚ Ă‚ 
onto our little indicator box here if i changeĂ‚ 
this to anything else let's try and plot a booleanĂ‚ Ă‚ 
plot true i'll save the script we'll get an errorĂ‚ 
cannot call plot with argument an argument of bullĂ‚ Ă‚ 
type was used but a series float type was expectedĂ‚ 
so we can only plot numbers let's try a stringĂ‚ Ă‚ 
text save error cannot call plot with argumentĂ‚ 
text an argument of string was used but float isĂ‚ Ă‚ 
expected so we cannot plot any data type otherĂ‚ 
than numbers using this function to plot otherĂ‚ Ă‚ 
data types we need to use other types of functionsĂ‚ 
but the plot function is still extremely usefulĂ‚ Ă‚ 
for plotting things obviously like indicatorĂ‚ 
values and so on and so for this particular lessonĂ‚ Ă‚ 
i'm just going to plot the closing price i'mĂ‚ 
going to walk you through the various parametersĂ‚ Ă‚ 
that this plot function takes that we can use toĂ‚ 
manipulate its output so the first thing it takesĂ‚ Ă‚ 
is a series and this could be anything it couldĂ‚ 
be a price value open high low close it could beĂ‚ Ă‚ 
an indicator value it could just be a number thatĂ‚ 
we want to plot so here we can plot the number oneĂ‚ Ă‚ 
across our chart we can also title the functionĂ‚ 
so i'm going to call this our plot save the scriptĂ‚ Ă‚ 
and now if i open up the settings menu by clickingĂ‚ 
on this little cog icon here on the style tab youĂ‚ Ă‚ 
can see that our plot now has a title if we don'tĂ‚ 
title our plot then it would just be called plotĂ‚ Ă‚ 
so the purpose of titling your plots is to makeĂ‚ 
it easy for your user the users of your scriptĂ‚ Ă‚ 
to know what your plot does and maybe they wantĂ‚ 
to turn it off or change its color or its styleĂ‚ Ă‚ 
the next parameter we have to work with with ourĂ‚ 
plot function is the color parameter so you canĂ‚ Ă‚ 
set the color to anything you want by defaultĂ‚ 
it's usually blue but you can set this to anyĂ‚ Ă‚ 
inbuilt color or custom color that you want soĂ‚ 
for now let's set it to purple save the scriptĂ‚ Ă‚ 
and we have a purple line the next parameterĂ‚ 
this plot function takes is line width so thisĂ‚ Ă‚ 
ranges from one to five i believe so by defaultĂ‚ 
it's one which is the thinnest line you can plotĂ‚ Ă‚ 
if we set it to five it becomes very thick like soĂ‚ 
one valuable use case for this line width functionĂ‚ Ă‚ 
is when plotting things like a moving averageĂ‚ 
maybe you want your longer term moving averageĂ‚ Ă‚ 
to be thicker than your shorter term movingĂ‚ 
average for example i created a script here calledĂ‚ Ă‚ 
multiple moving averages if i add that to my chartĂ‚ 
and we zoom out if i turn on all of the movingĂ‚ Ă‚ 
averages you can see that they range from thin toĂ‚ 
very thick at the bottom here based on the lengthĂ‚ Ă‚ 
of the look back period for that moving averageĂ‚ 
so that's one useful practical use case for thisĂ‚ Ă‚ 
line width parameter the next parameter we have isĂ‚ 
style so this is the style of the data that we'reĂ‚ Ă‚ 
plotting to change the style you just type in plotĂ‚ 
dot and then control space and any of these styleĂ‚ Ă‚ 
built-in variables will dictate what your dataĂ‚ 
looks like so you can set it to circles save theĂ‚ Ă‚ 
script now we're plotting um actual circlesĂ‚ 
under the chart you could set it to an areaĂ‚ Ă‚ 
for example it's hard to see the peaks and valleysĂ‚ 
of this area because price action is so tight onĂ‚ Ă‚ 
this time frame if we go out to the weekly we canĂ‚ 
see a little bit more of that we could also plotĂ‚ Ă‚ 
columns if we wanted to so if i set this toĂ‚ 
columns and save the script now we're plottingĂ‚ Ă‚ 
price as a column so this is obviously mostĂ‚ 
useful for something like plotting volume so ifĂ‚ Ă‚ 
we change the plot to volume now we're plottingĂ‚ 
volume as a column let's change this back toĂ‚ Ă‚ 
a simple line and i'll change our plot back toĂ‚ 
the closing price as well now the next parameterĂ‚ Ă‚ 
we have to work with with our plot function is theĂ‚ 
track price parameter so track price is a booleanĂ‚ Ă‚ 
parameter true or false it's false by defaultĂ‚ 
so it's turned off by default but if you want toĂ‚ Ă‚ 
turn it on you just set this to true and what thisĂ‚ 
will do is just plot a horizontal line across ourĂ‚ Ă‚ 
chart like you see there which is tracking theĂ‚ 
current price of our plot so if i go down to a oneĂ‚ Ă‚ 
minute time frame you'll notice this line movingĂ‚ 
around as price action moves and it's literallyĂ‚ Ă‚ 
just tracking the price the current price ofĂ‚ 
this plot the next parameter we can use in thisĂ‚ Ă‚ 
function is the hist base parameter so hist baseĂ‚ 
is short for histogram base and this is the baseĂ‚ Ă‚ 
price of histograms columns and let me open up theĂ‚ 
plot documentation so it says here that hist baseĂ‚ Ă‚ 
is the price value used as the reference levelĂ‚ 
when rendering plot with histograms columns orĂ‚ Ă‚ 
areas so if i drop out to the four hour chartĂ‚ 
and we change our hist base to let's say 117.Ă‚ Ă‚ 
if i set it to 1.17 and i set my style to styleĂ‚ 
columns and save the script our hist base is nowĂ‚ Ă‚ 
1.17 and so any value that prints above 1.17 willĂ‚ 
be a column that prints in the upper half and anyĂ‚ Ă‚ 
value that prints below 1.17 will be in the lowerĂ‚ 
half so that's the purpose of hist base you mightĂ‚ Ă‚ 
have seen this style of plotting in somethingĂ‚ 
like a macd and it works on columns as we justĂ‚ Ă‚ 
saw it works on areas as well so there's an areaĂ‚ 
with the hist base applied and it also works withĂ‚ Ă‚ 
histograms so if i save the script we get theĂ‚ 
same sort of effect now let me change this back toĂ‚ Ă‚ 
a simple line and get rid of our track price andĂ‚ 
hist base and we'll go over some of these otherĂ‚ Ă‚ 
parameters that we have to work with so theĂ‚ 
next parameter we have to work with is offsetĂ‚ Ă‚ 
now offset does exactly what it sounds likeĂ‚ 
it offsets the plot so if we want to shiftĂ‚ Ă‚ 
this plot 10 bars to the right you would setĂ‚ 
offset to 10 and now watch when i save the scriptĂ‚ Ă‚ 
our line here will shift over 10 bars to the rightĂ‚ 
here so if you want to shift to the left you wouldĂ‚ Ă‚ 
use negative 10 if you want to shift to the rightĂ‚ 
you use a positive number so let's shift this lineĂ‚ Ă‚ 
10 bars to the right let me save this scriptĂ‚ 
and now you can see that the plot is plottingĂ‚ Ă‚ 
with an offset of 10 bars so the current value isĂ‚ 
being plotted 10 bars to the right if we wantedĂ‚ Ă‚ 
to plot this line 10 bars back you would just addĂ‚ 
a negative sign in front of that offset parameterĂ‚ Ă‚ 
and now the line is drawing 10 bars back now thereĂ‚ 
are a handful of reasons why you would want toĂ‚ Ă‚ 
use this offset parameter in your plots we'reĂ‚ 
not going to cover them all in today's lessonĂ‚ Ă‚ 
but we will in future lessons when we get intoĂ‚ 
more practical use cases of these parameters inĂ‚ Ă‚ 
actual indicator and strategy scripts so nowĂ‚ 
let's get rid of this offset for a moment andĂ‚ Ă‚ 
look at the last few remaining parametersĂ‚ 
the next one is join and the join parameterĂ‚ Ă‚ 
is a true or false variable and it basicallyĂ‚ 
just adds a joining line between our plots nowĂ‚ Ă‚ 
when you're plotting a line like we are here youĂ‚ 
don't need to use this parameter it's useless butĂ‚ Ă‚ 
if we were using something like let's try a styleĂ‚ 
cross save the script zoom in a little bit youĂ‚ Ă‚ 
can see these crosses have gaps in between themĂ‚ 
we're plotting the closing price so it's jumpingĂ‚ Ă‚ 
up here as this big green bullish bar printedĂ‚ 
the cross jumped up here if we set join to trueĂ‚ Ă‚ 
and save the script now we have a line connectingĂ‚ 
all of our shapes so any shape that has a gapĂ‚ Ă‚ 
between it you can draw a line between themĂ‚ 
using the join parameter and setting it to trueĂ‚ Ă‚ 
the next parameter we can work with is editableĂ‚ 
so by default all of our plots are editable but ifĂ‚ Ă‚ 
you want to stop the user from changing your plotĂ‚ 
for whatever reason personally i've never usedĂ‚ Ă‚ 
this parameter but maybe there's a reason why youĂ‚ 
might want to prevent users from editing a certainĂ‚ Ă‚ 
plot in your script you can set this to falseĂ‚ 
so by default this is always true but if we setĂ‚ Ă‚ 
it to false watch what happens before i say theĂ‚ 
script we'll open up the settings menu and here'sĂ‚ Ă‚ 
our plot here if i save this script wait for itĂ‚ 
to update and then open the settings menu againĂ‚ Ă‚ 
you'll notice that our plot style settings areĂ‚ 
gone we can no longer change the style of thisĂ‚ Ă‚ 
purple plot again i don't know why you would wantĂ‚ 
to limit the customization of your script by yourĂ‚ Ă‚ 
users but the option is there if you need toĂ‚ 
now the next parameter we can work with is showĂ‚ Ă‚ 
underscore last and show last basically justĂ‚ 
tells the script um how many plots to displayĂ‚ Ă‚ 
on the chart so if we set show underscore last toĂ‚ 
10 for example we will only show the last 10 plotsĂ‚ Ă‚ 
onto our chart so let's save the script and nowĂ‚ 
you can see we only have 10 plots drawing onto theĂ‚ Ă‚ 
chart so you won't need to use this often i neverĂ‚ 
have but it could be useful in certain scriptsĂ‚ Ă‚ 
where you don't want to show historical analysisĂ‚ 
you only want to show the most recent analysisĂ‚ Ă‚ 
onto your chart for example maybe you wantĂ‚ 
to reduce clutter from your chart if you'reĂ‚ Ă‚ 
drawing something like support and resistanceĂ‚ 
levels for example maybe you don't want yourĂ‚ Ă‚ 
chart covered in lines that were drawn hundredsĂ‚ 
of bars ago you just want to show the last handfulĂ‚ Ă‚ 
and now the final parameter we have to work withĂ‚ 
when it comes to plotting data to our chart isĂ‚ Ă‚ 
the display parameter the display parameter justĂ‚ 
basically turns on or off the drawing of your plotĂ‚ Ă‚ 
so by default it's obviously enabled but if youĂ‚ 
want to you can hard code your plot to set it toĂ‚ Ă‚ 
display.none and then the plot will not draw ontoĂ‚ 
your chart this is great for debugging so maybeĂ‚ Ă‚ 
you have some functionality in your script thatĂ‚ 
you want to be able to check visually what it'sĂ‚ Ă‚ 
doing from time to time but you don't actuallyĂ‚ 
want that information drawing onto the chart allĂ‚ Ă‚ 
of the time you can set display.none in your plotĂ‚ 
parameter and then in order to actually see thisĂ‚ Ă‚ 
drawn to the chart you need to come up to theĂ‚ 
settings menu and turn it on here by clickingĂ‚ Ă‚ 
on this little checkbox now when you set thisĂ‚ 
to display.none it does stop drawing the actualĂ‚ Ă‚ 
indicator value as you can see here we don't getĂ‚ 
our value plotting so this essentially just turnsĂ‚ Ă‚ 
the plot off completely but let's say that youĂ‚ 
don't want the data actually plotting onto yourĂ‚ Ă‚ 
chart but you still want to see the number theĂ‚ 
best way to achieve this is using transparencyĂ‚ Ă‚ 
now we'll cover the color functionality inĂ‚ 
pan script in a future lesson we won't goĂ‚ Ă‚ 
into detail in this lesson but just really quicklyĂ‚ 
to show you how it's possible to set a plot to beĂ‚ Ă‚ 
transparent you can use the color.new function soĂ‚ 
here i'm creating a new color i'm using the colorĂ‚ Ă‚ 
dot new and then passing in the purple colorĂ‚ 
if i add a comma on the end here and type inĂ‚ Ă‚ 
100 and then close off this parentheses we openedĂ‚ 
here we're now creating a new color of the colorĂ‚ Ă‚ 
purple with 100 transparency and now if i saveĂ‚ 
the script you'll see our plots disappear but theĂ‚ Ă‚ 
value is still plotting on the indicator statusĂ‚ 
bar so this is another great debugging toolĂ‚ Ă‚ 
or a way to plot something like the atr forĂ‚ 
example maybe you want to plot the atr in yourĂ‚ Ă‚ 
script but you don't want the atr actually drawingĂ‚ 
over the top of price action this is how you couldĂ‚ Ă‚ 
go about doing that and that brings us to theĂ‚ 
end of this lesson i'll see you in the next one
let's have a look at one of the most simpleĂ‚ 
technical analysis conditions we can check forĂ‚ Ă‚ 
in pine script which is moving average crosses nowĂ‚ 
this is not a trading lesson i do not encourageĂ‚ Ă‚ 
you to trade moving average crossovers and crossĂ‚ 
hunters they may be profitable with the rightĂ‚ Ă‚ 
rules the right set of rules and entry reasons etcĂ‚ 
i personally don't trade them but for the purposesĂ‚ Ă‚ 
of this lesson they are a great example of howĂ‚ 
easy it is to detect certain market conditions inĂ‚ Ă‚ 
pine script so to get started we of course needĂ‚ 
two moving averages in order to detect a crossĂ‚ Ă‚ 
so let's get some mas and again just todayĂ‚ 
because it's my favorite moving average i'mĂ‚ Ă‚ 
going to use an ema an exponential moving averageĂ‚ 
but you can use any moving average type you likeĂ‚ Ă‚ 
i'm going to set ema 1 to ta.ema and we'll baseĂ‚ 
it on the closing price as always and this oneĂ‚ Ă‚ 
will have a length of 50. and then the secondĂ‚ 
ema will have a length of 100. so now before weĂ‚ Ă‚ 
proceed let's draw the mas onto our chart and iĂ‚ 
will style the first one with the color of colorĂ‚ Ă‚ 
green and the second one can be red so let's saveĂ‚ 
the code make sure that this is working fine thereĂ‚ Ă‚ 
we go two moving averages on our chart now let'sĂ‚ 
detect our crosses our crossovers and cross undersĂ‚ Ă‚ 
so we'll create two variables here two newĂ‚ 
variables called ma cross over and may crossĂ‚ Ă‚ 
under now for our crossover and cross underĂ‚ 
we're going to use the inbuilt function t aĂ‚ Ă‚ 
dot and then if i start writing cross here we haveĂ‚ 
our various options now cross will detect either aĂ‚ Ă‚ 
crossover or a cross under for today we're goingĂ‚ 
to separate these two into their own variablesĂ‚ Ă‚ 
and so for crossover i'm going to use crossoverĂ‚ 
obviously and for cross under we'll use ta.crossĂ‚ Ă‚ 
under and now these functions take twoĂ‚ 
parameters two values that need to crossĂ‚ Ă‚ 
these could be any values they don't need toĂ‚ 
be moving average values they could be anythingĂ‚ Ă‚ 
macd lines for example um or the rsi crossing overĂ‚ 
a certain threshold for today we're just goingĂ‚ Ă‚ 
to put in ema1 and ema2 if i copy that into ourĂ‚ 
second function here we are now detecting our m8Ă‚ Ă‚ 
crosses and at the bottom of our script i'm goingĂ‚ 
to draw crosses and here i'm going to set bg colorĂ‚ Ă‚ 
to ma cross over question mark if the movingĂ‚ 
average has crossed over the other movingĂ‚ Ă‚ 
average if ema1 is crossed over ema2 i'll set theĂ‚ 
background color to color.green otherwise i'll doĂ‚ Ă‚ 
nothing with the background color set it to n aĂ‚ 
i can copy this line of code paste it underneathĂ‚ Ă‚ 
i'll change this to cross under and we'll changeĂ‚ 
the color to red and now if i save my scriptĂ‚ Ă‚ 
we'll be getting the background color of ourĂ‚ 
chart changing based on which cross happenedĂ‚ Ă‚ 
so click save and that will do it for this lessonĂ‚ 
very simple one here just again introducing youĂ‚ Ă‚ 
to the ta namespace and combining some of theĂ‚ 
things we've learned so far to create practicalĂ‚ Ă‚ 
uh indicators that can be actually useful in ourĂ‚ 
trading anyway i'll leave this one here and i'llĂ‚ Ă‚ 
speak with you in the next lesson the source codeĂ‚ 
will be below if you want to play around with this
hello my friend well done you made it to the veryĂ‚ 
end of this very long video congratulations youĂ‚ Ă‚ 
are one of the few traders who actually commitĂ‚ 
to learning this stuff obviously i have accessĂ‚ Ă‚ 
to the metrics and analytics of my videos in bothĂ‚ 
youtube and on my course website and you would beĂ‚ Ă‚ 
surprised how few traders actually commit andĂ‚ 
make it to the end most give up halfway throughĂ‚ Ă‚ 
so well done for making it this far you should nowĂ‚ 
be able to go out and start playing around withĂ‚ Ă‚ 
pine script and start writing your own scriptsĂ‚ 
but if you do run into trouble or you wantĂ‚ Ă‚ 
further guidance make sure to check out my masteryĂ‚ 
course i recently re-recorded all of the contentĂ‚ Ă‚ 
for pine script version 5. so this is the new andĂ‚ 
improved version i've stepped up the content a lotĂ‚ Ă‚ 
and the new course has over 140 lessons andĂ‚ 
counting i haven't even started recording theĂ‚ Ă‚ 
strategy section yet i'm about to start doing thatĂ‚ 
this week so if you found this free basics courseĂ‚ Ă‚ 
interesting i think you will love the masteryĂ‚ 
course we have a lot of great reviews here fromĂ‚ Ă‚ 
traders who have gone through the courseĂ‚ 
you can come and read these here if you'reĂ‚ Ă‚ 
not convinced that it's worth your time andĂ‚ 
money there is a lot of content here fromĂ‚ Ă‚ 
obviously the basics which we just covered to umĂ‚ 
some of the more intermediate things like how toĂ‚ Ă‚ 
analyze price how to detect candlestick patternsĂ‚ 
how to use all of the various inbuilt functionsĂ‚ Ă‚ 
and variables as well as the inbuilt indicatorsĂ‚ 
i've got a lot of material on indicators here thenĂ‚ Ă‚ 
we go into information about accessing other timeĂ‚ 
frames and markets using the security functionĂ‚ Ă‚ 
we cover arrays this is still inĂ‚ 
progress this material here arraysĂ‚ Ă‚ 
are quite an advanced subject as far as pineĂ‚ 
script is concerned we cover libraries andĂ‚ Ă‚ 
soon i will be expanding into strategy scriptsĂ‚ 
and even automation how to automate your pineĂ‚ Ă‚ 
scripts through third-party apis so if thisĂ‚ 
sounds like something you'd be interested inĂ‚ Ă‚ 
head over to my website pine scriptmastery.comĂ‚ 
i have another course here as well where i shareĂ‚ Ă‚ 
the source code to all of my published indicatorsĂ‚ 
and strategy scripts including a bunch of privateĂ‚ Ă‚ 
scripts that i've never actually released andĂ‚ 
the source code to my really popular ultimateĂ‚ Ă‚ 
pullback indicator which is the most advancedĂ‚ 
and sophisticated profitable trading script i'veĂ‚ Ă‚ 
ever written but anyway i'll leave it hereĂ‚ 
thanks for watching the video again if youĂ‚ Ă‚ 
liked this content make sure to hit the subscribeĂ‚ 
button because i will be back soon with more freeĂ‚ Ă‚ 
pan script material if you're not yet ready toĂ‚ 
commit to learning more about pine script andĂ‚ Ă‚ 
signing up to the mastery course at least hitĂ‚ 
the subscribe button because i definitely willĂ‚ Ă‚ 
be back soon with more free content with all ofĂ‚ 
that said well done getting to the end good luckĂ‚ Ă‚ 
with your trading i hope you kill it out there inĂ‚ 
the markets i hope pinescript helps you to enhanceĂ‚ Ă‚ 
your edge and take your trading to that next levelĂ‚ 
and i'll leave you here have a great day or nightĂ‚ Ă‚ 
wherever you are in the world whatever time it isĂ‚ 
and i'll speak with you in the next video goodbye
[Music]
[Music]Ă‚ Ă‚ 
you

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