This is not a reference site for Pine scripting language (for more information visit Reference Manual and PineScript Tutorial)
Some statements needs to use more than one lines of code, this elements works as a block of commands.
The functions can be defined as only one line or have multiple lines too.
The “for” and “if” statements always have more than one line of code (like multiline functions)
The blocks of commands are defined adding a margin (a tabulator or 4 spaces) by each level.
When “if” and “for” statements are combined (or used into a user defined function) must to add tabulators in each level of nest to separate differents blocks of commands.
Example of structure…
commands of script
commands of script
UserFunction(param1,param2)=>
····commands of function
····var1=if close>open
········commands of if block
········commands of if block
········var2=for c=1 to 10 by 1
············commands of for block
············commands of for block
············value for assignation to var2
········commands of if block
········value for assign to var1 when true
····else
········commands of if block
········value to be assigned to var1 when false
····commands of function
····response value of the function
commands of script
commands of script
As you see, the blocks are defined including a tabulator (4 spaces) or some tabulators (4 + 4 + 4 … spaces) nested in base of the number of levels in the blocks.
The block of statements can recognize vars declared outer of the blocks but the variables defined into them are locals (and can not be accessible in the rest of the script)
ABOUT ‘IF’ STATEMENT
Can not have commands after definition in the first line.
The “else” word must be at the same level of margin of the “if” declaration and can not have other commands in the same line.
The variable gets its value from the last tabulated line of the block (or the last tabulated line of each “else” sub-block, depending on the result of the condition)
variable=if [condition]
····commands if true
····commands if true
····return value if true
else
····commands if false
····commands if false
····return value if false
ABOUT ‘FOR’ STATEMENT
The first line only can have the loop configuration.
The variable used for the loop counter can not be mutable.
Can include “break” or “continue” statements to manipulate the loop conditions interactively.
The variable gets its value from the last tabulated line of the block.
variable=for [counter initiation] to [limit] by [incremental value]
····commands of loop
····commands of loop
····continue
····commands of loop
····commands of loop
····break
····commands of loop
····return value
ABOUT USER FUNCTIONS
Remember the functions must be situated in the top of script (always before be called).
You can not declare functions into other functions, this is not allowed.
The definition of user functions can not have other commands before (in the same line).
The compiler understands as a multilinear function when (in the first line) there is nothing after the symbol ‘=>’
The function returns the value in the last tabulated line.
UserFunction(param1,param2)=>
····commands of function
····commands of function
····commands of function
····return value
REAL EXAMPLE
Here you have a simple code mixing all block statements…
//@version=2
study("WWW.PINESCRIPTS.ORG",overlay=false)
myFun(param)=>
v=for f=1 to param by 1
if close[f]>close
break
f
plot(myFun(100), style=columns)
IMPORTANT
If you get used to separate expresions by some lines you must always remember not to add margins with tabulators or spaces in quantity multiple of 4, because the interpreter could be confuse with BLOCKS of instructions and reports error. You can separate a expresion in some lines like this…
// this works fine
a=close>open?
1:
0
plot(a)
…but the compiler will reports error if you add a margin of separation of a tabulator or 4 spaces (or multiple of 4)….
// this results error
a=close>open?
1:
0
plot(a)
Can we do nested or functions in the if function? for example
var1 = if close > open or close[1] > open
Can we also nest a loop in here to test it against the pevious 25 closes?
Hi, as you see in the examples you can use nested blocks in the if statement.
Anyway you can test your proyect it in the Pine Editor of TradingView.
Regards.