def __init__(self, tickerSymbol, patternLength= 90, outcomeLength= 21, startDate= '2005-2-2'): self.ticker = tickerSymbol self.pL = patternLength self.oL = outcomeLength self.startDate = startDate if self.pL> 80: self.corr = .7 else: self.corr= .8 self.obj = Data(self.ticker,self.startDate) self.s = self.obj.getData()['Close'].pct_change().cumsum().dropna()*100 self.s.name = 'percentCum'
class PatternRec: def __init__(self, tickerSymbol, patternLength= 90, outcomeLength= 21, startDate= '2005-2-2'): self.ticker = tickerSymbol self.pL = patternLength self.oL = outcomeLength self.startDate = startDate if self.pL> 80: self.corr = .7 else: self.corr= .8 self.obj = Data(self.ticker,self.startDate) self.s = self.obj.getData()['Close'].pct_change().cumsum().dropna()*100 self.s.name = 'percentCum' @decorators.timer def pRec(self): i= len(self.s) self.valoreAtteso= np.nan self.dfPast = pd.DataFrame(self.s[i-self.pL:i]) firstdayFut = self.dfPast.index[-1]+BDay(1) self.dfFut = pd.DataFrame(index=pd.date_range(firstdayFut, periods=self.oL, freq='B'), columns=['out']) j = i -self.pL while j > self.pL: st = self.s.index[j].strftime('%y-%m-%d') pat = self.s[j-self.pL:j] self.dfPast[st] = pat.values c = self.dfPast['percentCum'].corr(self.dfPast[st]) if c < self.corr: self.dfPast = self.dfPast.drop(st, axis= 1) j-=1 else: self.dfFut[st]= self.s[j:j+self.oL].values j-=self.pL self.dfFut = self.dfFut.drop('out',1) if self.dfFut.shape[1] >=2: self.rets = self.dfFut.apply(lambda x: x[-1]- x[0]) positiveOut = [i for i in self.rets if i > 0] negativeOut = [i for i in self.rets if i < 0] positivePercent = (len(positiveOut)*100.)/len(self.rets) if positivePercent > 65: self.valoreAtteso =np.mean(positiveOut) elif positivePercent < 35: self.valoreAtteso = np.mean(negativeOut) else: self.valoreAtteso = np.nan self.maxVal = np.max(positiveOut) self.minVal = np.min(negativeOut) try: print 'pattern simili:\t\t{0}\nritorni positivi:\t{1} %\nvalore massimo:\t\t{2}\nvalore minimo:\t\t{3}'.format(len(self.rets),positivePercent,self.maxVal,self.minVal) except Exception, e: print 'pattern simili:\t\t{0}\nritorni positivi:\t{1} %\nvalore atteso:\t\t{2}'.format(len(self.rets),positivePercent,self.valoreAtteso) else: