0

Hi guys can someone help me to understand what is wrong with this code ? I keep receiving the msg syntax error at input barcolor

Here's the code

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

indicator(title="bar color", overlay=true)

fastMA = ta.sma(close,8)
slowMA = ta.sma(close,200)

plot(fastMA, color=color.blue)
plot(slowMA, color=color.red)

if  fastMA > slowMA
   barcolor(close > close[10] ? color.yellow : color.white)

if  fastMA < slowMA  
   barcolor(close < close [10] ? color.green : color.black)
2
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Oct 26, 2024 at 1:17
  • 2
    This is the second time I've corrected the formatting of your post. If you make another edit, format the code properly so that we don't have to do it for you again. Also, when you get an error message, it's better to include the complete, exact error message in your post - even if it makes no sense to you, it has information that can be useful to others. Commented Oct 26, 2024 at 2:47

1 Answer 1

0

You cannot use functions like barcolor in the local scope (e.g. within an if block).

You can set your color with your conditions and then call barcolor() in the global scope.

color my_color = na

if  fastMA > slowMA
    my_color := (close > close[10]) ? color.yellow : color.white

if  fastMA < slowMA  
    my_color := (close < close [10]) ? color.green : color.black

barcolor(my_color)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.