October 22, 2015

New feature of Pine Script Language: if statement

Working with code becomes more and more flexible! If-statement is now available in Pine Script language. This statement defines what block of statements must be checked when conditions of the expression are satisfied.

Attention! To have access to and use the if statement, one should specify the version of Pine Script language in the very first line of code: //@version=2

Example:

//@version=2
study("if statement")
out = if close > open
    high
else
    low
plot(out)

You can read more about if statement in the Pine Script Tutorial.

If statement may be also useful in strategy trading:

//@version=2
strategy("My Strategy", overlay=true)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

You can read more about strategies in the Pine Script Tutorial and the Pine Script Reference