Trevor Hammond
Trevor Hammond

Reputation: 31

alert, true or false, changes from true to false and false to true

TradingView PineScript code. Need an alert when it changes from true to false and false to true.

//@version=2
study

threshold = input(title="Threshold", type=float, defval=0.0014, 
step=0.0001)



buying = l3_0 > threshold ? true : l3_0 < -threshold ? false : buying[1]

hline(0, title="base line")
//bgcolor(l3_0 > 0.0014 ? green : l3_0 < -0.0014 ? red : gray, transp=20)
bgcolor(buying ? green : red, transp=20)
plot(l3_0, color=silver, style=area, transp=50)
plot(l3_0, color=aqua, title="prediction")

//longCondition = buying
//if (longCondition)
//    strategy.entry("Long", strategy.long)

//shortCondition = buying != true
//if (shortCondition)
//    strategy.entry("Short", strategy.short)

Have changed it to a study. As a strategy it gave an alert and plotted the direction on the chart. Changed it to a study but the alert is only supposed to go when it changes. On my study it alerts every candle and not on the change over. Anything I tried just gives a long or short on every candle.

Upvotes: 1

Views: 1698

Answers (1)

user10417531
user10417531

Reputation:

You can use the change() function to check if a variable has changed.

//@version=3
study("Custom alert condition", overlay=true)
my_variable = close > open

alertcondition(change(my_variable), title='Triggers when close is above the open or vice versa', message='Candle color changed!')
// this is here because a study chart requires at least one plot, bgcolor or barcolor call
// setting the bar color to na (i.e. N/A) does nothing
barcolor(na)

You can then go to the alarm clock icon in the upper right and create a new alert that uses this custom condition. There are options to trigger it once per minute, once per bar or other.


Here's a testing alert condition to see if you have created the alert correctly, set it to trigger every minute and it should trigger quickly.

//@version=3
study("Testing condition", overlay=true)
alertcondition(close, title='Triggers when close changes', message='Close has changed!')
barcolor(na)

Upvotes: 1

Related Questions