Bankruptcy - Chapter 7: 4 year waiting time from either discharge or dismissal date, depending on the bank or lender.
Bankruptcy - Chapter 13: 2 year waiting time from discharge date or 4 year waiting time from dismissal date.
Foreclosure: 5-7 year waiting time from completion date, auction sale. Principal residence occupied loans only, must have 10% minimum down payment, require 680 or better FICO tri-merge credit score. Cash out refinances are not permitted.
Deed-in-Lieu of Foreclosure: 4 year waiting time from completion date. Minimum 10% down payment, require 680 or better FICO tri-merge credit score. Cash out refinance is permitted.
Pre-foreclosure Sale, a.k.a. SHORT SALE: 2 year waiting time from close of escrow date. Requires 680 or better FICO tri-merge credit score. Cash out refinance is permitted.
I suggest you start with a Free Online Forensic Loan Audit. helps discover possible violations with your loan or the process.