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

//#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)

Related

Pine Script V5 - Not getting expected breakout entry price and entry in expected bar

I have a long entry buy condition where
Candle time frame is 15 minutes
Alert candle high is below lower band of bollinger bands.
entry when next candle crossover the alert candle high
This script gives entry oh high breakout + 0.01 but it does not give entry in the exact breakout candle and gives entry when price comes next time on expected entry price i.e. alert candle high + 0.01.
Requesting solution to get entry in breakout candle itself and at expected breakout price.
//#version=5
strategy("Bands Reversion", overlay=true, calc_on_every_tick=true)
//// Indicator Bollinger Bands
source = close
length = input.int(20, minval=1)
mult = input.float(1.5, minval=0.001, maxval=50)
direction = input.int(0, title = "Strategy Direction", minval=-1, maxval=1)
strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev
plot(basis, color = color.red)
plot(upper)
plot(lower)
/// Trade entry time and squareoff time
TradeTime = input(title="Trade Timings",defval="0930-1130")
SqoffTime = input(title="Squareoff Timings",defval="1530-1545")
Barsinsession(TradeTime) => time(timeframe.period,TradeTime) != 0
Insession = Barsinsession(TradeTime) ? 1 : 0
endofsession = Insession == 0 and Insession[1] == 1
Sqsession = Barsinsession(SqoffTime) ? 1 : 0
SqTime = Sqsession == 1 and Sqsession[1] == 0
//// Input control and conditions
buy_condition = high[1] < lower[1] and ta.crossover(high, high[1]) and Insession
short_condition = low[1] > upper[1] and ta.crossunder(low, low[1]) and Insession
buy_alert_high = ta.valuewhen(buy_condition, high[1],0)
buy_alert_low = ta.valuewhen(buy_condition, low[1],0)
short_alert_low = ta.valuewhen(short_condition, low[1],0)
short_alert_high = ta.valuewhen(short_condition, high[1],0)
buy_alert_high1 = ta.valuewhen(buy_condition, high,0)
plot(buy_alert_high, style = plot.style_circles)
plot(buy_alert_low, style = plot.style_circles, offset = -2)
plot(short_alert_low, style = plot.style_circles)
plot(short_alert_high, style = plot.style_circles)
sell = ta.crossunder(close, low) or SqTime //// or SqTime if for intraday exit
cover = ta.crossover(close,high) or SqTime //// or SqTime if for intraday exit
plotshape(buy_condition, style = shape.triangleup, location = location.belowbar, color = color.green, text = "BUY")
plotshape(short_condition, style = shape.triangledown, location = location.abovebar, color = color.red, text = "SHORT")
long_price = ta.valuewhen(buy_condition, (buy_alert_high + 0.01),0 )
longstop = buy_alert_low - 0.01
longtgt = basis
short_price = ta.valuewhen(short_condition,short_alert_low - 0.01,0) ////short_alert_low - (0.01 * 100 * syminfo.mintick) //// ta.valuewhen(short_condition,short_alert_low - 0.01,0)
shortstop = short_alert_high + 0.01
shorttgt = basis
strategy.entry("long",direction = strategy.long, when = buy_condition, limit = long_price, comment ="BUY")
strategy.close("long", when = sell, comment = "SELL")
strategy.exit("long", from_entry = "long", stop = longstop, limit = longtgt, comment = "TG/SL_EXIT")
strategy.entry("short",direction = strategy.short, when = short_condition,limit = short_price, comment ="SHORT")
strategy.close("short", when = cover, comment = "COVER")
strategy.exit("short", from_entry = "short", stop = shortstop, limit = shorttgt, comment = "TG/SL_EXIT")
plot(strategy.position_size > 0 ? longstop : na, style = plot.style_linebr, color = color.red)
plot(strategy.position_size > 0 ? longtgt : na, style = plot.style_linebr, color = color.green)
plot(strategy.position_size < 0 ? shortstop : na, style = plot.style_linebr, color = color.red)
plot(strategy.position_size < 0 ? shorttgt : na, style = plot.style_linebr, color = color.green)

Code not plotting - Function should be called on each calculation for consistency. It is recommended to extract the call from this scope"

I am getting this as an orange error - not sure what I have done wrong or whether this error is the reason that the code doesn't plot. I am brand new to Pine-script and any assistance would be invaluable. Many thanks in advance Alastair
//#version=5
indicator("PCY TEST4")
//Get User Inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
//Variables
a = 0.33
PCY = 0.0
PCYi = 0.0
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
minMacd=0.0
for i = 0 to (signal_length[1])
minMacd := ta.min(macd)
maxMacd=0.0
for i = 0 to (signal_length[1])
maxMacd := ta.max(macd)
//maxMacd = ta.min(macd)
//minMacd = ta.max(macd)
//signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
//hist = macd - signal
delta = maxMacd-(fast_ma - slow_ma)/(maxMacd -minMacd)
for i = 0 to (signal_length)
PCYi := a*((delta - PCYi[1])+PCYi[1])
PCYi
//Plotting
plot(PCYi)
I think it's because you're calculating the min/max in a for loop
Those functions should (not mandatory but strongly recommended) be calculated at every candle instead
for i = 0 to (signal_length[1])
minMacd := ta.min(macd)
maxMacd=0.0
for i = 0 to (signal_length[1])
maxMacd := ta.max(macd)
If you want to calculate the min/max over a X period of time, maybe use the ta.highest/ta.lowest functions instead :)
And of course, those 2 should be called at every candle (i.e. not in a if, for,... statement)

Lower time frame in pine script indicator

I want to fix calculation time period of my indicator as 1 minute .
That means
if I am checking on 5 minute chart . I want to see the current 5 minute indicator value as some of 5 one minute indicator values included in the current 5 minute candle.
If I am checking on 15 minute chart. I want to see the current 15 minute indicator value as some of 15 one minute indicator values included in the current 15 minute candle.
Same for all.
And I coded it as bellow . But as chart time frame changes calculation also changes.
This is how I Coded it
indicator(title = "OBV MULTI" , overlay = false)
var a = 0.0
var b = 0.0
var c = 0.0
var d = 0.0
a := ta.change(time("D")) ? 0.0 : a
b := ta.change(time("W")) ? 0.0 : b
c := ta.change(time("M")) ? 0.0 : c
d := ta.change(time("12M")) ? 0.0 :d
src1 = request.security_lower_tf(syminfo.tickerid" ,"1",close)
src2 = request.security_lower_tf(syminfo.tickerid","1",open)
src3 = request.security_lower_tf(syminfo.tickerid, "1",volume)
a := (close>open) ? a+volume : (close<open) ? a-volume : ((close==open)and(a[1]>a[2])) ? a +volume : ((close==open))and(a[1]<a[2])) ? a-volume : 0
b := (close>close) ? b+volume : (close<open) ? b-volume : ((close==open)and(b[1]>b[2])) ? b +volume : ((close==open))and(b[1]<b[2])) ? b-volume : 0
c := (close>open) ? c+volume : (close<open) ? c-volume : ((close==open)and(c[1]>c[2])) ? c +volume : ((close==open))and(c[1]<c[2])) ? c-volume : 0
d := (close>open) ? d+volume : (close<open) ? d-volume : ((close==open)and(d[1]>d[2])) ? d +volume : ((close==open))and(d[1]<d[2])) ? d-volume : 0
Plot(a , title = "D Line" , color = color.blue)
Plot(b , title = "W Line" , color = color.green)
Plot(c , title = "M Line" , color = color.yellow)
Plot(d , title = "Y Line" , color = color.orange)
Plot(0 , title = "Zero Line", color = color.red)

Need to remove the previous day plots in tradingview - pinescript

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)

ag-grid filter not working with formatted number values?

I'm using ag grid with angularjs and the filter does not work with formatted numbers. I use formatted numbers with currency values.
Below is the columndef code:
{ headerName:"GBO", field: "GBO", width: 200, editable:true, cellClass: "number-cell",filter:'agNumberColumnFilter',
cellRenderer : function(params){
if(params.value == "" || params.value == null)
return '-';
else return params.value;
}
}
Before assigning the data to the grid, I format the numbers using :
$scope.formatNumberOnly = function(num,c, d, t){
//console.log(num );
var n = getNumber(num);
//var n = this,
c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
});
The problem here is that the filter doesn't work with these formatted numbers and only seems to be working for values upto 999.
Can anyone please help me with a solution to this filtering problem?
If you want the filter to work on these formatted values, you should use a valueGetter instead of a valueFormatter
You should implement the above formatter function as a valueGetter in column Definition.
Also a number filter won't work as in order for your formatted number to be interpreted, it should be a text filter.
Here is an example from official docs.