fibUp = fibonacci(high,"up") - pine-script-v5

```// 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 ..

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.

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)

Pinescript V5 Multi Timeframe Strategy vs Indicator

I have an indicator working the way I want, but I'm trying to convert it to a strategy for backtesting.
I have bollinger bands plotted on the 15 minute timeframe, and on the one hour. This works in the indicator, but it does not work properly in the strategy.
The error I get is:
The strategy function does not have an argument with the name timeframe or timeframe_gaps
I tried removing that in the strategy, but now the 1 hr bollinger bands are incorrect:
strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)
Here is the full code of the strategy:
//#version=5
strategy(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true)
entryPrice = close
// Set input parameters
length = input.int(20, minval=1)
mult = input.float(2.5, minval=0.001, maxval=50)
offset = input.int(0, "Offset", minval = -500, maxval = 500)
// Calculate Bollinger Bands using 15 minute data
src = close
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev
// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h
// Enter long position when 15 minute chart Bollinger Band is touched and 1hr band is touched (or previous 1 hr candle)
longCondition = ta.crossover(close, lower) and (ta.crossover(low, lower1h) or ta.crossover(low[1], lower1h))
if longCondition
strategy.entry("BB Long", strategy.long)
entryPrice = close
// Enter short position when 15 minute chart Bollinger Band is touched and 1hr band is touched (or previous 1 hr candle)
shortCondition = ta.crossunder(close, upper) and (ta.crossover(high, upper1h) or ta.crossover(high[1], upper1h))
if shortCondition
strategy.entry("BB Short", strategy.short)
entryPrice = close
// Calculate profit target
profitTarget = 1 + 0.1 // 10% profit target
// Exit long position when profit target is reached
exitLongCondition = close > entryPrice * profitTarget
if exitLongCondition
strategy.close("BB Long")
// Exit short position when profit target is reached
exitShortCondition = close < entryPrice / profitTarget
if exitShortCondition
strategy.close("BB Short")
// Plot Bollinger Bands
plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)
plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)
Here is the full code of the indicator (Which plots the bands correctly)
// Function to plot Bollinger Bands on a 1 hour timeframe
// Define function and set input parameters
//#version=5
indicator(shorttitle="BB Multi", title="Bollinger Bands Strategy", overlay=true, timeframe="", timeframe_gaps=true)
//study("Bollinger Bands - 1 Hour", overlay=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.5, minval=0.001, maxval=50, title="StdDev")
middle = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = middle + dev
lower = middle - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
// Calculate Bollinger Bands using 1 hour data
src1h = request.security(syminfo.tickerid, '60', close)
middle1h = ta.sma(src1h, length)
dev1h = mult * ta.stdev(src1h, length)
upper1h = middle1h + dev1h
lower1h = middle1h - dev1h
// Plot Bollinger Bands
plot(upper, color=color.red, linewidth=2)
plot(lower, color=color.red, linewidth=2)
plot(upper1h, color=color.blue, linewidth=2)
plot(lower1h, color=color.blue, linewidth=2)
How can I use multi timeframe correctly in a strategy? The tutorials I found all use the indicator function, but I really need to backtest this!
Thanks!
I tried removing timeframe and timeframe_gaps from the strategy function, but the result is that the 1 hour Bollinger bands are incorrect.
Try this:
Change
src1h = request.security(syminfo.tickerid, '60', close)
To
src1h = request.security(syminfo.tickerid, '60', close, lookahead=barmerge.lookahead_on, gaps=barmerge.gaps_on)
I haven't tested this but have experienced something vaguely similar before, let us know how it goes.

How do I get a signal only on the last candle on the chart

Newbie on PineScript on Tradingview, so please excuse if this is silly.
I am trying out the following script on Tradingview,
//#version=5
strategy("My Strategy", overlay=true)
shortSMA = ta.sma(close, 10)
longSMA = ta.sma(close, 30)
rsi = ta.rsi(close, 14)
longCondition = ta.crossover(shortSMA, longSMA)
if (longCondition)
strategy.entry("long", strategy.long, 100, when = rsi > 50)
When I save and add the script to my chart, it gives me a long signal only for the first instance where the condition was met.
So when I add pyramiding to the script,
//#version=5
strategy("My Strategy", overlay=true, pyramiding=250)
shortSMA = ta.sma(close, 10)
longSMA = ta.sma(close, 30)
rsi = ta.rsi(close, 14)
longCondition = ta.crossover(shortSMA, longSMA)
if (longCondition)
strategy.entry("long", strategy.long, 100, when = rsi > 50)
I get a signal for all the instances where the condition is met.
What I am trying to achieve is get a signal only on the last candle (if the condition is met, of course), the previous ones can just go. Is there any way to do this?

Formulae to calculate PWM output Timer (Counter settings) for STM32

Need formulae to calculate Prescaler,Counter Period and a Pulse value of each channel on a Timer with given values of
Input clock frequency (APB)
Output Frequency (PWM)
Duty cycle (for each channel)
Can you solve this equations?
Frequency = ClockFreq / ((PSC + 1) * (ARR + 1))
Dutyin% = (CCRx * 100) / ARR for the fast PWM