Reputation: 1
I'm trying to write an indicator whose purpose is to identify support levels based on fractals. This code shows conditions only for lower fractals (to make it easier to navigate the code).
The main problem is the for loop - it constantly produces an error: Error on bar 6: In 'array.get() function. Index -1 is out of bounds, array size is 1.
I tried to iterate through the values in a for loop, but after numerous attempts, it seems to me that I do not understand the logic of the loop working with arrays of data. Details are written in the code itself in the form of comments.
//@version=5
indicator('Williams Fractals', shorttitle='WICK.ED Fractals', overlay=true, max_bars_back = 100)
n = 2
var int[] barIndexes = array.new_int()
upFractal = low[n - 1] > low[n] and low[n + 1] > low[n]
if upFractal
array.push(barIndexes, bar_index)
f_indexes(barIndexes) =>
if array.size(barIndexes) > 0
for i = array.size(barIndexes) - 1 to 0 by 1 //The purpose of this cycle: Iterate through the indices of bars that meet the upFractal condition.
PreviousBarIndex = array.get(barIndexes, i - 1) //PreviousBarIndex is the index of every first bar (if you read the chart from left to right).
CurrentBarIndex = array.get(barIndexes, i) //CurrentBarIndex is the index of every second bar (if you read the chart from left to right).
PreviousBarLow = low[PreviousBarIndex]
CurrentBarClose = close[CurrentBarIndex]
CurrentBarOpen = open[CurrentBarIndex]
if CurrentBarOpen > CurrentBarClose //If open is greater than close, then it is a red candle - the logic is written for it.
isPreviousBarOpenLesser = PreviousBarLow > CurrentBarClose //If Previous Low is greater than Current Close, then the price has “broken” the previous fractal
if isPreviousBarOpenLesser
label.new(bar_index[n], low, text="yes", yloc=yloc.belowbar) //If the above condition is met, mark where exactly
if array.size(barIndexes) > -100 //an attempt to limit the maximum number of array elements to avoid errors
array.shift(barIndexes)
f_indexes(barIndexes)
Upvotes: 0
Views: 342
Reputation: 1961
for i = array.size(barIndexes) - 1 to 0 by 1 //The purpose of this cycle: Iterate through the indices of bars that meet the upFractal condition.
PreviousBarIndex = array.get(barIndexes, i - 1) //PreviousBarIndex is the index of every first bar (if you read the chart from left to right).
when i = 0 PreviousBarIndex = array.get(barIndexes, i - 1)
points to -1 element which is not exists
Upvotes: 1