Ejemplo n.º 1
0
def find_trend(df, column, direction, dir_header):
    trend = df[column].copy()
    more_trend = True
    last_end = df.index[0]
    trend_gp_id = 0
    response = True
    while more_trend:
        input_df = df[last_end:].copy()
        res = trendet.identify_df_trends(df=input_df,
                                         column=column,
                                         identify=direction)
        if dir_header in res.columns.to_list():
            trend_temp = res[dir_header]
            trend_group_list = trend_temp.dropna().unique()
            if len(trend_group_list) == 0:
                more_trend = False
            else:
                trend.loc[last_end:] = trend_temp.apply(rename_trend_group,
                                                        gp_id=trend_gp_id)
                if trend_group_list[-1] == 'Z':
                    # more trend to be identified
                    last_end = trend_temp[trend_temp == 'Z'].index[-1]
                    trend_gp_id = trend_gp_id + 1
                else:
                    more_trend = False
        else:
            # suddenly the trendet.identify_df_trends fails to create column "Up Trend" or "Down Trend"
            trend = pd.Series(np.nan, index=df.index)
            more_trend = False
            response = False
    return trend, response
Ejemplo n.º 2
0
 def minMaxTrend_buylogic_benchmark(self,daysToSubtract=180) :
     resizedDf = self.stockValue.iloc[-daysToSubtract:]
     trends = identify_df_trends(df=resizedDf, column='Close')
     trends.reset_index(inplace=True)
     labels = trends['Up Trend'].dropna().unique().tolist()
     enterDays = [];     exitDays = []
     for label in labels :
         enterDays.append(trends[trends['Up Trend'] == label]['Date'].iloc[0])
         exitDays.append(trends[trends['Up Trend'] == label]['Date'].iloc[-1])
     return trends, enterDays, exitDays
Ejemplo n.º 3
0
def test_trendet():
    """
    This function checks that main functions of trendet work properly.
    """

    print(trendet.__author__)
    print(trendet.__version__)

    params = [
        {
            'stock': 'BBVA',
            'country': 'Spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 2,
            'labels': ['A', 'B'],
            'identify': 'both'
        },
        {
            'stock': 'BBVA',
            'country': 'Spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 2,
            'labels': None,
            'identify': 'up',
        },
        {
            'stock': 'BBVA',
            'country': 'Spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 2,
            'labels': ['A', 'B'],
            'identify': 'up',
        },
        {
            'stock': 'BBVA',
            'country': 'Spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 2,
            'labels': ['A', 'B'],
            'identify': 'down',
        },
        {
            'stock': 'BBVA',
            'country': 'Spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 2,
            'labels': None,
            'identify': 'down',
        }
    ]

    stocks = get_stocks_list(country='Spain')

    for stock in stocks[:25]:
        obj = {
            'stock': stock,
            'country': 'Spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 5,
            'labels': None,
            'identify': 'both',
        }

        params.append(obj)

    for param in params:
        trendet.identify_trends(stock=param['stock'],
                                country=param['country'],
                                from_date=param['from_date'],
                                to_date=param['to_date'],
                                window_size=param['window_size'],
                                trend_limit=param['trend_limit'],
                                labels=param['labels'],
                                identify=param['identify'])

        trendet.identify_all_trends(stock=param['stock'],
                                    country=param['country'],
                                    from_date=param['from_date'],
                                    to_date=param['to_date'],
                                    window_size=param['window_size'],
                                    identify=param['identify'])

    df = get_stock_historical_data(stock='REP',
                                   country='Spain',
                                   from_date='01/01/2018',
                                   to_date='01/01/2019')

    params = [
        {
            'column': 'Close',
            'window_size': 5,
            'identify': 'both'
        },
        {
            'column': 'Close',
            'window_size': 5,
            'identify': 'up'
        },
        {
            'column': 'Close',
            'window_size': 5,
            'identify': 'down'
        },
    ]

    for param in params:
        trendet.identify_df_trends(df=df,
                                   column=param['column'],
                                   window_size=param['window_size'],
                                   identify=param['identify'])
Ejemplo n.º 4
0
def test_errors():
    """
    This function raises trendet errors to improve coverage
    """

    params = [
        {
            'equity': ['error'],
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': None,
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'error',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': None,
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'error',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': ['error'],
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': None,
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': None,
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2019',
            'to_date': '01/01/2018',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01-2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '_01*01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 0,
            'trend_limit': 3,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': -1,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': None,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': None,
            'trend_limit': 1,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 'error',
            'trend_limit': 1,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 1,
            'trend_limit': 'error',
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 2,
            'trend_limit': 5,
            'labels': None,
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': ['a', 'b'],
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': 'error',
            'identify': 'both',
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': ['error'],
        },
        {
            'equity': 'bbva',
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 3,
            'labels': None,
            'identify': 'error',
        },
    ]

    for param in params:
        try:
            trendet.identify_trends(equity=param['equity'],
                                    country=param['country'],
                                    from_date=param['from_date'],
                                    to_date=param['to_date'],
                                    window_size=param['window_size'],
                                    trend_limit=param['trend_limit'],
                                    labels=param['labels'],
                                    identify=param['identify'])
        except:
            pass

        try:
            trendet.identify_all_trends(equity=param['equity'],
                                        country=param['country'],
                                        from_date=param['from_date'],
                                        to_date=param['to_date'],
                                        window_size=param['window_size'],
                                        identify=param['identify'])
        except:
            pass

    df = investpy.get_historical_data(equity='repsol',
                                      country='spain',
                                      from_date='01/01/2018',
                                      to_date='01/01/2019')

    df['str'] = 'error'

    params = [
        {
            'df': None,
            'column': 'Close',
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': ['error'],
            'column': 'Close',
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': df,
            'column': None,
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': df,
            'column': ['error'],
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': df,
            'column': 'error',
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': df,
            'column': 'str',
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': df,
            'column': 'Close',
            'window_size': None,
            'identify': 'both'
        },
        {
            'df': df,
            'column': 'Close',
            'window_size': 1,
            'identify': 'both'
        },
        {
            'df': df,
            'column': 'Close',
            'window_size': 5,
            'identify': ['error']
        },
        {
            'df': df,
            'column': 'Close',
            'window_size': 5,
            'identify': 'error'
        },
    ]

    for param in params:
        try:
            trendet.identify_df_trends(df=param['df'],
                                       column=param['column'],
                                       window_size=param['window_size'],
                                       identify=param['identify'])
        except:
            pass
Ejemplo n.º 5
0
def test_trendet():
    """
    This function checks that main functions of trendet work properly.
    """

    author = trendet.__author__
    print(author)
    version = trendet.__version__
    print(version)

    equities = investpy.get_equities(country='spain')

    equities = equities['name'].tolist()

    params = list()

    for equity in equities[:25]:
        obj = {
            'equity': equity,
            'country': 'spain',
            'from_date': '01/01/2018',
            'to_date': '01/01/2019',
            'window_size': 5,
            'trend_limit': 5,
            'labels': None,
            'identify': 'both',
        }

        params.append(obj)

    obj = {
        'equity': 'bbva',
        'country': 'spain',
        'from_date': '01/01/2018',
        'to_date': '01/01/2019',
        'window_size': 5,
        'trend_limit': 2,
        'labels': ['A', 'B'],
        'identify': 'both'
    }

    params.append(obj)

    obj = {
        'equity': 'bbva',
        'country': 'spain',
        'from_date': '01/01/2018',
        'to_date': '01/01/2019',
        'window_size': 5,
        'trend_limit': 2,
        'labels': None,
        'identify': 'up',
    }

    params.append(obj)

    obj = {
        'equity': 'bbva',
        'country': 'spain',
        'from_date': '01/01/2018',
        'to_date': '01/01/2019',
        'window_size': 5,
        'trend_limit': 2,
        'labels': ['A', 'B'],
        'identify': 'up',
    }

    params.append(obj)

    obj = {
        'equity': 'bbva',
        'country': 'spain',
        'from_date': '01/01/2018',
        'to_date': '01/01/2019',
        'window_size': 5,
        'trend_limit': 2,
        'labels': None,
        'identify': 'down',
    }

    params.append(obj)

    obj = {
        'equity': 'bbva',
        'country': 'spain',
        'from_date': '01/01/2018',
        'to_date': '01/01/2019',
        'window_size': 5,
        'trend_limit': 2,
        'labels': ['A', 'B'],
        'identify': 'down',
    }

    params.append(obj)

    for param in params:
        trendet.identify_trends(equity=param['equity'],
                                country=param['country'],
                                from_date=param['from_date'],
                                to_date=param['to_date'],
                                window_size=param['window_size'],
                                trend_limit=param['trend_limit'],
                                labels=param['labels'],
                                identify=param['identify'])

        trendet.identify_all_trends(equity=param['equity'],
                                    country=param['country'],
                                    from_date=param['from_date'],
                                    to_date=param['to_date'],
                                    window_size=param['window_size'],
                                    identify=param['identify'])

    df = investpy.get_historical_data(equity='repsol',
                                      country='spain',
                                      from_date='01/01/2018',
                                      to_date='01/01/2019')

    params = [
        {
            'df': df,
            'column': 'Close',
            'window_size': 5,
            'identify': 'both'
        },
        {
            'df': df,
            'column': 'Close',
            'window_size': 5,
            'identify': 'up'
        },
        {
            'df': df,
            'column': 'Close',
            'window_size': 5,
            'identify': 'down'
        },
    ]

    for param in params:
        trendet.identify_df_trends(df=param['df'],
                                   column=param['column'],
                                   window_size=param['window_size'],
                                   identify=param['identify'])