Change a box to a plot or plotshape - pine-script-v5

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)

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

How do I create an alert for a plotshape with an offset of -10, such that when the plotshape is displayed on the chart, an alert would trigger

I tried creating a label with an offset and having the alert trigger from the label displayed on the chart but I'm stuck at this point
prd = input.int(defval=10, title='Pivot Period', minval=4, maxval=30, group='Setup')
float src1 = ppsrc == 'High/Low' ? high : math.max(close, open)
float src2 = ppsrc == 'High/Low' ? low : math.min(close, open)
float ph = ta.pivothigh(src1, prd, prd)
float pl = ta.pivotlow(src2, prd, prd)
plotshape(ph and src1, style=shape.triangledown, text='SELL(H)', color=na, textcolor=color.new(#eeff00, 0), location=location.abovebar, offset=-prd)
plotshape(pl and src2, style=shape.triangleup, text='BUY(L)', color=na, textcolor=color.new(#ffffff, 0), location=location.belowbar, offset=-prd)
Lstyle = linestyle == 'Dashed' ? line.style_dashed : linestyle == 'Solid' ? line.style_solid : line.style_dotted
labeloffset = prd
labelcolor = color.red
labeltext = "Sell(R)"
labelcolor2 = color.green
labeltext2 = "Buy(S)"
value1 = ph and src1
value2 = pl and src2
var label ourlabel = na
label_ph = label.new(x = bar_index - labeloffset, y = na, text = labeltext, color = labelcolor, xloc = xloc.bar_index, yloc = yloc.abovebar, textcolor = color.white, style = label.style_label_down, size = size.normal, tooltip = "pl_label_id")
label_pl = label.new(x = bar_index - labeloffset, y = na, text = labeltext2, color = labelcolor2, xloc = xloc.bar_index, yloc = yloc.belowbar, textcolor = color.white, style = label.style_label_up, size = size.normal, tooltip = "ph_label_id")
// Defined my conditions for the label
if ph and src1
label_ph
else
label.delete(ourlabel)
if pl and src2
label_pl
else
label.delete(ourlabel)

number of items to replace is not a multiple of , MonteCarlo Stochastic Process R error

##
set.seed(123)
SimpleEulerApproximation = function(T,x,a,b,delta){
numberofSteps = T/delta;
TimeSteps = rep(numberofSteps,1);
Y = rep(numberofSteps,1)
Y[1] = x;
for (i in 1:numberofSteps){
TimeSteps[i] = 0 + i*delta;
}
for (j in 2:numberofSteps){
Y[j] = Y[j-1] + a*Y[j-1]*delta + b*Y[j-1]*rnorm(1,0,sqrt(delta));
}
##plot(TimeSteps,Y, type = "l")
}
SimpleEulerApproximation(1,20,-0.01,0.25,0.001)
set.seed(123)
MultipleEulerApproximation = function(T,x,a,b,delta,numberofTrajectories){
numberofSteps = round(T/delta);
TimeSteps = rep(numberofSteps,1);
Y = rep(numberofSteps,rep(numberofTrajectories))
Y = data.matrix(Y)
for (i in 1:numberofTrajectories){
Y[,i] = SimpleEulerApproximation(T,x,a,b,delta);
}
for (i in 1:numberofSteps){
TimeSteps[i] = 0 + i*delta;
}
AverageTrajectory = rep(numberofSteps,1)
for (i in 1:numberofSteps){
AverageTrajectory[i] = mean(Y[i,])
}
##plot(TimeSteps,AverageTrajectory)
}
MultipleEulerApproximation(1,52,0.12,0.30,0.0001,10000)
MonteCarloSimulation = function(T,x,r,sigma,K,delta,numberofTrajectories){
Y = MultipleEulerApproximation(T,x,r,sigma,delta,numberofTrajectories);
lastStep = round(T/delta);
max(Y[lastStep,]-K,0);
size(Y)
price = 1/numberofTrajectories * sum(max(Y[lastStep,]-K,0))*exp(-r*T)
}
MonteCarloSimulation(0.25,52,0.12,0.3,50,0.0001,10000)
When I run the code for multipleEulerApproximation, I get replacement has length 0 error. Can someone help me with this? Much Appreciated.
The first one is simple Euler Approximation for stochastic differential equation dXt =
−0.1Xtdt + 0.25XtdBt, X0 = 20 over the time interval [0, 1] with time step size
∆ = 0.001.
The second chunk of code is for multipleeulerapproximation that is where the error.
The third-chunk is for calculating European call option price using projections.

Sum of rectangles in Matlab

I have a code which doesnt work the way I want to. The problem is i need a sum of all rctangles from a picture as below.
My code:
imH = size(I, 1);
imW = size(I, 2);
windowWidth = 30;
windowHeight = 30;
step = 1;
for r = 1:step:imH - windowHeight + 1
for c = 1:step:imW - windowWidth + 1
Win = I(r:r + windowHeight - 1, c:c + windowWidth - 1, :);
post =[c r windowHeight windowWidth];
I think I lack sum here
%stop = waitforbuttonpress;
subplot(121); imshow(I); title 'Image';
hold on;
rectangle('Position', pos, 'EdgeColor', 'r');
hold off;
subplot (122); imshow(W); title 'ooo';
drawnow;
pause(0.0000001);
end
end
Everything works great but I need to sum separately every rectangle values
You only need to add & in the 1st and 2nd rgb intervals as below:
matchNh = (R > 45 & R < 180) & (G > 50 & G < 185) & (B > 160 & B < 215);
matchNd = (R > 40 & R < 115) & (G > 6 & G < 80) & (B > 10 & B < 75);
Then you are doing right to count the nonzero pixels i.e.
Nh = nnz(matchNh);
Nd = nnz(matchNd);
If you want to use for more than one image , then you can use another for loop outside the two for loops e.g.
imgnames = dir('*.jpg'); % get list of jpg images in current directory
NN = length(imgnames)
%sliding window size
windowWidth = 64;
windowHeight = 64;
%step
step = 64;
Nh = 0;
Nd = 0;
for ii = 1 : NN
I = imread(imgnames(ii).name);
% your code i.e. two for loops as you have written above
imH = size(I, 1);
imW = size(I, 2);
for r = 1:step:imH - windowHeight + 1
for c = 1:step:imW - windowWidth + 1
%sliding window value
W = I(r:r + windowHeight - 1, c:c + windowWidth - 1, :);
pos =[c r windowHeight windowWidth];
R = W(:,:,1);
G = W(:,:,2);
B = W(:,:,3);
% RGB intervals for every sliding window
matchNh = (R > 45 & R < 180) & ...
(G > 50 & G < 185) & ...
(B > 160 & B < 215);
matchNd = (R > 40 & R < 115) & ...
(G > 6 & G < 80) & ...
(B > 10 & B < 75);
Nh = Nh + nnz(matchNh);
Nd = Nd + nnz(matchNd);
end
end
PIc = Nd / (Nh + Nd)
end

Move y axis labels to left side of heatmap.2

I would like to move the y-axis labels to the left side of heatmap.2. (This is similar, but not the same, as the question regarding moving the axis on heatmap)
While I am able to move the axis by editing line 290 of the heatmap.2 function, it the values then overwrite the actual heatmap.
if (is.null(srtRow) && is.null(colRow)) {
axis(4, iy, labels = labRow, las = 2, line = -0.5 + offsetRow,
tick = 0, cex.axis = cexRow, hadj = adjRow[1], padj = adjRow[2])
}
else {
if (is.null(srtRow) || is.numeric(srtRow)) {
xpd.orig <- par("xpd")
par(xpd = NA)
ypos <- axis(4, iy, labels = rep("", nr), las = 2, #change
line = -0.5, tick = 0)
text(x = par("usr")[2] + (1 + offsetRow) * strwidth("M"),
y = ypos, labels = labRow, adj = adjRow, cex = cexRow,
srt = srtRow, col = colRow)
par(xpd = xpd.orig)
}
I tried moving the location of the heatmap.2 by mucking about with the lwid and lhei options, but the overwrite problem persisted.
Thank you
Eric