Home > Software engineering >  Why does this conditional statement return with the undeclared identifier error for ColorA?
Why does this conditional statement return with the undeclared identifier error for ColorA?

Time:09-02

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)")

CodePudding user response:

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

  • Related