Условная строковая переменная Tradingview

#pine-script

Вопрос:

У меня есть предупреждение индикатора, которое срабатывает при нескольких OR условиях. Предупреждающее сообщение должно быть динамичным и соответствовать тому, какое условие было выполнено. Можно ли создать условную строковую переменную с помощью IF скрипта Pine?

Например:

 Stringvar = ""
Stringvar = if (setupLong > 0 and setupShort == 0 and perfectSetup and setupCount == 9)
     "Bullish Perfect 9"
else (setupLong > 0 and setupShort == 0 and not perfectSetup and setupCount == 9)
     "Bullish IMP9",
else (setupLong > 0 and setupShort == 0 and setupCount == 13)
     "Bullish Extended 13",
else (setupShort > 0 and setupLong == 0 and not perfectSetup and setupCount == 9)
     "Bearish IMP9",
else (setupShort > 0 and setupLong == 0 and perfectSetup and setupCount == 9)
     "Bearish Perfect 9",
else (setupShort > 0 and setupLong == 0 and setupCount == 13)
     "Bearish Extended 13"
 

Это не работает, но логика этого является основной предпосылкой.

Ответ №1:

Вы можете использовать условные операторы ( condition ? resultIfTrue : resultIfFalse ). Это выглядело бы примерно так:

 stringVar = (setupLong > 0 and setupShort == 0 and perfectSetup and setupCount == 9) ? "Bullish Perfect 9" 
          : (setupLong > 0 and setupShort == 0 and not perfectSetup and setupCount == 9) ? "Bullish IMP9" 
          : (setupLong > 0 and setupShort == 0 and setupCount == 13) ? "Bullish Extended 13" 
          : (setupShort > 0 and setupLong == 0 and not perfectSetup and setupCount == 9) ? "Bearish IMP9" 
          : (setupShort > 0 and setupLong == 0 and perfectSetup and setupCount == 9) ? "Bearish Perfect 9" 
          : (setupShort > 0 and setupLong == 0 and setupCount == 13) ? "Bearish Extended 13" 
          : na
 

ИЛИ используйте оператор := , чтобы придать новое значение переменной.

 stringVar = ""

if (setupLong > 0 and setupShort == 0 and perfectSetup and setupCount == 9)
     stringVar := "Bullish Perfect 9"
if (setupLong > 0 and setupShort == 0 and not perfectSetup and setupCount == 9)
     stringVar := "Bullish IMP9"
and so on...
 

Редактировать:

 if (setupLong > 0 and setupShort == 0 and perfectSetup and showSetup and setupCount == 9) or (setupShort > 0 and setupLong == 0 and perfectSetup and showSetup and setupCount == 9) or (setupLong > 0 and setupShort == 0 and setupCount == 13) or (setupShort > 0 and setupLong == 0 and setupCount == 13)
     alert(stringVar)
 

Комментарии:

1.Спасибо за ответ. Я использовал 1-ю логику, и она работает. Но когда я подключаю его к условию оповещения, я получаю сообщение об ошибке. Не alertcondition((setupLong > 0 and setupShort == 0 and perfectSetup and showSetup and setupCount == 9) or (setupShort > 0 and setupLong == 0 and perfectSetup and showSetup and setupCount == 9) or (setupLong > 0 and setupShort == 0 and setupCount == 13) or (setupShort > 0 and setupLong == 0 and setupCount == 13), title="TD Setup Count", message=stringVar) удается вызвать «условие оповещения» с помощью «сообщение» =серия[строка]. Аргумент должен иметь тип: строка const

2. Вместо этого вы должны использовать функцию alert (), которая разрешает последовательные строки.

3. предупреждение() , к сожалению, не тот вариант использования, который я ищу. 🙁

4. Невозможно использовать динамические предупреждающие сообщения с помощью alertcondition().

5. Если вы когда-нибудь решите использовать функцию alert (), вы можете использовать код, который я добавил в свой ответ, который я только что отредактировал.