def segtrends(dataframe, field='close', segments=2, charts=False):
    """
    Turn minitrends to iterative process more easily adaptable to
    implementation in simple trading systems; allows backtesting functionality.

    :param dataframe: incomming data matrix
    :param field: for which column would you like to generate the trendline
    :param x: One-dimensional data set
    :param window: How long the trendlines should be. If window < 1, then it
                   will be taken as a percentage of the size of the data
    :param charts: Boolean value saying whether to print chart to screen
    """
    x = dataframe[field]
    import numpy as np
    y = np.array(x)
    segments = int(segments)
    maxima = np.ones(segments)
    minima = np.ones(segments)
    segsize = int(len(y) / segments)
    for i in range(1, segments + 1):
        ind2 = i * segsize
        ind1 = ind2 - segsize
        maxima[i - 1] = max(y[ind1:ind2])
        minima[i - 1] = min(y[ind1:ind2])
    x_maxima = np.ones(segments)
    x_minima = np.ones(segments)
    for i in range(0, segments):
        x_maxima[i] = np.where(y == maxima[i])[0][0]
        x_minima[i] = np.where(y == minima[i])[0][0]
    if charts:
        import matplotlib.pyplot as plt
        plt.plot(y)
        plt.grid(True)
    for i in range(0, segments - 1):
        maxslope = (maxima[i + 1] - maxima[i]) / (x_maxima[i + 1] - x_maxima[i]
            )
        a_max = maxima[i] - maxslope * x_maxima[i]
        b_max = maxima[i] + maxslope * (len(y) - x_maxima[i])
        maxline = np.linspace(a_max, b_max, len(y))
        minslope = (minima[i + 1] - minima[i]) / (x_minima[i + 1] - x_minima[i]
            )
        a_min = minima[i] - minslope * x_minima[i]
        b_min = minima[i] + minslope * (len(y) - x_minima[i])
        minline = np.linspace(a_min, b_min, len(y))
        if charts:
            plt.plot(maxline, 'g')
            plt.plot(minline, 'r')
    if charts:
        plt.show()
    import pandas as pd
    trends = np.transpose(np.array((x, maxline, minline)))
    trends = pd.DataFrame(trends, index=np.arange(0, len(x)), columns=[
        'Data', 'Max Line', 'Min Line'])
    return trends
