Beispiel #1
0
    async def evaluate(self, cryptocurrency, symbol, time_frame, close_candles,
                       high_candles, low_candles, candle):
        self.eval_note = commons_constants.START_PENDING_EVAL_NOTE
        if len(close_candles) >= self.minimal_data:
            min_adx = 7.5
            max_adx = 45
            neutral_adx = 25
            adx = tulipy.adx(high_candles, low_candles, close_candles,
                             self.period_length)
            instant_ema = data_util.drop_nan(tulipy.ema(close_candles, 2))
            slow_ema = data_util.drop_nan(tulipy.ema(close_candles, 20))
            adx = data_util.drop_nan(adx)

            if len(adx):
                current_adx = adx[-1]
                current_slows_ema = slow_ema[-1]
                current_instant_ema = instant_ema[-1]

                multiplier = -1 if current_instant_ema < current_slows_ema else 1

                # strong adx => strong trend
                if current_adx > neutral_adx:
                    # if max adx already reached => when ADX forms a top and begins to turn down, you should look for a
                    # retracement that causes the price to move toward its 20-day exponential moving average (EMA).
                    adx_last_values = adx[-15:]
                    adx_last_value = adx_last_values[-1]

                    local_max_adx = adx_last_values.max()
                    # max already reached => trend will slow down
                    if adx_last_value < local_max_adx:

                        self.eval_note = multiplier * (
                            current_adx - neutral_adx) / (local_max_adx -
                                                          neutral_adx)

                    # max not reached => trend will continue, return chances to be max now
                    else:
                        crossing_indexes = EvaluatorUtil.TrendAnalysis.get_threshold_change_indexes(
                            adx, neutral_adx)
                        chances_to_be_max = \
                            EvaluatorUtil.TrendAnalysis.get_estimation_of_move_state_relatively_to_previous_moves_length(
                                crossing_indexes, adx) if len(crossing_indexes) > 2 else 0.75
                        proximity_to_max = min(1, current_adx / max_adx)
                        self.eval_note = multiplier * proximity_to_max * chances_to_be_max

                # weak adx => change to come
                else:
                    self.eval_note = multiplier * min(1, (
                        (neutral_adx - current_adx) / (neutral_adx - min_adx)))
        await self.evaluation_completed(
            cryptocurrency,
            symbol,
            time_frame,
            eval_time=evaluators_util.get_eval_time(full_candle=candle,
                                                    time_frame=time_frame))
    async def evaluate(self, cryptocurrency, symbol, time_frame, candle_data,
                       candle):
        try:
            if len(candle_data) >= self.period * 2:
                stochrsi_value = tulipy.stochrsi(
                    data_util.drop_nan(candle_data), self.period)[-1]

                if stochrsi_value * self.TULIPY_INDICATOR_MULTIPLICATOR >= self.evaluator_config[
                        self.HIGH_LEVEL]:
                    self.eval_note = 1
                elif stochrsi_value * self.TULIPY_INDICATOR_MULTIPLICATOR <= self.evaluator_config[
                        self.LOW_LEVEL]:
                    self.eval_note = -1
                else:
                    self.eval_note = stochrsi_value - 0.5
        except tulipy.lib.InvalidOptionError as e:
            self.logger.debug(f"Error when computing StochRSI: {e}")
            self.logger.exception(e, False)
            self.eval_note = commons_constants.START_PENDING_EVAL_NOTE
        await self.evaluation_completed(
            cryptocurrency,
            symbol,
            time_frame,
            eval_time=evaluators_util.get_eval_time(full_candle=candle,
                                                    time_frame=time_frame))
Beispiel #3
0
    async def evaluate(self, cryptocurrency, symbol, time_frame, high_candles, low_candles,
                       close_candles, volume_candles, candle):
        if len(high_candles) >= self.short_period:
            kvo = tulipy.kvo(high_candles,
                             low_candles,
                             close_candles,
                             volume_candles,
                             self.short_period,
                             self.long_period)
            kvo = data_util.drop_nan(kvo)
            if len(kvo) >= self.ema_signal_period:

                kvo_ema = tulipy.ema(kvo, self.ema_signal_period)
                ema_difference = kvo - kvo_ema

                if len(ema_difference) > 1:
                    zero_crossing_indexes = EvaluatorUtil.TrendAnalysis.get_threshold_change_indexes(ema_difference, 0)
                    max_elements = 7
                    to_consider_kvo = min(max_elements, len(ema_difference) - zero_crossing_indexes[-1])
                    self.eval_note = EvaluatorUtil.TrendAnalysis.min_has_just_been_reached(
                        ema_difference[-to_consider_kvo:],
                        acceptance_window=0.9, delay=1)
        await self.evaluation_completed(cryptocurrency, symbol, time_frame,
                                        eval_time=evaluators_util.get_eval_time(full_candle=candle,
                                                                                time_frame=time_frame))
Beispiel #4
0
    async def evaluate(self, cryptocurrency, symbol, time_frame, candle_data, candle):
        self.eval_note = commons_constants.START_PENDING_EVAL_NOTE
        if len(candle_data) > self.long_period_length:
            macd, macd_signal, macd_hist = tulipy.macd(candle_data, self.short_period_length,
                                                       self.long_period_length, self.signal_period_length)

            # on macd hist => M pattern: bearish movement, W pattern: bullish movement
            #                 max on hist: optimal sell or buy
            macd_hist = data_util.drop_nan(macd_hist)
            zero_crossing_indexes = EvaluatorUtil.TrendAnalysis.get_threshold_change_indexes(macd_hist, 0)
            last_index = len(macd_hist) - 1
            pattern, start_index, end_index = EvaluatorUtil.PatternAnalyser.find_pattern(macd_hist,
                                                                                         zero_crossing_indexes,
                                                                                         last_index)

            if pattern != EvaluatorUtil.PatternAnalyser.UNKNOWN_PATTERN:

                # set sign (-1 buy or 1 sell)
                sign_multiplier = -1 if pattern == "W" or pattern == "V" else 1

                # set pattern time frame => W and M are on 2 time frames, others 1
                pattern_move_time = 2 if (pattern == "W" or pattern == "M") and end_index == last_index else 1

                # set weight according to the max value of the pattern and the current value
                current_pattern_start = start_index
                price_weight = macd_hist[-1] / macd_hist[current_pattern_start:].max() if sign_multiplier == 1 \
                    else macd_hist[-1] / macd_hist[current_pattern_start:].min()

                if not math.isnan(price_weight):
                    self._analyse_pattern(pattern, macd_hist, zero_crossing_indexes, price_weight,
                                          pattern_move_time, sign_multiplier)
        await self.evaluation_completed(cryptocurrency, symbol, time_frame,
                                        eval_time=evaluators_util.get_eval_time(full_candle=candle,
                                                                                time_frame=time_frame))
Beispiel #5
0
 def _get_rsi_averages(self, symbol_candles, time_frame, include_in_construction):
     # compute the slow and fast RSI average
     candle_data = trading_api.get_symbol_close_candles(symbol_candles, time_frame,
                                                        include_in_construction=include_in_construction)
     if len(candle_data) > self.period_length:
         rsi_v = tulipy.rsi(candle_data, period=self.period_length)
         rsi_v = data_util.drop_nan(rsi_v)
         if len(rsi_v):
             slow_average = numpy.mean(rsi_v[-self.slow_eval_count:])
             fast_average = numpy.mean(rsi_v[-self.fast_eval_count:])
             return slow_average, fast_average, rsi_v
     return None, None, None
Beispiel #6
0
    async def evaluate(self, cryptocurrency, symbol, time_frame, high_candles, low_candles,
                       close_candles, volume_candles, candle):
        eval_proposition = commons_constants.START_PENDING_EVAL_NOTE
        kvo = tulipy.kvo(high_candles,
                         low_candles,
                         close_candles,
                         volume_candles,
                         self.short_period,
                         self.long_period)
        kvo = data_util.drop_nan(kvo)
        if len(kvo) >= self.ema_signal_period:
            kvo_ema = tulipy.ema(kvo, self.ema_signal_period)

            ema_difference = kvo - kvo_ema

            if len(ema_difference) > 1:
                zero_crossing_indexes = EvaluatorUtil.TrendAnalysis.get_threshold_change_indexes(ema_difference, 0)

                current_difference = ema_difference[-1]
                significant_move_threshold = numpy.std(ema_difference)

                factor = 0.2

                if EvaluatorUtil.TrendAnalysis.peak_has_been_reached_already(
                        ema_difference[zero_crossing_indexes[-1]:]):
                    if abs(current_difference) > significant_move_threshold:
                        factor = 1
                    else:
                        factor = 0.5

                eval_proposition = current_difference * factor / significant_move_threshold

                if abs(eval_proposition) > 1:
                    eval_proposition = 1 if eval_proposition > 0 else -1
        self.eval_note = eval_proposition
        await self.evaluation_completed(cryptocurrency, symbol, time_frame,
                                        eval_time=evaluators_util.get_eval_time(full_candle=candle,
                                                                                time_frame=time_frame))
Beispiel #7
0
    def get_moving_average_analysis(data, current_moving_average, time_period):

        time_period_unit_moving_average = tulipy.sma(data, time_period)

        # equalize array size
        min_len_arrays = min(len(time_period_unit_moving_average),
                             len(current_moving_average))

        # compute difference between 1 unit values and others ( >0 means currently up the other one)
        values_difference = \
            (current_moving_average[-min_len_arrays:] - time_period_unit_moving_average[-min_len_arrays:])
        values_difference = data_util.drop_nan(values_difference)

        if len(values_difference):
            # indexes where current_unit_moving_average crosses time_period_unit_moving_average
            crossing_indexes = EvaluatorUtil.TrendAnalysis.get_threshold_change_indexes(
                values_difference, 0)

            multiplier = 1 if values_difference[-1] > 0 else -1

            # check at least some data crossed 0
            if crossing_indexes:
                normalized_data = data_util.normalize_data(values_difference)
                current_value = min(abs(normalized_data[-1]) * 2, 1)
                if math.isnan(current_value):
                    return 0
                # check <= values_difference.count()-1if current value is max/min
                if current_value == 0 or current_value == 1:
                    chances_to_be_max = EvaluatorUtil.TrendAnalysis.get_estimation_of_move_state_relatively_to_previous_moves_length(
                        crossing_indexes, values_difference)
                    return multiplier * current_value * chances_to_be_max
                # other case: maxima already reached => return distance to max
                else:
                    return multiplier * current_value

        # just crossed the average => neutral
        return 0
Beispiel #8
0
def test_drop_nan():
    assert np.array_equal(drop_nan(np.array([1, np.nan, 2, 3, np.nan])),
                          np.array([1, 2, 3]))
    assert np.array_equal(drop_nan(np.array([np.nan, np.nan, np.nan])),
                          np.array([]))