-- Oscar's Grind (+1 unit per cycle)
-- Each cycle aims to finish exactly one unit (the base bet) ahead. Keep the bet after a
-- loss, raise it by one unit after a win, but never above the amount that would take
-- the cycle past +1 unit. When the cycle reaches +1 unit, start a new one.

chance  = 49.5
bethigh = false

basebet = params.basebet or balance / 5000
nextbet = basebet

local unit   = basebet                       -- one unit = the base bet
local payout = (100 - houseedge) / chance    -- 2.00x at a 1% house edge

local cycleProfit = 0

function dobet()
  if win then
    cycleProfit = cycleProfit + previousbet * (payout - 1)
  else
    cycleProfit = cycleProfit - previousbet
  end

  -- cycle complete: bank the unit and start over
  if cycleProfit >= unit - unit * 1e-9 then
    cycleProfit = 0
    nextbet = unit
    return
  end

  local bet = previousbet
  if win then
    bet = previousbet + unit
  end

  -- never bet more than a win needs to finish the cycle at +1 unit
  local needed = (unit - cycleProfit) / (payout - 1)
  if bet > needed then
    bet = needed
  end
  nextbet = bet
end
