Some studies suggest that regular exercise can improve a person's sleep. However, for some people, exercising too late (for example, shortly before going to bed) might affect sleep.
Most people with chronic insomnia have higher levels of stress hormones in their blood, indicating that they are overly sensitive to stress. Generally, the higher your stress hormone levels, the poorer your sleep quality. When people exercise, their stress hormone levels rise, but these elevated levels return to normal within a few hours after exercising. This means that if you exercise too late, it may indeed make it harder to fall asleep.
However, the time it takes for stress hormone levels to return to normal after exercise varies from person to person. Therefore, the impact of exercising late on sleep also varies from person to person. To determine how late you can exercise before it affects your sleep and to what extent, you may need to experiment for a period of time.