How can a label be deleted if the condition becomes false - pine-script-v5

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)

Related

Add extended trading hours (premarket and afterhours) to current pine script code

I have an indicator that plots percentage levels above current high/low/open/close (user selected) for the intraday levels. I would like to incorporate extended trading hours into the code. For example if the premarket high of day is higher than regular hours high of day, I'd like the indicator to calculate the percentage levels using the premarket high instead of the intraday. I'm not sure how to code this into the script but I assume it would be fairly simple (I'm just not a coder). Script below:
study(title="% Levels", overlay=true)
//Select Source to Plot Levels
calc = input(title="Calculation Source", defval="Open", options=["Open","High", "Low", "Close"])
showlast = input(title="Historical Bars to Display", defval=3, options= [1,2,3,4,5,10,15,20,30,50,100,200,300], group="Custom Line Plot Extension Settings || Base Settings for Stocks/ETF's are '1' & '0' Respectively || To Extend Lines: Ideally both values should be equal when adjusting || For Futures: 1 & 0 Recommended")
extendLines = input(title="Offset Starting Plot", defval=0, options=[0,1,3,5,10,15,20,30,50,100,200,300])
//Ticker Variables
o = security(syminfo.tickerid, "D", open)
h = security(syminfo.tickerid, "D", high)
l = security(syminfo.tickerid, "D", low)
c = security(syminfo.tickerid, "D", close)
calcm = if calc == "High"
h
else if calc == "Low"
l
else if calc == "Close"
c
else if calc == "Open"
o
//Calculations for % Levels
pct10= calcm*1.10
pctm10=calcm*0.90
pct12_5 = calcm*1.125
pctm12_5 = calcm*0.875
pct15= calcm*1.15
pctm15=calcm*0.85
//% Levels plotted based on Daily Open, High, Low, or Close
plot(pct10, title="10%", color=color.white, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pct12_5, title="12.5%", color=color.white, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pct15, title="15%", color=color.white, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pctm10, title="-10%", color=color.red, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pctm12_5, title="-12.5%", color=color.red, style=plot.style_line, show_last=showlast, offset=extendLines)
plot(pctm15, title="-15%", color=color.red, style=plot.style_line, show_last=showlast, offset=extendLines)
Not a coder so not sure what to try.
There are three built-in variables which you can use:
session.ismarket: Returns true if the current bar is a part of the regular trading hours (i.e. market hours), false otherwise
session.ispostmarket: Returns true if the current bar is a part of the post-market, false otherwise. On non-intraday charts always returns false.
session.ispremarket: Returns true if the current bar is a part of the pre-market, false otherwise. On non-intraday charts always returns false.

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

Script for indicating order history - Tradeview Pine

I want to indicate historical trades in tradingview charts via a script based on information on time and price for entry and close.
My best idea is to search through "time" to find matches for entry and close, and then change the background color according to short or long position or draw a horizontal line. However, this seems not optimal. Any suggestions?
I'd implement that in the next way:
//#version=3
strategy("Background changing", overlay=true)
NONE = 0
LONG = 1000
SHORT = -1000
position = NONE
position := nz(position[1], NONE)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("LongEntryId", strategy.long)
position := LONG
if (close < high[1])
strategy.close("LongEntryId")
position := NONE
getColor(state) =>
state == LONG ? green :
state == SHORT ? red :
white
bgcolor(color=getColor(position))
Or you can put arrows to the chart:
//#version=3
study("My Script", overlay=true)
order = 0
if time >= timestamp(2018, 1, 10, 0, 0)
order := 1
if time == timestamp(2018, 1, 17, 0, 0)
order := -1
plotarrow(order)

SSRS - Conditional formatting not getting applied?

I have the following expression to apply background colour to a text box but only the red colour is getting applied when that condition is true. All the other conditions are showing up as white? For example when the first condition is true when the report renders, the background colour is white instead of green?
=IIF(Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 86, "Green",
IIF(Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 79 AND
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) <= 85, "Light Green",
IIF(Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 64 AND
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) <= 78, "Yellow", "Red" )))
It would seem that your SUM(CINT(Fields!TotalAchieved.Value) * 7.14) calculation is not giving you the results you expect . The first thing I would do is add a column that shows this value to make sure that it's gives you what you expect.
Once you have that correct then I would also suggest that you use a SWITCH statement rather than nested IIFs, they are much easier to read/debug.
You expression would be
=SWITCH(
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 86, "Green",
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 79, "LightGreen",
Round(SUM(CInt(Fields!TotalAchieved.Value) * 7.14)) >= 64, "Yellow",
TRUE, "Red"
)
This way you don't need to check for ranges as, for example, if the value was 75, the 1st expression fails but the second one is true so SWITCH will stop at that point and not evaluate the rest, if all fail then the final TRUE will act like an else.

Range inside switch case statement in Coffeescript

I am using Handlebar in my Rails 3.2 jquery mobile application.
I am trying to write a switch case statement inside a Coffeescript method like
Handlebars.registerHelper 'status', (blog) ->
switch(parseInt(blog.status))
when [0..20]
status = "active"
when [20..40]
status = "Moderately Active"
when [40..60]
status = "Very Active"
when [60..100]
status = "Hyper Active"
return status
I am not getting any result . How to use range in when . Please suggest
Your switch won't work as Cygal notes in the comments (i.e. see issue 1383). A switch is just a glorified if(a == b) construct and you need to be able to say things like:
a = [1,2,3]
switch a
...
and have it work when you switch on an array. The CoffeeScript designers thought adding a (fragile) special case to handle arrays (which is all [a..b] is) specially wasn't worth it.
You can do it with an if:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
if 0 <= status <= 20
'Active'
else if 20 < status <= 40
'Moderately Active'
else if 40 < status <= 60
'Very Active'
else if 60 < status <= 100
'Hyper Active'
else
# You need to figure out what to say here
Or with short circuiting returns like this:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
return 'Something...' if status <= 0
return 'Active' if status <= 20
return 'Moderately Active' if status <= 40
return 'Very Active' if status <= 60
return 'Hyper Active' if status <= 100
return 'Something else' # This return isn't necessary but I like the symmetry
Note that you have three special cases that you need to add strings for:
status < 0.
status > 100.
status is NaN. This case would usually fall under the final "it isn't less than or equal to 100" branch since NaN => n and NaN <= n are both false for all n.
Yes, you're absolutely certain that the status will always fall within the assumed range. On the other hand, the impossible happens all the time software (hence the comp.risks mailing list) and there's no good reason to leave holes that are so easily filled.
Also note the addition of the radix argument to the parseInt call, you wouldn't want a leading zero to make a mess of things. Yes, the radix argument is optional but it really shouldn't be and your fingers should automatically add the , 10 to every parseInt call you make.
Adding a tiny bit to mu is too short's answer, you can transform its second code snippet into a switch expression:
Handlebars.registerHelper 'status', (blog) ->
status = parseInt(blog.status, 10)
switch
when status <= 0 then 'Something...'
when status <= 20 then 'Active'
when status <= 40 then 'Moderately Active'
when status <= 60 then 'Very Active'
when status <= 100 then 'Hyper Active'
else 'Something else'
This is basically equivalent to doing a switch (true) in JavaScript (though the CS compiler will generate a switch (false) statement with the negated conditions to ensure boolean results from the expressions... i think).
And the reason why the switch over ranges doesn't work is that ranges literals in CS represent plain old JS arrays (though the compiler will do some optimization tricks when doing something like for i in [1..something]), so when they are found inside a switch they are treated just like normal array values:
// Generated JS for question's CS code:
switch (parseInt(blog.status)) {
case [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]:
status = "active";
break;
case [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]:
status = "Moderately Active";
break;
case [40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]:
status = "Very Active";
break;
case (function() {
_results = [];
for (_i = 60; _i <= 100; _i++){ _results.push(_i); }
return _results;
}).apply(this):
status = "Hyper Active";
}
The value inside the switch statement is basically compared to each case value using ===, which only works for primitives, not for arrays (and even if it worked for arrays, it would be testing array equality, not if the switched value is contained in the caseed arrays).