예제 #1
0
파일: equity.py 프로젝트: namitkewat/finpy
 def bollinger_band(self, tick, window=20, k=2, mi_only=False):
     """
     Return four arrays for Bollinger Band.
     The first one is the moving average.
     The second one is the upper band.
     The thrid one is the lower band.
     The fourth one is the Bollinger value.
     If mi_only, then return the moving average only.
     """
     ldt_timestamps = self.ldt_timestamps()
     pre_timestamps = ut.pre_timestamps(ldt_timestamps, window)
     # ldf_data has the data prior to our current interest.
     # This is used to calculate moving average for the first window.
     ldf_data = ut.get_tickdata([tick], pre_timestamps)
     merged_data = pd.concat([ldf_data[tick]['close'], self['close']])
     bo = dict()
     bo['mi'] = pd.rolling_mean(merged_data, window=window)[ldt_timestamps] 
     if mi_only:
         return bo['mi']
     else:
         sigma = pd.rolling_std(merged_data, window=window)
         bo['hi'] = bo['mi'] + k * sigma[ldt_timestamps] 
         bo['lo'] = bo['mi'] - k * sigma[ldt_timestamps] 
         bo['ba'] = (merged_data[ldt_timestamps] - bo['mi']) / (k * sigma[ldt_timestamps])
         return bo
예제 #2
0
 def drawdown(self, window=10):
     """
     Find the peak within the retrospective window.
     Drawdown is the difference between the peak and the current value.
     """
     ldt_timestamps = self.ldt_timestamps()
     pre_timestamps = ut.pre_timestamps(ldt_timestamps, window)
     # ldf_data has the data prior to our current interest.
     # This is used to calculate moving average for the first window.
     merged_data = self.total[pd.Index(pre_timestamps[0]), ldt_timestamps[-1]]
     total_timestamps = merged_data.index
     dd = pd.Series(index=ldt_timestamps)
     j = 0
     for i in range(len(pre_timestamps), len(total_timestamps)):
         win_start = total_timestamps[i - window]
         win_end = total_timestamps[i]
         ts_value = merged_data[win_start:win_end]
         current = merged_data[win_end]
         peak = np.amax(ts_value)
         dd[j] = (peak-current)/peak
         j += 1
     return dd