-- Flat betting with stop loss and take profit
-- Bet the same amount on every roll and stop when the session is down or up by a set
-- share of the starting balance. No progression, so this is the baseline to compare
-- other systems against: the expected loss is the house edge times the total wagered.

chance  = params.chance or 49.5
bethigh = false

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

local stopLoss   = params.stoploss or 0.10     -- stop when down 10% of the starting balance (0 = off)
local takeProfit = params.takeprofit or 0.10   -- stop when up 10% of the starting balance (0 = off)

local start = balance
local eps   = basebet * 1e-6   -- float tolerance on the thresholds

function dobet()
  if stopLoss > 0 and profit <= eps - start * stopLoss then
    print("Stop loss hit after " .. bets .. " bets, profit " .. string.format("%.8f", profit))
    stop()
    return
  end
  if takeProfit > 0 and profit >= start * takeProfit - eps then
    print("Take profit hit after " .. bets .. " bets, profit " .. string.format("%.8f", profit))
    stop()
    return
  end
  nextbet = basebet
end
