-- Martingale with a profit vault
-- Classic martingale: multiply the bet after a loss, reset after a win. Each time the
-- session profit climbs another `step`, move part of that step to the vault with
-- vault(). Vaulted funds count as profit but can never be bet, so a later bust only
-- takes what is still in the balance.

chance  = 49.5
bethigh = false

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

local multiplier = params.multiplier or 2.0      -- bet multiplier after a loss
local step       = params.step or balance / 100   -- profit gained between two vault deposits
local share      = params.share or 0.5            -- fraction of each step moved to the vault

local nextLock = step

function dobet()
  if win then
    nextbet = basebet
  else
    nextbet = previousbet * multiplier
  end

  -- profit includes the vault, so vaulting does not move the next threshold
  while step > 0 and profit >= nextLock do
    vault(step * share)
    nextLock = nextLock + step
  end
end
