Awareness. You need to start keeping a log of the events preceding an episode. Give no regard to the scientific method or anything, just start writing them down. Once you have a solid collection (however many that is to you) then start re-reading them and look for common threads. The hardest part of this is being brutally honest with yourself.
Also, as another commenter said, professional help is gold. If you have access to it. If you don't, then you should explore ways to get such access. I dealt with debilitating depression in the past, and ended up getting care from government-run facilities. They were excellent.