Index theory revealed its outstanding role in the study of periodic orbits of Hamiltonian systems and the dynamical consequences of this theory are enormous. Although the index theory in the periodic case is well-established, very few results are known in the case of homoclinic orbits of Hamiltonian systems. Moreover, to the authors' knowledge, no results have been yet proved in the case of heteroclinic and halfclinic (i.e. parametrised by a half-line) orbits.Motivated by the importance played by these motions in understanding several challenging problems in Classical Mechanics, we develop a new index theory and we prove at once a general spectral flow formula for heteroclinic, homoclinic and halfclinic trajectories. Finally we show how this index theory can be used to recover all the (classical) existing results on orbits parametrised by bounded intervals.