The optimal amount of dysprosium in the highly magnetostrictive rare-earth compounds Tb$_{1-x}$Dy$_x$Fe$_2$ for room temperature applications has long been known to be $x$=0.73 (Terfenol-D). Here, we derive this value from first principles by calculating the easy magnetization direction and magnetostriction as a function of composition and temperature. We use crystal field coefficients obtained within density-functional theory to construct phenomenological anisotropy and magnetoelastic constants. The temperature dependence of these constants is obtained from disordered local moment calculations of the rare earth magnetic order parameter. Our calculations find the critical Dy concentration required to switch the magnetization direction at room temperature to be $x_c$=0.78, with magnetostrictions $lambda_{111}$=2700 and $lambda_{100}$=-430~ppm, close to the Terfenol-D values.