There may be earlier renditions, but this story is in the Huainanzi compiled around 139 BCE:
Among the people who lived close to the border, there was a man who
led a righteous life. Without reason, his horse escaped, and fled into
barbarian territory. Everyone pitied him, but the old man said : "what
makes you think this is not a good thing?"
Several months later, his horse returned, accompanied by a superb
barbarian stallion. Everyone congratulated him. But the old man said:
"what makes you think this is cannot be a bad thing?"
The family was richer from a good horse, his son enjoyed riding it. He
fell and broke his hip. Everyone pitied him, but the old man said:
"what makes you think this is not a good thing!"
One year later, a large party of barbarians entered the border. All
the valid men drew their bows and went to battle. From the people
living around the border, nine out of ten died. But just because he
was lame, the old man and his son were both spared.
Thence, good fortune turns into bad fortune, and bad fortune turns
into good fortune. These changes never reach an end, their complexity
can never be fathomed.