Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this conditional statement return with the undeclared identifier error for ColorA?

This is an extract from the code that is specifically causing me problems. Please bear in mind that this for Pinescript.

The aim is for both of the moving averages that I’m coding to be green when they both have a positive slope and be red when they both have a negative slope, but stay silver when neither of those conditions are met

if positiveSlopeA and positiveSlopeB
        colorA = color.green
    else if negativeSlopeA and negativeSlopeB
        colorA = color.red
    else 
        colorA = color.silver
    
    
    
    
    plot(outA, color=colorA, title="SMA(15)")
    plot(outB, color=colorA, title="SMA(30)")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to use the := assignment operator when assigning values to already defined variables.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius

//@version=5
indicator("My script")

outA = ta.sma(close, 15)
outB = ta.sma(close, 30)

positiveSlopeA = outA > outA[1]
positiveSlopeB = outB > outB[1]
negativeSlopeA = outA < outA[1]
negativeSlopeB = outB < outB[1]

colorA = color.blue

if positiveSlopeA and positiveSlopeB
    colorA := color.green
else if negativeSlopeA and negativeSlopeB
    colorA := color.red
else 
    colorA := color.silver
    
plot(outA, color=colorA, title="SMA(15)")
plot(outB, color=colorA, title="SMA(30)")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading