1

I'm attempting to determine the number of consecutive wins and losses. I get the number with my current code, but when I test its reliability with the total number of wins and losses, it doesn't match the total number of wins and losses shown by the strategy tester, so I'm not sure if it's giving me the right answer. `

print(string txt1, string txt2,string txt) => 
    var table t = table.new(position.middle_right, 1, 3)
    table.cell(t, 0, 0, txt1, bgcolor = color.rgb(0, 254, 21))
    table.cell(t, 0, 1, txt2, bgcolor = color.rgb(0, 254, 21))
    table.cell(t, 0, 2, txt, bgcolor = color.rgb(0, 254, 21))

print2(string txt) => 
    var table t = table.new(position.top_right, 1, 1)
    table.cell(t, 0, 0, txt, bgcolor = color.rgb(246, 4, 4))

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

var streakLenloss = 0
var lossnum = 0
 
if (newLoss)
    streakLenloss := streakLenloss + 1
else
    if (strategy.wintrades > strategy.wintrades[1])
        streakLenloss := 0
    else
        streakLenloss := streakLenloss

if ( streakLenloss>= 1)// or streakLenloss == 4 or streakLenloss == 8......
    lossnum := lossnum+1


b = lossnum
print2(str.tostring(b))

`

Previously, I used the conditions below to calculate the total number of two consecutive losses, but when I tested the reliability of the last if statement with the above condition, it generated more losses than the total number of trades.

if ( streakLenloss== 2 or streakLenloss == 4 or streakLenloss == 6 or streakLenloss == 8 or streakLenloss == 10)

Sewatech
  • 13
  • 3
  • What is the print2 function ? – G.Lebret Nov 20 '22 at 08:09
  • ````print(string txt1, string txt2,string txt) => var table t = table.new(position.middle_right, 1, 3) table.cell(t, 0, 0, txt1, bgcolor = color.rgb(0, 254, 21)) table.cell(t, 0, 1, txt2, bgcolor = color.rgb(0, 254, 21)) table.cell(t, 0, 2, txt, bgcolor = color.rgb(0, 254, 21))```` Those are for displaying the number in screen using table – Sewatech Nov 20 '22 at 08:12

2 Answers2

0

What you want is to check the streakLenloss only at the end of the streak. Otherwise, it will add 1 to lossnum on many (or fewer) bars you didn't intended it to be calculated. You can try:

if (streakLenloss >= 1) and not newLoss and newLoss[1]
    lossnum := lossnum+1
mr_statler
  • 1,913
  • 2
  • 3
  • 14
0

Your error comes from using strategy.losstrades[1] which is always inferior to strategy.losstrades
You can code like this :

var StreakLenWin = 0
var StreakLenLoss = 0
var LastTrade = ""
var tradesperdus = 0
var tradesgagnes = 0

if strategy.losstrades > tradesperdus
    tradesperdus := strategy.losstrades

    if LastTrade == "loss"
        StreakLenLoss := StreakLenLoss + 1
    LastTrade := "loss"

if strategy.wintrades > tradesgagnes
    tradesgagnes := strategy.wintrades
    
    if LastTrade == "win"
        StreakLenWin := StreakLenWin + 1
    LastTrade := "win"

StreakLenWin will be the number of two consecutives win (ie : 3 consecutives wins will increase StreakLenWin of +2)
StreakLenLoss will be the number of two consecutives loss.

G.Lebret
  • 2,826
  • 2
  • 16
  • 27