def cci(self, name='cci', period=20): if len(self.df_ohlc) > period: self._add_column_to_ohlc( name, ti.cci(self.df_ohlc.pricehigh.values, self.df_ohlc.pricelow.values, self.df_ohlc.priceclose.values, period)) return True else: return False
def cci(self, name='cci', period=20): try: self._add_column_to_ohlc( name, ti.cci(self.df_ohlc.pricehigh.values, self.df_ohlc.pricelow.values, self.df_ohlc.priceclose.values, period)) except Exception as err: print("cci error>>>", err.__repr__()) return False return True
def calculate(self, candles): candles_len = len(candles) if candles_len < self.period: return 0 close_array = np.array( [float(x['ohlc'].close) for x in candles[-self.period:]]) high_array = np.array( [float(x['ohlc'].high) for x in candles[-self.period:]]) low_array = np.array( [float(x['ohlc'].low) for x in candles[-self.period:]]) #calculate cci = ti.cci(high_array, low_array, close_array, period=self.period) return cci[-1]
async def evaluate(self, ctx): self.eval_pending() if len(ctx.close) >= self.period: try: cci = tulipy.cci(ctx.high, ctx.low, ctx.close, self.period)[-1] except tulipy.lib.InvalidOptionError as e: self.logger.debug(f"Error when computing Commodity Channel Index: {e}") self.logger.exception(e, False) else: if cci <= self.long: self.eval_long() elif cci >= self.short: self.eval_short() await self.evaluation_completed(ctx.cryptocurrency, ctx.symbol, ctx.time_frame, eval_time=ctx.eval_time())
def inds(self): Indicators = {} for i in range(len(self.time)): #i/o? ''' 2 = High, 3 = Low, 4 = Close, 5 = Volume collects the needed market data into one list to push to the indicators''' close = self.time[i][4].values.copy(order='C') high = self.time[i][2].values.copy(order='C') low = self.time[i][3].values.copy(order='C') volume = self.time[i][5].values.copy(order='C') # !!!This needs to be changed. Each volume of the base time must be indexed up to the slice __time = self.time[i][6] # these are the indicators currently being used, and recored Indicators[i] = { 'stochrsi': ti.stochrsi(close, 5), 'rsi': ti.rsi(close, 5), # indicators that need to be doublechecked 'mfi': ti.mfi(high, low, close, volume, 5), 'sar': ti.psar(high, low, .2, 2), 'cci': ti.cci(high, low, close, 5), 'ema': ti.ema(close, 5) } # this one is good Indicators[i]['stoch_k'], Indicators[i]['stoch_d'] = ti.stoch( high, low, close, 5, 3, 3) # check on this, to see if it functions properly Indicators[i]['bbands_lower'], Indicators[i]['bbands_middle'], Indicators[i]['bbands_upper'] = ti.bbands(close, 5, 2) Indicators[i]['macd'], Indicators[i]['macd_signal'], Indicators[i]['macd_histogram'] = ti.macd(close, 12, 26, 9) Indicators[i]['time'] = __time Indicators[i]['close'] = close ''' below changes the length of each array to match the longest one, for a pandas df np.nan for the tail of the shorter ones''' Indicators[i] = to_pd(Indicator=Indicators[i]).dropna(how='all') return Indicators
def add_cci(df, period=5): l = len(df.close.values) df[f'cci_{period}'] = pad_left(ti.cci( np.array(df.high), np.array(df.low), np.array(df.close), period), l)