samedi 17 août 2019

ternary operator ?: is throwing errors about wrong arguments

I'm trying to display a different time frame macd on a given time frame chart. so display 5 min macd on 1 min chart etc.

I've decided to accomplish that by multiplying a number 5 to the interval which is an integer and then turn that into a string and use that in the plot. This works fine since I don;'t have to change it every time I change the time frame of the chart from 1 to 10 min etc, and it will still display the longer time frame macd based on the multiple.

This following code works fine using the ternary operator ?:

//@version = 2
study(title="test")

source = close
fastLength = input(12, minval=1)
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

// res5 mutiplies the current interval which is an integer by a factor 5 and turns it into a string with the value of "interval*5" or "1D" depending on the value of interval*5

res5= interval*5 < 1440 ? tostring(interval*5) : "1D"

src5=security(tickerid, res5, close)

fastMA5 = ema(src5, fastLength)
slowMA5 = ema(src5, slowLength)

macd5 = fastMA5 - slowMA5
signal5 = sma(macd5, signalLength)
outMacD5 = security(tickerid, res5, macd5)

plot( outMacD5 ? outMacD5 : na, color= red)

But if I were to change it to have more conditions like below, the ternary operator fails.

//@version = 2
study(title="test")

source = close
fastLength = input(12, minval=1)
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)

// res5 mutiplies the current interval which is an integer by a factor 5 and turns it into a string with the value of "interval*5" or "1D" depending on the value 9of inteval*5

//res5= interval*5 < 1440 ? tostring(interval*5) : "1D"

res5= interval*5 < 1440 ? tostring(interval*5) : interval >= 1440 and interval*5 < 2880 ? "1D":na

src5=security(tickerid, res5, close)

fastMA5 = ema(src5, fastLength)
slowMA5 = ema(src5, slowLength)

macd5 = fastMA5 - slowMA5
signal5 = sma(macd5, signalLength)
outMacD5 = security(tickerid, res5, macd5)

plot( outMacD5 ? outMacD5 : na, color= red)

That brings back the error

Add to Chart operation failed, reason: Error: Cannot call `operator ?:` with arguments (bool, literal__string, na); available overloads ...

Using the iff brings back the same error about the arguments being incorrect.

I could really use some help here. I'm so lost in using these conditional operators.

Any tips are helpful.

Aucun commentaire:

Enregistrer un commentaire