gr68
gr68

Reputation: 502

how to add a time control over a Pine script strategy

I have this Pine script Strategy and it seems to work fine with Cripto and Stocks too

//@version=4
strategy("SSL Channel Cross", overlay=true)
period=input(title="Period", defval=10)
len=input(title="Period", defval=10)
smaHigh=sma(high, len)
smaLow=sma(low, len)
Hlv = 0
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh: smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh

plot(sslDown, linewidth=2, color=color.red)
plot(sslUp, linewidth=2, color=color.lime)

longCondition = crossover(sslUp, sslDown)
shortCondition = crossunder(sslUp, sslDown)

if (longCondition)
    strategy.close("Short", strategy.long)
    strategy.entry("Long", strategy.long)
    
if (shortCondition)
    strategy.close("Long", strategy.long)
    strategy.entry("Short", strategy.short)

I'm trying to add a time control section code so I could test the strategy in a specific time. I modified code as follow

//@version=4
strategy("SSL Channel Cross", overlay=true)
period=input(title="Period", defval=10)
len=input(title="Period", defval=10)
// From Date Inputs
fromDay = input(defval=16, title="From Day", minval=1, maxval=31)
fromMonth = input(defval=2, title="From Month", minval=1, maxval=12)
fromYear = input(defval=2021, title="From Year", minval=1970)
// To Date Inputs
toDay = input(defval=17, title="To Day", minval=1, maxval=31)
toMonth = input(defval=2, title="To Month", minval=1, maxval=12)
toYear = input(defval=2021, title="To Year", minval=1970)
// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate

smaHigh=sma(high, len)
smaLow=sma(low, len)
Hlv = 0
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh: smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh
    
plot(sslDown, linewidth=2, color=color.red)
plot(sslUp, linewidth=2, color=color.lime)
    
longCondition = crossover(sslUp, sslDown)
shortCondition = crossunder(sslUp, sslDown)
    
if (longCondition)
strategy.close("Short", strategy.long, when=time_cond)
strategy.entry("Long", strategy.long, when=time_cond)
        
if (shortCondition)
strategy.close("Long", strategy.long, when=time_cond)
strategy.entry("Short", strategy.short, when=time_cond)

but it returns the error

line 32: Mismatched input 'strategy.close' expecting 'end of line without line continuation'.

How to fix the script ?

Upvotes: 0

Views: 930

Answers (1)

AnyDozer
AnyDozer

Reputation: 2568

The operator if must have 4 spaces or a tab

//@version=4
strategy("SSL Channel Cross", overlay=true)
period=input(title="Period", defval=10)
len=input(title="Period", defval=10)
// From Date Inputs
fromDay = input(defval=16, title="From Day", minval=1, maxval=31)
fromMonth = input(defval=2, title="From Month", minval=1, maxval=12)
fromYear = input(defval=2021, title="From Year", minval=1970)
// To Date Inputs
toDay = input(defval=17, title="To Day", minval=1, maxval=31)
toMonth = input(defval=2, title="To Month", minval=1, maxval=12)
toYear = input(defval=2021, title="To Year", minval=1970)
// Calculate start/end date and time condition
startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finishDate = timestamp(toYear, toMonth, toDay, 00, 00)
time_cond = time >= startDate and time <= finishDate

smaHigh=sma(high, len)
smaLow=sma(low, len)
Hlv = 0
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh: smaLow
sslUp   = Hlv < 0 ? smaLow : smaHigh
    
plot(sslDown, linewidth=2, color=color.red)
plot(sslUp, linewidth=2, color=color.lime)
    
longCondition = crossover(sslUp, sslDown)
shortCondition = crossunder(sslUp, sslDown)

if (longCondition)
    strategy.close("Short", when=time_cond)
    strategy.entry("Long", strategy.long, when=time_cond)
        
if (shortCondition)
    strategy.close("Long", when=time_cond)
    strategy.entry("Short", strategy.short, when=time_cond)

Upvotes: 1

Related Questions