text: # -*- coding: utf-8 -*-
  """
  Created on Mon Jun 12 16:07:55 2017
  
  @author: Varun Divakar
  """
  import numpy as np
  import pandas as pd
  from pandas_datareader import data as web
  from sklearn import mixture as mix
  import seaborn as sns
  import matplotlib.pyplot as plt
  import talib as ta
  from sklearn.preprocessing import StandardScaler
  from sklearn.svm import SVC
  import fix_yahoo_finance as yf
  
  
  # yf.pdr_override() # <== that's all it takes :-)
  #stocks = ["IBM"]
  df= web.get_data_yahoo('IBM',start='1990-01-01', end='2018-06-01')
  df=df[['Open','High','Low','Close']]
  
  n = 10
  t = 0.8
  split =int(t*len(df))
  
  df['high']=df['High'].shift(1)
  df['low']=df['Low'].shift(1)
  df['close']=df['Close'].shift(1)
  
  df['RSI']=ta.RSI(np.array(df['close']), timeperiod=n)
  df['SMA']= df['close'].rolling(window=n).mean()
  df['Corr']= df['SMA'].rolling(window=n).corr(df['close'])
  df['SAR']=ta.SAR(np.array(df['high']),np.array(df['low']),\
                    0.2,0.2)
  df['ADX']=ta.ADX(np.array(df['high']),np.array(df['low']),\
                    np.array(df['close']), timeperiod =n)
  df['Corr'][df.Corr>1]=1
  df['Corr'][df.Corr<-1]=-1 
  df['Return']= np.log(df['Open']/df['Open'].shift(1))
  
  
  df=df.dropna()
  
  ss= StandardScaler()
  unsup = mix.GaussianMixture(n_components=4,
                              covariance_type="spherical", 
                              n_init=150,
                              random_state=42)
  
  df=df.drop(['High','Low','Close'],axis=1)
  X_train = df[:split]
  X_test = df[split:]
  y_train = (-1, df.shape[1])
  unsup.fit(np.reshape(ss.fit_transform(X_train), y_train))
  regime = unsup.predict(np.reshape(ss.fit_transform(X_test), y_train))
  
  
  
  Regimes=pd.DataFrame(regime, columns=['Regime'], index=X_test.index)\
                       .join(X_test, how='inner')\
                            .assign(market_cu_return=X_test \
                                    .Return.cumsum())\
                                    .reset_index(drop=False)\
                                    .rename(columns={'index':'Date'})
  
  order=[0,1,2,3]
  fig = sns.FacetGrid(data=Regimes,hue='Regime',hue_order=order,aspect=2,size= 4)
  fig.map(plt.scatter,'Date','market_cu_return', s=4).add_legend()
  plt.show()
  
  for i in order:
      print('Mean for regime %i: '%i,unsup.means_[i][0])
      print('Co-Variance for regime %i: '%i,(unsup.covariances_[i]))
  
  print(Regimes.head())
  
  ss1 =StandardScaler()
  columns =Regimes.columns.drop(['Regime','Date'])    
  Regimes[columns]= ss1.fit_transform(Regimes[columns])
  Regimes['Signal']=0
  Regimes.loc[Regimes['Return']>0,'Signal']=1
  Regimes.loc[Regimes['Return']<0,'Signal']=-1
  Regimes['return'] = Regimes['Return'].shift(1)
  Regimes=Regimes.dropna()
         
  cls= SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
      decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
      max_iter=-1, probability=False, random_state=None, shrinking=True,
      tol=0.001, verbose=False)
  
  split2= int(.8*len(Regimes))
  
  X = Regimes.drop(['Signal','Return','market_cu_return','Date'], axis=1)
  y= Regimes['Signal']
  
  cls.fit(X[:split2],y[:split2])
  
  p_data=len(X)-split2
  
  df['Pred_Signal']=0
  df.iloc[-p_data:,df.columns.get_loc('Pred_Signal')]=cls.predict(X[split2:])
  #
  # import pdb
  # pdb.set_trace()
  
  print(df['Pred_Signal'][-p_data:])
  
  df['str_ret'] =df['Pred_Signal']*df['Return'].shift(-1)
  
  df['strategy_cu_return']=0.
  df['market_cu_return']=0.
  df.iloc[-p_data:,df.columns.get_loc('strategy_cu_return')] \
         = np.nancumsum(df['str_ret'][-p_data:])
  df.iloc[-p_data:,df.columns.get_loc('market_cu_return')] \
         = np.nancumsum(df['Return'][-p_data:])
  
  print(df['Pred_Signal'])
  
  # import pdb
  # pdb.set_trace()
  
  Sharpe = (df['strategy_cu_return'][-1]-df['market_cu_return'][-1])\
             /np.nanstd(df['strategy_cu_return'][-p_data:])
  
  print(Sharpe)
  plt.plot(df['strategy_cu_return'][-p_data:],color='g',label='Strategy Returns')
  plt.plot(df['market_cu_return'][-p_data:],color='r',label='Market Returns')
  plt.figtext(0.14,0.9,s='Sharpe ratio: %.2f'%Sharpe)
  plt.legend(loc='best')
  plt.show()
  
  