how to calculate the valency of any compound?
As said by Vrinda, it is not the compounds which possess valencies. Rather the combining atoms or ions possess different valencies, and these valencies give rise to different chemical formulae for different compounds. For ions, the valency is simply equal to the charged possessed by the molecule. Let us consider the case of sodium chloride (NaCl). It consists of sodium ion (Na+ ion) and chloride (Cl-) ion. Both of these ions carry a charge of 1, and hence the valencies of both these compounds is 1. Now let us consider CaCl2. It consists of Ca+2 cation, which carries a charge of +2, and two Cl- ions, each of which carry a charge of -1. Thus the valency of Ca is 2, while that of Cl is 1.
For atoms, valency is simply the number of electrons gained, lost or shared by an atom to complete its octet that is achieve a noble gas configuration.
If the number of valence electrons (electrons in the outermost shell) is more than 4 then the valency is (8 - number of valence electrons).
If the number of electrons is less than 4 or 4 then it is the valency is the same of valence electrons.