With the logic you have... nextbet is reset to basebet because of:
if count4==1 and win then
nextbet=basebet;count4=0
end
or
if nextbet<10 then
nextbet=basebet
end
For the 2nd option, nextbet will be less than basebet (10) after a couple of wins in a row... due to:
if win then nextbet=previousbet*.75
end
For the "count4==1 and win" condition, count4 is set to 1 when "count2==1 and count==3":
if count2==1 and count==3
then count4=1
end
Each bet, count is initially set to 0, then 1 if it's a win... then 2 added if nextbet is > 20... the problem you have is that the nextbet calculations are done AFTER the nextbet check!
if nextbet>20 then <-- nextbet is checked here
count=count+2
end
if win then nextbet=previousbet*.75 <-- but nextbet isn't set until here!!
end
if !win then nextbet=previousbet*1.15 <-- or here!!
end
Because of this, the nextbet>20 resolving to "true", might be occurring a bet later than you think and thus causing you some confusion:
Bet amount: 18 -> LOSS
nextbet >20? false
if !win then nextbet = previousbet * 1.15 = 18 *1.15 = 20.7
Bot is now going to bet 20.7!
Bet: 20.7 -> WIN
nextbet >20? TRUE
if win then nextbet = previousbet * 0.75 = 20.7 * .75 = 15.525
Bot would try to bet 15.525, but the bet nextbet has already be checked, so now count==3, which means it could be possible for it to get into the "if count2==1 and count==3" section and set count4==1 and with the win could trigger the reset to base.
I suspect that this might be causing the "random" resets...
I've taken the liberty of tidying up the code, and refactoring it slightly... see if this works...
NOTE: UNTESTED!basebet=10
nextbet=basebet
count2=0
count3=0
count4=0
function dobet()
count=0
chance=33
count3=count3+1
if count3>3 then
count3=1;count2=0
end
if win then
count=count+1
nextbet=previousbet*.75
else
count2=count2+1
nextbet=previousbet*1.15
end
if nextbet>20 then
count=count+2
end
if count2==1 and count==3 then
count4=1
end
if count4==1 and win then
nextbet=basebet
count4=0
end
if nextbet<10 then
nextbet=basebet
end
end