Look into "captive insurance" I'm sure this particular scheme I'm talking about goes by other names also. The main point is to transfer all or most your companies (company X) net income to another company (company Y) (preferably company Y manages multiple companies and not just company X). Company Y's entire business is creating contracts that state something along the lines of "Pay us a variable amount each year and we promise to allow x amount payed back in the case of a yearly net loss." Company Y will generate very small realized gains, as a result so will Company X.
I am not a lawyer and I am also not a businessman, so my explanation is intentionally vague. In reality this whole setup is more complex and varies depending on local laws and business needs.
This sort of system is how many companies are able to generate a huge amount of money and pay barely any taxes, you can even use variations of this system to get your money into foreign countries/currencies.
Please note that I have not described an "exit strategy" : i.e. I have not explained how you eventually get your money out of company Y and into a position where you can freely spend/reinvest it. Your "exit strategy" will vary depending on exactly how you set everything up and how you intend to use the money.
What I have stated is not financial or legal advice and should not be taken as such. Do your own research. I am merely sharing ideas which I think relate to the topic at hand.
edit: changed companies to be called X and Y to avoid confusion with OP's idea