How can I color candles based on my strategy conditions?

Asked by Radhesh Joshi · SOLVED
1 reply 219 views Jump to solution Reply
Radhesh Joshi Original Post
Strategy Builder

I’m developing a trading strategy in Pine Script and want to highlight candles whenever my buy or sell conditions are met. Instead of only plotting signals, I’d like the candle colors to change so I can easily identify bullish and bearish setups on the chart.

Is there a way to change candle colors dynamically based on custom strategy conditions in Pine Script?

Posted Aug 5
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    Yes sir, you can definitely do that.

    Pine Script provides the barcolor() function, which allows you to change the color of chart candles based on any condition in your script. This is a simple and effective way to visually highlight trading opportunities without modifying the actual price data.

    For example, if you want to color candles green when your buy condition is true and red when your sell condition is true, you can use the following code:

    //@version=6
    indicator("Strategy Candle Colors", overlay=true)
    
    fastEMA = ta.ema(close, 10)
    slowEMA = ta.ema(close, 20)
    
    buyCondition = ta.crossover(fastEMA, slowEMA)
    sellCondition = ta.crossunder(fastEMA, slowEMA)
    
    barcolor(
         buyCondition ? color.green :
         sellCondition ? color.red :
         na
    )

    In this example:

    -> Green candles indicate that the buy condition has been triggered.

    -> Red candles indicate that the sell condition has been triggered.

    -> If neither condition is true, the candle keeps its default chart color.

    You can also use any custom condition from your strategy. For example:

    longCondition = close > ta.ema(close, 50)
    shortCondition = close < ta.ema(close, 50)
    
    barcolor(
         longCondition ? color.lime :
         shortCondition ? color.orange :
         na
    )

    You are not limited to just buy and sell signals. Candle colors can also represent different market conditions, such as:

    -> Trend direction

    -> High volume candles

    -> Breakout candles

    -> Overbought or oversold conditions

    -> Volatility-based signals

    Using candle colors makes it much easier to analyze your strategy because important conditions become visible directly on the chart without relying only on labels or shapes.

    For more details about the available color functions and customization options, you can refer to the official Pine Script documentation:
    https://www.tradingview.com/pine-script-docs/

    So that’s how you can color candles dynamically based on your strategy conditions in Pine Script. This is the recommended way to make your trading strategy more visual and easier to analyze.

    Verified Solution
      Markdown supported · ```json for code blocks · paste or drop screenshots

      Be specific and kind. Never share API keys, passwords or personal account details.

      Welcome to the community

      Log in or create your account in under a minute.

      1. 1Mobile
      2. 2E-mail
      3. 3Password
      +91

      Have a password?

      By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.