If we knew the number of electrons in the outermost shell, the valence electrons, then we could find the valency of the element by calculating how many electrons need to be gained or lost to achieve stable electronic configuration.
For example, if an element has 7 valence electrons, and requires 8 valence electrons to be stable, then its valency will be -1.