Praise be to Allah.
The one who has gotten involved in the stock market and purchased haraam shares, then repented, must hasten to get out of it, by taking his capital and ridding himself of any profits by donating them to charitable causes. This has been explained in the answer to question no. 35726.
What you must do is get rid of what you have left of shares in this riba-based bank, because one of the conditions of repentance is giving up this sin, and giving up this sin in the case of such transactions can only be done by getting out of these riba-based transactions. You do not have the right to keep anything except your capital, after this loss.
Allah, may He be exalted, says (interpretation of the meaning):
“Those who consume Riba will not stand (on the Day of Resurrection) except like the standing of a person beaten by Shaytaan (Satan) leading him to insanity. That is because they say: ‘Trading is only like Riba,’ whereas Allah has permitted trading and forbidden Riba. So whosoever receives an admonition from his Lord and stops consuming Riba, shall not be punished for the past; his case is for Allah (to judge); but whoever returns (to Riba), such are the dwellers of the Fire — they will abide therein.
276. Allah will destroy Riba and will give increase for Sadaqaat (deeds of charity, alms). And Allah likes not the disbelievers, sinners”
Shaykh as-Sa‘di (may Allah have mercy on him) said:
“So whosoever receives an admonition from his Lord” that is, an admonition, reminder and warning against dealing in usury, that came to him through whomever Allah decreed to admonish him and convey this message to him, as a mercy from Allah towards those to the one to whom it is addressed, and serves to establish proof against him.
“and stops consuming Riba” that is, gives up what he is doing and is deterred from it
“shall not be punished for the past” that is, previous transactions in which he engaged before this reminder reached him; he may keep any gains he acquired, as a reward for accepting the advice. This verse indicates that the one who does not desist will be punished for both his earlier and later actions.
End quote from Tafseer as-Sa‘di (116)
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said concerning what we learn from the verses quoted above:
(We also learn) that whatever a person has taken of riba before he came to know of the prohibition is permissible for him on condition that he repent and stop dealing in it, because Allah, may He be exalted, says (interpretation of the meaning): “So whosoever receives an admonition from his Lord and stops consuming Riba, shall not be punished for the past” [al-Baqarah 2:275].
(We also learn) that if he repents from riba before taking possession of it, then he must waive it, because Allah, may He be exalted, says (interpretation of the meaning): “and stops consuming Riba” [al-Baqarah 2:275]. So whoever takes it after coming to know of the prohibition has not stopped consuming it. Hence the Prophet (blessings and peace of Allah be upon him) said in ‘Arafah during the Farewell Pilgrimage: “The riba of the Jaahiliyyah is abolished, and the first riba that I abolish is the riba of al-‘Abbaas ibn ‘Abd al-Muttalib; it is all abolished.” Narrated by Muslim. So the Prophet (blessings and peace of Allah be upon him) explained that whatever had not yet been collected of riba was abolished. End quote.
Tafseer Soorat al-Baqarah (3/377)
With regard to keeping the shares to compensate for the loss you have incurred, this means either continuing with the same haraam riba-based transaction, or entering into a new transaction which is also haraam, using your current capital, to make up for the losses you have incurred. That is haraam in either case.
And Allah knows best.