admin管理员组

文章数量:1123192

I'm trying to write an indicator in pine-script based on the supertrend indicator. The supertrend indicator has 2 variables (Up Trend and Down Trend) that we can see in the "data window" panel. when in uptrend, the "Up trend" is set and "Down trend" is not set. In downtrend, it's the other way around. The current alert condition do half of what I want. Right now I can trigger an alert when the stock switches to uptrend. But I want to add the following criteria : I want the closing price to not be more than 10% away from the "Up trend" value shown in the data window. How would you do add this criteria ? thanks

here is my script :

//@version=5
indicator("BEN - Supertrend", overlay = true, timeframe = "", timeframe_gaps = true)

atrPeriod = input.int(30,    "ATR Length", minval = 1)
factor =    input.float(6.0, "Factor",     minval = 0.01, step = 0.01)

[supertrend, direction] = ta.supertrend(factor, atrPeriod)

supertrend := barstate.isfirst ? na : supertrend
upTrend =    plot(direction < 0 ? supertrend : na, "Up Trend",   color = color.green, style = plot.style_linebr)
downTrend =  plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red,   style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)

fill(bodyMiddle, upTrend,   color.new(color.green, 90), fillgaps = false)
fill(bodyMiddle, downTrend, color.new(color.red,   90), fillgaps = false)

alertcondition(direction[1] > direction, title='Downtrend to Uptrend', message='The Supertrend value switched from Downtrend to Uptrend ')

I tried adding this criteria :

alertcondition((close / upTrend) > 1.1 , title='Stop Loss', message='')

but it fails with the following error :

Cannot call 'operator /' with argument 'expr1'='upTrend'. An argument of 'plot' type was used but a 'series float' is expected.

I'm trying to write an indicator in pine-script based on the supertrend indicator. The supertrend indicator has 2 variables (Up Trend and Down Trend) that we can see in the "data window" panel. when in uptrend, the "Up trend" is set and "Down trend" is not set. In downtrend, it's the other way around. The current alert condition do half of what I want. Right now I can trigger an alert when the stock switches to uptrend. But I want to add the following criteria : I want the closing price to not be more than 10% away from the "Up trend" value shown in the data window. How would you do add this criteria ? thanks

here is my script :

//@version=5
indicator("BEN - Supertrend", overlay = true, timeframe = "", timeframe_gaps = true)

atrPeriod = input.int(30,    "ATR Length", minval = 1)
factor =    input.float(6.0, "Factor",     minval = 0.01, step = 0.01)

[supertrend, direction] = ta.supertrend(factor, atrPeriod)

supertrend := barstate.isfirst ? na : supertrend
upTrend =    plot(direction < 0 ? supertrend : na, "Up Trend",   color = color.green, style = plot.style_linebr)
downTrend =  plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red,   style = plot.style_linebr)
bodyMiddle = plot(barstate.isfirst ? na : (open + close) / 2, "Body Middle",display = display.none)

fill(bodyMiddle, upTrend,   color.new(color.green, 90), fillgaps = false)
fill(bodyMiddle, downTrend, color.new(color.red,   90), fillgaps = false)

alertcondition(direction[1] > direction, title='Downtrend to Uptrend', message='The Supertrend value switched from Downtrend to Uptrend ')

I tried adding this criteria :

alertcondition((close / upTrend) > 1.1 , title='Stop Loss', message='')

but it fails with the following error :

Cannot call 'operator /' with argument 'expr1'='upTrend'. An argument of 'plot' type was used but a 'series float' is expected.
Share Improve this question asked 7 hours ago benben 11 bronze badge New contributor ben is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

for debug, plot the line 10% to upTrend

upTrendLine = direction < 0 ? supertrend : na
upTrend10 = upTrendLine * 1.1
plot(upTrend10)

Now create the alert

croosUpTrend10 = ta.crossunder(low, upTrend10)
plotshape(croosUpTrend10)
alertcondition(croosUpTrend10, "croosUpTrend10" )

本文标签: pine script v5adding an alertcondition on an indicatorStack Overflow