Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

code me pine script rsi for tradingview

  1. User

    code me pine script rsi for tradingview

  2. Assistant

    Certainly, here is an example of a Pine Script code for RSI on TradingView:

    //@version=4
    study(title="Relative Strength Index (RSI)", shorttitle="RSI", overlay=false)
    
    // Inputs
    rsiLength = input(14, "RSI Length")
    
    // Calculate RSI
    rsi = rsi(close, rsiLength)
    
    // Plot RSI
    plot(rsi, title="RSI", color=color.blue)
    
    // Overbought and Oversold levels (optional)
    overboughtLevel = input(70, "Overbought Level")
    oversoldLevel = input(30, "Oversold Level")
    
    hline(overboughtLevel, "Overbought", color=color.red)
    hline(oversoldLevel, "Oversold", color=color.green)
    
    // RSI Divergence
    rsiDivergence = rsi - rsi[1]
    plot(rsiDivergence, title="RSI Divergence", color=color.orange, linewidth=2)
    

    This code creates a study to plot the RSI on a chart. It uses the default RSI length of 14, but you can change it according to your preference. It also includes optional overbought and oversold levels as horizontal lines. Additionally, it calculates the RSI divergence between consecutive RSI values and plots it on the chart.

  3. User

    Error at 2:0 no viable alternative at character

  4. Assistant

    The error "no viable alternative at character" usually occurs when there is a syntax error or an unexpected character in the code. In this case, it seems like there might be an issue with the character encoding or formatting in the code.

    To resolve this issue, you can try copying the code again from the answer and make sure there are no invisible characters or formatting errors. Additionally, please ensure that you are using the latest version of Pine Script (version 4 in this case) on TradingView.

    If the issue persists, please provide the full error message along with the code you are trying to execute, and I'll be happy to help you further.

  5. User

    Error at 2:0 no viable alternative at character

  6. Assistant

    Apologies for the confusion. The error you're encountering isn't related to the code itself, but rather how it's being executed or integrated with TradingView.

    Here are a few potential solutions to resolve the "no viable alternative at character" error:

    1. Ensure the Pine Script code is being pasted into the Pine Editor on TradingView correctly. Copy the code again from the answer and be cautious of any formatting issues or invisible characters that may have been accidentally introduced.

    2. Verify that the Pine Script version selected in the Pine Editor matches the version specified in the code (e.g., //@version=4). If the script you're attempting to execute is written in a different Pine Script version, update the //@version directive to match.

    3. Double-check that you are using the Pine Editor on TradingView or a Pine Script-compatible platform. The code won't run properly outside of a platform that supports Pine Script.

    If you've confirmed the code is accurately pasted into TradingView's Pine Editor and you're still encountering the error, it could be helpful to share the entire code snippet you're using, along with the full error message or additional details. This information would allow for further analysis and a more precise solution.

  7. User

    //@version=5 indicator(title="RSI Divergence Indicator", format=format.price, timeframe="", timeframe_gaps=true) len = input.int(title="RSI Period", minval=1, defval=14) src = input(title="RSI Source", defval=close) lbR = input(title="Pivot Lookback Right", defval=5) lbL = input(title="Pivot Lookback Left", defval=5) rangeUpper = input(title="Max of Lookback Range", defval=60) rangeLower = input(title="Min of Lookback Range", defval=5) plotBull = input(title="Plot Bullish", defval=true) plotHiddenBull = input(title="Plot Hidden Bullish", defval=false) plotBear = input(title="Plot Bearish", defval=true) plotHiddenBear = input(title="Plot Hidden Bearish", defval=false) bearColor = color.red bullColor = color.green hiddenBullColor = color.new(color.green, 80) hiddenBearColor = color.new(color.red, 80) textColor = color.white noneColor = color.new(color.white, 100) osc = ta.rsi(src, len)

    plot(osc, title="RSI", linewidth=2, color=#2962FF) hline(50, title="Middle Line", color=#787B86, linestyle=hline.style_dotted) obLevel = hline(70, title="Overbought", color=#787B86, linestyle=hline.style_dotted) osLevel = hline(30, title="Oversold", color=#787B86, linestyle=hline.style_dotted) fill(obLevel, osLevel, title="Background", color=color.rgb(33, 150, 243, 90))

    plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true _inRange(cond) => bars = ta.barssince(cond == true) rangeLower <= bars and bars <= rangeUpper

    //------------------------------------------------------------------------------ // Regular Bullish // Osc: Higher Low

    oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

    // Price: Lower Low

    priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1) bullCondAlert = priceLL and oscHL and plFound bullCond = plotBull and bullCondAlert

    plot( plFound ? osc[lbR] : na, offset=-lbR, title="Regular Bullish", linewidth=2, color=(bullCond ? bullColor : noneColor) )

    plotshape( bullCond ? osc[lbR] : na, offset=-lbR, title="Regular Bullish Label", text=" Bull ", style=shape.labelup, location=location.absolute, color=bullColor, textcolor=textColor )

    //------------------------------------------------------------------------------ // Hidden Bullish // Osc: Lower Low

    oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

    // Price: Higher Low

    priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1) hiddenBullCondAlert = priceHL and oscLL and plFound hiddenBullCond = plotHiddenBull and hiddenBullCondAlert

    plot( plFound ? osc[lbR] : na, offset=-lbR, title="Hidden Bullish", linewidth=2, color=(hiddenBullCond ? hiddenBullColor : noneColor) )

    plotshape( hiddenBullCond ? osc[lbR] : na, offset=-lbR, title="Hidden Bullish Label", text=" H Bull ", style=shape.labelup, location=location.absolute, color=bullColor, textcolor=textColor )

    //------------------------------------------------------------------------------ // Regular Bearish // Osc: Lower High

    oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

    // Price: Higher High

    priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)

    bearCondAlert = priceHH and oscLH and phFound bearCond = plotBear and bearCondAlert

    plot( phFound ? osc[lbR] : na, offset=-lbR, title="Regular Bearish", linewidth=2, color=(bearCond ? bearColor : noneColor) )

    plotshape( bearCond ? osc[lbR] : na, offset=-lbR, title="Regular Bearish Label", text=" Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor )

    //------------------------------------------------------------------------------ // Hidden Bearish // Osc: Higher High

    oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

    // Price: Lower High

    priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)

    hiddenBearCondAlert = priceLH and oscHH and phFound hiddenBearCond = plotHiddenBear and hiddenBearCondAlert

    plot( phFound ? osc[lbR] : na, offset=-lbR, title="Hidden Bearish", linewidth=2, color=(hiddenBearCond ? hiddenBearColor : noneColor) )

    plotshape( hiddenBearCond ? osc[lbR] : na, offset=-lbR, title="Hidden Bearish Label", text=" H Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor )

    alertcondition(bullCondAlert, title='Regular Bullish Divergence', message="Found a new Regular Bullish Divergence, Pivot Lookback Right number of bars to the left of the current bar") alertcondition(hiddenBullCondAlert, title='Hidden Bullish Divergence', message='Found a new Hidden Bullish Divergence, Pivot Lookback Right number of bars to the left of the current bar') alertcondition(bearCondAlert, title='Regular Bearish Divergence', message='Found a new Regular Bearish Divergence, Pivot Lookback Right number of bars to the left of the current bar') alertcondition(hiddenBearCondAlert, title='Hidden Bearisn Divergence', message='Found a new Hidden Bearisn Divergence, Pivot Lookback Right number of bars to the left of the current bar')

  8. Assistant

    The code you provided seems to be written for Pine Script version 5. However, currently, TradingView only supports up to Pine Script version 4.

    To resolve the error you mentioned, you can either modify the code to be compatible with Pine Script version 4 or use a platform that supports Pine Script version 5. Alternatively, you can reach out to TradingView support for further assistance in using Pine Script version 5 on their platform.

  9. User

    code me pine script MACD for tradingview Pine Script version 5

  10. Assistant

    Certainly! Here's an example of a Pine Script code for the MACD (Moving Average Convergence Divergence) indicator in Pine Script version 5:

    //@version=5
    indicator("MACD", overlay=false)
    
    // Inputs
    lengthShort = input(12, title="Short EMA Length")
    lengthLong = input(26, title="Long EMA Length")
    lengthSignal = input(9, title="Signal EMA Length")
    
    // Calculate MACD
    [macdLine, signalLine, _] = ta.macd(close, lengthShort, lengthLong, lengthSignal)
    
    // Plot MACD Line and Signal Line
    plot(macdLine, color=color.blue, title="MACD Line")
    plot(signalLine, color=color.red, title="Signal Line")
    
    // Plot Histogram
    hist = macdLine - signalLine
    plot(hist, color=color.green, style=plot.style_histogram, title="Histogram")
    

    This code creates a study to plot the MACD indicator on a chart. It uses the ta.macd() function to calculate the MACD line, signal line, and histogram. The default input values for the short EMA length, long EMA length, and signal EMA length are set to 12, 26, and 9, respectively. You can adjust these input values according to your preferences. The MACD line is plotted in blue, the signal line in red, and the histogram bars in green.

  11. User

    Error at 2:11 no viable alternative at character '“'

  12. Assistant

    I apologize for the confusion. It seems that the double quotes used in the code snippet provided are not recognized as valid characters in Pine Script version 5.

    Please try using regular double quotes (") instead of the special characters in the code. Here's the modified code without any special characters:

    //@version=5
    indicator("MACD", overlay=false)
    
    // Inputs
    lengthShort = input(12, title="Short EMA Length")
    lengthLong = input(26, title="Long EMA Length")
    lengthSignal = input(9, title="Signal EMA Length")
    
    // Calculate MACD
    [macdLine, signalLine, _] = ta.macd(close, lengthShort, lengthLong, lengthSignal)
    
    // Plot MACD Line and Signal Line
    plot(macdLine, color=color.blue, title="MACD Line")
    plot(signalLine, color=color.red, title="Signal Line")
    
    // Plot Histogram
    hist = macdLine - signalLine
    plot(hist, color=color.green, style=plot.style_histogram, title="Histogram")
    

    Please copy and paste the modified code into the Pine Editor on TradingView, and it should resolve the error you encountered.

code me pine script rsi for tradingview · The Chat Archive