예제 #1
0
파일: DEMA.py 프로젝트: solocarrie/talipp
class DEMA(Indicator):
    """
    Double Exponential Moving Average

    Output: a list of floats
    """
    def __init__(self,
                 period: int,
                 input_values: List[float] = None,
                 input_indicator: Indicator = None):
        super().__init__()

        self.period = period

        self.ema = EMA(period)
        self.add_sub_indicator(self.ema)

        self.ema_ema = EMA(period)
        self.add_managed_sequence(self.ema_ema)

        self.initialize(input_values, input_indicator)

    def _calculate_new_value(self) -> Any:
        if not self.ema.has_output_value():
            return None

        self.ema_ema.add_input_value(self.ema[-1])

        if not self.ema_ema.has_output_value():
            return None

        return 2.0 * self.ema[-1] - self.ema_ema[-1]
예제 #2
0
class ForceIndex(Indicator):
    """
    Force Index

    Output: a list of floats
    """
    def __init__(self, period: int, input_values: List[OHLCV] = None):
        super().__init__()

        self.ema = EMA(period)
        self.add_managed_sequence(self.ema)

        self.initialize(input_values)

    def _calculate_new_value(self) -> Any:
        if len(self.input_values) < 2:
            return None

        self.ema.add_input_value(
            (self.input_values[-1].close - self.input_values[-2].close) *
            self.input_values[-1].volume)

        if len(self.ema) > 1:
            return self.ema[-1]
        else:
            return None
예제 #3
0
class MassIndex(Indicator):
    """
    Mass Index

    Output: a list of floats
    """
    def __init__(self,
                 ema_period: int,
                 ema_ema_period: int,
                 ema_ratio_period: int,
                 input_values: List[OHLCV] = None):
        super().__init__()

        self.ema_ratio_period = ema_ratio_period

        self.ema = EMA(ema_period)
        self.ema_ema = EMA(ema_ema_period)
        self.ema_ratio = []

        self.add_managed_sequence(self.ema)
        self.add_managed_sequence(self.ema_ema)
        self.add_managed_sequence(self.ema_ratio)

        self.initialize(input_values)

    def _calculate_new_value(self) -> Any:
        value = self.input_values[-1]
        self.ema.add_input_value(value.high - value.low)

        if not self.ema.has_output_value():
            return None

        self.ema_ema.add_input_value(self.ema[-1])

        if not self.ema_ema.has_output_value():
            return None

        self.ema_ratio.append(self.ema[-1] / float(self.ema_ema[-1]))

        if len(self.ema_ratio) < self.ema_ratio_period:
            return None

        return sum(self.ema_ratio[-self.ema_ratio_period:])
예제 #4
0
class TSI(Indicator):
    """
    True Strength Index

    Output: a list of floats
    """
    def __init__(self,
                 fast_period: int,
                 slow_period: int,
                 input_values: List[float] = None,
                 input_indicator: Indicator = None,
                 value_extractor: ValueExtractorType = None):
        super().__init__(value_extractor=value_extractor)

        self.slow_ema = EMA(slow_period)
        self.add_managed_sequence(self.slow_ema)
        self.fast_ema = EMA(fast_period, input_indicator=self.slow_ema)

        self.abs_slow_ema = EMA(slow_period)
        self.add_managed_sequence(self.abs_slow_ema)
        self.abs_fast_ema = EMA(fast_period, input_indicator=self.abs_slow_ema)

        self.initialize(input_values, input_indicator)

    def _calculate_new_value(self) -> Any:
        if len(self.input_values) < 2:
            return None

        self.slow_ema.add_input_value(self.input_values[-1] -
                                      self.input_values[-2])
        self.abs_slow_ema.add_input_value(
            abs(self.input_values[-1] - self.input_values[-2]))

        if len(self.fast_ema) < 1:
            return None

        if self.abs_fast_ema[-1] != 0:
            return 100.0 * (self.fast_ema[-1] / self.abs_fast_ema[-1])
        else:
            if len(self.output_values) > 0:
                return self.output_values[-1]
            else:
                return None
예제 #5
0
class MACD(Indicator):
    """
    Moving Average Convergence Divergence

    Output: a list of MACDVal
    """
    def __init__(self,
                 fast_period: int,
                 slow_period: int,
                 signal_period: int,
                 input_values: List[float] = None,
                 input_indicator: Indicator = None):
        super().__init__()

        self.ema_fast = EMA(fast_period)
        self.ema_slow = EMA(slow_period)
        self.signal_line = EMA(signal_period)

        self.add_sub_indicator(self.ema_fast)
        self.add_sub_indicator(self.ema_slow)
        self.add_managed_sequence(self.signal_line)

        self.initialize(input_values, input_indicator)

    def _calculate_new_value(self) -> Any:
        if len(self.ema_fast) > 0 and len(self.ema_slow) > 0:
            macd = self.ema_fast[-1] - self.ema_slow[-1]
            self.signal_line.add_input_value(macd)

            if len(self.signal_line) > 0:
                signal = self.signal_line[-1]
            else:
                signal = None

            histogram = None
            if macd is not None and signal is not None:
                histogram = macd - signal

            return MACDVal(macd, signal, histogram)
        else:
            return None
예제 #6
0
class ChaikinOsc(Indicator):
    """
    Chaikin Oscillator

    Output: a list of floats
    """
    def __init__(self,
                 period_fast: int,
                 period_slow: int,
                 input_values: List[OHLCV] = None):
        super().__init__()

        self.period_fast = period_fast
        self.period_slow = period_slow

        self.accu_dist = AccuDist()
        self.add_sub_indicator(self.accu_dist)

        self.ema_fast = EMA(period_fast)
        self.add_managed_sequence(self.ema_fast)

        self.ema_slow = EMA(period_slow)
        self.add_managed_sequence(self.ema_slow)

        self.initialize(input_values)

    def _calculate_new_value(self) -> Any:
        if not self.accu_dist.has_output_value():
            return None

        self.ema_fast.add_input_value(self.accu_dist[-1])
        self.ema_slow.add_input_value(self.accu_dist[-1])

        if not self.ema_fast.has_output_value(
        ) or not self.ema_slow.has_output_value():
            return None

        return self.ema_fast[-1] - self.ema_slow[-1]
예제 #7
0
파일: KVO.py 프로젝트: solocarrie/talipp
class KVO(Indicator):
    """
    Klinger Volume Oscillator

    Output: a list of floats
    """
    def __init__(self,
                 fast_period: int,
                 slow_period: int,
                 input_values: List[OHLCV] = None):
        super().__init__()

        self.fast_ema = EMA(fast_period)
        self.add_managed_sequence(self.fast_ema)

        self.slow_ema = EMA(slow_period)
        self.add_managed_sequence(self.slow_ema)

        self.trend = []
        self.add_managed_sequence(self.trend)

        self.cumulative_measurement = []
        self.add_managed_sequence(self.cumulative_measurement)

        self.initialize(input_values)

    def _calculate_new_value(self) -> Any:
        if len(self.input_values) < 2:
            return None

        value = self.input_values[-1]
        value2 = self.input_values[-2]

        if len(self.trend) < 1:
            self.trend.append(0.0)
        else:
            if (value.high + value.low +
                    value.close) > (value2.high + value2.low + value2.close):
                self.trend.append(1.0)
            else:
                self.trend.append(-1.0)

        if len(self.trend) < 2:
            return None

        dm = value.high - value.low
        dm2 = value2.high - value2.low

        if len(self.cumulative_measurement) < 1:
            prev_cm = dm
        else:
            prev_cm = self.cumulative_measurement[-1]

        if self.trend[-1] == self.trend[-2]:
            self.cumulative_measurement.append(prev_cm + dm)
        else:
            self.cumulative_measurement.append(dm2 + dm)

        if self.cumulative_measurement[-1] == 0:
            volume_force = 0.0
        else:
            volume_force = value.volume * abs(
                2 * (dm / self.cumulative_measurement[-1] -
                     1)) * self.trend[-1] * 100

        self.fast_ema.add_input_value(volume_force)
        self.slow_ema.add_input_value(volume_force)

        if len(self.fast_ema) < 1 or len(self.slow_ema) < 1:
            return None

        #print(f"kvo: {self.fast_ema[-1] - self.slow_ema[-1]} vf: {volume_force} dm: {dm} cm: {self.cumulative_measurement[-1]} trend: {self.trend[-1]} volume: {value.volume}")

        return self.fast_ema[-1] - self.slow_ema[-1]