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

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.

Related

Counting bars since highest high of last 50 bars in Pine Script

i am trying to count number o fbars since highest high of last 50 bars in pine script. But it is giving me error that "Study error, too many candles referenced in history". Can anyone help
//#version=5
indicator("Trendline" , overlay = true)
h1=ta.highest(high, 50)
l1=ta.lowest(low, 50)
bari=ta.barssince(h1)
bari2=ta.barssince(l1)
if(barstate.islast)
highline=line.new(x1=bari, y1=high[h1], x2=bar_index, y2=high)
line.set_color(highline, color.red)
line.set_extend(highline, extend.none)
line.set_width(highline, 2)
line.delete(highline[1])``
at x2, it should be the last candle (bar_index). so x1 must be the last candle minus 50 periods
y1 and y2, must be the value of highest high (h1)
Something like that:
//#version=5
indicator("Trendline" , overlay = true)
h1 = ta.highest(high, 50)
if(barstate.islast)
highline=line.new(x1=bar_index - 50, y1=h1, x2=bar_index, y2=h1)
line.set_color(highline, color.red)
line.set_extend(highline, extend.none)
line.set_width(highline, 2)
line.delete(highline[1])

Fixing the Plot Shape signals on a highly profitable Pinescript strategy

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)

fibUp = fibonacci(high,"up")

```// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © r
//#version=5
// This script triggers a trade when RSI is overbought/oversold and there is a convergence with MACD
// It also implements Tom Demark signals and only triggers a trade when all conditions are met
// The trade is only triggered when near a Fibonacci resistance or support line
// Define constants
rsiLength = 14
overbought = 70
oversold = 30
macdLength = 12
signalLength = 26
// Include libraries
study(title="Trade Trigger", overlay=true)
// Get current symbol and time frame
symbol = syminfo.ticker
timeframe = syminfo.timeframe
// Calculate RSI
rsi = rsi(close, rsiLength)
// Calculate MACD and signal line
macd = macd(close, macdLength, signalLength)
signal = macd[1]
// Check if RSI is overbought/oversold and there is a convergence with MACD
if (rsi > overbought and macd > signal) or (rsi < oversold and macd < signal)
// Calculate Fibonacci levels
fibUp = fibonacci(high,"up")
if (close > high) or (close < low)
fibDown = fibonacci(low, "down")
// Check if price is near a Fibonacci resistance or support line
if (close > high) or (close < low)
// Trigger trade
trade()
endif
endif```
is fibUp = fibonacci(high,"up") a valid statement?
In PineScript I get the compilation error: mismatched input 'fibUp' expecting 'end of line without line continuation'.
I tried removing indents and spaces without any improvement, error still occurs.
in your code you must change :
if (rsi > overbought and macd > signal) or (rsi < oversold and macd < signal)
// Calculate Fibonacci levels
fibUp = fibonacci(high,"up")
if (close > high) or (close < low)
by this code : the idea is to respect to ´if’ block with 4 spaces after (only 4)
if (rsi > overbought and macd > signal) or (rsi < oversold and macd < signal)
// Calculate Fibonacci levels
fibUp = fibonacci(high,"up")
if (close > high) or (close < low)
.. and so on ..

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.

Programmatically analyze a line graph

I got a line graph with Y axis having value and X axis having time. The X axis have 5 minute resolution. I'm looking for some kind of an algorithm to help me teach the iPhone to understand where the line is going. I've never taken an algorithms class, so any help would be appreciated. What I need to know is if the line has been rising for a certain number of segments continuously.
Right now I'm implementing the following:
If the current data point has Y value greater than the previous one, increment the slope counter by one. If it is equal, increment slope counter by 0. If the value is less, decrement the slope counter.
if(current>previous)
{
counter++;
}
else if(current<previous)
{
counter--;
}
This produces a sawtooth like graphs, which is easier to analyze. However due to the problems with the window size, the graph may "bounce". This is where I expect my logic to have problems.
I hope there's some kind of CS algorithm to help me with this task, as I don't even know what kinds of keywords to type into google for this problem.
If all you need to know is if the line has been rising for a certain number of segments continuously, why not just have a counter that increments until it hits that certain number of segments, or resets if the line goes down, like:
int counter = 0;
for (int i = 1; i < datasize; i++) {
if (data[i] > data[i - 1]) {
++counter;
if (counter == THRESHOLD) {
println("trending up at %d.", i);
}
} else if (data[i] < data[i - 1]) {
counter = 0;
}
}
If you're just looking to see if the line is trending up or down overall, could you just do this:
if (data[datasize - 1] > data[0]) {
println("Overall trend is up.");
} else if (data[datasize - 1] < data[0]) {
println("Overall trend is down.");
} else {
print("Overall trend is flat.");
}
If you want better prediction -- like, here's the line up to this point in time, here's a guess at what it's going to look like in the future, there are two avenues to explore. The first is "regression analysis" or "regression lines". This will work best for data which is generally increasing or decreasing as time goes on, and will get you the rate of those increases or decreases (the average slope of the line).
The second is "Fast Fourier Transform" - this is useful for lines which are like waves, in that they stay between a min and a max bound and have some regular cycle (or a number of regular cycles, which is what the equation will divine).
Have fun. This is an enjoyable problem to solve.
What you might be looking for is Linear Regression , which is estimating a good straight line match to your data (in one least squares sense). The slope of this line might help you tell "where it is going", depending on the behavior of the underlying model.