Exemple #1
0
def test_trending_up():

    df = to_dataframe(data)
    ma = TA.HMA(df)
    assert isinstance(trending_up(ma, 10), Series)

    assert not trending_up(ma, 10).values[-1]
Exemple #2
0
    def buy(self,
            period: int = 14,
            period2: int = 14,
            lower_bound: int = 10) -> np.bool_:
        """
        :param period:
        :param period2:
        :param lower_bound:
        :return:
        """
        try:
            raw_mfi = TA.MFI(self.df, period)
        except TradeSignalException as error:
            raise error
        else:
            buying = raw_mfi.iloc[-1] > lower_bound and raw_mfi.iloc[
                -2] <= lower_bound and trending_up(
                    raw_mfi.iloc[:-2], period=int(period / 2)).iloc[-1]
            if SU.is_valid_dataframe(self.df2):
                try:
                    raw_mfi2 = TA.MFI(self.df2, period2)
                except TradeSignalException as error:
                    raise error
                else:
                    buying = buying and raw_mfi2.iloc[
                        -1] > lower_bound and raw_mfi2.iloc[
                            -2] <= lower_bound and trending_up(
                                raw_mfi2.iloc[:-2], period=int(
                                    period2 / 2)).iloc[-1]

            return buying
    def buy(self,
            period_fast: int = 12,
            period_slow: int = 26,
            signal: int = 9,
            column: str = "close",
            adjust: bool = True,
            period_fast2: int = 12,
            period_slow2: int = 26,
            signal2: int = 9) -> np.bool_:
        """Calculate a moving average convergence-divergence buy signal from a bullish signal crossover.

        An optional second dataframe can be used to calculate the signal for a different time period

        :param period_fast:
        :param period_slow:
        :param signal:
        :param column:
        :param adjust:
        :param period_fast2:
        :param period_slow2:
        :param signal2:
        :return bool:
        """

        try:
            raw_macd = TA.MACD(self.df, period_fast, period_slow, signal, column, adjust)
        except TradeSignalException as error:
            raise error

        else:

            _is_negative = raw_macd["MACD"].iloc[-1] < 0
            _below_signal = raw_macd["MACD"].iloc[-1] < raw_macd["SIGNAL"].iloc[-1]
            _trending_up = trending_up(raw_macd["MACD"].iloc[:-2], period=int(period_fast/2)).iloc[-1]
            buying = _is_negative and _below_signal and _trending_up

            if SU.is_valid_dataframe(self.df2):
                try:
                    raw_macd2 = TA.MACD(self.df2, period_fast2, period_slow2, signal2, column, adjust)
                except TradeSignalException as error:
                    raise error
                else:
                    _is_negative2 = raw_macd2["MACD"].iloc[-1] < 0
                    _below_signal2 = raw_macd2["MACD"].iloc[-1] < raw_macd2["SIGNAL"].iloc[-1]
                    _trending_up2 = trending_up(raw_macd2["MACD"].iloc[:-2], period=int(period_fast2/2)).iloc[-1]
                    buying = buying and _is_negative2 and _below_signal2 and _trending_up2

            return buying
Exemple #4
0
    def buy(self, column: str = "close", lookback: int = 5) -> np.bool_:

        try:
            raw_obv = TA.OBV(self.df, column)
        except TradeSignalException as error:
            raise error
        else:
            buying = trending_up(self.df["close"], lookback).iloc[-1] and trending_down(raw_obv, lookback).iloc[-1]
            if SU.is_valid_dataframe(self.df2):
                try:
                    raw_obv2 = TA.OBV(self.df2, column)
                except TradeSignalException as error:
                    raise error
                else:
                    buying = buying and trending_up(self.df2["close"], lookback).iloc[-1] and trending_down(raw_obv2, lookback).iloc[-1]

            return buying
Exemple #5
0
    def buy(self,
            period: int = 14,
            ema_period: int = 60,
            column: str = "close",
            adjust: bool = True,
            period2: int = 14,
            ema_period2: int = 60) -> np.bool_:
        """Bullish buy signal calculation based on volume zone oscillator (VZO).

        The flag is generated based on a number of conditional statements generated from technical indicators.
        The basic buy rule is to buy when a bullish crossover happens -- the VZO crossing over the -40% line

        Along with the bullish crossover, we can use a 14 period average directional index (ADX), directional
        movement index (DMI), and a 60 period EMA to confirm the trend. We do this by making sure the ADX is
        at least 20, the DMI is trending upward (DI+ > DI-) and that the close price has crossed over the 60 day EMA.

        An example usage might look like:

        if vzo.buy() and some_other_indicator():
            execute_buy_order()

        :param period:
        :param ema_period:
        :param column:
        :param adjust:
        :param period2:
        :param ema_period2:
        :return bool:
        """
        try:
            # grab the indicators we need (VZO, ADX, DMI, EMA) for this signal
            vzo = TA.VZO(self.df, period, column, adjust)
            adx = TA.ADX(self.df, period, adjust)
            dmi = TA.DMI(self.df, period, adjust)
            ema = TA.EMA(self.df, ema_period, column, adjust)
        except TradeSignalException as error:
            raise error
        else:
            _vzo_bullish_cross = vzo.iloc[-1] > -40 and trending_up(vzo.iloc[:-2], period=int(period/2)).iloc[-1]
            _adx_trending = adx.iloc[-1] > 20
            _dmi_positive = dmi["DI+"].iloc[-1] > dmi["DI-"].iloc[-1]
            _ema_bullish_cross = self.df["close"].iloc[-1] > ema.iloc[-1] and \
                trending_up(self.df["close"].iloc[:-2], period=int(period/2)).iloc[-1]
            buying = _vzo_bullish_cross and \
                (_adx_trending or _dmi_positive) and \
                _ema_bullish_cross

            if SU.is_valid_dataframe(self.df2):
                try:
                    # grab the indicators we need (VZO, ADX, DMI, EMA) for this signal
                    vzo2 = TA.VZO(self.df2, period2, column, adjust)
                    adx2 = TA.ADX(self.df2, period2, adjust)
                    dmi2 = TA.DMI(self.df2, period2, adjust)
                    ema2 = TA.EMA(self.df2, ema_period2, column, adjust)
                except TradeSignalException as error:
                    raise error
                else:
                    _vzo_bullish_cross2 = vzo2.iloc[-1] > -40 and trending_up(vzo2.iloc[:-2], period=int(period2/2)).iloc[-1]
                    _adx_trending2 = adx2.iloc[-1] > 20
                    _dmi_positive2 = dmi2["DI+"].iloc[-1] > dmi2["DI-"].iloc[-1]
                    _ema_bullish_cross2 = self.df2["close"].iloc[-1] > ema2.iloc[-1] and \
                        trending_up(self.df2["close"].iloc[:-2], period=int(period2/2)).iloc[-1]
                    buying = buying and _vzo_bullish_cross2 and (_adx_trending2 or _dmi_positive2) and _ema_bullish_cross2

            return buying