From Plotchar to line.new ? How to draw line HL2? - pine-script-v5

I would like to draw a line of hl2 when the candles make more than 40pips in 5min. For this I succeeded with plotchar but I would like to do it with one line like line.new function.
Here the script i use
//#version=5
indicator('Dax Patronnes ATR ', shorttitle='Patronne ATR', overlay=true, precision=6)
////////////////////// MIDDLE BOUGIE
// Input variable for maximum candlestick body size in relation to high low candlestick range.
MaxBodySize = input.float(defval=1000, title='Minimal Body Size (%)')
// If you want to draw a shape in the middle of candlestick body then this variable is needed.
// Use with plotshape and location = location.absolute
MidCandleBody = (high + low) / 2
// Create candlestick low high range comparison variable
LowHighRange = (high - low) / 100 * MaxBodySize
// Avoid negative total Candlestick body size
CandleBodySize = close >= open ? close - open : open - close
// Evaluation to determine drawing points
DrawPoint = CandleBodySize <= LowHighRange ? MidCandleBody : na
// Plot drawing points
plotchar(DrawPoint, char='.', color=color.new(color.white, 0), location=location.absolute, size=size.tiny)
///////////////////////////////////////////////// BOUGIE PATRONNE 5min ///////////////////////////////
open_posp = open * 1
close_posp = close * 1
// Body size as a percentage of the total candle
diffp = math.abs(close_posp - open_posp)
plot(diffp, color=color.new(color.white, 100))
open_pos2p = high * 1
close_pos2p = low * 1
// Body size as a percentage of the total candle
diff2p = (math.abs(close_posp - open_posp)) - (math.abs(close_pos2p - open_pos2p))
////////////// Condition bougie patronne DAX si pas avec ATR mettre s a la place de 40
isdiff2p = diffp > 40
is_five_min = (timeframe.isminutes) and (timeframe.multiplier == 5)
plotshape(is_five_min ? isdiff2p : na, style=shape.square, color=color.new(color.white, 0), location=location.abovebar, size=size.tiny, textcolor=color.white, text="★P★\n​")
D2 = isdiff2p ? DrawPoint : na
// Plot drawing points
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=1)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=2)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=3)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=4)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=5)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=6)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=7)
plotchar(is_five_min ? D2 : na, char='▬', color=color.new(color.white, 0), location=location.absolute, size=size.tiny, offset=8)
image with my plotchar ---
Can you help me ? Thank you

and with line.new code but i but I can't get it to work like my plochar,
////////////////////////// LINE NEW HL2
f_delete_line_and_label(line_id) =>
line.delete(line_id[1])
m = request.security(syminfo.tickerid,"5",hl2,barmerge.gaps_off,barmerge.lookahead_on)
var line line_m = na
if (barstate.islast)
line_m := line.new(bar_index[1], m, bar_index, m, extend=extend.both, color=color.blue,width=4)
line.delete(line_m[1])

Related

need help to complete my pine script code

//#version=5
indicator(shorttitle="BB and Relative Strength Index", title="Bollinger Bands and RSI", overlay=true, format=format.price, timeframe="", timeframe_gaps=true)
//BB
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
// RSI
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
rsiUpperBand = hline(60, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(40, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
//Plotting Buy Sell Signal
Can anybody help me complete this code:
I want to have Buy sell signal on this on below mentioned conditions:
buy entry- when candle's high low closes below BB(Lower band) and RSI should be below 35
target- when candle's high low closes above BB( Upper band) or RSI should be 60
Sell Entry-when candle's high low closes above BB(Upper band) and RSI should be below 65
target- when candle's high low closes below BB( Lower band) or RSI should be 40
//#version=5
indicator(shorttitle="BB and Relative Strength Index", title="Bollinger Bands and RSI", overlay=true, format=format.price, timeframe="", timeframe_gaps=true)
//BB
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
// RSI
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#7E57C2)
rsiUpperBand = hline(60, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(40, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
// Buy and Sell Signals
buySignal = (low < lower) and (close < lower) and (rsi < 35)
buyTarget = (high > upper) or (rsi > 60)
sellSignal = (high > upper) and (close > upper) and (rsi < 65)
sellTarget = (low < lower) or (rsi > 40)
plotshape(buySignal, title="Buy Signal", style=shape.triangleup, location=location.belowbar, color=color.green, transp=0)
plotshape(sellSignal, title="Sell Signal", style=shape.triangledown, location=location.abovebar, color=color.red, transp=0)
// Plot Buy and Sell Targets
var buyTargetPrice = 0.0
var sellTargetPrice = 0.0
if (buyTarget)
buyTargetPrice := upper
if (sellTarget)
sellTargetPrice := lower
plot(buyTargetPrice, title="Buy Target", color=color.lime, offset=offset, linewidth=2, style=plot.style_circles)
plot(sellTargetPrice, title="Sell Target", color=color.red, offset=offset, linewidth=2, style=plot.style_circles)
//Plotting Buy Sell Signal

Please how do i find the inverse kinematics for a 5 dof serial manipulator using matlab ikine()

I am trying to find the inverse kinematics for a 5 DOF robot. These are my parameters.
L(1) = Link([0, 0, 0, pi/2]);
L(2) = Link([0, 0, 37.5, 0]);
L(3) = Link([0, 0, 37.5, 0]);
L(4) = Link([0, 0, 0, pi/2]);
L(5) = Link([0, 30, 0, 0]);`
`Robot.tool = transl(0,150,0)*trotx(pi/2);`
I am trying to find the inverse kinematics for this position

Change a box to a plot or plotshape

I have this code for imbalance
//-----------------------------------------------------------------------------}
//Fair Value Gaps
//-----------------------------------------------------------------------------{
//Bullish
[bull_fvg, bull_fvg_count] = imbalance_detection(show_fvg, fvg_usewidth, fvg_method, fvg_gapwidth, low, high[2], low > high[2] and close[1] > high[2])
bull_fvg_filled = bull_filled(bull_fvg, high[2])
//Bearish
[bear_fvg, bear_fvg_count] = imbalance_detection(show_fvg, fvg_usewidth, fvg_method, fvg_gapwidth, low[2], high, high < low[2] and close[1] < low[2])
bear_fvg_filled = bear_filled(bear_fvg, low[2])
if bull_fvg
avg = math.avg(low, high[2])
box.new(n-2, low, n + fvg_extend, high[2], border_color = na, bgcolor = color.new(bull_fvg_css, 80))
line.new(n-2, avg, n + fvg_extend, avg, color = bull_fvg_css)
if bear_fvg
avg = math.avg(low[2], high)
box.new(n-2, low[2], n + fvg_extend, high, border_color = na, bgcolor = color.new(bear_fvg_css, 80))
line.new(n-2, avg, n + fvg_extend, avg, color = bear_fvg_css)
I try to change the box.new by a plot like this but error :Cannot use 'plotshape' in local scope
plotshape_bull = n-2, avg, n + fvg_extend, avg
plotshape(plotshape_bull, color = bull_fvg_css, "BB10 up", style=shape.labelup, color = #00ff0a, location = location.belowbar)
Thanks for your help
The solution
plotshape(bull_fvg_count ? bull_fvg : na ,"Bull", style=shape.arrowup, color = #00ff0a, location = location.top, offset = -1)
plotshape(bear_fvg_count ? bear_fvg : na ,"Bear", style=shape.arrowdown, color = color.red, location = location.top, offset = -1)

Calculate Pearson's R for Linear Regression PineScript

I have a pinescript strategy below in which I am trying to calculate the Pearsons R correlation value. I got the code from here and modified it a bit (https://www.tradingview.com/script/CD7yUWRV-Linear-Regression-Trend-Channel/).
His code does not include the Pearson's R correlation which is very important to the trading strategy I'm trying to use since it indicates the strength of the trend and it's direction (up or down). To see a working examle of the Pearson's R, add the default indicator linear regression and it will be the number to the bottom left. I'll attach a screenshot for an example.
How do I calculate the Pearson's R Value from the code I have so far?
I have looked for sample pine scripts that have the Pearson's R calculation for linear regression in pinescript and could not find anything.
strategy(title="Linear Regression Trend Channel Strategy", overlay=true,initial_capital=1000,commission_type=strategy.commission.percent,commission_value=0.26,default_qty_type=strategy.percent_of_equity,default_qty_value=100)
period = input( 240, "Period" , input.integer, minval=3)//288
deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
extendType = input("Right", "Extend Method", input.string , options=["Right","None"])=="Right" ? extend.right : extend.none
periodMinusOne = period-1
Ex = 0.0, Ey = 0.0, Ex2 = 0.0, Exy = 0.0, for i=0 to periodMinusOne
closeI = nz(close[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i), Exy := Exy + (closeI * i)
ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
linearRegression = (Ey - slope * Ex) / period
intercept = linearRegression + bar_index * slope
deviation = 0.0, for i=0 to periodMinusOne
deviation := deviation + pow(nz(close[i]) - (intercept - slope * (bar_index[i])), 2.0)
deviation := deviations * sqrt(deviation / periodMinusOne)
startingPointY = linearRegression + slope * periodMinusOne
var line upperChannelLine = na , var line medianChannelLine = na , var line lowerChannelLine = na
line.delete(upperChannelLine[1]), line.delete(medianChannelLine[1]), line.delete(lowerChannelLine[1])
upperChannelLine := line.new(bar_index - period + 1, startingPointY + deviation, bar_index, linearRegression + deviation, xloc.bar_index, extendType, color.new(#FF0000, 0), line.style_solid , 2)
medianChannelLine := line.new(bar_index - period + 1, startingPointY , bar_index, linearRegression , xloc.bar_index, extendType, color.new(#C0C000, 0), line.style_solid , 1)
lowerChannelLine := line.new(bar_index - period + 1, startingPointY - deviation, bar_index, linearRegression - deviation, xloc.bar_index, extendType, color.new(#00FF00, 0), line.style_solid , 2)
if(crossunder(close,line.get_y2(lowerChannelLine)))
strategy.entry("Long", strategy.long)
if(crossover(close,line.get_y2(upperChannelLine)))
strategy.entry("Short", strategy.short)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © x11joe
// Credit given to #midtownsk8rguy for original source code. I simply modified to add Pearson's R
//#version=4
study("Linear Regression Trend Channel With Pearson's R", "LRTCWPR", true, format.inherit)
period = input( 20, "Period" , input.integer, minval=3)
deviations = input( 2.0, "Deviation(s)" , input.float , minval=0.1, step=0.1)
extendType = input("Right", "Extend Method", input.string , options=["Right","None"])=="Right" ? extend.right : extend.none
periodMinusOne = period-1
Ex = 0.0, Ey = 0.0, Ex2 = 0.0,Ey2 =0.0, Exy = 0.0, for i=0 to periodMinusOne
closeI = nz(close[i]), Ex := Ex + i, Ey := Ey + closeI, Ex2 := Ex2 + (i * i),Ey2 := Ey2 + (closeI * closeI), Exy := Exy + (closeI * i)
ExT2 = pow(Ex,2.0) //Sum of X THEN Squared
EyT2 = pow(Ey,2.0) //Sym of Y THEN Squared
PearsonsR = (Exy - ((Ex*Ey)/period))/(sqrt(Ex2-(ExT2/period))*sqrt(Ey2-(EyT2/period)))
ExEx = Ex * Ex, slope = Ex2==ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
linearRegression = (Ey - slope * Ex) / period
intercept = linearRegression + bar_index * slope
deviation = 0.0, for i=0 to periodMinusOne
deviation := deviation + pow(nz(close[i]) - (intercept - slope * (bar_index[i])), 2.0)
deviation := deviations * sqrt(deviation / periodMinusOne)
startingPointY = linearRegression + slope * periodMinusOne
var label pearsonsRLabel = na
label.delete(pearsonsRLabel[1])
pearsonsRLabel := label.new(bar_index,startingPointY - deviation*2,text=tostring(PearsonsR), color=color.black,style=label.style_labeldown,textcolor=color.white,size=size.large)
var line upperChannelLine = na , var line medianChannelLine = na , var line lowerChannelLine = na
line.delete(upperChannelLine[1]), line.delete(medianChannelLine[1]), line.delete(lowerChannelLine[1])
upperChannelLine := line.new(bar_index - period + 1, startingPointY + deviation, bar_index, linearRegression + deviation, xloc.bar_index, extendType, color.new(#FF0000, 0), line.style_solid , 2)
medianChannelLine := line.new(bar_index - period + 1, startingPointY , bar_index, linearRegression , xloc.bar_index, extendType, color.new(#C0C000, 0), line.style_solid , 1)
lowerChannelLine := line.new(bar_index - period + 1, startingPointY - deviation, bar_index, linearRegression - deviation, xloc.bar_index, extendType, color.new(#00FF00, 0), line.style_solid , 2)
Okay so I posted my solution below. I got the idea thanks to a youtube video here, https://www.youtube.com/watch?v=2B_UW-RweSE
This really helped me figure out the formula. Hope this helps someone else on tradingView that needed this!
i think this
PearsonsR = (Exy - ((Ex*Ey)/period))/(sqrt(Ex2-(ExT2/period))*sqrt(Ey2-(EyT2/period)))
should be
PearsonsR = (((Ex*Ey)/period)-Exy)/(sqrt(Ex2-(ExT2/period))*sqrt(Ey2-(EyT2/period)))
according with:https://it.wikipedia.org/wiki/Indice_di_correlazione_di_Pearson#/media/File:Correlation_coefficient.png

How can I solve a system of 'n' (non linear) equations with 'n' variables (second degree polynomial) in Matlab?

Is possible to solve a system of 'n' (no linear) equations with 'n' variables (grade 2 or 1) in Matlab?
Can you point me an example?
I've tried to use fsolve function but the next error appear:
??? Error using ==> feval
Undefined function or method 'ecuaciones' for input arguments of type
'double'.
example with fsolve
Edited:
I've a file called 'ecuaciones.m' which has the next content:
function [f]=ecuaciones(x)
f(1)=x(1) + x(2) + x(3) + 2*x(4) -1,905;
f(2)=2*x(3) + 3*x(4) + x(5) + x(6) - 3,922;
f(3)=x(1) + 2*x(2) + x(4) + x(5) + x(6) - 1,961;
f(4)=1,961*x(3) + x(3)*x(5) - x(2)*x(3) - 0,02834*(x(5)^2);
f(5)=9,32845*(x(3)^2) - x(4)*x(6);
f(6)=61,4723*x(1)*x(6) - x(2)*x(5);
J = zeros (n,n);
J =
[ 1, 1, 1, 2, 0, 0]
[ 0, 0, 0, 0, 0, 0]
[ 0, 0, 2, 3, 1, 1]
[ 1, 2, 0, 1, 1, 1]
[ 0, -x3, x5 - x2 + 1961/1000, 0, x3 - (1417*x5)/25000, 0]
[ 0, 0, (2625725246496519*x3)/140737488355328, -x6, 0, -x4]
[ (8651457105425229*x6)/140737488355328, -x5, 0, 0, -x2, (8651457105425229*x1)/140737488355328];
Then, in Matlab console I try:
>> x0=[1,1,1,1,1,1];
>> [x,feval,flag]=fsolve('ecuaciones',x0);
Use a function handle
>> x = fsolve( #ecuaciones, x0 );
You need to make sure ecuaciones.m is in matlab's path.
UPDATE:
for better convergence, you might want to compute the Jacobian as well.
In your case, the Jacobian is an n by n matrix with J(l,k) is the partial derivative of f(l) w.r.t x(k) at the point x.
You will need to modify ecuaciones.m to return two outputs the first is f and the second is J
function [f J] = ecuaciones( x )
f(1)=x(1) + x(2) + x(3) + 2*x(4) -1,905;
f(2)=2*x(3) + 3*x(4) + x(5) + x(6) - 3,922;
f(3)=x(1) + 2*x(2) + x(4) + x(5) + x(6) - 1,961;
f(4)=1,961*x(3) + x(3)*x(5) - x(2)*x(3) - 0,02834*(x(5)^2);
f(5)=9,32845*(x(3)^2) - x(4)*x(6);
f(6)=61,4723*x(1)*x(6) - x(2)*x(5);
J = [...
1, 1, 1, 2, 0, 0;...
0, 0, 0, 0, 0, 0;...
0, 0, 2, 3, 1, 1;...
1, 2, 0, 1, 1, 1;...
0, -x3, x5 - x2 + 1961/1000, 0, x3 - (1417*x5)/25000, 0;...
0, 0, (2625725246496519*x3)/140737488355328, -x6, 0, -x4;...
(8651457105425229*x6)/140737488355328, -x5, 0, 0, -x2, (8651457105425229*x1)/140737488355328];
And you may call fsolve
>> x = fsolve( #ecuaciones, x0, struct('Jacobian','on') );