1

I am trying a fib level based trailing stop and trailing target strategy.i want the strategy to check and recalculate the trailing stop and target after close of each candle. for long trades the fibonachi retracement levels above will be target and below will the stoploss on trailing basis. for short trades it will be the opposite. the positions need to calculated in there is an open trade position(long or short)

i had initially tried using if instead of while statement, however i was not able to make the array sort after each candle and set trail dynamically. after the below code is input i am getting study error "loop is taking too long to execute" I am giving below my code. please advice as to how to clear the error.

//fib level based array

var fiblevel= array.from(l5_dn, l4_dn,l3_dn,l2_dn, l1_dn,lvl0, lvl1,l1_up, l2_up,l3_up,l4_up,l5_up)

t1= close[1] 
var float TTG =0 //trailing target 
var float TSL =0 //trailig stop

while strategy.opentrades!=0
    z= array.copy(fiblevel)
    array.push(z,t1)
    array.sort(z)
    j= array.indexof(z,t1)

    for i = 0 to j-1 [1]
        if t1 == array.get(z,i)
            if i==0
                TSL := low[1]
                if i==j-1
                    TSL := array.get(z, i-1)
                    if i!=0 and 1!= j-1
                        TTG := array.get(z,i+1)
                        TSL := array.get(z, i-1)
        break
    continue
plot(TTG)
plot(TSL)

0 Answers0