I have a TradingView Pine v5 strategy script that buys if the price closes a certain percentage below the set EMA level and sells if the price closes a certain percentage above the same EMA level.
Here's the working code for that:
// EMA Length
emaLength = input.int(title="EMA Length", defval=200, minval=0)
// EMA Price Percentages
emaPercBuy = input.float(title="Buy % Below EMA", defval=1.006, minval=0, step=0.001) // 1.006 means 0.6%
emaPercSell = input.float(title="Sell % Above EMA", defval=1.006, minval=0, step=0.001) // 1.006 means 0.6%
// EMA Line Function
emaLine = ta.ema(close, emaLength)
// EMA Plot line
plot(emaLine, color=close[1] > emaLine and close > emaLine ? colorGreen : colorRed, linewidth=2)
// Buy and Sell
longCond = close < emaLine/emaPercBuy
shortCond = close > emaLine*emaPercSell
defval=1.006
is 0.6% as a decimal and the line longCond = close < emaLine/emaPercBuy
divides the EMA by that decimal number (aka "emaPercBuy") to reduce the EMA level by -0.6%. That all works fine, but it looks messy putting 1.006
and I want to be able to put 0.6
in the input.
That means making the code do a calculation so if I do 0.6 +100 /100
that converts it back to what the code needs to use 1.006
With that in mind I altered the code to allow normal looking percentages to be put in, but this doesn't work - it doesn't show any trades on the chart:
// EMA Length
emaLength = input.int(title="EMA Length", defval=200, minval=0)
// EMA Price Percentages
emaPercBuy = input.float(title="Buy % Below EMA", defval=0.6, minval=0, step=0.1)
emaPercSell = input.float(title="Sell % Above EMA", defval=0.6, minval=0, step=0.1)
// EMA Line Function
emaLine = ta.ema(close, emaLength)
// EMA Plot line
plot(emaLine, color=close[1] > emaLine and close > emaLine ? colorGreen : colorRed, linewidth=2)
// Buy and Sell
longCond = close < emaLine/emaPercBuy+100/100
shortCond = close > emaLine*emaPercSell+100/100
The line close < emaLine/emaPercBuy+100/100
should be the right calculation but it's not working - no trades appear on the chart unlike with the first block of code above that does work - but that looks messy with 1.006 to mean 0.6%.
I don't know why doing +100/100
hasn't fixed it and since that is the correct calculation, I don't know what else I could try. It seems even though the code compiles, there's something simple I have overlooked?
Thanks in advance to anyone Pine coders that can answer this.
With longCond = close < emaLine/emaPercBuy+100/100
, you are simply adding 1
to the result of emaLine/emaPercBuy
.
That expression will be evaluated as (emaLine/emaPercBuy) + (100/100)
.
I would do this in another way. I would get the percentage value in place like so:
emaPercBuy = input.float(title="Buy % Below EMA", defval=0.6, minval=0, step=0.001) * 0.01
Then calculate the value without modifying emaPercBuy
later on like below:
longCond = (emaLine * (1 - emaPercBuy))
Otherwise, you need to convert emaPercBuy
to percentage every time you use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.