Error on bar 0: array.get() function, index -1 is out of bounds, array size is 0

30 views Asked by At
/ Define variables
var float target = na
var bool inLong = false
var float pip_distance_threshold = 0.03 / syminfo.mintick
var float tp = pip_distance_threshold / 2
var int count_of_trade = 0
var float entryPrice = na
var float stopLoss = na

// Arrays to store trade information
var float[] entryPrices = array.new_float(0)
var float[] stopLosses = array.new_float(0)
var float[] targets = array.new_float(0)

// Function to find the nearest demand zone
f_nearest_demand_zone(demand_zones) =>
    float nearest_demand_zone = na
    float min_distance = 10000000000.01
    for i = 0 to array.size(demand_zones) - 1
        top_price = box.get_top(array.get(demand_zones, i))
        bottom_price = box.get_bottom(array.get(demand_zones, i))
        demand_zone_price = (top_price + bottom_price) / 2
        if open >= demand_zone_price
            distance = open - demand_zone_price
            if distance < min_distance
                min_distance := distance
                nearest_demand_zone := demand_zone_price
    nearest_demand_zone

nearestDemandZone = f_nearest_demand_zone(current_demand_box)
distanceToDemandZone = na(nearestDemandZone) ? na : open - nearestDemandZone

buyCondition = tradeSignal and bullishCandle and not na(nearestDemandZone) and (distanceToDemandZone / syminfo.mintick <= pip_distance_threshold)
plotshape(buyCondition, title="buy", location=location.belowbar, color=color.green, style=shape.triangleup, text="confirm buy")

if (buyCondition and not inLong)
    entryPrice := close
    stopLoss := nearestDemandZone - (tp * syminfo.mintick)
    target := close + (close- (nearestDemandZone - (tp*syminfo.mintick)))
    inLong := true
    count_of_trade += 1
    array.push(entryPrices, entryPrice)
    array.push(stopLosses, stopLoss)
    array.push(targets, target)

switchCondition = high >= target

if (switchCondition and array.size(stopLosses) > 0 and array.size(targets) > 0)
    stopLoss := array.get(stopLosses, array.size(stopLosses) - 1)
    entryPrice := array.get(targets, array.size(targets) - 1)
    target := entryPrice + (entryPrice - stopLoss)
    array.set(stopLosses, array.size(stopLosses) - 1, stopLoss)
    array.set(targets, array.size(targets) - 1, target)

exitCondition = close <= array.get(stopLosses, count_of_trade - 1)

if (exitCondition and inLong and array.size(stopLosses) > 0)
    inLong := false
    var string exitText = "Exit Buy Position " + str.tostring(count_of_trade)
    label.new(x=bar_index, y=low, text=exitText, style=label.style_label_down, color=color.red, textcolor=color.white)
    // Clear arrays
    array.clear(entryPrices)
    array.clear(stopLosses)
    array.clear(targets)

This is the code that I'm pretty sure is throwing out the error.

Here, what im thinking of doing is storing the enters, stop losses and targets in an array and then use the values respectively during the strategy because I want to retain the previous values. I want the array to hold values when it is in a trade and then deplete when the exit conditions are met and the stop loss it met.

Basically, the trade will go like this. The stop loss is initialized and so is the target price of the candle where the buysignal is true and inLong is false for the first candle and the entry price (close) as well as the stop loss and target will be put into the array. then once the target is hit, I want to adjust the stop loss so that it is the price of the previous entry (pull that from the array) and the set the current entry to the previous target. The trade will still be going on. Then T want the target to be this current entry added to the difference between this current entry and the stop loss. I want to put all these values into the array again. This will repeat until the stop loss is hit and then a "exit buy position" will print and the array will deplete of all values and once a new buysignal is initiated the process will go again.

The problem is that now it is giving me an error after compiling the script on TradingView:

error on bar 0: array.get() function, index -1 is out of bounds, array size is 0

I am not able to figure out this because I think that I've declared the check of whether the size of the arrays is greater than 0, but it still gives the error.

0

There are 0 answers