0

In the Pine script reference manual the is a code snippet on https://www.tradingview.com/pine-script-docs/en/v5/language/Objects.html

I want to know how to access the values of the array pivotHighArray. This is because I don't want to draw a line only between the last pivot high and the previous pivot high. I want to draw a line/lines from the current pivot high to ANY previous pivot highs that meet certain conditions (e.g. previous pivot high must be higher than the current).

//@version=5
indicator("Pivot Points High", overlay = true)

int legsInput = input(10)

// Define the `pivotPoint` UDT containing the time and price of pivots.
type pivotPoint
    int openTime
    float level

// Create an empty `pivotPoint` array.
var pivotHighArray = array.new<pivotPoint>()

// Detect new pivots (`na` is returned when no pivot is found).
pivotHighPrice = ta.pivothigh(legsInput, legsInput)

// Add a new `pivotPoint` object to the end of the array for each detected pivot.
if not na(pivotHighPrice)
    // A new pivot is found; create a new object of `pivotPoint` type, setting its `openTime` and `level` fields.
    newPivot = pivotPoint.new(time[legsInput], pivotHighPrice)
    // Add the new pivot object to the array.
    array.push(pivotHighArray, newPivot)

// On the last historical bar, draw pivot labels and connecting lines.
if barstate.islastconfirmedhistory
    var pivotPoint previousPoint = na
    for eachPivot in pivotHighArray
        // Display a label at the pivot point.
        label.new(eachPivot.openTime, eachPivot.level, str.tostring(eachPivot.level, format.mintick), xloc.bar_time, textcolor = color.white)
        // Create a line between pivots.
        if not na(previousPoint)
            // Only create a line starting at the loop's second iteration because lines connect two pivots.
            line.new(previousPoint.openTime, previousPoint.level, eachPivot.openTime, eachPivot.level, xloc = xloc.bar_time)
        // Save the pivot for use in the next iteration.
        previousPoint := eachPivot

I have tried pivotHighArray.level[someindex], array.get(pivotHighArray.level, someindex) but failed. How do I reference these values? This question is related to my old question which I'm now trying another approach on since it fails when a new bar is printed both live and during backtesting

1 Answers1

0

You're on the right track. Try:

pivotPoint t = na
if 0 < array.size(pivotHighArray)
    t := array.get(pivotHighArray, 0)
plot(na(t) ? na : t.level, color = color.red)

First you just read the array element, after that you can access its properties.

elod008
  • 1,227
  • 1
  • 5
  • 15
  • Thanks, it retrieves the value of the very first index in the pivotHighArray. But how do I get the N:th index? – Dennis Nilsson Dec 19 '22 at 08:48
  • You can very well do that in your loop. The solution I provided is a general one for the general question. In a loop replace the 0 by the current iteration index. – elod008 Dec 19 '22 at 12:43
  • Not sure I fully understand. I create the for loop with: `size = array.size(pivotHighArray) pivotPoint t = na if 0 < size for cnt = 0 to size t := array.get(pivotHighArray, cnt)` When I try to find the value with `plot(na(t) ? na : t.level, color = color.red)` will give me an error or `plot(array.get(t.level,0), "pivot", display = display.data_window)` will also give an error – Dennis Nilsson Dec 19 '22 at 16:24