Let analyze the sequence of events...
--if I sell all the appreciated stocks and pay the capital gain tax for this -
there is no issues... when shares are sold - the gain is realized. If you are a nonresident alien - the gain is not taxable for you in the US, but if you are a resident alien - it is taxable... if shares are held more than a year - reduced long term capital
gain tax rate
-- then I transfer the cash to my nonresident parents - here we need to clarify - what do you mean under "transfer" as that it is related to the ownership... Is that a gift? a loan? or you are hiding money from creditors? Who owns the money while they are siting in your parent's account?
-- Later they may use the cash to do whatever investment they want to and may also make more gains. - I see no issues as long as there is no tax avoidance scheme - and all income
is correctly reported according to US and their home country laws.
-- Then after that maybe many years later, they transfer me cash (part of it may be indirectly from the cash I gave to them before - but it is hard to define as it is just cash) -- so far - I might conclude that original transfer was a loan - as your parents are obligated to return the money back to you - correct? If that was a loan - there should be interest
charge.. if there is no interest a so-called imputed interest is used.
-- In this process, is it possible that IRS also judges that they used my cash to invest and make gains, and then give it to me, thus I also need to pay capital gain tax against the gains they made? - I would say - that is possible... If you simply used their names - but operated that account as your own - and in fact enjoy all benefits
of the ownership - it is possible that they received income as nominees - and you retained the full ownership over that account - and could be considered as the actual owner and as such is responsible for all tax liability realized on that account.