Trading View plotting false statements? - boolean

I'm having an issue with pinescript plotting false statements as part of the plot function. For example, I can type a basic true false formula such as (close < 30) and set my plot to put a lime background against all candles in which the statement is true. But trading view has been acting up and not distinguishing true and false in some situations. Why is this happening? Can anyone help?
if close < 30
longstay := false
shortstay := true
debugger = shortstay ? color.new(color.lime, 30) : na
bgcolor(debugger)

//#version=5
indicator("test",overlay=true)
var longstay=true
var shortstay=false
if close < 30
longstay := false
shortstay := true
else
longstay := true
shortstay := false
debugger = shortstay ? color.new(color.lime, 30) : na
bgcolor(debugger)
You have been setting the value of variable shortstay as true when close was below 30
But it remained true for all the further candles
So I have changed its value to false when the close is above 30 and now it will only show colored bars when the condition is true

Related

Pine Script 5 - Unwanted alert messages

I receive unwanted alert messages. I would like to have only 1 "Go Long" alert when going long and then no other "Go Long" alerts until after the first "Go Short" alert. I'm having trouble getting this right.
//#version=5
strategy("Single Alert Strategy", overlay=true)
// Create flags to avoid multiple Long signals when already in Long and multiple Short signals when already in Short
isLong = false // We initialize a value and give it false (we are not in a Long position)
isLong := nz(isLong[1]) // We check the previous value and assign the previous value to this variable (when no previous value exist it will become false)
isShort = false // We initialize a value and give it false (we are not in a Short position)
isShort := nz(isShort[1]) // We check the previous value and assign the previous value to this variable (when no previous value exist it will become false)
// Long Short conditions
tot_rsi = ta.rsi(close, 14)
bullLevel = 30
bearLevel = 70
longCondition = ta.crossover(tot_rsi, bullLevel) and not isLong // Go Long only if we are not already Long
shortCondition = ta.crossunder(tot_rsi, bearLevel) and not isShort // Go Short only if we are not already Short
// Change the flags to avoid duplicate signals
if (longCondition)
isLong := true
isShort := false
if (shortCondition)
isLong := false
isShort := true
if longCondition
strategy.entry('Long', strategy.long, alert_message = "GoLong")
if shortCondition
strategy.entry('Short', strategy.short, alert_message = "GoShort")
// Plot the signals and compare them with the Alerts log
plotshape(series=longCondition, title="Long", text="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=shortCondition, title="Short", text="Short", style=shape.triangledown, location=location.belowbar, color=color.red, size=size.small)
I followed the instructions in the answer provided here. While the signal indicators plot correctly (no additional signals), I do get an alert message when there was no signal plotted. This strategy is newly created with a new alert and is only added to 1 chart. The alert was created at 06:58, there were signals plotted before the alert was created but it triggered at 07:05 even though there was no signal plotted.
RSI level at 07:05 was 48.20
Any ideas what may have caused this alert to trigger and how to prevent additional alerts like these?
Link to screenshot
Edit: I'm testing an alternative approach. The unwanted alerts do not come frequently so I may have to run it for some time. If anyone knows of a different work around then I'm happy to give that a try.
//#version=5
strategy("Single Alert Strategy", overlay=true, calc_on_every_tick = true)
noOrder = strategy.closedtrades == 0 // There is no previous order
openLong = strategy.position_size > 0 // We are going Long
openShort = strategy.position_size < 0 // We are going Short
// Long Short conditions
tot_rsi = ta.rsi(close, 14)
bullLevel = 30
bearLevel = 70
longCondition = ta.crossover(tot_rsi, bullLevel) and (openShort[1] or noOrder) // Go Long only if the previous order was Short
shortCondition = ta.crossunder(tot_rsi, bearLevel) and (openLong[1] or noOrder) // Go Short only if the previous order was Long
if longCondition
strategy.entry('Long', strategy.long, alert_message = "GoLong")
if shortCondition
strategy.entry('Short', strategy.short, alert_message = "GoShort")
// Plot the signals and compare them with the Alerts log
plotshape(series=longCondition, title="Long", text="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=shortCondition, title="Short", text="Short", style=shape.triangledown, location=location.belowbar, color=color.red, size=size.small)
Edit 2: The 2nd approach also created an alert despite there not being any signal plotted. Screenhot here
You should test also with strategy.opentrades which will return something !=0 if a trade (Long or Short) is currently open :
if longCondition and strategy.opentrades==0
strategy.close_all(comment="close", immediately=true)
strategy.entry('Long', strategy.long, alert_message = "GoLong")
if shortCondition and strategy.opentrades==0
strategy.close_all(comment="close", immediately=true)
strategy.entry('Short', strategy.short, alert_message = "GoShort")

How can a label be deleted if the condition becomes false

If my condition is true and plot a label to the chart but it should become false if the next bars high is higher then the high of the bar where the condition is true.
the condtion should plot a label if bear is true, but when the next bar is higher it should delete the label. Also I want that the label, if its true it will be true for 5 candles and in this period it shouldn't plot another label. the (for i = 1 to 4) is working but it calculates from the wrong bar because the script didn't delete the false bar label that became untrue.
bearlabel = if bear == true
myLabel1 = label.new(x=bar_index, y=high, color=color.rgb(255, 82, 82, 49), text= "S", textcolor=color.white, style=label.style_label_down, size=size.tiny)
if bear[1] == true and high[1] < high
label.delete(bearlabel)
for i = 1 to 4
if bear[i] == true
label.delete(bearlabel)
If I use bear1 I think it takes the wrong label. So how can I get the high of the conditions Bar to check if the new high is higher?
if bear[1] == true and high[1] < high
label.delete(bearlabel)
To delete a label, you must use the name of this label.
here in your code the label name is myLabel1 :
myLabel1 = label.new(x=bar_index, y=high, color=color.rgb(255, 82, 82, 49), text= "S", textcolor=color.white, style=label.style_label_down, size=size.tiny)
So you must use myLabel1 to delete it :
label.delete(myLabel1)

Pinescript variable doesn't change its value

So I have a problem, thought a lot about it but not able to fix it so I would appreciate some help from you.
To make it simple, I'll give another piece of code.
currentRSI = ta.rsi(close,14)
var tradeExists = 0
if (currentRSI > 50 and tradeExists == 0)
tradeExists := 1
alert("Long trade")
In my case, if currentRSI crosses over 50, so it gets to 51, AND in the same candle of the timeframe it gets to 49.5, the tradeExists value will remain 0 but the alert has been sent
how could I fix to detect that and close the trade, any idea if I can do this?
I want to specify that I also tried using varip tradeExists = 0 but the variable still gets rollback at the close of the candle.
I just had that problem and I fixed it: use
the barstate.isconfirmed in your conditions, it's the only way to change a var in pinescript.
if (barstate.isconfirmed and first_time_signal == false)
if nT3Average >= ok_to_buy and nT3Average_2 <= oversold
last_signal := 1
label.new(bar_index,close,'Long',color=color.rgb(49, 161, 64),style = label.style_label_up,xloc=xloc.bar_index,yloc=yloc.belowbar)
first_time_signal := true
if nT3Average <= ok_to_sell and nT3Average_2 >= overbought
last_signal := -1
label.new(bar_index,close,'Short',color=color.rgb(175, 55, 95),style = label.style_label_down,xloc=xloc.bar_index,yloc=yloc.abovebar,textcolor=color.rgb(10,10,10))
first_time_signal := true

PineScript - zigzag trend change counter

I've made an indicator that is similiar to zigzag. I want to write a formula that will count number of up trends or number of trend changes (from up to down and from down to up). I have problem with it, because my variable is still setting to 0. Could you help me to correct it?
//#version=3
study("ZigZag Poker", overlay=true)
//INPUTS
trend = 0
trend := na(trend[1]) ? 1 : trend[1] //Beggining trend set to up
LL = 0.0
LL := na(LL[1]) ? low : LL[1] //LastLow
HH = 0.0
HH := na(HH[1]) ? high : HH[1] //LastHigh
LO = 0.0
LO := na(LO[1]) ? open : LO[1] //LastOpen
LC = 0.0
LC := na(LC[1]) ? close : LC[1] //LastClose
LOLO = 0.0
LOLO := na(LOLO[1]) ? low : LOLO[1] //LowestLow
HIHI = 0.0
HIHI := na(HIHI[1]) ? high : HIHI[1] //HighestHigh
zigzag = na
kolor = 0 //variable that counts number of trend changes
imp = input(true, "Alt imp")
kolor := imp == true ? 2 : 0
if (trend > 0) // trend is up, look for new swing low
if close >= min(LO, LC)
LC := close
LL := low
LO := open
LOLO := low
HIHI := high
else
zigzag := HIHI
trend := -1
HH := high
HIHI := high > HIHI ? high : HIHI
LC := close
LL := low
LO := open
LOLO := low
kolor := kolor[1] + 1
else // trend is down, look for new swing high
if close <= max(LO, LC)
HH := high
HIHI := high
LC := close
LL := low
LO := open
LOLO := low < LOLO ? low : LOLO
else
zigzag := LOLO
trend := 1
HH := high
LC := close
LL := low
LO := open
kolor: = kolor[1] + 1
plot(kolor)
plot(zigzag, color = trend < 0 ? blue : orange, linewidth=2, offset=-1)
I know it's too late to help the OP, but the error is in the line
kolor := imp == true ? 2 : 0, that always sets the value of kolor to 2 or 0, for all the candles that are in the current trend.
What is missing is copying the last kolor's value on every loop, so kolor[1] can have a valid counter.
Replacing that line with kolor := na(kolor[1])? 0: kolor[1] will do it.

In Pine Script, how can you do something once per day, or keep track if something has been done yet that day?

I'm working on a TradingView script (Pine) and i'm trying to use my Daily-bar strategy on the 5-minute chart. To do this, I need to basically only check conditions once per day.
I can do this by having a boolean variable such as dailyCheck = false and set it to true when I run that code and then reset it on a new day.
Does anyone know how to go about doing this? From what I read in the pine manual it says you can get unix time...but I don't know how to work with this and I can't print anything except numbers in the form of plot, so I can't figure out how to tell when a new day has started. Thanks in advance!
Version 1
There are lots of ways to detect a change of day. The Session and time information User Manual page shows a few.
I like detecting a change in the dayofweek or dayofmonth built-in variables:
//#version=4
study("Once per Day")
var dailyTaskDone = false
newDay = change(dayofweek)
doOncePerDay = rising(close, 2) // Your condition here.
dailyTaskDone := doOncePerDay or (dailyTaskDone and not newDay)
plotchar(newDay, "newDay", "▼", location.top, transp = 60)
plotchar(doOncePerDay, "doOncePerDay", "•", location.top, transp = 0)
bgcolor(dailyTaskDone ? color.orange : na)
Version 2
Following Michel's comment, this uses a more robust detection of the day change:
//#version=4
study("Once per Day")
var dailyTaskDone = false
newDay = change(time("D"))
doOncePerDay = rising(close, 2) // Your condition here.
dailyTaskDone := doOncePerDay or (dailyTaskDone and not newDay)
plotchar(newDay, "newDay", "▼", location.top, transp = 60)
plotchar(doOncePerDay, "doOncePerDay", "•", location.top, transp = 0)
bgcolor(dailyTaskDone ? color.orange : na)
And for the OP, a v3 version:
//#version=3
study("Once per Day v3")
dailyTaskDone = false
newDay = change(time("D"))
doOncePerDay = rising(close, 2) // Your condition here.
dailyTaskDone := doOncePerDay or (dailyTaskDone[1] and not newDay)
plotchar(newDay, "newDay", "▼", location.top, transp = 60)
plotchar(doOncePerDay, "doOncePerDay", "•", location.top, transp = 0)
bgcolor(dailyTaskDone ? orange : na)