Trendoscope

Thinking in Pine - Functions Containing Var Variables

교육
BINANCE:BTCUSDT   Bitcoin / TetherUS
Hello everyone, welcome back to "Thinking in Pine" short video tutorials. In this video, we have discussed special cases of using var variables inside function definitions.

If you are not familiar with var variables, please take a step back and watch our earlier video - "Thinking in Pine - var, varip and regular variables"

🎲 Summary
  • Using var within a function scope and how it behaves with multiple invocations.
  • Using the functions containing var variable definitions within a loop.

🎯 Example Program Used
increment()=>
    var i = 0
    i+=1

var1 = increment()
var2 = increment()
var3 = increment()

// The code above is equivalent to
// var i1 = 0
// i1+=1
// var1 = i1

// var i2 = 0
// i2+=1
// var2 = i2

// var i3 = 0
// i3+=1
// var3 = i3

plot(var1, "Counter 1", color=color.blue)
plot(var2, "Counter 2", color=color.red)
plot(var3, "Counter 3", color=color.purple)

arr = array.from(var1, var2, var3)
for i=1 to 3
    arr.push(increment())
    // The code above is equivalent to
    // var i4 = 0
    // i4+=1
    // arr.push(i4)

if(bar_index == 4)
    log.info('Value of array containing incremental values : {0}', arr)

🎲 References

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.