from matplotlib.finance import quotes_historical_yahoo_ochl as quotes_yahoo # Input symbol file symbol_file = 'symbol_map.json' # Choose a time period start_date = datetime.datetime(2004, 4, 5) end_date = datetime.datetime(2007, 6, 2) # Load the symbol map with open(symbol_file, 'r') as f: symbol_dict = json.loads(f.read()) symbols, names = np.array(list(symbol_dict.items())).T quotes = [quotes_yahoo(symbol, start_date, end_date, asobject=True) for symbol in symbols] # Extract opening and closing quotes opening_quotes = np.array([quote.open for quote in quotes]).astype(np.float) closing_quotes = np.array([quote.close for quote in quotes]).astype(np.float) # The daily fluctuations of the quotes delta_quotes = closing_quotes - opening_quotes # Build a graph model from the correlations edge_model = covariance.GraphLassoCV() # Standardize the data X = delta_quotes.copy().T X /= X.std(axis=0)
import datetime import warnings import numpy as np import matplotlib.pyplot as plt from matplotlib.finance import quotes_historical_yahoo_ochl as quotes_yahoo from hmmlearn.hmm import GaussianHMM #载入历史股票数据 start =datetime.date(1970,9,4) end = datetime.date(2016,5,17) stock_quotes =quotes_yahoo('INTC',start,end) #提取每天的收盘情况 closing_quotes =np.array([quote[2] for quote in stock_quotes]) volumes =np.array([quote[5] for quote in stock_quotes])[1:] diff_percentage =100.0*np.diff(closing_quotes)/closing_quotes[:-1] # 重第二个值开始取日期 dates = np.array([quotes[0] for quote in stock_quotes],dtype=np.int)[1:] #使用不同的大量值进行训练 training_data = np.column_stack([diff_percentage,volumes]) hmm = GaussianHMM(n_components =7,covariance_type='diag',n_iter=1000) with warnings.catch_warnings(): warnings.simplefilter('ignore') hmm.fit(training_data) #使用HMM生成数据