You can geld as soon as both testicles are present. There are 2 trains of thought as to when the best time is though, 1 on the mare about 10 days before weaning as it seems to be less stressful for the foal - gets a lot of comfort from still suckling etc and there isn't an overall change in routine. 2 anytime after foals first winter, leaving it until spring or the following autumn when the flies are gone but this can knock some backwards as the older the horse gets, the bigger the op.
It does also depend on what the foals temperament and personality is like and whether they are mature enough to go through the op.
Basically you have to play it by ear and work out what suits you and foaly best.