0

Trying to count how many streaks of losses I had in a specified timeframe. My code:

newLoss = (strategy.losstrades > strategy.losstrades[1]) and
     (strategy.wintrades == strategy.wintrades[1]) and
     (strategy.eventrades == strategy.eventrades[1])

var three_losses = 0
var four_losses = 0
var five_losses = 0
var streakLen = 0
var longestStreak = 0

streakLen := if (newLoss and backtestStartDate <= time and time <= backtestEndDate)
    nz(streakLen[1]) + 1
else
    if (strategy.wintrades > strategy.wintrades[1]) and streakLen == 3
        three_losses += 1
        0
    else if (strategy.wintrades > strategy.wintrades[1]) and streakLen == 4
        four_losses += 1
        0
    else if (strategy.wintrades > strategy.wintrades[1]) and streakLen == 5
        five_losses += 1
        0
    else
        nz(streakLen[1])
longestStreak := streakLen > longestStreak ? streakLen : longestStreak

This code doesnt return correct values, no idea how to fix.

1 Answers1

0

If you have a winning trade, but the streakLen is not 3 or 4 or 5, then you keep counting. You want to reset the value of streakLen in any case there is a winning trade:

newLoss = (strategy.losstrades > strategy.losstrades[1]) and
     (strategy.wintrades == strategy.wintrades[1]) and
     (strategy.eventrades == strategy.eventrades[1])

var three_losses = 0
var four_losses = 0
var five_losses = 0
var streakLen = 0
var longestStreak = 0

streakLen := if newLoss and (backtestStartDate <= time and time <= backtestEndDate)
    nz(streakLen[1]) + 1
else
    if (strategy.wintrades > strategy.wintrades[1]) and streakLen == 3
        three_losses += 1
        0
    else if (strategy.wintrades > strategy.wintrades[1]) and streakLen == 4
        four_losses += 1
        0
    else if (strategy.wintrades > strategy.wintrades[1]) and streakLen == 5
        five_losses += 1
        0
    else if strategy.wintrades > strategy.wintrades[1]
        0
    else
        nz(streakLen[1])
mr_statler
  • 1,913
  • 2
  • 3
  • 14