Hidden Markov model has been widely applied in various fields and has obtained excellent effects. In this paper, we verify the feasibility of applying HMM to quantitative finance and the potential to obtain stable profits and detect coming bear market to avoid sharp falling process. We creatively make full use of raw data and list a few candidate features. Corresponding feature selection method, which uses HMM itself to test performance on each single feature, has been proposed. The comprehensive model is trained using selected features and is tested performance on CSI 300 index in Chinese Astock market and S&P 500 index in American stock market. Experiments on both markets illustrate that HMM has great ability to identify market states and obtain excess return. And HMM-based strategy has better stability and profitability compared with strategies based on double-MA and K-means. HMM is appropriate to be applied as the core of quantitative strategies to judge the trends of financial markets.