It is not lawful to sell the goods to others until it does not come into the possession physically or legally (as per the rule). The physical possession is obvious, but legal possession is that its risk (zaman) is transferred to the buyer i.e. if any damage or profit takes place the buyer shall be responsible for the same. Hence if in the question mentioned above the transfer of risk is towards you then it shall be lawful for you to sell it ahead. I do not know whether the transfer of risk happens immediately after buying shares or not. You yourself should investigate the same. It should be noted that the sale and purchase of shares is lawful when that company is not involved in unlawful business, as well as the company owns some immoveable property. If it is learnt at a later stage that the company was involved in haram business then this matter should be raised in its meetings and the profit which came though haram source should be given to poor and needy ones.
Allah (Subhana Wa Ta’ala) knows Best
Darul Uloom Deoband