首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pine tradingview:一次,如果直到新事件

Pine tradingview:一次,如果直到新事件
EN

Stack Overflow用户
提问于 2020-06-01 13:56:48
回答 1查看 416关注 0票数 0

我在复制其他公共脚本的基础上“创建”了我的第一个Tradingview脚本。除了最后一部分,大部分我都能弄明白。我想知道是否有可能添加一次if语句,直到另一个特定事件发生。

在下面的printscreen中我有一个例子。在我的脚本中,我有一条彩色的趋势线和几根蜡烛。我想输入一个短的时候,趋势线是红色的,蜡烛是黑色的(第一个绿色的圆圈)。但该系列图再次将条形颜色从白色变为黑色(红色圆圈)。我只想在发生新事件之前第一次添加警报(绿色圆圈);当趋势线改变颜色时(紫色圆圈)。

Chart example

代码语言:javascript
复制
BBperiod          = input(defval = 19,     title = "BB Period",    type = input.integer, minval       = 1)
BBdeviations      = input(defval = 1.00,     title = "BB Deviations",    type = input.float, minval = 0.1, step=0.05)
UseATRfilter      = input(defval = true, title = "ATR Filter",  type = input.bool)
ATRperiod         = input(defval = 7,     title = "ATR Period",    type = input.integer, minval = 1)

BBUpper=sma (close,BBperiod)+stdev(close, BBperiod)*BBdeviations
BBLower=sma (close,BBperiod)-stdev(close, BBperiod)*BBdeviations
//
TrendLine = 0.0
iTrend = 0.0
buy = 0.0
sell = 0.0
//
BBSignal = close>BBUpper? 1 : close<BBLower? -1 : 0
// 
if BBSignal == 1 and UseATRfilter == 1
TrendLine:=low-atr(ATRperiod)
if TrendLine<TrendLine[1] 
    TrendLine:=TrendLine[1]
if BBSignal == -1 and UseATRfilter == 1
TrendLine:=high+atr(ATRperiod)
if TrendLine>TrendLine[1]
    TrendLine:=TrendLine[1]
if BBSignal == 0 and UseATRfilter == 1
TrendLine:=TrendLine[1]
//
if BBSignal == 1 and UseATRfilter == 0
TrendLine:=low
if TrendLine<TrendLine[1] 
    TrendLine:=TrendLine[1]
if BBSignal == -1 and UseATRfilter == 0
TrendLine:=high
if TrendLine>TrendLine[1]
    TrendLine:=TrendLine[1]
if BBSignal == 0 and UseATRfilter == 0
TrendLine:=TrendLine[1]
//
iTrend:=iTrend[1]
if TrendLine>TrendLine[1] 
iTrend:=1
if TrendLine<TrendLine[1] 
iTrend:=-1
//
buy:=iTrend[1]==-1 and iTrend==1 ? 1 : na
sell:=iTrend[1]==1 and iTrend==-1? 1 : na
//
plot(TrendLine, color=iTrend > 0?color.blue:color.red         ,style=plot.style_line,linewidth=2,transp=0,title="Trend Line") 


//BarcolorT1
length = input(title="Length", type=input.integer, defval=50)
src_=input(close, title="Source", type=input.source)
mult=input(6.0, title="Mult")
barc=input(true, title="Use barcolor?")
plots=input(false, title="Show plots?")


r(src, n)=>
s = 0.0
for i = 0 to n-1
    s := s + ((n-(i*2+1))/2)*src[i]
x=s/(n*(n+1))
x

l=sma(low, length)
h=sma(high, length)
lr= l+mult*r(low, length)
hr= h+mult*r(high, length)

trend=0
trend:=src_ > lr and src_ > hr ? 1 : src_ < lr and src_ < hr ? -1 : trend[1]

long=0
short=0
long:= trend==1 and src_>h ? 1 : trend==-1 ? -1 : long[1]
short:= trend==-1 and src_<l ? 1 : trend==1 ? -1 : short[1]

barcolor(barc? (long>0? color.green : short>0? color.black : trend>0? color.white: trend<0 ?     color.white : color.blue) : na)

//plot shape
data = short!=short[1] and short==1 and iTrend < 0
plotshape(data, style=shape.xcross)
EN

回答 1

Stack Overflow用户

发布于 2020-06-03 14:26:14

您可以通过一个额外的标志来实现您的目标,该标志将在所需的事件上激活和停用。代码如下:

代码语言:javascript
复制
var active = false

// Activation
active := active or data

// Deactivation
if nz(iTrend[1]) <= 0 and iTrend > 0
    active := false

activated = not nz(active[1], false) and active

plotshape(activated, style=shape.triangledown, color=color.red, size=size.small, location=location.belowbar)

将其添加到脚本的末尾,当趋势线为红色且条形图颜色首次从白色变为黑色时,它将显示红色三角形。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62126459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档