This is besides the point, but is this actually valid Python?
if not account.allow_negative:
if (balance.available_ignoring_collateral if ignore_collateral else balance.available) < 0:
raise BadRequest('Account does not have enough balances')
Not sure what's going on inside that second if statement.