Pine script 试图用barssince替换手动计数器
我有两个问题:Pine script 试图用barssince替换手动计数器,pine-script,Pine Script,我有两个问题: 我想用barssince替换红烛台,因为它看起来更好。问题是当我这么做的时候:if(barssince(不是isCandleGreen和close>middleBand和open2和isLong)有点打破逻辑,在下一支蜡烛上卖出,这不是我想要的。顺便说一句,测试是在GTO/USDT(双星)30米间隔上进行的,图片是在3月21日18:30 不期望: 预期: 替换isCandleGreen=close>open的更性感的方法是什么 代码 /@version=4 策略(title=
if(barssince(不是isCandleGreen和close>middleBand和open2和isLong)
有点打破逻辑,在下一支蜡烛上卖出,这不是我想要的。顺便说一句,测试是在GTO/USDT(双星)30米间隔上进行的,图片是在3月21日18:30isCandleGreen=close>open的更性感的方法是什么
/@version=4
策略(title=“MACD+BB strategy”,overlay=true,max\u labels\u count=500,initial\u capital=300,currency=currency.USD,聚合=0,默认数量类型=策略股权百分比,默认数量值=100,精度=2,佣金类型=策略佣金百分比,佣金值=0.1000)
//MACD输入
fast_length=input(title=“fast length”,type=input.integer,deffal=12)
slow_length=input(title=“slow length”,type=input.integer,deffal=26)
src=input(title=“Source”,type=input.Source,deffal=close)
信号长度=输入(title=“信号平滑”,type=input.integer,minval=1,maxval=50,deffal=9)
sma_源=输入(title=“简单MA(振荡器)”,type=input.bool,deffal=false)
sma_信号=输入(title=“简单MA(信号线)”,type=input.bool,deffal=false)
//MACD计算
fast\u ma=sma\u源?sma(src,快速长度):ema(src,快速长度)
慢速\u ma=sma\u源?sma(src,慢速长度):ema(src,慢速长度)
macd=快-慢
//=========================================================================================
//布林带
长度=输入(20,最小值=1)
srcBB=输入(关闭,title=“源”)
mult=输入(2.0,最小值=0.001,最大值=50,title=“StdDev”)
中间带=sma(srcBB,长度)
dev=mult*stdev(srcBB,长度)
上频带=中间频带+dev
lowerBand=中间带-开发
偏移量=输入(0,“偏移量”,类型=input.integer,最小值=-500,最大值=500)
绘图(中间带,“基础”,颜色=#198787,偏移量=偏移量)
p1=绘图(上标带,“上标”,颜色=#198787,偏移=偏移)
p2=绘图(下边,“下边”,颜色=#198787,偏移量=偏移量)
填充(p1,p2,title=“Background”,color=#198787,transp=95)
//=========================================================================================
//测试开始日期
testStartYear=输入(2020,“回溯测试开始年份”)
testStartMonth=输入(1,“反向测试开始月份”)
testStartDay=输入(1,“回溯测试开始日”)
testPeriodStart=时间戳(testStartYear、testStartMonth、testStartDay,0,0)
//如果要使用特定的日期范围,请停止日期
testStopYear=输入(2030,“反向测试停止年”)
testStopMonth=输入(1,“反向测试停止月”)
testStopDay=输入(1)
testPeriodStop=时间戳(testStopYear、testStopMonth、testStopDay、0,0)
testPeriod()=>
时间>=测试周期开始和时间0)
isShort=(strategy.position\u size<0)
var redcandlesconter=0
var wasblowmiddlebandonce=false
buySignal=false
sellSignal=false
//确定蜡烛是否是绿色的
isCandleGreen=关闭>打开
if(打开<中间带,关闭<中间带)
wasBelowMiddleBandOnce:=真
如果(macd>0且低于中间带一次,isCandleGreen且打开>中间带,关闭<上带)
buySignal:=真
红色烛光计数器:=0
if(非isCandleGreen and close>middleBand和open
我想,你可以试着像这样使用barssince:
***
some_fun() =>
barssince(not isCandleGreen and close > middleBand and open < upperBand)
// this means that previous 2 bars is red, their close > middleBand and open < upperBand
plot(some_fun() == 0 and some_fun()[1] == 0 ? 1 : 0)
***
***
一些乐趣()=>
barssince(非isCandleGreen和close>中间带和open<上带)
//这意味着前两条线是红色的,它们的闭合>中间带,开放<上带
绘图(some_-fun()==0和some_-fun()[1]==0?1:0)
***
用户手册告诉我们,当表达式为true时,barssince应该返回从位置开始的条数
更新:另一个正确但不是最佳的解决方案,具有值当
n = valuewhen(macd > 0 and wasBelowMiddleBandOnce and isCandleGreen and open > middleBand and close < upperBand, bar_index, 0)
v1 = valuewhen(not isCandleGreen and close > middleBand and open < upperBand, bar_index, 0)
v2 = valuewhen(not isCandleGreen and close > middleBand and open < upperBand, bar_index, 1)
cond = v1 > n and v2 > n
plot(cond and isLong ? 1 : 0)
n=valuewhen(macd>0且小于IDLBANDONCE且为isCandleGreen与open>MIDLEGBAND与closemiddleBand和open中间带和open<上带,条形索引,1)
cond=v1>n和v2>n
绘图(条件和isLong?1:0)
这两支红蜡烛可能不在一个序列中。中间可能有绿色蜡烛。我不确定,但在上面的图片上它看起来像预期的那样工作。如果没有,请指定详细信息您在答案中粘贴的代码不知怎的不起作用。我上面的代码可以工作,但我想使用barssince
,因为它看起来很难看tredCandlesCounter
。基本上,我们期望的是代码与我问题中的代码一样工作,但使用barssince。我理解,谢谢。我认为使用counter的解决方案是最佳的。我认为我们不能像您期望的那样使用barssince。我找到了另一个具有函数值When的解决方案,但它不干净
n = valuewhen(macd > 0 and wasBelowMiddleBandOnce and isCandleGreen and open > middleBand and close < upperBand, bar_index, 0)
v1 = valuewhen(not isCandleGreen and close > middleBand and open < upperBand, bar_index, 0)
v2 = valuewhen(not isCandleGreen and close > middleBand and open < upperBand, bar_index, 1)
cond = v1 > n and v2 > n
plot(cond and isLong ? 1 : 0)