chance     = 49.
5
div        = 20
base       = balance / div
bet1       = base
nextbet    = bet1
--bet2       = base + bet1
--bet3       = base + 3*bet2
--bet4       = (base + 3*bet2) / 2
ath    =   balance -- All-time high (profit check)
stage =    1        -- To track the betting stages    -- First bet
lost1 =    0
won1 =     0
won2 =     0-- Third bet
function dobet()
    --stage = 1
    div = 200000
    base = balance / div
    bet1 = base
    --bet1 = base
    bet2 = bet1 * 3       -- Second bet
    bet3 = bet1 + (3 * bet1)
    bet4 = (bet1 + 3 * bet1) / 2
    nextbet = bet1
    if (currentstreak == -1) then
         lost1 = lost1 + 1
    else
         lost1 = 0
    end
    if (currentstreak == 1) then
         won1 = won1 + 1
     else
          won1 = 0
     end
     if (currentstreak > 5) then
          won2 = 1
     else
          won2 = 0
     end
     if (lost1==1) then
         stage = stage + 1
      else
        lost1 = 0
        --won1 = 1
     end
    -- Stage 2: Second bet, if lost, sum the previous two bets
    if (stage == 1) then
            base    = bet1
            nextbet = base -- Triple the next bets
            if (lost1==1) then -- Lost
            stage = 2
            print("Stage 2 lost. Summing bets to:", nextbet)
        else -- Won
            stage = 1
            print("Stage 2 won. Betting base bet again:", nextbet)
    end
end
-- Stage 2: Second bet, if lost, sum the previous two bets
if (stage == 2) then
        base    = bet2
        nextbet = base -- Triple the next bets
        if (lost1==1) then -- Lost
        stage = 3
        print("Stage 2 lost. Summing bets to:", nextbet)
    else -- Won
        stage = 1
        print("Stage 2 won. Betting base bet again:", nextbet)
    end
end
-- Stage 3: Third bet, if lost, halve the bet and keep betting until loss
if (stage == 3) then
        base    = bet3
        nextbet = base -- Triple the next bet
        if (lost1==1) then -- Lost
        stage = 4
        print("Stage 3 lost. Halving bet to:", nextbet)
    else -- Won
        stage = 1
        print("Stage 3 won. Betting base bet again:", nextbet)
    end
end
-- Stage 4: Third bet, if lost, halve the bet and keep betting until loss
if (stage == 4) then
         base    = bet4
         nextbet = base -- Triple the next bet
         if (won2==1) then -- Lost
         stage = 1
         print("Stage 3 lost. Halving bet to:", nextbet)
    else
         stage = 5
    end
end
if (stage >= 4) then
    if (profit < 0) then
        stage   = 1
        base    = 2 * base
        --nextbet = base
        print("Doubling bet as close to profit:", nextbet)
    else
        stage   = 1
        base    = 3 * base
        --nextbet = base
        print("Tripling bet as far from profit:", nextbet)
    end
end
if balance > ath then
    -- Reset the system when in profit
    ath     = balance
    div     = 200000
         base    =   balance / div
         bet1    =   base
         nextbet =   bet1
         stage   =   1
         print("In   profit, resetting bets.")
      end
      print("Last bet rolled:", lastBet.roll)
      -- Set the bet amount for the next round
      --nextbet = math.max(nextbet, base) -- Ensure nextbet does not drop below base
bet
end