I'm pretty sure the luck came first because optimism/pessimism is a learned trait.
I say this as someone who considers themselves "Optimistic by nature, pessimistic by experience."
I was born in lucky circumstances but that luck turned in my teens due to factors outside of my control. I have seen firsthand how it works.
Even now, I constantly have to catch myself and force myself to think pessimistically... And my pessimistic projections are usually right or sometimes not pessimistic enough.
But I know I'm a natural optimist by the fact that I don't give up. I've built so much software and startups over the years; most of them I'm still running on the side and keeping up to date even though I know consciously that there is zero chance they will succeed. Deep down I have a deep optimism that something will change and all the opportunities will come at once. Consciously, I know it is delusional but I'm fundamentally motivated by emotions, not thoughts.
It's a weird feeling having built products that work very similarly to (or better than) other products which rake in millions of dollars but not being able to find a single customer due to all sorts of weird contrived socio-political reasons.