def eval_impl(self): self.eval_note = START_PENDING_EVAL_NOTE long_period_length = 26 if len(self.data[PriceIndexes.IND_PRICE_CLOSE.value]) >= long_period_length: macd, macd_signal, macd_hist = tulipy.macd(self.data[PriceIndexes.IND_PRICE_CLOSE.value], 12, long_period_length, 9) # on macd hist => M pattern: bearish movement, W pattern: bullish movement # max on hist: optimal sell or buy macd_hist = DataUtil.drop_nan(macd_hist) zero_crossing_indexes = TrendAnalysis.get_threshold_change_indexes(macd_hist, 0) last_index = len(macd_hist) - 1 pattern, start_index, end_index = PatternAnalyser.find_pattern(macd_hist, zero_crossing_indexes, last_index) if pattern != 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)
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 = DataUtil.drop_nan(values_difference) if len(values_difference): # indexes where current_unit_moving_average crosses time_period_unit_moving_average crossing_indexes = 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 = DataUtil.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 = 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
async def eval_impl(self): self.eval_note = False short_period = 35 # standard with klinger long_period = 55 # standard with klinger ema_signal_period = 13 # standard ema signal for klinger kvo = tulipy.kvo(self.data[PriceIndexes.IND_PRICE_HIGH.value], self.data[PriceIndexes.IND_PRICE_LOW.value], self.data[PriceIndexes.IND_PRICE_CLOSE.value], self.data[PriceIndexes.IND_PRICE_VOL.value], short_period, long_period) kvo = DataUtil.drop_nan(kvo) if len(kvo) >= ema_signal_period: kvo_ema = tulipy.ema(kvo, ema_signal_period) ema_difference = kvo - kvo_ema if len(ema_difference) > 1: zero_crossing_indexes = 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 = TrendAnalysis.min_has_just_been_reached( ema_difference[-to_consider_kvo:], acceptance_window=0.9, delay=1)
def eval_impl(self): self.eval_note = START_PENDING_EVAL_NOTE period_length = 14 if len(self.data[ PriceIndexes.IND_PRICE_HIGH.value]) > period_length + 10: min_adx = 7.5 max_adx = 45 neutral_adx = 25 adx = tulipy.adx(self.data[PriceIndexes.IND_PRICE_HIGH.value], self.data[PriceIndexes.IND_PRICE_LOW.value], self.data[PriceIndexes.IND_PRICE_CLOSE.value], period_length) instant_ema = tulipy.ema( self.data[PriceIndexes.IND_PRICE_CLOSE.value], 2) slow_ema = tulipy.ema( self.data[PriceIndexes.IND_PRICE_CLOSE.value], 20) adx = DataUtil.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 = TrendAnalysis.get_threshold_change_indexes( adx, neutral_adx) chances_to_be_max = \ 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)))
def eval_impl(self): eval_proposition = START_PENDING_EVAL_NOTE short_period = 35 # standard with klinger long_period = 55 # standard with klinger ema_signal_period = 13 # standard ema signal for klinger kvo = tulipy.kvo(self.data[PriceIndexes.IND_PRICE_HIGH.value], self.data[PriceIndexes.IND_PRICE_LOW.value], self.data[PriceIndexes.IND_PRICE_CLOSE.value], self.data[PriceIndexes.IND_PRICE_VOL.value], short_period, long_period) kvo = DataUtil.drop_nan(kvo) if len(kvo) >= ema_signal_period: kvo_ema = tulipy.ema(kvo, ema_signal_period) ema_difference = kvo - kvo_ema if len(ema_difference) > 1: zero_crossing_indexes = TrendAnalysis.get_threshold_change_indexes( ema_difference, 0) current_difference = ema_difference[-1] significant_move_threshold = numpy.std(ema_difference) factor = 0.2 if 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