Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

pine script - "alertcondition" fails when the condition comes from a security function

I'm trying to create alerts based on conditions that derive from security functions.

Below, the relevant piece of the code, and here is the entire code.

s01 = input('BINANCE:BTCUSDT', type=input.symbol)
screenerFunc() => triggerA and triggerB
c01 = security(s01, res, screenerFunc())
alertcondition(condition=c01, title="Alert", message="Alert!")

But I'm getting the following error for the last line:

Cannot use a mutable variable as an argument of the security function.

Appreciate the help!

question from:https://stackoverflow.com/questions/65863553/alertcondition-fails-when-the-condition-comes-from-a-security-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There were two problems in the code, the first, the function screenerFunc must combine all the strings to calculate the condition, the second, in calculating the range. Strings with drawing lines are also commented out, since I don't know for which ticker they are relevant.

//@version=4
//42quants.com
//@42piratas

study("Grid Bots Screening", overlay=true)

// INPUTS

res = input(type=input.resolution, defval="60", title="Resolution")
lookBack = input(title="Lookback", type=input.integer, defval=24, minval=2)
range = input(title="Upper & Lower Range %", type=input.integer, defval=25, minval=10)

screenList  = input(false, "═════════ Screening ════════")
s01 = input('BINANCE:BTCUSDT', type=input.symbol)
s02 = input('BINANCE:ETHBTC', type=input.symbol)
s03 = input('BINANCE:XRPBTC', type=input.symbol)
s04 = input('BINANCE:LTCBTC', type=input.symbol)
s05 = input('BINANCE:BCHBTC', type=input.symbol)

s06 = input('BINANCE:LINKBTC', type=input.symbol)
s07 = input('BINANCE:ADABTC', type=input.symbol)
s08 = input('BINANCE:DOTBTC', type=input.symbol)
s09 = input('BINANCE:BNBBTC', type=input.symbol)
s10 = input('BINANCE:XLMBTC', type=input.symbol)

s11 = input('BINANCE:SNXBTC', type=input.symbol)
s12 = input('BINANCE:XMRBTC', type=input.symbol)
s13 = input('BINANCE:EOSBTC', type=input.symbol)
s14 = input('BINANCE:XEMBTC', type=input.symbol)
s15 = input('BINANCE:WBTCBTC', type=input.symbol)

s16 = input('BINANCE:TRXBTC', type=input.symbol)
s17 = input('BINANCE:XTZBTC', type=input.symbol)
s18 = input('BINANCE:COMPBTC', type=input.symbol)
s19 = input('BINANCE:FILBTC', type=input.symbol)
s20 = input('BINANCE:MKRBTC', type=input.symbol)

// INDICATORS & VARIABLES

var upperLimitLine = line.new(na, na, na, na, color=color.red,  width= 3, extend=extend.none)
var lowerLimitLine = line.new(na, na, na, na, color=color.black,  width= 3, extend=extend.none)
var upperRangeLine = line.new(na, na, na, na, color=color.blue, width= 2, extend=extend.none)
var lowerRangeLine = line.new(na, na, na, na, color=color.green, width= 2, extend=extend.none)

var range_corr = 100/range // the error was here

screenerFunc() => 
    
    highestHigh = highest(high, lookBack)
    lowestLow = lowest(low, lookBack)
    
    xAxisStartsAt = bar_index[lookBack]
    xAxisFinishesAt = bar_index
    
    upperLimit = highestHigh
    lowerLimit = lowestLow
    
    upperRange = highestHigh - ((highestHigh - lowestLow)/range_corr) 
    lowerRange = ((highestHigh - lowestLow)/range_corr) + lowestLow
    
    HighAboveUpperRange = high > upperRange
    LowBelowLowerRange = low < lowerRange
    
    occurrencesAboveTotal   = sum(HighAboveUpperRange ? 1 : 0, lookBack)
    occurrencesAboveSecondHalf = sum(HighAboveUpperRange ? 1 : 0, lookBack/2)
    occurrencesAboveFirstHalf  = occurrencesAboveTotal - occurrencesAboveSecondHalf
    
    occurrencesBelowTotal   = sum(LowBelowLowerRange ? 1 : 0, lookBack)
    occurrencesBelowSecondHalf = sum(LowBelowLowerRange ? 1 : 0, lookBack/2)
    occurrencesBelowFirstHalf  = occurrencesBelowTotal - occurrencesBelowSecondHalf
    
    // SCREENING
    
    triggerA = occurrencesAboveFirstHalf >= 1 ? true : false
    triggerB = occurrencesAboveSecondHalf >= 1 ? true : false
    triggerC = occurrencesBelowFirstHalf >= 1 ? true : false
    triggerD = crossunder(low, lowerRange)
    
    triggerA and triggerB and triggerC and triggerD

c01 = security(s01, res, screenerFunc())
// c02 = security(s02, res, screenerFunc())
// c03 = security(s03, res, screenerFunc())
// c04 = security(s04, res, screenerFunc())
// c05 = security(s05, res, screenerFunc())

// c06 = security(s06, res, screenerFunc())
// c07 = security(s07, res, screenerFunc())
// c08 = security(s08, res, screenerFunc())
// c09 = security(s09, res, screenerFunc())
// c10 = security(s10, res, screenerFunc())

// c11 = security(s11, res, screenerFunc())
// c12 = security(s12, res, screenerFunc())
// c13 = security(s13, res, screenerFunc())
// c14 = security(s14, res, screenerFunc())
// c15 = security(s15, res, screenerFunc())

// c16 = security(s16, res, screenerFunc())
// c17 = security(s17, res, screenerFunc())
// c18 = security(s18, res, screenerFunc())
// c19 = security(s19, res, screenerFunc())
// c20 = security(s20, res, screenerFunc())

// PAINTBRUSH

// line.set_xy1(upperLimitLine, xAxisStartsAt, upperLimit)
// line.set_xy2(upperLimitLine, xAxisFinishesAt, upperLimit)

// line.set_xy1(lowerLimitLine, xAxisStartsAt, lowerLimit)
// line.set_xy2(lowerLimitLine, xAxisFinishesAt, lowerLimit)

// line.set_xy1(upperRangeLine, xAxisStartsAt, upperRange)
// line.set_xy2(upperRangeLine, xAxisFinishesAt, upperRange)

// line.set_xy1(lowerRangeLine, xAxisStartsAt, lowerRange)
// line.set_xy2(lowerRangeLine, xAxisFinishesAt, lowerRange)

plotchar(screenerFunc())

// ALERTS
alertcondition(c01, title="Alert", message="Alert!")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...