PineScript / 转换为 V4

时间:2021-05-16 19:06:55

标签: pine-script

我很难将此脚本转换为最新版本 4。如果可能,请有人帮助我。我所做的是将我最喜欢的一项研究转化为可操作的策略。我尝试向其中添加新功能就像一个可选择的时间范围,在该范围内我可以限制我的回测运行,但由于 pine 脚本版本而无法运行。

strategy(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator [LazyBear]", overlay=true)

length = input(20, title="BB Length")
mult = input(2.0,title="BB MultFactor")
lengthKC=input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")

useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)

// Calculate BB
source = close
basis = sma(source, length)
dev = multKC * stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = sma(source, lengthKC)
range = useTrueRange ? tr : (high - low)
rangema = sma(range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz  = (sqzOn == false) and (sqzOff == false)

val = linreg(source  -  avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)), 
            lengthKC,0)

bcolor = iff( val > 0, 
            iff( val > nz(val[1]), lime, green),
            iff( val < nz(val[1]), red, maroon))
scolor = noSqz ? blue : sqzOn ? black : gray 
//plot(val, color=bcolor, style=histogram, linewidth=4)
//plot(0, color=scolor, style=cross, linewidth=2)



// Signal


buyEntry = crossover((bcolor == lime)? 1 : 0, 0)
sellEntry = crossover((bcolor == red)? 1 : 0, 0)

//plotarrow((buyEntry ? 1 : 0))
//plotarrow((sellEntry ? -1 : 0))

/////////////////////////////////////////////////////////////////////
//This controls the time when the backtest runs
//start = timestamp(2020,1,1)
//end = timestamp(2021,4,29)
//t = time(timeframe.period)



////////////////////////////////////////////////////////////////////////

//if time >= start and time <= end 
strategy.entry("Entry", strategy.long, 10000, when=buyEntry)
strategy.close("Entry", when = sellEntry)
   

1 个答案:

答案 0 :(得分:0)

//@version=4
strategy(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator [LazyBear]", overlay=true)

length = input(20, title="BB Length")
mult = input(2.0,title="BB MultFactor")
lengthKC=input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")

useTrueRange = input(true, title="Use TrueRange (KC)", type=input.bool)

// Calculate BB
source = close
basis = sma(source, length)
dev = multKC * stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev

// Calculate KC
ma = sma(source, lengthKC)
range = useTrueRange ? tr : (high - low)
rangema = sma(range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC

sqzOn  = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz  = (sqzOn == false) and (sqzOff == false)

val = linreg(source  -  avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)), lengthKC,0)

bcolor = iff( val > 0, iff( val > nz(val[1]), color.lime, color.green), iff( val < nz(val[1]), color.red, color.maroon))
scolor = noSqz ? color.blue : sqzOn ? color.black : color.gray 
//plot(val, color=bcolor, style=histogram, linewidth=4)
//plot(0, color=scolor, style=cross, linewidth=2)



// Signal


buyEntry = crossover((bcolor == color.lime)? 1 : 0, 0)
sellEntry = crossover((bcolor == color.red)? 1 : 0, 0)

//plotarrow((buyEntry ? 1 : 0))
//plotarrow((sellEntry ? -1 : 0))

/////////////////////////////////////////////////////////////////////
//This controls the time when the backtest runs
//start = timestamp(2020,1,1)
//end = timestamp(2021,4,29)
//t = time(timeframe.period)



////////////////////////////////////////////////////////////////////////

//if time >= start and time <= end 
strategy.entry("Entry", strategy.long, 10000, when=buyEntry)
strategy.close("Entry", when = sellEntry)