The interplay of structural and magnetic properties of magnetic shape memory alloys is closely related to their composition. In this study the influence of the valence electron concentration on the tetragonal transformation in Ni2Mn1 + xZ1 − x (Z = Ga, In, Sn, Sb) and Co2Ni1 + xGa1 − x is investigated by means of ab initio calculations. While the type of magnetic interaction is different for the two series, the trends of the total energy changes under a tetragonal transformation are very similar. We find that tetragonal structures become energetically preferred with respect to the cubic one as the valence electron concentration e/a is increased regardless of the system under consideration. In particular, the energy difference between the austenite and martensite structures increases linearly with e/a, which is in part responsible for the linear increase of the matensite transformation temperature. The substitution of nickel by platinum increases even further the transformation temperature.