Предупреждение Tradingview Pinescript не работает должным образом

Я использую pinescript ниже, и я не получаю предупреждений, как ожидалось. Я знаю, что это скрипт перерисовки. Он отлично работает с обратным тестированием. Длинное оповещение настроено на «Один раз на закрытие бара», а короткое оповещение установлено на «один раз на бар».

Неожиданное поведение: 1) Несколько раз я получаю короткое предупреждение, хотя соответствующего длинного предупреждения нет (хотя я позаботился в своем сценарии, короткое предупреждение будет отправлено только при наличии длинного). 2) Несколько последовательных коротких алертов на бар. Я знаю, что в баре реального времени короткое условие может быть истинным несколько раз. Но поскольку я установил оповещение «один раз за бар», оповещение должно появляться только в первый раз, когда короткое условие становится истинным.

Пожалуйста, дайте мне знать, если я делаю что-то не так?

Заранее спасибо.

//@version=4
study("My Script",overlay = true)

ST = input(true, title = "Activate Strategy Tester")
T_SY = input(2020, title = "Strategy Start Year")
T_SM = input(5, title = "Start Month")
T_SD = input(1, title = "Strategy Start Day")
T_EY = input(2025, title = "Strategy End Year")
T_EM = input(1, title = "End Month")
T_ED = input(1, title = "Strategy End Day")
T_S = timestamp(T_SY, T_SM, T_SD,00,00)
T_E = timestamp(T_EY, T_EM, T_ED,00,00)
T= ST and time >= T_S and time <= T_E 

firstrun = true
bought = false
longcondition = false
shortcondition = false

//Just to track first run

firstrun := firstrun[1]


if (firstrun == false)
    bought := bought[1]

//once condition is met, send a buy alert and make "bought" equal to true  //to enable selling

if (close <= 8600 and bought==false and T)
    bought := true
    longcondition :=true

alertcondition(longcondition,  "Long",  "Long")  

plotshape(longcondition,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.abovebar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)


if (longcondition)
    longcondition :=false

//once condition is met, sent a sell alert.

if (bought and close>=9000 and T)  
    shortcondition := true
    bought := false

alertcondition(shortcondition,  "short",  "short") 
plotshape(shortcondition,  title = "Sell",  text = 'Sell',  style = shape.labelup,   location = location.belowbar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)


if (shortcondition)
    shortcondition :=false

plotchar(bought, "bought", "", location = location.top)   

firstrun := false


person Raj    schedule 23.05.2020    source источник


Ответы (1)


Ваша переменная bought теперь автоматически сохраняет свое значение bar в bar, и я удалил поздние повторные инициализации переменных в вашем скрипте. Кажется, здесь все работает нормально:

//@version=4
study("My Script",overlay = true)

ST = input(true, title = "Activate Strategy Tester")
T_SY = input(2000, title = "Strategy Start Year")
T_SM = input(5, title = "Start Month")
T_SD = input(1, title = "Strategy Start Day")
T_EY = input(2025, title = "Strategy End Year")
T_EM = input(1, title = "End Month")
T_ED = input(1, title = "Strategy End Day")
T_S = timestamp(T_SY, T_SM, T_SD,00,00)
T_E = timestamp(T_EY, T_EM, T_ED,00,00)
T= ST and time >= T_S and time <= T_E 

var bought = false
longcondition = false
shortcondition = false

//once condition is met, send a buy alert and make "bought" equal to true  //to enable selling
if (close <= 8600 and bought==false and T)
    bought := true
    longcondition :=true

//once condition is met, sent a sell alert.
if (bought and close>=9000 and T)  
    shortcondition := true
    bought := false

alertcondition(longcondition,  "Long",  "Long")  
alertcondition(shortcondition,  "short",  "short") 
plotshape(longcondition,  title = "Buy",  text = 'Buy',  style = shape.labelup,   location = location.abovebar, color= color.green, textcolor = color.white, transp = 0, size = size.tiny)
plotshape(shortcondition,  title = "Sell",  text = 'Sell',  style = shape.labelup,   location = location.belowbar, color= color.red, textcolor = color.white, transp = 0, size = size.tiny)
// For debugging.
plotchar(bought, "bought", "•", location = location.top)

введите здесь описание изображения

person PineCoders-LucF    schedule 28.05.2020
comment
Спасибо. Позвольте мне проверить это - person Raj; 29.05.2020