But first you have to write the ending.
Correction: First off, you must avoid any ending in which some godlike savior comes into the story to take care of everything and everyone. This is a deus ex machina, a last-minute, last-ditch, make-everything-right, sort-out-the-kinks-and-crinkles ending that satisfies no one.
Now: write an ending that is not deus ex machina.
We have already described one kind of ending. Beginning in medias res allows us to end with the beginning, which, done well, will then be surprising or informative, or both. More… “Cut the Cord”