I am new to Pinescript and trying to code an indicator.
- If following condition is met
OPSCond := barstate.isconfirmed and close > low[1] and low < low[1] and close > low
2. Make the variable isOPS true
isOPS := OPSCond ? true : false
3. Assign the OPS parameters ie OPS value, OPSIndex (bar index) and OPSTime (when isOPS is true)
OPS := isOPS ? low[1] : nz(OPS[1]) OPSIndex := isOPS ? bar_index[1] : nz(OPSIndex[1]) OPSTime := isOPS ? timeline() : nz(OPSTime[1])
This seems is to be working fine as lines are drawn correctly.
The code written to check when the condition become invalid also seems to work fine. Not repeating it here.
However the isOPS value is shown as false on every bar even when it should be true. See the attached picture.
I need the script to have isOPS as true when the chart is showing OPS Line (Green Line) correctly at the OPS level so that can develop the script further.
I have tried many things but nothing seems to work. Please guide me, where I am going wrong and what can I do to correct it.
Full Script is below
//@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 = "OPSn" + 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)
Thanks
SKChartist is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.