ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • pinescript: arrays
    개발 2023. 6. 7. 23:30

    array 선언

    <type>[] <identifier> = <expression>
    var <type>[] <identifier> = <expression>
    array<type> <identifier> = <expression>
    var array<type> <identifier> = <expression>

    array with 'var' keyword

    //@version=5
    indicator("Using `var`")
    var a = array.new_float(0)
    array.push(a, close)
    if barstate.islast
        label.new(bar_index, 0, "Array size: " + str.tostring(array.size(a)) + "\nbar_index: " + str.tostring(bar_index), size = size.large)

    'var' with constant array

    var int[] lengths = array.from(2, 12, 20, 50, 100, 200)
    

    using 'var' is more efficient as it avoids unnecessary computation but it still works.

    //@version=5
    indicator("Distance from high", "", true)
    lookbackInput = input.int(100)
    FILL_COLOR = color.green
    // Declare array and set its values on the first bar only.
    var fillColors = array.new_color(5)
    if barstate.isfirst
        // Initialize the array elements with progressively lighter shades of the fill color.
        array.set(fillColors, 0, color.new(FILL_COLOR, 70))
        array.set(fillColors, 1, color.new(FILL_COLOR, 75))
        array.set(fillColors, 2, color.new(FILL_COLOR, 80))
        array.set(fillColors, 3, color.new(FILL_COLOR, 85))
        array.set(fillColors, 4, color.new(FILL_COLOR, 90))
    
    // Find the offset to highest high. Change its sign because the function returns a negative value.
    lastHiBar = - ta.highestbars(high, lookbackInput)
    // Convert the offset to an array index, capping it to 4 to avoid a runtime error.
    // The index used by `array.get()` will be the equivalent of `floor(fillNo)`.
    fillNo = math.min(lastHiBar / (lookbackInput / 5), 4)
    // Set background to a progressively lighter fill with increasing distance from location of highest high.
    bgcolor(array.get(fillColors, fillNo))
    // Plot key values to the Data Window for debugging.
    plotchar(lastHiBar, "lastHiBar", "", location.top, size = size.tiny)
    plotchar(fillNo, "fillNo", "", location.top, size = size.tiny)

    array.set(id,index,value) : set a value at a specific index in an array

    array.get(id,index): retrieve the value at a specific index

     

    id: identifier of the array

    index: position in the array

     

     

    array를 0으로 declare하고 array.push()로 하나씩 집어넣기

    // Declare array and set its values on the first bar only.
    var fillColors = array.new_color(0)
    if barstate.isfirst
        // Initialize the array elements with progressively lighter shades of the fill color.
        array.push(fillColors, color.new(FILL_COLOR, 70))
        array.push(fillColors, color.new(FILL_COLOR, 75))
        array.push(fillColors, color.new(FILL_COLOR, 80))
        array.push(fillColors, color.new(FILL_COLOR, 85))
        array.push(fillColors, color.new(FILL_COLOR, 90))

    array.push(): adds a new element at the end of the array

     

    array.from(): array선언할 때 모든 value를 알고 있다면 사용하기 좋음

    //@version=5
    indicator("Using `var`")
    FILL_COLOR = color.green
    var color[] fillColors =
      array.from(
       color.new(FILL_COLOR, 70),
       color.new(FILL_COLOR, 75),
       color.new(FILL_COLOR, 80),
       color.new(FILL_COLOR, 85),
       color.new(FILL_COLOR, 90))
    // Cycle background through the array's colors.
    bgcolor(array.get(fillColors, bar_index % array.size(fillColors)))
     

     

    '개발' 카테고리의 다른 글

    pinescript: bar plotting  (0) 2023.06.27
    pinescript: bar coloring  (0) 2023.06.26
    pinescript: loops  (0) 2023.06.06
    pinescript: conditional structures  (0) 2023.06.05
    pinescript: variable declaration  (0) 2023.06.04
Designed by Tistory.