i am currently struggling storing correct pivot highs and lows into variables. in the example screenshot i am showing pivot highs and lows with length 3. i want to store the latest pivot low into variable i.e. P1. now i want to look back on the chart and store the next pivot low which is lower than P1 into a variable P2, but i have no clue how to code that.
here is the example screenshot: example Screenshot Gold 1H Chart
Thanks in advance.
Edit: Here is my code so far:
//@version=4
study("Arrays Test, only Float Values", title = "Arrays Test, only Float Values", overlay=true)
PivotLevel = input(3)
pivotHigh = pivothigh(PivotLevel, PivotLevel)
pivotLow = pivotlow(PivotLevel, PivotLevel)
Array_Size = 10
var H_Array = array.new_float(Array_Size, na)
var L_Array = array.new_float(Array_Size, na)
if not na(pivotHigh)
array.push(H_Array, pivotHigh)
array.shift(H_Array)
if not na(pivotLow)
array.push(L_Array, pivotLow)
array.shift(L_Array)
CurrentPivotHigh = array.get(H_Array, Array_Size - 1)
CurrentPivotLow = array.get(L_Array, Array_Size - 1)
// Find the previous higher pivot high
var float lastPivotHigh = na
for i = 0 to Array_Size - 1
float pivot = array.get(H_Array, i)
if not na(pivot) and (na(lastPivotHigh) or pivot > CurrentPivotHigh)
lastPivotHigh := pivot
// Find the previous lower pivot low
var float lastPivotLow = na
for i = 0 to Array_Size - 1
float pivot = array.get(L_Array, i)
if not na(pivot) and (na(lastPivotLow) or pivot < CurrentPivotLow)
lastPivotLow := pivot
// For debugging, plot a label with arrays as string
if barstate.islast
label.new(
bar_index, 0,
"Array H: " + tostring(H_Array) +
"\nArray L: " + tostring(L_Array) +
"\nCurrent Pivot High: " + tostring(CurrentPivotHigh) +
" Current Pivot Low: " + tostring(CurrentPivotLow) +
"\nPrevious Higher Pivot High: " + tostring(lastPivotHigh) +
" Previous Lower Pivot Low: " + tostring(lastPivotLow),
color=color.blue, textcolor=color.black, style=label.style_label_down)
it stores the last 10 pivot Highs and lows into arrays and draws a label to see the current High/Low and the previous higher pivot high/lower pivot low. these would be the y coordinates for drawing a trendline. now i need the corresponding x coordinates from the arrays, but right now i can not get these values.
do i need to store these values in separate arrays(int instead of float), or can i get the corresponding bar_index from the existing array?
So, i have solved it with the following code:
//@version=4
study("Arrays Test, Float and Int Values", title = "Arrays Test, Float and Int Values")
PivotLevel = input(3)
pivotHigh = pivothigh(PivotLevel, PivotLevel)
pivotLow = pivotlow(PivotLevel, PivotLevel)
Array_Size = 10
var H_Array = array.new_float(Array_Size, na)
var L_Array = array.new_float(Array_Size, na)
var H_Index_Array = array.new_int(Array_Size, na)
var L_Index_Array = array.new_int(Array_Size, na)
if not na(pivotHigh)
array.push(H_Array, pivotHigh)
array.push(H_Index_Array, bar_index - PivotLevel)
array.shift(H_Array)
array.shift(H_Index_Array)
if not na(pivotLow)
array.push(L_Array, pivotLow)
array.push(L_Index_Array, bar_index - PivotLevel)
array.shift(L_Array)
array.shift(L_Index_Array)
CurrentPivotHigh = array.get(H_Array, Array_Size - 1)
CurrentPivotHighIndex = array.get(H_Index_Array, Array_Size - 1)
CurrentPivotLow = array.get(L_Array, Array_Size - 1)
CurrentPivotLowIndex = array.get(L_Index_Array, Array_Size - 1)
// Find the previous higher pivot high
var float lastPivotHigh = na
var int lastPivotHighIndex = na
for i = 0 to Array_Size - 1
float pivot = array.get(H_Array, i)
int pivotIndex = array.get(H_Index_Array, i)
if not na(pivot) and (na(lastPivotHigh) or pivot > CurrentPivotHigh)
lastPivotHigh := pivot
lastPivotHighIndex :=pivotIndex
// Find the previous lower pivot low
var float lastPivotLow = na
var int lastPivotLowIndex = na
for i = 0 to Array_Size - 1
float pivot = array.get(L_Array, i)
int pivotIndex = array.get(L_Index_Array, i)
if not na(pivot) and (na(lastPivotLow) or pivot < CurrentPivotLow)
lastPivotLow := pivot
lastPivotLowIndex := pivotIndex
// For debugging, plot a label with arrays as string
if barstate.islast
label.new(
bar_index, 0,
"Array H: " + tostring(H_Array) +
"\nArray L: " + tostring(L_Array) +
"\nCurrent Pivot High: " + tostring(CurrentPivotHigh) +
" Current Pivot Low: " + tostring(CurrentPivotLow) +
"\nCurrent Pivot High Index: " + tostring(CurrentPivotHighIndex) +
" Current Pivot Low Index: " + tostring(CurrentPivotLowIndex) +
"\nPrevious Higher Pivot High: " + tostring(lastPivotHigh) +
" Previous Lower Pivot Low: " + tostring(lastPivotLow) +
"\nPrevious Higher Pivot High Index: " + tostring(lastPivotHighIndex) +
" Previous Lower Pivot Low Index: " + tostring(lastPivotLowIndex),
color=color.blue, textcolor=color.black, style=label.style_label_down)