While cointegration models with constant parameters generate statistical arbitrage, the cointegration feature may change and even disappear due to regime shifts. This paper studies the time-consistent mean-variance portfolio problem in a Markov-modulated regime-switching cointegration economy. We derive a novel closed-form solution to the equilibrium strategy. This analytical solution allows us to investigate statistical arbitrage with regime-switching pairs-trading rules. The presence of regime switching increases the risk of such trading strategies, especially near the switching times. Empirical analysis demonstrates the use of the derived formulas and shows the advantages of incorporating different market modes. Copyright © 2019 Society for Industrial and Applied Mathematics.