Need to remove the previous day plots in tradingview - pinescript - charts

This is my code. Can you please update your answer in this bcz i tried and the indicator is not compiled properly.
//#version=3
study(title="TEST", overlay=true)
fib1 = security(tickerid,"D",high[1], lookahead=barmerge.lookahead_on)
fib0 = security(tickerid,"D",low[1], lookahead=barmerge.lookahead_on)
plotS1 = input(title="Plot S1", type=bool, defval=true)
plotR1 = input(title="Plot R1", type=bool, defval=true)
R1 = (fib0-fib1)*0.215+fib1
S1 = (fib0-fib1)*0.79+fib1
plot(series=plotR1 ? R1 : na, title="R1", style=cross, linewidth=1, color=#EEC900) plot(series=plotS1 ? S1 : na, title="S1", style=cross, linewidth=1, color=#EEC900)

//#version=4
study("Fib", "FiB", true)
[fib1,fib0] = security(syminfo.tickerid, "D", [high[2], low[2]], lookahead=barmerge.lookahead_on)
is_today = year == year(timenow) and month == month(timenow) and dayofmonth == dayofmonth(timenow)
plot(fib0, "fib0", is_today ? color.green : na)
plot(fib1, "fib1", is_today ? color.blue : na)

Related

Compare bool and float to determine plot (pine script v5)

You folks helped me with another part of this script and I'm making good progress. However, I'm stuck again.
I added a code to produce a dynamic overbought/oversold line on the indicator. That part works great. Now, I'm wanting to add a plotshape when conditions are met to signal long or short. Unfortunately, I can't figure out how to compare a bool variable and a float variable. The dynamic OB/OS lines are floats. The crossover lines are bool.
What I want is for the (green/long) shape to plot when a crossover happens below the dynamic oversold line and the (red/short) shape to plot when the crossunder happens above the dynamic overbought line.
I've tried all kinds of things (var, loop with "while" using sb value as a trigger). I either break the code or I get every crossover plotted. Here is what I have that is stable. I took all my junk that didn't work out:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HammerGeek
//#version=5
indicator('Impulse MACD [HammerGeek - LazyBear]', shorttitle='IMACD_HG_LB', overlay=false)
lengthMA = input(34)
lengthSignal = input(9)
//OverBought = input.float(0.1)
//OverSold = input.float(-0.1)
calc_smma(src, len) =>
smma = 0.0
sma_1 = ta.sma(src, len)
smma := na(smma[1]) ? sma_1 : (smma[1] * (len - 1) + src) / len
smma
calc_zlema(src, length) =>
ema1 = ta.ema(src, length)
ema2 = ta.ema(ema1, length)
d = ema1 - ema2
ema1 + d
src = hlc3
hi = calc_smma(high, lengthMA)
lo = calc_smma(low, lengthMA)
mi = calc_zlema(src, lengthMA)
md = mi > hi ? mi - hi : mi < lo ? mi - lo : 0
sb = ta.sma(md, lengthSignal)
sh = md - sb
mda = ta.sma(md, lengthSignal)
OverBoughtLine = ta.highest(sb * 0.5, lengthMA*100)
OverSoldLine = ta.lowest(sb * 0.5, lengthSignal*100)
shsbCrossOver = ta.crossover(md, sb)
shsbCrossUnder = ta.crossunder(md, sb)
//mdc = src > mi ? src > hi ? color.lime : color.green : src < lo ? color.red : color.orange
mdc = color.green
plot(0, color=color.new(color.gray, 0), linewidth=1, title='MidLine')
plot(md, color=mdc, linewidth=2, title='ImpulseMACD', style=plot.style_line)
plot(sh, color=color.rgb(122, 5, 168, 40), linewidth=2, title='ImpulseHisto', style=plot.style_area)
plot(sb, color=color.rgb(255, 255, 255, transp = 40), linewidth=2, title='ImpulseMACDCDSignal')
plot(OverBoughtLine, color=color.new(#f3e032, 0), linewidth=1, title = 'Overbought Line')
plot(OverSoldLine, color=color.new(color.yellow, 0), linewidth=1, title = 'Oversold Line')
plotshape(shsbCrossOver, "LONG", shape.triangleup, location.bottom, color.new(color.green, 0))
plotshape(shsbCrossUnder, "SHORT", shape.triangledown, location.top, color.new(#cd0808, 0))
//ebc = input(false, title='Enable bar colors')
//barcolor(ebc ? mdc : na)
You cannot compare a bool with a float. It doesn't make any sense and it is not legal in pinescript.
What I want is for the (green/long) shape to plot when a crossover
happens below the dynamic oversold line and the (red/short) shape to
plot when the crossunder happens above the dynamic overbought line.
You want to check if the values you use in that crossover is under your dynamic oversold line at the time of crossover.
OverSoldLine = ta.lowest(sb * 0.5, lengthSignal*100)
shsbCrossOver = ta.crossover(md, sb)
is_good = shsbCrossOver and (md < OverSoldLine) and (sb < OverSoldLine)

Historical Camarilla Levels

I've got a simple historical camarilla indicator that I'm mostly happy with, save for one detail. My horizontal lines are plotted from 9:30 until 9:30 the next day, except on the Friday to Monday transition. I don't really understand why this happens. So what I'd like is for the lines to start from 7:00 to 18:00 every day, in order to align with price action in the pre/post market. I'm not very good at pinescript, and I can't seem to find a solution. Is this even possible using 'plot', or should I be using 'line.new'? Any help would be greatly appreciated. The script I have is as follows;
//#version=5
indicator('Camarilla Levels', overlay=true)
// User inputs
showHistoricalCams = input(title='Show Historical Cam Pivots', defval=true)
//Camarilla Calculations
tR3 = close + (high - low) * 1.1 / 4.0
tS3 = close - (high - low) * 1.1 / 4.0
tR4 = close + (high - low) * 1.1 / 2.0
tS4 = close - (high - low) * 1.1 / 2.0
//Pivot Range
_tR4 = request.security(syminfo.tickerid, 'D', tR4)
_tR3 = request.security(syminfo.tickerid, 'D', tR3)
_tS3 = request.security(syminfo.tickerid, 'D', tS3)
_tS4 = request.security(syminfo.tickerid, 'D', tS4)
//Plot Historical Cam
plot(showHistoricalCam ? _tR3 : na, title=' R3', color=_tR3 != _tR3[1] ?
#eeeeee00 : #ff000080, style=plot.style_line, linewidth=1)
plot(showHistoricalCam ? _tR4 : na, title=' R4', color=_tR4 != _tR4[1] ?
#eeeeee00 : #ff000080, style=plot.style_line, linewidth=1)
plot(showHistoricalCam ? _tS3 : na, title=' S3', color=_tS3 != _tS3[1] ?
#eeeeee00 : #008000FF, style=plot.style_line, linewidth=1)
plot(showHistoricalCam ? _tS4 : na, title=' S4', color=_tS4 != _tS4[1] ?
#eeeeee00 : #008000FF, style=plot.style_line, linewidth=1)
**The lines are plotted this way to remove the 'connecting' lines
I've tried setting time parameters, but can't make the script work.

How do I plot a buy alert on an array anytime a bar reaches 100 upon close of that bar? I am using the "Supertrend Oscillator" in pinescript

I've been at this for hours and I just can't figure it out. I've tried searching all over and watching videos. Here's code to the script I am working with. it's called Supertrend Oscillator by LuxAlgo on tradingview.
atr = ta.atr(osclength) * mult
up = hl2 + atr
dn = hl2 - atr
upper := src[1] < upper[1] ? math.min(up, upper[1]) : up
lower := src[1] > lower[1] ? math.max(dn, lower[1]) : dn
trend := src > upper[1] ? 1 : src < lower[1] ? 0 : trend[1]
Spt = trend * lower + (1 - trend) * upper
//----
ama = 0.
osc = math.max(math.min((src - Spt) / (upper - lower), 1), -1)
alpha = math.pow(osc, 2) / osclength
ama := nz(ama[1] + alpha * (osc - ama[1]), osc)
hist = ta.ema(osc - ama, smooth)
//----
fix_css = osc > 0 ? #0cb51a : #ff1100
var_css = osc > 0 ? array.get(up_col, math.round(osc * 99)) : array.get(dn_col, math.round(osc * -1 * 99))
sig_css = ama > 0 ? #2157f3 : #673ab7
plot(fixed ? osc * 100 : na, 'Main Fixed', fix_css, 1, plot.style_area)
plot(fixed ? na : osc * 100, 'Main Transp', var_css, 1, plot.style_columns, editable=false)
plot(hist * 100, 'Histogram', color.new(#808080, 40), 1, plot.style_area)
plot(ama * 100, 'Signal', sig_css)
//----
a = hline(80)
b = hline(-80)
fill(a, b, color.new(#2157f3, 90))
//----
css = osc > 0 ? #ff1100 : #0cb51a
sig = ta.change(math.sign(osc)) ? osc * -100 : na
plot(show_ln ? sig : na, color=css)
//----
n = bar_index
cross = ta.cross(src, Spt)
x2 = ta.valuewhen(cross, n, 0)
//----
false_buy = ta.valuewhen(ta.crossunder(src, Spt), src, 0) < ta.valuewhen(ta.crossover(src, Spt), src, 0)
false_sell = ta.valuewhen(ta.crossover(src, Spt), src, 0) > ta.valuewhen(ta.crossunder(src, Spt), src, 0)
num = ta.cum(cross and (false_buy or false_sell) ? 1 : 0)
den = ta.cum(cross ? 1 : 0)
per = num / den * 100
//----
if barstate.islast and show_ln
line.delete(line.new(x2, math.sign(osc) * -100, n, osc * 100, color=osc < 0 ? #ff1100 : #0cb51a)[1])
if show_lb
if ta.crossover(src, Spt)
txt = false_sell ? '❌' : '✔️'
label.new(x2,-80,txt,color=#00000000,style=label.style_label_up,textcolor=color.gray,textalign=text.align_center)
if ta.crossunder(src, Spt)
txt = false_buy ? '❌' : '✔️'
label.new(x2,80,txt,color=#00000000,style=label.style_label_down,textcolor=color.gray,textalign=text.align_center)
if barstate.islast and show_per
txt = '❌' + str.tostring(per, '#.##') + '%'
label.delete(label.new(n, osc, txt, color=#00000000, style=label.style_label_left, textcolor=color.gray, textalign=text.align_left)[1])
How do I make it so a "buy" alert displays on the chart anytime a bar closes with an oscillator value reaching 100 or more?

How to convert this self-referenced variable in pine v2 to pine v3?

//#version=2
study("Support and Resistance", shorttitle="SR", overlay=true)
//////////////////////////1H
tf1 = input(title="Resolution 7", type=resolution, defval = "60")
vamp = input(title="VolumeMA", type=integer, defval=6)
vam = sma(volume, vamp)
//////////////////////////
up = high[3]>high[4] and high[4]>high[5] and high[2]<high[3] and high[1]<high[2] and volume[3]>vam[3]
down = low[3]<low[4] and low[4]<low[5] and low[2]>low[3] and low[1]>low[2] and volume[3]>vam[3]
fractalup = up ? high[3] : fractalup[1]
fractaldown = down ? low[3] : fractaldown[1]
/////////////////////////////////////1
fuptf1 = security(tickerid,tf1 == "current" ? period : tf1, fractalup)
fdowntf1 = security(tickerid,tf1 == "current" ? period : tf1, fractaldown)
/////////////////////////////////////1
plot(fuptf1, "FractalUp 1", color=red, linewidth=1, style=circles, transp=0, offset =-3, join=false)
plot(fdowntf1, "FractalDown 1", color=#d6fbbb, linewidth=1, style=circles, transp=0, offset=-3, join=false)
It's a quiet simple script but I am not able to figure out how to convert it to version 3. I have read the migration guide and still don't understand how to deal with the fractalup variable.
When I tried changing it the way it was suggested in the guide I will get the error message:
"line 25: Variable fractalup was declared with series[integer]
type. Cannot assign it expression of type series"
To convert this script to v3, you need to not just declare the variables as na before assigning them values, but also to wrap them in a function (otherwise the compiler will throw an error related to using a mutable variable with the security() function.
The working v3 code equivalent to your current v2 code looks like this:
//#version=3
study("Support and Resistance", shorttitle="SR", overlay=true)
//////////////////////////1H
tf1 = input(title="Resolution 7", type=resolution, defval = "60")
vamp = input(title="VolumeMA", type=integer, defval=6)
vam = sma(volume, vamp)
//////////////////////////
up = high[3]>high[4] and high[4]>high[5] and high[2]<high[3] and high[1]<high[2] and volume[3]>vam[3]
down = low[3]<low[4] and low[4]<low[5] and low[2]>low[3] and low[1]>low[2] and volume[3]>vam[3]
fractalup() =>
fractalup = na
fractalup := up ? high[3] : fractalup[1]
fractaldown() =>
fractaldown = na
fractaldown := down ? low[3] : fractaldown[1]
/////////////////////////////////////1
fuptf1 = security(tickerid,tf1 == "current" ? period : tf1, fractalup(), lookahead=barmerge.lookahead_on)
fdowntf1 = security(tickerid,tf1 == "current" ? period : tf1, fractaldown(), lookahead=barmerge.lookahead_on)
/////////////////////////////////////1
plot(fuptf1, "FractalUp 1", color=red, linewidth=1, style=circles, transp=0, offset =-3, join=false)
plot(fdowntf1, "FractalDown 1", color=#d6fbbb, linewidth=1, style=circles, transp=0, offset=-3, join=false)

Pine Script - Tradingview Draw a daily rectangle

I'm working on a TradingView script (Pine) and I would to develop a simply script that draw a rectangle from a start of current day to the end based on my current timeframe from Monday To Friday...
Example: First rectangle drawed from 24/03/2021 to 25/03/2021, Second Rectangle drawed from 25/03/2021 to 26/03/2021, etc...
Any solutions to achieve this result?
Thank's in advance
I think I have understood your request. The below should help assist you
//#version=4
study("Daily Box", overlay=true)
Bottom = input(title="Bottom", type=input.session, defval="0000-2359:1234567")
colourcheck = 1.0
boxheight = input(title="Box Height", type=input.float, defval=3)
DailyHigh = security(syminfo.tickerid, "D", high, lookahead=true)
DailyLow = security(syminfo.tickerid, "D", low, lookahead=true)
dayrange = DailyHigh - DailyLow
BottomLowBox = DailyLow + (dayrange * 0.01 * boxheight)
TopLowBox = DailyHigh - (dayrange * 0.01 * boxheight)
BarInSession(sess) => time(timeframe.period, sess) != 0
//ASIA
BottomL = plot(DailyLow and BarInSession(Bottom) ? DailyLow : na, title="Bottom High", style=plot.style_linebr, linewidth=3, color=na)
TopL = plot(DailyHigh and BarInSession(Bottom) ? DailyHigh : na, title="Bottom Low", style=plot.style_linebr, linewidth=3, color=na)
fill(BottomL, TopL, color=color.purple, title="Fill Box", transp=50)