code: def recalculate_balance(stmt):
      
  
      total_amount = sum(sl.amount for sl in stmt.lines)
  
      stmt.start_balance = stmt.start_balance or D(0)
      stmt.end_balance = stmt.start_balance + total_amount
      stmt.start_date = min(sl.date for sl in stmt.lines)
      stmt.end_date = max(sl.date for sl in stmt.lines)
docs: Recalculate statement starting and ending dates and balances.
  
      When starting balance is not available, it will be assumed to be 0.
  
      This function can be used in statement parsers when balance information is
      not available in source statement.