0

Can someone please help me to get Longest losing streak in specific time frame? It should look at all of the trades in a daterange and find the longest loss streak. My code currently only gets the current streak, I can't figure out how to make it look at all executed trades and return the longest losing streak.

// Determine current losing streak length
streakLen = 0
streakLen := if (newLoss)
    nz(streakLen[1]) + 1
else
    if (strategy.wintrades > strategy.wintrades[1]) or
         (strategy.eventrades > strategy.eventrades[1])
        0
    else
        nz(streakLen[1])

1 Answers1

0

You can add another variable and counter, and set that variable to the higher of current counter and current streakLen:

var streakLen = 0
var maxStreakLen = 0

streakLen := if (newLoss)
    nz(streakLen[1]) + 1
else
    if (strategy.wintrades > strategy.wintrades[1]) or
         (strategy.eventrades > strategy.eventrades[1])
        maxStreakLen := math.max(maxStreakLen, streakLen)
        0
    else
        nz(streakLen[1])
mr_statler
  • 1,913
  • 2
  • 3
  • 14