Scenario. I have decided that I should not buy widgets from Company X because I do not want to support various immoral things Company X does to produce said widgets.
Claim. It is nonetheless morally permissible for me to buy stock in Company X, as long as I am buying that stock from a third party (ie, not from Company X itself or any subsidiary).
Argument. First, let's ask: what should I do if I happen to already own stock in Company X? As a shareholder, I regularly receive money from Company X as dividends. Since Company X is evil, this money I receive seems "tainted." What can I do?
Action: Sell the stock to someone else (or give it to them for free).
Result: Someone else now receives the "tainted" dividends. This seems to be a morally neutral outcome.
Action: Refuse the dividends, or give the stock back to Company X, or sell the stock back to Company X at a discounted value. (Assume for the sake of argument that Company X is not interested in buying the stock for its true value.)
Result: Company X gets to keep more of their money. This is like giving them free money (even worse than buying their widgets!).
Comment. When Company X first went public and put its stock for sale, it was immoral to buy that stock (since Company X would receive the money). Some people bought the stock, anyway, and that's a done deal. Now, I argue, there is no moral difference between one person owning the stock and a different person owning the stock, and it is morally negative to return the stock back to Company X. Therefore, it is permissible for me to buy stock from a third party.
Possible objection. Suppose 50% of people scrupulously refuse to buy stock in Company X, even from third parties, and 50% act out of self-interest and do not worry about supporting evil companies. When Company Y (also evil) goes public, the scrupulous 50% refuse to buy stock; the self-interested 50%, who would have been willing to buy, are then more likely to instead buy stock in non-evil Company Z (also going public), since there are twice as many potential buyers of that stock should they decide to sell it in the future. Thus, while buying stock in Company X from a third party does not benefit Company X, it does benefit rising new evil companies that have yet to go public.
EDIT. I think most people's (myself included) gut reaction is to say that buying Company X stock or holding Company X stock that one already happens to own is morally negative, while selling it is morally positive. As an objection to this position, consider the following:
Scenario. 50% of investors decide to be scrupulous and follow this position, while 50% follow self-interest only.
Result. Whatever Company X stock is currently held by the scrupulous 50% will eventually be sold to people among the self-interested 50%, because every scrupulous person will feel obligated to sell (positive) and not hold (negative) or buy (negative).
Comment. When scrupulous people were shareholders of Company X, they had more power to demand change in Company X's behavior. Now the shareholders are self-interested people who are less likely to demand change. Thus, the outcome is morally negative.