1

Am getting an error on this code,it sucessfully compiled but on the indicator window,.it gives an error which reads

error on bar 0, loops takes too long to execute (>500ms).

I don't know how to go about it. Thanks in advance.


// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © felixxzz

//@version=5
indicator("Custom Indicator", overlay=false, max_bars_back = 5000)
import TradingView/ta/5
import mentalRock19315/NumberOfVisibleBars/3



len = input.int(15, title="Len")
historyBars = input.int(2345, title="HistoryBars")
urovenSignal = input.int(5, title="UrovenSignal")

tf1 =input.timeframe("", "Tf1")
tf2 = input.timeframe("", "Tf2")

alert = input(false, title="Alert")
sound = input(false, title="Sound")
gv = input(false, title="GV")

var float[] UP = array.new_float()
var float[] DN = array.new_float()
var float Clos = na
var float Hig = na
var float Loww = na
var int curBarIndex = na
var int barssCounter = na
var float Highest = na
var float Lowest = na
var float sumUpward = na 
var float sumDownward = na
var float sumHigh = na
var float sumLow = na
var int barSinceSignal = na
var ArrayClose = array.new_float(100000)
var ArrayLo = array.new_float(100000)
var ArrayHi = array.new_float(100000)

var int cnt = na
var int barsInTf2 =na
var int barsCounter2 =na

historyBars := math.round(historyBars / (time(tf2) / time(timeframe.period)))



barSinceSignal_value = ta.barssince(request.security(syminfo.tickerid, tf1, request.security(syminfo.tickerid,tf1, historyBars + len)))

barsinTf2_value = request.security(syminfo.tickerid,timeframe.period, request.security(syminfo.tickerid,tf2,historyBars + len))
Bars() =>
     barsInTf2 - (time(tf2) / time(timeframe.period))


closedValue() =>
    j = barSinceSignal
    closed_value = request.security(syminfo.tickerid, tf1,close[j])
highValue() =>
    j = barSinceSignal
    high_value = request.security(syminfo.tickerid, tf1,high[j])
lowValue() =>
    j = barSinceSignal
    low_value = request.security(syminfo.tickerid, tf1,low[j])

i = historyBars + len 
while i > 0
    cnt := 0
    j = barSinceSignal_value
    while   j > (barSinceSignal_value - time(tf2))  
        array.set(ArrayClose, i,closedValue())
        array.set(ArrayClose, cnt,closedValue())
        
        array.set(ArrayHi,i,highValue())
        array.set(ArrayHi,cnt,highValue())

        array.set(ArrayLo,i,lowValue())
        array.set(ArrayLo,cnt,lowValue())
        j := j + 1
    i := i - 1
    cnt := cnt - 1
closV() =>
    k = 0
    array.get(ArrayClose,curBarIndex)
    array.get (ArrayClose, k)
HigV() =>
    k = 0
    array.get(ArrayHi,curBarIndex)
    array.get (ArrayHi, k)
LoV() =>
    k = 0
    array.get(ArrayLo,curBarIndex)
    array.get (ArrayLo, k)
    

    

   


var int history_value = historyBars
i := historyBars
while i > 0
    barssCounter := 0
    sumUpward := 0
    sumDownward := 0
    Highest := 0
    Lowest := 1000000
    while barssCounter < len
        curBarIndex := i + barssCounter
        sumHigh := 0
        sumLow := 0
        var int k = 0
        
        while k < time(tf2)
            if closV() > 0.0
                Clos := closV()
            if HigV() > 0.0
                Hig := HigV()
            if LoV() > 0.0
                Loww := LoV()

            if (Hig > Highest)
                Highest := Hig
                sumHigh := sumHigh + Clos
            if (Loww < Lowest)
                Lowest := Loww
                sumLow := sumLow + Clos
            k := k + 1
        if (sumHigh > 0.0)
            sumUpward += sumHigh
        if (sumLow > 0.0)
            sumDownward += sumLow
        barssCounter := barssCounter + 1
    i := i - 1

if (sumUpward > 0.0 and sumDownward > 0.0)
    barsCounter2 := barsinTf2_value
    while barsCounter2 > Bars()
        array.set(UP, barsCounter2, sumUpward / sumDownward)
        array.set(DN, barsCounter2, sumDownward / sumUpward)
        barsCounter2 := barsCounter2 - 1


UP_value = array.get(UP, barsCounter2)
DN_value = array.get(DN, barsCounter2)
 

plot(UP_value, "Label1", color=color.green, linewidth=1)
plot(DN_value, "Label2", color=color.red, linewidth=1)

 


I don't know how to go about the error, I tried reading the debug error on trading view but I still can't comprehend it.

Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • Nested `while` loops is never a good idea in `pinescript`. Either try to `break` at some point or find a more optimum solution. – vitruvius Aug 24 '23 at 12:34
  • Thanks for the response @vitruvius .. by optimum solution can you give a suggestion with example. Thanks – felix Chidubem Aug 24 '23 at 13:07
  • [Here](https://www.pinecoders.com/faq_and_code/#how-can-i-count-the-occurrences-of-a-condition-in-the-last-x-bars) is an example. Method 3 is what you are trying (kind of) to do. You can try something like Method 2. – vitruvius Aug 24 '23 at 13:28

0 Answers0