It is not possible to know the current amount of spread. You can code a strategy to not trade at times when spread is known to be large such as when a market is closed or you can allow for fixed levels of spread at different times of the day to be considered in your code but you cannot know if for example spread has suddenly widened due to high volatility.