了解为什么 Condition 不断变为 false

问题描述 投票:0回答:1

我是 Pinescript 新手,正在尝试编写指标。

如果满足以下条件

OPSCond :=  barstate.isconfirmed and close > low[1] and low < low[1] and  close > low 

使变量isOPS为true

isOPS :=  OPSCond ? true : false

分配 OPS 参数,即 OPS 值、OPSIndex(柱索引)和 OPSTime(当 isOPS 为 true 时)

OPS      :=  isOPS   ? low[1]        : nz(OPS[1])
OPSIndex :=  isOPS   ? bar_index[1]  : nz(OPSIndex[1])
OPSTime  :=  isOPS   ? timeline()    : nz(OPSTime[1])

这似乎工作正常,因为线条绘制正确。

为检查条件何时无效而编写的代码似乎也可以正常工作。这里就不重复了。

然而,isOPS 值在每个柱上都显示为 false,即使它应该为 true。请参阅附图。

当图表在 OPS 级别正确显示 OPS 线(绿线)时,我需要脚本将 isOPS 设置为 true,以便可以进一步开发脚本。

我尝试了很多方法,但似乎没有任何效果。我哪里出错了,我能做些什么来纠正它?

完整脚本:

//@version=5
indicator("OPS only",max_bars_back = 500, overlay = true)

//Function region
    
timeline()=>
    txt1 = str.tostring(year(time),"#")
    txt2 = month(time)>9?str.tostring(month(time),"#"):"0"+ str.tostring(month(time),"#")
    txt3 = dayofmonth(time)>9?str.tostring(dayofmonth(time),"#"):"0"+ str.tostring(dayofmonth(time),"#")
    txt4 = str.tostring(hour(time)*100+minute(time),"#")
    timeline1 = txt1 + txt2 + txt3 + txt4
    timeline = math.round(str.tonumber(timeline1))
//Input Region
var color ColorOPS      =   #08803a
var bool long   =   true

//Input Region Ends

// Variable Declarations


var bool OPSCond    =   false, OPSCond := na(OPSCond[1])
var bool isOPS      =   false, isOPS :=nz(isOPS[1])
var OPSLine         =   line.new(na,na,na,na,color=ColorOPS,style = line.style_solid,width=2)
var float OPS       =   na, OPS := nz(OPS[1])
var label OPSLabel  =   label.new(na,na,text = "",xloc=xloc.bar_index, yloc=yloc.belowbar,color=color.new(color.black,100),textcolor = ColorOPS)
var int OPSIndex    =   na, OPSIndex := nz(OPSIndex[1])
var int OPSTime     =   na, OPSTime := nz(OPSTime[1])

//OPS Region Starts

OPSCond             :=  barstate.isconfirmed and close > low[1] and low < low[1] and  close > low 
isOPS               :=  OPSCond ? true                              : false
OPS                 :=  isOPS   ? low[1]                            : nz(OPS[1])
OPSIndex            :=  isOPS   ? bar_index[1]                      : nz(OPSIndex[1])
OPSTime             :=  isOPS   ? timeline()    : nz(OPSTime[1])

// If any bar closes below OPS level then invalidate OPS
if barstate.isconfirmed and close < OPS
    isOPS       :=  false
    OPS         :=  na
    OPSIndex    :=  na
    OPSTime     :=  na

    line.set_xy1(OPSLine,na,na)
    line.set_xy2(OPSLine,na,na)    
    // Delete OPSLabel  - inserted on 18.10.23
    
    label.set_xy(OPSLabel,na,na)
    label.set_text(OPSLabel,na)

    // Redraw OPSLine
if isOPS
    line.set_xy1(long?OPSLine:na,OPSIndex,OPS)
    line.set_xy2(long?OPSLine:na,OPSIndex+5,OPS)    
    // Redraw OPSLabel  - inserted on 18.10.23
    
    txt = "OPS\n" + str.tostring(OPS,"0.00")
    label.set_xy(long?OPSLabel:na,OPSIndex,OPS-5)
    label.set_text(long?OPSLabel:na,txt)
    
plotshape(isOPS,title="OPS",style = shape.arrowup,location=location.belowbar)   
//OPS Region ends
    
// All Data Table Printing for debugging

var table AllTable = table.new(position.top_right, 2, 23, bgcolor=color.white, frame_color=color.white, frame_width=2, border_width=2)
//Initialise the table
txt000 = isOPS?"isOPS:True":not(isOPS)?"isOPS:false":"na"
table.cell(AllTable, 0, 0, text=txt000, bgcolor=color.silver, text_color=color.navy, text_size=size.small)
txt001 = "OPS:" + str.tostring(OPS,'#')
table.cell(AllTable, 0, 1, text=txt001, bgcolor=color.silver, text_color=color.navy, text_size=size.small)
txt002 = "OPSIndex:" + str.tostring(OPSIndex,'#')
table.cell(AllTable, 0, 2, text=txt002, bgcolor=color.silver, text_color=color.navy, text_size=size.small)
txt003 = "OPSt:" + str.tostring(OPSTime,'#')
table.cell(AllTable, 0, 3, text=txt003, bgcolor=color.silver, text_color=color.navy, text_size=size.small)

示例图表

为了回应 Gustavo 的评论,我添加了 2 张照片来展示我想说的内容。

isOPS = false

isOPS 绘制正确,但其值在表中显示为 false

conditional-statements pine-script-v5
1个回答
0
投票

找到解决方案。修改后的代码如下。

//@version=5
indicator("OPS only",max_bars_back = 500, overlay = true)

//Function region
    
timeline()=>
    txt1 = str.tostring(year(time),"#")
    txt2 = month(time)>9?str.tostring(month(time),"#"):"0"+ str.tostring(month(time),"#")
    txt3 = dayofmonth(time)>9?str.tostring(dayofmonth(time),"#"):"0"+ str.tostring(dayofmonth(time),"#")
    txt4 = str.tostring(hour(time)*100+minute(time),"#")
    timeline1 = txt1 + txt2 + txt3 + txt4
    timeline = math.round(str.tonumber(timeline1))
//Input Region
var color ColorOPS      =   #08803a
var bool long   =   true

//Input Region Ends

// Variable Declarations


var bool OPSCond    =   false, OPSCond := na(OPSCond[1])
var bool isOPS      =   false, isOPS :=nz(isOPS[1])
var line OPSLine         =   line.new(na,na,na,na,color=ColorOPS,style = line.style_solid,width=2)
var float OPS       =   na, OPS := nz(OPS[1])
var label OPSLabel  =   label.new(na,na,text = "",xloc=xloc.bar_index, yloc=yloc.belowbar,color=color.new(color.black,100),textcolor = ColorOPS)
var int OPSIndex    =   na, OPSIndex := nz(OPSIndex[1])
var int OPSTime   =   na, OPSTime := nz(OPSTime[1])
var bool OPSFresh = false 
var int OPSCount = 0 // Counts No of OPS Formed. Is reset to 0 when an OPS is ivalidated. If is  greater than 0 new OPS is not alllowed.
var bool OPSCondFirst    =   false, OPSCondFirst := na(OPSCondFirst[1])
var bool isOPSFirst      =   false, isOPSFirst :=nz(isOPSFirst[1])
var float OPSFirst       =   na, OPSFirst := nz(OPSFirst[1])
var int OPSIndexFirst    =   na, OPSIndexFirst := nz(OPSIndexFirst[1])
var int OPSTimeFirst   =   na, OPSTimeFirst := nz(OPSTimeFirst[1])
var bool OPSFreshFirst = false 
//New Session
newDay = bool(ta.change(dayofweek))
if newDay
    OPSCond    :=   false
    isOPS      :=   false
    OPSLine         :=   line.new(na,na,na,na,color=ColorOPS,style = line.style_solid,width=2)
    OPS       :=   na
    OPSLabel  :=   label.new(na,na,text = "",xloc=xloc.bar_index, yloc=yloc.belowbar,color=color.new(color.black,100),textcolor = ColorOPS)
    OPSIndex    :=   na
    OPSTime   :=   na
    OPSFresh := false  
    OPSCount := 0
    alert("First bar", freq = alert.freq_all)
//OPS Region Starts
if OPSCount == 0
    isOPS           :=  barstate.isconfirmed and close > low[1] and low < low[1] and  close > low // and not(low>OPS) //and not(close>OPS[1])
    if isOPS
        OPS                 :=  low[1]
        OPSCount            :=  OPSCount + 1
        OPSIndex            :=  bar_index[1]
        OPSTime             :=  timeline()

// If any bar closes below OPS level then OPS invalidated
if barstate.isconfirmed and close < OPS
    isOPS       :=  false
    OPS         :=  na
    OPSIndex    :=  na
    OPSTime     :=  na
    OPSCount    := 0
    line.set_xy1(OPSLine,na,na)
    line.set_xy2(OPSLine,na,na)    
    // Delete OPSLabel  - inserted on 18.10.23
    
    label.set_xy(OPSLabel,na,na)
    label.set_text(OPSLabel,na)

    // Redraw OPSLine

if isOPS
    line.set_xy1(long?OPSLine:na,OPSIndex,OPS)
    line.set_xy2(long?OPSLine:na,OPSIndex+5,OPS)    
    // Redraw OPSLabel  - inserted on 18.10.23
    
    txt = "OPS\n" + str.tostring(OPS,"0.00")
    label.set_xy(long?OPSLabel:na,OPSIndex,OPS-5)
    label.set_text(long?OPSLabel:na,txt)
    
 
plotshape(isOPS and ta.change(OPSCount)==1,title="OPS",style = shape.arrowup,location=location.belowbar)    
//OPS Region ends
© www.soinside.com 2019 - 2024. All rights reserved.