|
| 1 | +from enum import Enum, auto |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from .abstract import BaseScorer |
| 6 | + |
| 7 | + |
| 8 | +class PointEventType(Enum): |
| 9 | + CUT_BODY = auto() |
| 10 | + CUT_WICK = auto() |
| 11 | + TOUCH_DOWN_HIGHLOW = auto() |
| 12 | + TOUCH_DOWN = auto() |
| 13 | + TOUCH_UP_HIGHLOW = auto() |
| 14 | + TOUCH_UP = auto() |
| 15 | + |
| 16 | + |
| 17 | +class PointEvent: |
| 18 | + def __init__(self, event_type, timestamp, score_change): |
| 19 | + self.type = event_type |
| 20 | + self.timestamp = timestamp |
| 21 | + self.score_change = score_change |
| 22 | + |
| 23 | + |
| 24 | +class PointScore: |
| 25 | + |
| 26 | + def __init__(self, point, score, point_event_list): |
| 27 | + self.point = point |
| 28 | + self.score = score |
| 29 | + self.point_event_list = point_event_list |
| 30 | + |
| 31 | + |
| 32 | +class TouchScorer(BaseScorer): |
| 33 | + |
| 34 | + def __init__(self, min_candles_between_body_cuts=5, diff_perc_from_extreme=0.05, |
| 35 | + min_distance_between_levels=0.1, min_trend_percent=0.5, diff_perc_for_candle_close=0.05, |
| 36 | + score_for_cut_body=-2, score_for_cut_wick=-1, score_for_touch_high_low=1, score_for_touch_normal=2): |
| 37 | + |
| 38 | + self.DIFF_PERC_FOR_CANDLE_CLOSE = diff_perc_for_candle_close |
| 39 | + self.MIN_DIFF_FOR_CONSECUTIVE_CUT = min_candles_between_body_cuts |
| 40 | + self.DIFF_PERC_FROM_EXTREME = diff_perc_from_extreme |
| 41 | + self.DIFF_PERC_FOR_INTRASR_DISTANCE = min_distance_between_levels |
| 42 | + self.MIN_PERC_FOR_TREND = min_trend_percent |
| 43 | + |
| 44 | + self.score_for_cut_body = score_for_cut_body |
| 45 | + self.score_for_cut_wick = score_for_cut_wick |
| 46 | + self.score_for_touch_high_low = score_for_touch_high_low |
| 47 | + self.score_for_touch_normal = score_for_touch_normal |
| 48 | + |
| 49 | + self._scores = None |
| 50 | + |
| 51 | + def closeFromExtreme(self, key, min_value, max_value): |
| 52 | + return abs(key - min_value) < (min_value * self.DIFF_PERC_FROM_EXTREME / 100.0) or \ |
| 53 | + abs(key - max_value) < (max_value * self.DIFF_PERC_FROM_EXTREME / 100) |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def getMin(candles): |
| 57 | + return candles['Low'].min() |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def getMax(candles): |
| 61 | + return candles['High'].max() |
| 62 | + |
| 63 | + def similar(self, key, used): |
| 64 | + for value in used: |
| 65 | + if abs(key - value) <= (self.DIFF_PERC_FOR_INTRASR_DISTANCE * value / 100): |
| 66 | + return True |
| 67 | + return False |
| 68 | + |
| 69 | + def fit(self, levels, ohlc_df): |
| 70 | + scores = [] |
| 71 | + high_low_list = self._get_high_low_list(ohlc_df) |
| 72 | + |
| 73 | + for i, obj in enumerate(levels): |
| 74 | + if isinstance(obj, float): |
| 75 | + price = obj |
| 76 | + elif isinstance(obj, dict): |
| 77 | + try: |
| 78 | + price = obj['price'] |
| 79 | + except KeyError: |
| 80 | + raise Exception('`levels` supposed to be a list of floats or list of dicts with `price` key') |
| 81 | + else: |
| 82 | + raise Exception('`levels` supposed to be a list of floats or list of dicts with `price` key') |
| 83 | + |
| 84 | + score = self._get_level_score(ohlc_df, high_low_list, price) |
| 85 | + scores.append((i, price, score)) |
| 86 | + |
| 87 | + self._scores = scores |
| 88 | + |
| 89 | + @property |
| 90 | + def scores(self): |
| 91 | + return self._scores |
| 92 | + |
| 93 | + @staticmethod |
| 94 | + def _get_high_low_list(ohlc_df): |
| 95 | + rolling_lows = ohlc_df['Low'].rolling(window=3).min().shift(-1) |
| 96 | + rolling_highs = ohlc_df['High'].rolling(window=3).min().shift(-1) |
| 97 | + high_low_list = np.where(ohlc_df['Low'] == rolling_lows, True, False) |
| 98 | + high_low_list = np.where(ohlc_df['High'] == rolling_highs, True, high_low_list) |
| 99 | + |
| 100 | + return high_low_list.tolist() |
| 101 | + |
| 102 | + def _get_level_score(self, candles, high_low_marks, price): |
| 103 | + events = [] |
| 104 | + score = 0.0 |
| 105 | + pos = 0 |
| 106 | + last_cut_pos = -10 |
| 107 | + for i in range(len(candles)): |
| 108 | + candle = candles.iloc[i] |
| 109 | + # If the body of the candle cuts through the price, then deduct some score |
| 110 | + if self.cut_body(price, candle) and pos - last_cut_pos > self.MIN_DIFF_FOR_CONSECUTIVE_CUT: |
| 111 | + score += self.score_for_cut_body |
| 112 | + last_cut_pos = pos |
| 113 | + events.append(PointEvent(PointEventType.CUT_BODY, candle['Datetime'], self.score_for_cut_body)) |
| 114 | + # If the wick of the candle cuts through the price, then deduct some score |
| 115 | + elif self.cut_wick(price, candle) and (pos - last_cut_pos > self.MIN_DIFF_FOR_CONSECUTIVE_CUT): |
| 116 | + score += self.score_for_cut_wick |
| 117 | + last_cut_pos = pos |
| 118 | + events.append(PointEvent(PointEventType.CUT_WICK, candle['Datetime'], self.score_for_cut_body)) |
| 119 | + # If the if is close the high of some candle and it was in an uptrend, then add some score to this |
| 120 | + elif self.touch_high(price, candle) and self.in_up_trend(candles, price, pos): |
| 121 | + high_low_value = high_low_marks[pos] |
| 122 | + # If it is a high, then add some score S1 |
| 123 | + if high_low_value: |
| 124 | + score += self.score_for_touch_high_low |
| 125 | + events.append( |
| 126 | + PointEvent(PointEventType.TOUCH_UP_HIGHLOW, candle['Datetime'], self.score_for_touch_high_low)) |
| 127 | + # Else add S2. S2 > S1 |
| 128 | + else: |
| 129 | + score += self.score_for_touch_normal |
| 130 | + events.append(PointEvent(PointEventType.TOUCH_UP, candle['Datetime'], self.score_for_touch_normal)) |
| 131 | + |
| 132 | + # If the if is close the low of some candle and it was in an downtrend, then add some score to this |
| 133 | + elif self.touch_low(price, candle) and self.in_down_trend(candles, price, pos): |
| 134 | + high_low_value = high_low_marks[pos] |
| 135 | + # If it is a high, then add some score S1 |
| 136 | + if high_low_value is not None and not high_low_value: |
| 137 | + score += self.score_for_touch_high_low |
| 138 | + events.append( |
| 139 | + PointEvent(PointEventType.TOUCH_DOWN, candle['Datetime'], self.score_for_touch_high_low)) |
| 140 | + # Else add S2. S2 > S1 |
| 141 | + else: |
| 142 | + score += self.score_for_touch_normal |
| 143 | + events.append( |
| 144 | + PointEvent(PointEventType.TOUCH_DOWN_HIGHLOW, candle['Datetime'], self.score_for_touch_normal)) |
| 145 | + |
| 146 | + pos += 1 |
| 147 | + |
| 148 | + return PointScore(price, score, events) |
| 149 | + |
| 150 | + def in_down_trend(self, candles, price, start_pos): |
| 151 | + # Either move #MIN_PERC_FOR_TREND in direction of trend, or cut through the price |
| 152 | + pos = start_pos |
| 153 | + while pos >= 0: |
| 154 | + if candles['Low'].iat[pos] < price: |
| 155 | + return False |
| 156 | + if candles['Low'].iat[pos] - price > (price * self.MIN_PERC_FOR_TREND / 100): |
| 157 | + return True |
| 158 | + pos -= 1 |
| 159 | + |
| 160 | + return False |
| 161 | + |
| 162 | + def in_up_trend(self, candles, price, start_pos): |
| 163 | + pos = start_pos |
| 164 | + while pos >= 0: |
| 165 | + if candles['High'].iat[pos] > price: |
| 166 | + return False |
| 167 | + if price - candles['Low'].iat[pos] > (price * self.MIN_PERC_FOR_TREND / 100): |
| 168 | + return True |
| 169 | + pos -= 1 |
| 170 | + |
| 171 | + return False |
| 172 | + |
| 173 | + def touch_high(self, price, candle): |
| 174 | + high = candle['High'] |
| 175 | + ltp = candle['Close'] |
| 176 | + return high <= price and abs(high - price) < ltp * self.DIFF_PERC_FOR_CANDLE_CLOSE / 100 |
| 177 | + |
| 178 | + def touch_low(self, price, candle): |
| 179 | + low = candle['Low'] |
| 180 | + ltp = candle['Close'] |
| 181 | + return low >= price and abs(low - price) < ltp * self.DIFF_PERC_FOR_CANDLE_CLOSE / 100 |
| 182 | + |
| 183 | + def cut_body(self, point, candle): |
| 184 | + return max(candle['Open'], candle['Close']) > point and min(candle['Open'], candle['Close']) < point |
| 185 | + |
| 186 | + def cut_wick(self, price, candle): |
| 187 | + return not self.cut_body(price, candle) and candle['High'] > price and candle['Low'] < price |
0 commit comments