-- Labouchere (cancellation system)
-- Keep a list of units, e.g. {1, 2, 3, 4}. Bet first + last. On a win, cross both
-- off; on a loss, append the lost amount to the end. When the list is empty the
-- cycle has banked the sum of the original list, and it restarts.

chance  = 49.5
bethigh = false

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

local sequence = {}
local initial  = { 1, 2, 3, 4 }  -- in units of basebet

local function restart()
  sequence = {}
  for i = 1, #initial do sequence[i] = initial[i] end
end

local function currentUnits()
  if #sequence == 0 then restart() end
  if #sequence == 1 then return sequence[1] end
  return sequence[1] + sequence[#sequence]
end

restart()
nextbet = currentUnits() * basebet

function dobet()
  local units = previousbet / basebet
  if win then
    if #sequence <= 2 then
      sequence = {}
    else
      table.remove(sequence, #sequence)
      table.remove(sequence, 1)
    end
  else
    sequence[#sequence + 1] = units
  end
  nextbet = currentUnits() * basebet
end
