Search code examples
pine-scriptpine-script-v5

Pine Script How to use current indicator plotted on the screen


I have selected a community indicator and it is plotted active on my screen.

I am struggling to figure out how to replace the indicator in the 3rd code line below (I have tried referring to the indicators name but to no avail)

Basically I just want to buy or sell once daily based on whether the indicator is above or below 50.

 //@version=5

strategy("Indicator Strategy", overlay=true)

indicatorValue = ta.movingaverage(close, 10) // Replace with your actual indicator

buyCondition = indicatorValue < 50
sellCondition = indicatorValue > 50

if (buyCondition)
    strategy.entry("Buy", strategy.long)

if (sellCondition)
    strategy.close("Buy")

I have found the indicator function but I cant call my currently loaded indicator.


Solution

  • You can make the indicatorValue a source input and select the indicator you are using as the source. As long as it has a plot, not just lines or boxes it should work.

    strategy("Indicator Strategy", overlay=true)
    
    indicatorValue = input.source(close, 'Select Indicator You Want From Settings Here')
    
    buyCondition = indicatorValue < 50
    sellCondition = indicatorValue > 50
    
    if (buyCondition)
        strategy.entry("Buy", strategy.long)
    
    if (sellCondition)
        strategy.close("Buy")