Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

Color a single bar based on MA color change

Jul 17, 2021 - 2:08 PM

Viewed 786 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/25110 COPY
  • Hi, I'm new to Pine Script and learning to write some simple codes. I've made a HMA script that changes color based on direction. Here's the code:

    study(title=HMA, overlay=true)
    src = input(close, title=Source)
    len = input(20, title=HMA Length)
    hma1 = hma(src, len)
    hmacolor = change(hma1)>=0 ? color.green : color.red
    plot(hma1, color=hmacolor, title=HMA)

    I'm able to change the series of bar color using the following code:

    barcolor(change(hma1)>=0 ? color.green : color.red)

    but what i'd like instead, is to be able to change just a single bar's color(as a signal) when the direction/color of HMA changes.

    Any help will be greatly appreciated.

    Thanks

    0
  • Try this

    study(title=HMA, overlay=true)
    src = input(close, title=Source)
    len = input(20, title=HMA Length)
    hma1 = hma(src, len)
    hmacolor = change(hma1)>=0 ? color.green : color.red
    plot(hma1, color=hmacolor, title=HMA)
    
    condition = crossover(hma1, 0) ? color.green : crossunder(hma1, 0) ? color.red : na
    barcolor(condition)
    
    This post was edited Jul 21, 2021 04:36PM
    0
CONTENTS