Validity of the Fast While Masturbating and Not Ejaculating
Question

 
When I was a teenager, I used to masturbate sometimes during the day in Ramadan, but I did not let the semen come out of the penis because I blocked it, but I still reached climax.
 What is the ruling on my fast, and how can I expiate for this grave sin? Please note that I do not know how many days I did this.
Praise be to Allah.It should be noted that masturbation is haram according to Shari’ah, as is indicated in the Book of Allah and the Sunnah of His Prophet (peace and blessings of Allah be upon him). 
Masturbation is something that is repulsive to sound human nature and reason, and it is not befitting for a Muslim to lower himself to this level. 
It should also be noted that masturbation has negative consequences for a person , in this world and in the Hereafter, if he does not repent and if Allah does not bestow His mercy upon him. 
With regard to the ruling on the matter mentioned in the question, if you engaged in masturbation but no semen came out as a result, then the fast is not invalidated, according to the most sound scholarly opinion, because what matters is the emission of semen . If it did come out then the fast was invalidated and it has to be made up. If it did not come out then the fast is not invalidated. But whatever the case you have to repent to Allah and seek His forgiveness for wasting the fast by doing such things. 
Semen may be emitted a short while later even if you prevented it from coming out. In that case the fast of that day is invalidated and you have to make it up. If you do not know how many days your fast was invalidated, then try to work it out until you reach a figure you think is most likely, then make up the days that you owe. 
Shaykh Ibn ‘Uthaymin said in Sharh Zad al-Mustaqni’: Is it possible for semen to move without being emitted? 
“Yes, that is possible, when a person’s desire subsides for some reason, then semen does not come out. 
And they gave another example: if a person holds his penis so that no semen comes out. Even though the fuqaha gave this as an example, it is very harmful. The fuqaha (may Allah have mercy on them) gave something as an example regardless of how harmful or not it may be. But usually in such cases the semen is released after the person lets go of his penis. 
Some of the scholars said that ghusl is not required if the semen moves. This is the view favoured by Shaykh al-Islam and is the correct view. The evidence for that is as follows:  
1- The hadith of Umm Salamah in which it says: “Yes, if she sees water.” He did not say, If she feels it moving. If ghusl were required when it moves, then he (peace and blessings of Allah be upon him) would have pointed it out because there would have been a need to do so.
2- The hadith of Abu Sa’id al-Khudri: “Water is for water.” In this case there is no water (i.e., emission). So this hadith indicates that if there is no water (emission) there is no need for water (ghusl).
3- The basic principle is that the person remains pure (tahir) and no ghusl is required. This principle cannot be changed unless there is evidence to that effect.” (Al-Sharh al-Mumti’, 1/280)
See also: al-Furu’, 1/197; al-Mabsut, 1/67; al-Mughni, 1/128; al-Majmu’, 2/159; al-Mawsu’ah al-Fiqhiyyah al-Kuwaitiyyah, 4/99 
And Allah knows best.