async def evaluate(self, cryptocurrency, symbol, time_frame, candle, high, low, close): hl2 = EvaluatorUtil.CandlesUtil.HL2(high, low)[-1] atr = tulipy.atr(high, low, close, self.length)[-1] previous_value = self.get_previous_value(symbol, time_frame) upper_band = hl2 + self.factor * atr lower_band = hl2 - self.factor * atr prev_upper_band = previous_value.get(self.PREV_UPPER_BAND, 0) prev_lower_band = previous_value.get(self.PREV_LOWER_BAND, 0) lower_band = lower_band if ( lower_band > prev_lower_band or close[-2] < prev_lower_band) else prev_lower_band upper_band = upper_band if ( upper_band < prev_upper_band or close[-2] > prev_upper_band) else prev_upper_band prev_super_trend = previous_value.get(self.PREV_SUPERTREND, 0) if previous_value.get(self.PREV_ATR, None) is None: self.eval_note = -1 elif prev_super_trend == prev_upper_band: self.eval_note = 1 if close[-1] > upper_band else -1 else: self.eval_note = -1 if close[-1] < lower_band else 1 previous_value[self.PREV_ATR] = atr previous_value[self.PREV_UPPER_BAND] = upper_band previous_value[self.PREV_LOWER_BAND] = lower_band previous_value[ self. PREV_SUPERTREND] = lower_band if self.eval_note == 1 else upper_band return
def _ATR(self, data, timeframe=None, symbol=None): timeframe = '1h' ohlcv_high = self.ohlcv[:, 1].copy(order='C') ohlcv_low = self.ohlcv[:, 2].copy(order='C') ohlcv_close = self.ohlcv[:, 3].copy(order='C') __atr = ti.atr(ohlcv_high, ohlcv_low, ohlcv_close, 14) self.ATR = __atr[-1]
def test_atr(data): arr_high = data["high"].to_numpy().astype(float) arr_low = data["low"].to_numpy().astype(float) arr_close = data["close"].to_numpy().astype(float) ti_atr = np.round(np.append([np.nan for i in range(13)], ti.atr(arr_high, arr_low, arr_close, period=14)), decimals=3) atr = indicator.Indicate(data)\ .average_true_range()['atr']\ .astype(float) assert np.allclose(ti_atr, atr, atol=1e-03, equal_nan=True)
def calculate(self, candles): candles_len = len(candles) if candles_len < self.period + 1: return float(0) high_array = np.array( [float(x['ohlc'].high) for x in candles[-(self.period + 1):]]) low_array = np.array( [float(x['ohlc'].low) for x in candles[-(self.period + 1):]]) close_array = np.array( [float(x['ohlc'].close) for x in candles[-(self.period + 1):]]) #calculate cur_atr = ti.atr(high_array, low_array, close_array, period=self.period) return float(cur_atr[-1])
# # positions = api.list_positions() # api.submit_order{ # symbol = 'IWM', # side='sell', # qty=57, # time_in_force = 'day', # type = 'trailing_stop', # trail_price = '0.20' # } # api.submit_order{ # symbol = 'DIA', # side='sell', # qty=5, # time_in_force = 'day', # type = 'trailing_stop', # trail_percent = '0.70' # } daily_bars = api.polygon.historic_agg_v2('NIO',1,'day',_from='2021-01-20', to='2021-01-21').df print(daily_bars) atr = tulipy.atr(daily_bars.high.values, daily_bars.low.values, daily_bars. close.values, 14) print(atr)
# trail_price='0.20' # ) # # api.submit_order( # symbol='DIA', # side='sell', # qty=5, # time_in_force='day', # type='trailing_stop', # trail_percent='0.70' # ) start_bar = "2020-10-01T09:30:00-05:00" end_bar = "2020-11-13T10:00:00-05:00" # print(start_bar) # print(type(start_bar)) # # current_date = date.today().isoformat() # NY = 'America/New_York' # end = pd.Timestamp(f"{current_date} 16:00", tz=NY).isoformat() # print(end) # print(type(end)) # get data daily_bars = api.get_barset('NIO', 'day', start=start_bar, end=end_bar).df atr = tulipy.atr(daily_bars['NIO']['high'].values, daily_bars['NIO']['low'].values, daily_bars['NIO']['close'].values, 14) print(atr)
def add_atr(df, period=5): l = len(df.close.values) df[f'atr_{period}'] = pad_left(ti.atr( np.array(df.high), np.array(df.low), np.array(df.close), period), l)
def compute(self, data_dict): high = data_dict.get('high') low = data_dict.get('low') close = data_dict.get('close') return ti.atr(high, low, close, self.per)