0

I'm trying to push cumDistance into cumDistances array for every iteration of the for loop but I am having trouble.

var inTrade = false
var entry = 0.
var exit = 0.
distance = 0.

entryCon = ta.crossover(ta.rsi(close, 10), 20) and not inTrade
exitCon = ta.crossunder(ta.rsi(close, 10), 80) and inTrade

cumDistances = array.new_float()
for i=1 to 5
    if entryCon[1]
        entry := close[1]
        inTrade := true

    if exitCon[1]
        exit := close[1]
        inTrade := false
        distance := exit - entry*i

    cumDistance = ta.cum(distance)
    array.push(cumDistances, cumDistance)

The values inside the cumDistances array that I get are all the same:

  1. -43.41588
  2. -43.41588
  3. -43.41588
  4. -43.41588
  5. -43.41588

.

if entryCon[1]
    entry := close[1]
    inTrade := true

if exitCon[1]
    exit := close[1]
    inTrade := false
    distance := exit - entry*i //changed i to 1,2,3,4,5

cumDistance = ta.cum(distance)

If I run the code without for loops(like the code just above), these are the values of cumDistance I get for their respective iteration.

  1. 0.02016
  2. -10.83885
  3. -21.69786
  4. -32.55687
  5. -43.41588

It seems that the array cumDistances only saves the value of cumDistance for the last iteration of the for loop to all index of the array.

How do I fix this?

EDIT: Response to elod008:

cumDistances = array.new_float()
for i=1 to 5
    var distances = array.new_float()
    if entryCon[1]
        entry := close[1]
        inTrade := true

    if exitCon[1]
        exit := close[1]
        inTrade := false
        distance := exit - entry*i
        array.push(distances, distance)


    cumDistance = array.sum(distances)
    array.push(cumDistances, cumDistance)
    //array.clear(distances) <---- placing this here, I will get NaN,NaN,NaN,NaN,Nan as result

plot(array.size(cumDistances), "no. of index", display=display.data_window)

table1 = table.new(position.top_right, 10, 10)
table.cell(table1, 0, 0, str.tostring(array.get(cumDistances, 0)) + ", "
                         + str.tostring(array.get(cumDistances, 1)) + ", "
                         + str.tostring(array.get(cumDistances, 2)) + ", "
                         + str.tostring(array.get(cumDistances, 3)) + ", "
                         + str.tostring(array.get(cumDistances, 4)))
bbnm
  • 1,034
  • 1
  • 4
  • 14

2 Answers2

1

I ran your code. There seems to be no error in the calculation. What you're experiencing is that ta.cum() works on a series float and sums for you the absolute total as it is supposed to.

However if your conditional that adds new values to the array is false in some iterations, ta.cum() receives n times the default 0.0 of distance, what does not meet your expectations. Your array gets pushed the series cumDistance altered by the built-in function then.

I would not recommend using ta.cum() in this case. I'd rather collect and sum up the relevant results in a var variable/object/matrix etc. depending on your needs.

elod008
  • 1,227
  • 1
  • 5
  • 15
  • I made a var distances array and used array.sum() on that array and pushed it inside cumDistances. I still get same answers for the same every index. I edited my post, added the new code. – bbnm Jul 01 '23 at 16:05
  • It's because you have apparently 5+ bars until your exit condition meets. That was the problem with the 1st ta.cum() version too. If on a bar no exit condition meets, your distances array remains the same resulting in the same "sum" result until you update the array. Use label.new(bar_index, high, str.tostring(cumDistances)) instead of table. You'll see that if exit condition meets the result will change. But if it does not, you'll get repetative values. – elod008 Jul 02 '23 at 07:24
  • Thank you! I can get what I need now thanks to you, just need to tweak it a little bit more. Really appreciate the help, almost gave up on this one hahaha – bbnm Jul 02 '23 at 09:05
0

You declare both entry and exit as close[1], which means that you just do the same calculation five times and push it into array (also five times). I assume you want to cycle over the last 5 bars; in this case, you need to change close[1] to close[i] so that on each loop iteration, it requests the close value farther into the past.

beeholder
  • 1,444
  • 1
  • 3
  • 6
  • Im essentially trying to backtest, expecting a different cumulative distance (cumDistance) for every iteration of the for loop. It is my intention for it to do the same calculation five times and push cumDistance into array. Except I am expecting a different values for each iteration since cumDistance = exit - entry*i – bbnm Jun 29 '23 at 07:39
  • and I'm not trying to cycle over only the last five bars, I'm trying cycle through all bars – bbnm Jun 29 '23 at 07:52