Fixing the Plot Shape signals on a highly profitable Pinescript strategy - pine-script-v5

I want to fix the attached pine script code in version 5 of Tradingview for a strategy I formed which is highly profitable but the signals for Buy, Sell, Exit Buy and Exit Sell are not occurring on the chart at the right points.
If someone can help me fix the attached code it would be great for even them as the strategy is highly profitable in trending markets and can be easily used to implement on a Trading Bot.
The strategy is as follows
I have 2 Exponential Moving Averages plotted on the chart, mainly EMA of 144 and EMA of 169. Once you plot these EMA's on the chart, they appear like a tunnel.
Buy Condition is - If the candle closes above both the EMA's, then a Buy Plot shape should occur on that candle.
Exit Buy Condition is - if the candle breaks the low of both the EMA's, even if not on a closing basis (even if the low of the candle is below both the EMA's), then a Exit Buy Plot shape should occur on that candle.
Sell Condition is - If the candle closes below both the EMA's, then a Sell Plot shape should occur on that candle.
Exit Sell Condition is - if the candle breaks the high of both the EMA's, even if not on a closing basis (even if the high of the candle is above both the EMA's), then a Exit Buy Plot shape should occur on that candle.
Also the most important aspect is that these shapes should not occur repeatedly. They should only occur on the specific candle when the condition is met.
I have attached the current pinescript code that I drafted but it isint working properly.
//#version=5
indicator("WavyCrorepati v2.0", overlay=true)
tunnel1 = ta.ema(close, 144)
tunnel2 = ta.ema(close, 169)
plot(tunnel1, color=color.white, linewidth=2)
plot(tunnel2, color=color.white, linewidth=2)
intradelong = 0
intradeshort = 0
// BUY
long = close > tunnel1 and close > tunnel2 or (ta.crossover(close,tunnel1) and ta.crossover(close,tunnel2))
if long
intradelong := 10
exitlong = low < tunnel2 and low < tunnel1
if exitlong
intradelong := 5
// SHORT
short = close < tunnel1 and close < tunnel2 or (ta.crossunder(close, tunnel1) and ta.crossunder(close, tunnel2))
if short
intradeshort := -10
exitshort = high > tunnel1 and high > tunnel2
if exitshort
intradeshort := -5
// PLOT SHAPES
plotshape(long and intradelong[1] == 5 , style=shape.labelup, color=color.green, location=location.belowbar, size=size.small,text="B",textcolor=color.white)
plotshape(exitlong and long[1], style=shape.diamond,color=color.white,location=location.belowbar,size=size.tiny)
plotshape(short and intradeshort[1] == -5 , style=shape.labeldown, color=color.red, location=location.abovebar, size=size.small,text="S",textcolor=color.yellow)
plotshape(exitshort and short[1], style=shape.diamond,color=color.yellow, location=location.abovebar,size=size.tiny)
plot(intradelong, color = color.green, display = display.status_line)
plot(intradeshort, color = color.red, display = display.status_line)

Related

I want to write a pine code strategy but everytime I get a malfunction that says 'could not find indicator 'sma'

i want to create a strategy with pine code that gives me a buy signal everytime when the price of the stock is above the SMA200 and the MACD is below 0 and when the MACD level line crosses the MACD signal line up. Furthermore I want it to give me a short signal everytime the price of the stock is below the SMA200 and when the MACD is above 0 and when the MACD level line crosses the MACD signal line down.
Here is my code
//#version=5
strategy("MACD Strategy", overlay=true)
// EMA200
ema200 = sma(close, 200)
// MACD
macdLine = sma(close, 12) - sma(close, 26)
macdSignal = sma(macdLine, 9)
// MACD Level crosses MACD Signal Line up
macdCrossUp = crossover(macdLine, macdSignal)
// MACD Level crosses MACD Signal Line down
macdCrossDown = crossunder(macdLine, macdSignal)
// MACD is above 0
macdAboveZero = macdLine > 0
// MACD is below 0
macdBelowZero = macdLine < 0
// Buy signal
buySignal = close > ema200 and macdCrossUp and macdBelowZero
// Short signal
shortSignal = close < ema200 and macdCrossDown and macdAboveZero
// Plot signals
plot(macdCrossUp ? 1 : na, "MACD Cross Up", color=color.green)
plot(macdCrossDown ? 1 : na, "MACD Cross Down", color=color.red)
// Buy and Short strategy
if (buySignal)
strategy.entry("Buy", buySignal, when = buy)
I expected it to show me Buy and Short signals on my TradingView chart. Unfortunately this did not happen as it can not find the indicator called 'sma'.
But it does not work and says everytime that it could not find the function 'sma'
I would really appreciate it if somebody could help me. :)
And sorry for my bad english.
If you need further information, just tell me.
Thank you very much!
Functions have been moved to dedicated namespaces in v5.
So for functions like, sma, crossover, you should use the ta namespace.
macdSignal = ta.sma(macdLine, 9)
macdCrossUp = ta.crossover(macdLine, macdSignal)
Of course you need to fix that in other places too.

How to get some objects to move randomly in a space lets say a grid of [-5,5]

I need to move some objects lets say 50 in a space (i.e a grid of [-5,5]) and making sure that if the grid is divided into 100 portions most of the portions (90% or more) are once visited by any object
constraints :
object should move in random directions in the grid changing their velocities frequently (change speed and direction in each iteration)
I was thinking of bouncing balls ( BUT moving in random directions even if not hit by anything in space, not they way a real ball moves) , if we could leave them into space in different positions with different forces and each time they hit each other (or getting closer to a specific distance ) they move to different directions with different speed and could give us a result near to 90% hit of portions in the grid .
I also need to make sure objects are not getting out of grid ( could make lb and ub limits and get them back in case they try to leave the grid)
My code is different from the idea I have written above ...
ux = 1;
uy = 15;
g = 9.81;
t = 0; x(1) = 0;
y(1) = 0;
tf = 2.0 * uy / g; % time of flight back to the ground
dt = tf / 20; % time increment - taking 20 steps
while t < tf
t = t + dt;
if((uy - 0.5 * g * t) * t >= 0)
x(end + 1) = ux * t;
y(end + 1) = (uy - 0.5 * g * t) * t;
end
end
plot(x,y)
this code makes the ball to go with Newton's law which is not the case
Bottom line i just need to be able to visit many portions of grid in a short time so this is why i want the objects to moves in a chaotic way in the space in a random manner (each time running the code i need different result so it needs to be random path) and to get a better result i could make the objects bounce to different directions if they hit or visit each other in the same portions , this probably give me a better result .

offset in atr in tradingview - pine script

Need to have offset in ATR function in pine script
Background: The indicator script below is based on the hypothesis that this period's range will be mostly within [last period high + atr(14)] and [last period low - atr(14)]. I want to sell the high call option and low put option and enjoy the premium at the end of the period (week, month).
I have created a pine script that will calculate this period range based on [last period high + atr(14)] and [last period low - atr(14)].
However, because atr(14) applies to current period as well, it plots the dots that change with the current price.
I need to have an atr(14) days till the last period and not considering this current period. Could you please advise how to achieve that.
//#version=3
study(title="High and Low Levels", shorttitle="HL Levels", overlay = true)
Width = input(2, minval=1)
SelectPeriod = input("W", defval="W", type=string)
LookBack = input(1, minval=1)
xHigh = high[LookBack]
xHigh := xHigh + (atr(14))
xLow = low[LookBack] - atr(14)
vS1 = xHigh
vR1 = xLow
plot(vS1, color=#ff0000, title="S1", style = circles, linewidth = Width)
plot(vR1, color=#009600, title="R1", style = circles, linewidth = Width)
Expected: the dots plotted should be plotted based on last period high + last period atr(14) and last period low - last period atr(14)
Actual: the dots plotted based on last week high + atr(14) till the current period and last week low - atr(14) till the current period. This is changing the dots based on the current price movement.
Maybe, I got it wrong, but I think what you want is to take the previous value of atr(14). So it looks like that:
xLow = low[LookBack] - atr(14)[1]
I think, you've got my idea.
This should help....
plot(vS1[1], color=#ff0000, title="S1", style = circles, linewidth = Width)
plot(vR1[1], color=#009600, title="R1", style = circles, linewidth = Width)

Image Processing - Dress Segmentation using opencv

I am working on dress feature identification using opencv.
As a first step, I need to segment t-shirt by removing face and hands from the image.
Any suggestion is appreciated.
I suggest the following approach:
Use Adrian Rosebrock's skin detection algorithm for detecting the skin (thank you for Rosa Gronchi for his comment).
Use region growing algorithm on the variance map. The initial seed can be calculated by using stage 1(see the attached code for more information).
code:
%stage 1: skin detection - Adrian Rosebrock solution
im = imread(<path to input image>);
hsb = rgb2hsv(im)*255;
skinMask = hsb(:,:,1) > 0 & hsb(:,:,1) < 20;
skinMask = skinMask & (hsb(:,:,2) > 48 & hsb(:,:,2) < 255);
skinMask = skinMask & (hsb(:,:,3) > 80 & hsb(:,:,3) < 255);
skinMask = imclose(skinMask,strel('disk',6));
%stage 2: calculate top, left and right centroid from the different connected
%components of the skin
stats = regionprops(skinMask,'centroid');
topCentroid = stats(1).Centroid;
rightCentroid = stats(1).Centroid;
leftCentroid = stats(1).Centroid;
for x = 1 : length(stats)
centroid = stats(x).Centroid;
if topCentroid(2)>centroid(2)
topCentroid = centroid;
elseif centroid(1)<leftCentroid(1)
leftCentroid = centroid;
elseif centroid(1)>rightCentroid(1)
rightCentroid = centroid;
end
end
%first seed - the average of the most left and right centroids.
centralSeed = int16((rightCentroid+leftCentroid)/2);
%second seed - a pixel which is right below the face centroid.
faceSeed = int16(topCentroid);
faceSeed(2) = faceSeed(2)+40;
%stage 3: std filter
varIm = stdfilt(rgb2gray(im));
%stage 4 - region growing on varIm using faceSeed and centralSeed
res1=regiongrowing(varIm,centralSeed(2),centralSeed(1),8);
res2=regiongrowing(varIm,faceSeed(2),faceSeed(1),8);
res = res1|res2;
%noise reduction
res = imclose(res,strel('disk',3));
res = imopen(res,strel('disk',2));
result after stage 1(skin detection):
final result:
Comments:
Stage 1 is calculated using the following algorithm.
The region growing function can be downloaded here.
The solution is not perfect. For example, it may fail if the texture of the shirt is similar to the texture of the background. But I think that it can be a good start.
Another improvement which can be done is to use a better region growing algorithm, which doesn't grows into the skinMask location. Also, instead of using the region growing algorithm twice independently, the result of the second call of region growing can can be based on the result from the first one.

Boundary Condition at the segmentation

I have an image with three phases with different grey-values ( Black, Grey [85 - 110] and another grey [115 - 148] ). I get the segmentation between all phases but I have a boundary condition that has to be taken into account:
Between the black phase and the third phase there are some pixels (more than 5 pixels) with values of the second phase. I want to add these pixels to the third phase because they only appear due to a contrast change between the two phases (1 and 3).
The program is:
Bild1 = imread('croped0015.tif');
for j=1:size(Bild1,2)
for l=1:size(Bild1,1)
if max(Bild1(l:l+5,j))<85 && min(Bild1(l+5:l+10,j))> 123 && min(Bild1(l+10:l+15,j))<123
Bild1(l+5:l+10,j)=min(Bild1(l+10:l+15,j));
end
if max(Bild1(l:l+5,j))<123 && min(Bild1(l+5:l+10,j))> 123 && min(Bild1(l+10:l+15,j))<85
Bild1(l+5:l+10,j)= min(Bild1(l:l+5,j));
end
if (l+16)>=size(Bild1,1)
break;
end
end
end
imshow(Bild1)
impixelregion
How can I use a region growth algorithm for this?