Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Create Sell Signal When Pattern made after RSI Touch or Cross 65: //@version=5…

  1. User

    Create Sell Signal When Pattern made after RSI Touch or Cross 65: //@version=5 indicator("Flexible Candlestick Patterns V5 with Shortcodes", overlay=true)

    // Calculate body size bodySize = math.abs(close - open)

    // Calculate wick sizes using max function (available from v4 onwards) upperWick = if open > close high - open else high - close

    lowerWick = if open > close close - low else open - low

    // Variables var int numberOfCandles = input(5, "Number of Candles") // Input for the number of last candles to consider var float maxVolume = na

    // Function to find maximum volume within the specified number of last candles if bar_index >= numberOfCandles - 1 maxVolume := volume for i = 1 to numberOfCandles - 1 maxVolume := math.max(maxVolume, volume[i])

    // Input parameters for flexibility shootingStarBodyRatio = input.float(0.35, title="Shooting Star Body Ratio") shootingStarWickMultiplier = input.float(3, title="Shooting Star Wick Multiplier") hammerUpperWickRatio = input.float(0.25, title="Hammer Upper Wick Ratio") hammerBodyRatio = input.float(0.35, title="Hammer Body Ratio") dojiBodyRatio = input.float(0.05, title="Doji Body Ratio") dragonflyDojiWickRatio = input.float(0.05, title="Dragonfly Doji Wick Ratio") gravestoneDojiWickRatio = input.float(0.05, title="Gravestone Doji Wick Ratio") hangingManBodyRatio = input.float(0.35, title="Hanging Man Body Ratio") spinningTopBodyRatio = input.float(0.3, title="Spinning Top Body Ratio") insidePatternBodyRatio = input.float(0.4, title="Inside Pattern Body Ratio")

    // Checkboxes to enable/disable each pattern showShootingStar = input(true, title="Show Shooting Star") showHammer = input(true, title="Show Hammer") showInvertedHammer = input(true, title="Show Inverted Hammer") showDoji = input(true, title="Show Doji") showDragonflyDoji = input(true, title="Show Dragonfly Doji") showGravestoneDoji = input(true, title="Show Gravestone Doji") showHangingMan = input(true, title="Show Hanging Man") showSpinningTop = input(true, title="Show Spinning Top") showInsidePattern = input(true, title="Show Inside Pattern")

    // Define Candlestick Patterns with refined conditions isShootingStar = showShootingStar and bodySize < (high - low) * shootingStarBodyRatio and upperWick > lowerWick * shootingStarWickMultiplier and volume == maxVolume isHammer = showHammer and close > open and (high - close) < (open - low) * hammerUpperWickRatio and bodySize < (high - low) * hammerBodyRatio and volume == maxVolume isInvertedHammer = showInvertedHammer and open > close and (high - open) < (close - low) * hammerUpperWickRatio and bodySize < (high - low) * hammerBodyRatio and volume == maxVolume isDoji = showDoji and bodySize <= (high - low) * dojiBodyRatio and math.abs(open - close) <= 0.001 and volume == maxVolume isDragonflyDoji = showDragonflyDoji and isDoji and close > open and (high - close) < (open - low) * dragonflyDojiWickRatio and volume == maxVolume isGravestoneDoji = showGravestoneDoji and isDoji and open > close and (high - open) < (close - low) * gravestoneDojiWickRatio and volume == maxVolume isHangingMan = showHangingMan and isHammer and close < open and bodySize < (high - low) * hangingManBodyRatio and volume == maxVolume isSpinningTop = showSpinningTop and bodySize < (high - low) * spinningTopBodyRatio and upperWick < (high - low) * spinningTopBodyRatio and lowerWick < (high - low) * spinningTopBodyRatio and volume == maxVolume isInsidePattern = showInsidePattern and high <= high[1] and low >= low[1] and bodySize < (high - low) * insidePatternBodyRatio

    // Plot the indicator only if it's one of the specified patterns with higher volume plotshape(isHammer, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="H") plotshape(isShootingStar, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="SS") plotshape(isInvertedHammer, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="IH") plotshape(isDoji, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="D") plotshape(isDragonflyDoji, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="DD") plotshape(isGravestoneDoji, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="GD") plotshape(isHangingMan, location=location.belowbar, style=shape.labeldown, color=color.red, size=size.small, text="HM") plotshape(isSpinningTop, location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="ST") plotshape(isInsidePattern, location=location.abovebar, style=shape.labeldown, color=color.red, text="")

    // Alerts alertcondition((isInsidePattern or isHammer or isShootingStar or isInvertedHammer or isDoji or isDragonflyDoji or isGravestoneDoji or isHangingMan or isSpinningTop) and volume > maxVolume, title='Candlestick Pattern Detected with High Volume', message='A candlestick pattern with high volume has been detected!')

  2. Assistant

    Certainly! To create a sell signal when a candlestick pattern is formed after the RSI touches or crosses 65, we need to modify your existing script to include the RSI calculation and implement the logic accordingly.

    Below is the modified script that fulfills your requirement:

    //@version=5
    indicator("Flexible Candlestick Patterns V5 with Shortcodes", overlay=true)
    
    // Calculate RSI with a period of 14
    rsiValue = ta.rsi(close, 14) 
    
    // Variable to track if RSI has touched or crossed 65
    var bool rsiTouched65 = false
    
    // Update the variable when RSI touches or crosses 65
    if rsiValue >= 65
        rsiTouched65 := true
    
    // Calculate body size
    bodySize = math.abs(close - open)
    
    // Calculate wick sizes
    upperWick = open > close ? high - open : high - close
    lowerWick = open > close ? close - low : open - low
    
    // Variables
    numberOfCandles = input.int(5, "Number of Candles") // Input for the number of last candles to consider
    
    // Function to find maximum volume within the specified number of last candles
    maxVolume = ta.highest(volume, numberOfCandles)
    
    // Input parameters for flexibility
    shootingStarBodyRatio = input.float(0.35, title="Shooting Star Body Ratio")
    shootingStarWickMultiplier = input.float(3, title="Shooting Star Wick Multiplier")
    hammerUpperWickRatio = input.float(0.25, title="Hammer Upper Wick Ratio")
    hammerBodyRatio = input.float(0.35, title="Hammer Body Ratio")
    dojiBodyRatio = input.float(0.05, title="Doji Body Ratio")
    dragonflyDojiWickRatio = input.float(0.05, title="Dragonfly Doji Wick Ratio")
    gravestoneDojiWickRatio = input.float(0.05, title="Gravestone Doji Wick Ratio")
    hangingManBodyRatio = input.float(0.35, title="Hanging Man Body Ratio")
    spinningTopBodyRatio = input.float(0.3, title="Spinning Top Body Ratio")
    insidePatternBodyRatio = input.float(0.4, title="Inside Pattern Body Ratio")
    
    // Checkboxes to enable/disable each pattern
    showShootingStar = input(true, title="Show Shooting Star")
    showHammer = input(true, title="Show Hammer")
    showInvertedHammer = input(true, title="Show Inverted Hammer")
    showDoji = input(true, title="Show Doji")
    showDragonflyDoji = input(true, title="Show Dragonfly Doji")
    showGravestoneDoji = input(true, title="Show Gravestone Doji")
    showHangingMan = input(true, title="Show Hanging Man")
    showSpinningTop = input(true, title="Show Spinning Top")
    showInsidePattern = input(true, title="Show Inside Pattern")
    
    // Define Candlestick Patterns with refined conditions
    isShootingStar = showShootingStar and bodySize < (high - low) * shootingStarBodyRatio and upperWick > lowerWick * shootingStarWickMultiplier and volume == maxVolume
    isHammer = showHammer and close > open and (high - close) < (open - low) * hammerUpperWickRatio and bodySize < (high - low) * hammerBodyRatio and volume == maxVolume
    isInvertedHammer = showInvertedHammer and open > close and (high - open) < (close - low) * hammerUpperWickRatio and bodySize < (high - low) * hammerBodyRatio and volume == maxVolume
    isDoji = showDoji and bodySize <= (high - low) * dojiBodyRatio and math.abs(open - close) <= 0.001 and volume == maxVolume
    isDragonflyDoji = showDragonflyDoji and isDoji and close > open and (high - close) < (open - low) * dragonflyDojiWickRatio and volume == maxVolume
    isGravestoneDoji = showGravestoneDoji and isDoji and open > close and (high - open) < (close - low) * gravestoneDojiWickRatio and volume == maxVolume
    isHangingMan = showHangingMan and isHammer and close < open and bodySize < (high - low) * hangingManBodyRatio and volume == maxVolume
    isSpinningTop = showSpinningTop and bodySize < (high - low) * spinningTopBodyRatio and upperWick < (high - low) * spinningTopBodyRatio and lowerWick < (high - low) * spinningTopBodyRatio and volume == maxVolume
    isInsidePattern = showInsidePattern and high <= high[1] and low >= low[1] and bodySize < (high - low) * insidePatternBodyRatio
    
    // Sell signal condition: Pattern formed after RSI touches or crosses 65
    sellSignal = rsiTouched65 and (isShootingStar or isHammer or isInvertedHammer or isDoji or isDragonflyDoji or isGravestoneDoji or isHangingMan or isSpinningTop or isInsidePattern)
    
    // Reset rsiTouched65 when sell signal occurs
    if sellSignal
        rsiTouched65 := false
    
    // Plot the patterns
    plotshape(isHammer, title="Hammer", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.small, text="H")
    plotshape(isShootingStar, title="Shooting Star", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="SS")
    plotshape(isInvertedHammer, title="Inverted Hammer", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.small, text="IH")
    plotshape(isDoji, title="Doji", location=location.abovebar, style=shape.labeldown, color=color.orange, size=size.small, text="D")
    plotshape(isDragonflyDoji, title="Dragonfly Doji", location=location.belowbar, style=shape.labeldown, color=color.green, size=size.small, text="DD")
    plotshape(isGravestoneDoji, title="Gravestone Doji", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="GD")
    plotshape(isHangingMan, title="Hanging Man", location=location.abovebar, style=shape.labeldown, color=color.red, size=size.small, text="HM")
    plotshape(isSpinningTop, title="Spinning Top", location=location.abovebar, style=shape.labeldown, color=color.purple, size=size.small, text="ST")
    plotshape(isInsidePattern, title="Inside Pattern", location=location.belowbar, style=shape.labeldown, color=color.blue, size=size.small, text="IP")
    
    // Plot Sell Signal
    plotshape(sellSignal, title="Sell Signal", location=location.abovebar, style=shape.triangleup, color=color.red, size=size.normal, text="Sell")
    
    // Alert when sell signal is generated
    alertcondition(sellSignal, title="Sell Signal Alert", message="Sell signal generated after RSI touched or crossed 65.")
    

    Explanation of the Modifications:

    1. RSI Calculation:

      rsiValue = ta.rsi(close, 14)
      
      • Calculates the RSI with a period of 14.
    2. Tracking RSI Crossing/Touching 65:

      var bool rsiTouched65 = false
      
      if rsiValue >= 65
          rsiTouched65 := true
      
      • Initializes a boolean variable rsiTouched65 to false.
      • Updates rsiTouched65 to true when RSI touches or crosses 65.
    3. Sell Signal Condition:

      sellSignal = rsiTouched65 and (isShootingStar or isHammer or isInvertedHammer or isDoji or isDragonflyDoji or isGravestoneDoji or isHangingMan or isSpinningTop or isInsidePattern)
      
      • Generates a sell signal only when a candlestick pattern is formed after the RSI has touched or crossed 65.
    4. Resetting RSI Condition:

      if sellSignal
          rsiTouched65 := false
      
      • Resets rsiTouched65 to false after a sell signal is generated to ensure the logic waits for the RSI to touch or cross 65 again before the next sell signal.
    5. Plotting the Sell Signal:

      plotshape(sellSignal, title="Sell Signal", location=location.abovebar, style=shape.triangleup, color=color.red, size=size.normal, text="Sell")
      
      • Plots a red triangle above the bar where the sell signal is generated.
    6. Generating an Alert:

      alertcondition(sellSignal, title="Sell Signal Alert", message="Sell signal generated after RSI touched or crossed 65.")
      
      • Sets up an alert condition so that you get notified when the sell signal occurs.

    Usage Notes:

    • Make sure to add this script to your chart and enable alerts if you wish to receive notifications.
    • You can adjust the RSI period and the threshold value (65) if needed.
    • The maxVolume calculation uses the highest volume over the specified number of bars, ensuring the patterns are significant.
    • All patterns and parameters are customizable via input settings.

    Example Scenario:

    1. The RSI touches or crosses 65, setting rsiTouched65 to true.
    2. A candlestick pattern (e.g., Shooting Star) is detected in a subsequent bar.
    3. Since rsiTouched65 is true and a pattern is found, a sell signal is generated.
    4. The sell signal is plotted on the chart, and an alert is triggered.
    5. rsiTouched65 is reset to false to wait for the next RSI touch/cross above 65.

    Feel free to let me know if you need further adjustments or explanations!