1

I am trying to write a strategy "to test which indicator is better"

So I ask in the inputs if to use an indicator or not to enter a trade.

enter image description here

For example, stochastics. If user selects stochastics to enter the strategy, then Tradingview will have to enter the trade only in case k crosses over d.

Problem is, in the IF function I get this error: "Syntax error at input 'StochIN'"

Clearly I am making some very basic mistake, but I cannot get my head around it...

Here is a sample of the script:

StochIN = input(title="Stoch IN", type=input.bool, defval=true, group="Indicators", inline="5")

/// Stochastic
periodK = input(14, title="%K Length", minval=1, group="Stochastic")
smoothKSto = input(7, title="%K Smoothing", minval=1, group="Stochastic")
periodD = input(3, title="%D Smoothing", minval=1, group="Stochastic")
stok = sma(stoch(close, high, low, periodK), smoothKSto)
stod = sma(stok, periodD)

/////If Stochastic is selected, then StochIn variable becomes true only at crossover, otherwise it is always true
if StochIN
     StochIN := crossover(stok, stod) or crossover(stok[1], stod[1])
else
     StochIN := true

///second condition
rsiIN = input(title="RSI IN", type=input.bool, defval=true, group="Indicators", inline="8")

rsi = rsi(close, 14)

if rsiIN
     rsiIN := crossover(rsi, 30) or crossover(rsi[1], 30)
else
     rsiIN := true

////Enter the trade when all conditions are true. 
if StochIn and rsiIn
    strategy.entry("Long", strategy.long)

1 Answers1

1

Your if blocks must be indented by 4 spaces or 1 tab. Your blocks have 5 spaces.

Also, pinescript is case-sensitive.

StochIn and rsiIn in the below piece should be StochIN and rsiIN.

if StochIn and rsiIn
    strategy.entry("Long", strategy.long)

Complete code:

StochIN = input(title="Stoch IN", type=input.bool, defval=true, group="Indicators", inline="5")

/// Stochastic
periodK = input(14, title="%K Length", minval=1, group="Stochastic")
smoothKSto = input(7, title="%K Smoothing", minval=1, group="Stochastic")
periodD = input(3, title="%D Smoothing", minval=1, group="Stochastic")
stok = sma(stoch(close, high, low, periodK), smoothKSto)
stod = sma(stok, periodD)

/////If Stochastic is selected, then StochIn variable becomes true only at crossover, otherwise it is always true
if StochIN
    StochIN := crossover(stok, stod) or crossover(stok[1], stod[1])
else
    StochIN := true

///second condition
rsiIN = input(title="RSI IN", type=input.bool, defval=true, group="Indicators", inline="8")

rsi = rsi(close, 14)

if rsiIN
    rsiIN := crossover(rsi, 30) or crossover(rsi[1], 30)
else
    rsiIN := true

////Enter the trade when all conditions are true. 
if StochIN and rsiIN
    strategy.entry("Long", strategy.long)
vitruvius
  • 15,740
  • 3
  • 16
  • 26