Esempio n. 1
0
    def get_order(cls,
                  data,
                  segments=2,
                  window=7,
                  writer=None,
                  charts=False,
                  verbose=False):
        ''' generate orders from segtrends '''
        price = data[cls.field]
        x_maxima, maxima, x_minima, minima = segtrends(price,
                                                       segments,
                                                       window,
                                                       charts=charts)

        if writer or cls.predict:
            features = cls.get_order_features_from_trend(
                segments, x_maxima, maxima, x_minima, minima)
            vol_pct_change = data['Volume'][-(window +
                                              1):].pct_change()[-window:]
            last = data[cls.field][-1]
            roll_mean_var = (pd.rolling_mean(data[cls.field][-window:],
                                             window)[-1] - last) / last
            roll_median_var = (pd.rolling_median(data[cls.field][-window:],
                                                 window)[-1] - last) / last
            for add in (vol_pct_change, roll_mean_var, roll_median_var):
                features = np.append(features, add)
            if writer:
                writer.writerow(features)

        if cls.predict:
            order = -1 if cls.predict([features]) == 0 else 1
            return order
        else:
            return cls.get_order_from_trend(minima, maxima, verbose)
Esempio n. 2
0
    def get_orders(cls, x, segments=2, window=7, charts=True, verbose=False):
        ''' generate orders from segtrends '''
        from filter import movingaverage
        from trendy import segtrends
        x_maxima, maxima, x_minima, minima = segtrends(x, segments, window,
                                                       charts)
        n = len(x)
        y = np.array(x)
        movy = movingaverage(y, window)

        # generate order strategy
        orders = np.zeros(n)
        last_buy = y[0]

        for i in range(1, n):
            # get 2 latest support point y values prior to x
            pmin = minima[np.where(x_minima <= i)]
            pmax = maxima[np.where(x_maxima <= i)]
            buy, last_buy = cls.get_order_from_trend(pmin, pmax, y[i], movy[i],
                                                     last_buy)
            orders[i] = buy

        if verbose:
            print "orders", orders
        return orders
Esempio n. 3
0
def get_trendlines(df, window=1 / 3.0, segments=4, charts=False):
    #gtrends, maxslope, minslope = trdy.gentrends(df, window=window, charts=charts)
    # Modifying the number of segments in trdy.segtrends, modifies the number of S/R lines returned
    strends = trdy.segtrends(df, segments=segments, charts=charts)
    #trdy.minitrends(df.values)
    #strends.append(gtrends)
    points = trdy.iterlines(df, window=20, charts=charts)
    return strends, points
Esempio n. 4
0
    def orders_from_trends(cls, x, segments=2, charts=True, window=7, 
                           sell_momentum=no_momentum, 
                           buy_momentum=no_momentum,
                           title=None):
        ''' generate orders from segtrends '''
        x_maxima, maxima, x_minima, minima = segtrends(x, segments, charts, window, title)
        n = len(x)
        y = np.array(x)
        movy = movingaverage(y, window)
        
        # generate order strategy
        orders = np.zeros(n)
        last_buy = y[0]
        last_sale = y[0]
        
        for i in range(1,n):
            # get 2 latest support point y values prior to x
            pmin = list(minima[np.where(x_minima<=i)][-2:])
            pmax = list(maxima[np.where(x_maxima<=i)][-2:])
            # sell if support slop is negative
            min_sell = True if ((len(pmin)==2) and (pmin[1]-pmin[0])<0) else False 
            max_sell = True if ((len(pmax)==2) and (pmax[1]-pmax[0])<0) else False 

            # if support down, sell
            buy = -1 if (min_sell and max_sell) else 0
            # buy only if lower the moving average else sale
            buy = 1 if ((buy == 0) and (y[i]<movy[i])) else -1
            # sell only if ... # MUCH BETTER WITHOUT IT
            #buy= -1 if ((buy == -1) and y[i]>last_buy) else 1
      
            buy_price_dec = y[i]<last_buy
            sale_price_dec = y[i]<last_sale
            orders[i] = buy
            last_buy = y[i] if (buy==1) else last_buy
            last_sale = y[i] if (buy==-1) else last_sale
        
            
            # add momentum for buy 
            if buy_momentum and (buy==1) and (orders[i-1]>=1):
                #if buy_price_dec:
                #orders[i]=orders[i-1]*2#round(math.log(2*orders[i-1])+1)
                orders[i]=buy_momentum(orders[i-1])#round(math.log(2*orders[i-1])+1)
                #else:
                #   orders[i]=max(1, round(orders[i-1]/2))
                # add momentum for sale
            elif sell_momentum and (buy==-1) and (orders[i-1]<=-1):
                #if sale_price_dec:
                #orders[i]*=round(math.log(abs(orders[i-1]*2))+1)
                orders[i]=-sell_momentum(orders[i-1])
                #else:
                #    orders[i]=max(1, round(orders[i-1]/2))
        
        # ensure no order are taken at the begining
        for i in range(window):
            orders[i]=0
        return orders
Esempio n. 5
0
def orders_from_trends(x, segments=2, charts=True, window=7, momentum=False):
    ''' generate orders from segtrends '''
    x_maxima, maxima, x_minima, minima = segtrends(x, segments, charts, window)
    n = len(x)
    y = np.array(x)
    movy = movingaverage(y, window)

    # generate order strategy
    orders = np.zeros(n)
    last_buy = y[0]
    last_sale = y[0]

    for i in range(1, n):
        # get 2 latest support point y values prior to x
        pmin = list(minima[np.where(x_minima <= i)][-2:])
        pmax = list(maxima[np.where(x_maxima <= i)][-2:])
        # sell if support slop is negative
        min_sell = True if ((len(pmin) == 2) and
                            (pmin[1] - pmin[0]) < 0) else False
        max_sell = True if ((len(pmax) == 2) and
                            (pmax[1] - pmax[0]) < 0) else False

        # if support down, sell
        buy = -1 if (min_sell and max_sell) else 0
        # buy only if lower the moving average else sale
        buy = 1 if ((buy == 0) and (y[i] < movy[i])) else -1
        # sell only if ...
        buy = -1 if ((buy == -1) and y[i] > last_buy) else 1

        buy_price_dec = y[i] < last_buy
        sale_price_dec = y[i] < last_sale
        orders[i] = buy
        last_buy = y[i] if (buy == 1) else last_buy
        last_sale = y[i] if (buy == -1) else last_sale

        if momentum:
            # add momentum for buy
            if (buy == 1) and (orders[i - 1] >= 1):
                #if buy_price_dec:
                orders[i] = orders[i -
                                   1] * 2  #round(math.log(2*orders[i-1])+1)
                #else:
                #   orders[i]=max(1, round(orders[i-1]/2))
            # add momentum for sale
            elif (buy == -1) and (orders[i - 1] <= -1):
                #if sale_price_dec:
                orders[i] *= round(math.log(abs(orders[i - 1] * 2)) + 1)
                #else:
                #    orders[i]=max(1, round(orders[i-1]/2))

    # OUTPUT
    return orders
def orders_from_trends(x, segments=2, charts=True, window=7, momentum=False):
    """ generate orders from segtrends """
    x_maxima, maxima, x_minima, minima = segtrends(x, segments, charts, window)
    n = len(x)
    y = np.array(x)
    movy = movingaverage(y, window)

    # generate order strategy
    orders = np.zeros(n)
    last_buy = y[0]
    last_sale = y[0]

    for i in range(1, n):
        # get 2 latest support point y values prior to x
        pmin = list(minima[np.where(x_minima <= i)][-2:])
        pmax = list(maxima[np.where(x_maxima <= i)][-2:])
        # sell if support slop is negative
        min_sell = True if ((len(pmin) == 2) and (pmin[1] - pmin[0]) < 0) else False
        max_sell = True if ((len(pmax) == 2) and (pmax[1] - pmax[0]) < 0) else False

        # if support down, sell
        buy = -1 if (min_sell and max_sell) else 0
        # buy only if lower the moving average else sale
        buy = 1 if ((buy == 0) and (y[i] < movy[i])) else -1
        # sell only if ...
        buy = -1 if ((buy == -1) and y[i] > last_buy) else 1

        buy_price_dec = y[i] < last_buy
        sale_price_dec = y[i] < last_sale
        orders[i] = buy
        last_buy = y[i] if (buy == 1) else last_buy
        last_sale = y[i] if (buy == -1) else last_sale

        if momentum:
            # add momentum for buy
            if (buy == 1) and (orders[i - 1] >= 1):
                # if buy_price_dec:
                orders[i] = orders[i - 1] * 2  # round(math.log(2*orders[i-1])+1)
                # else:
                #   orders[i]=max(1, round(orders[i-1]/2))
            # add momentum for sale
            elif (buy == -1) and (orders[i - 1] <= -1):
                # if sale_price_dec:
                orders[i] *= round(math.log(abs(orders[i - 1] * 2)) + 1)
                # else:
                #    orders[i]=max(1, round(orders[i-1]/2))

    # OUTPUT
    return orders
Esempio n. 7
0
# Download Apple price history and save adjusted close prices to numpy array
import pandas.io.data as pd
x = pd.DataReader("AAPL", "yahoo")['Adj Close']

# Make some trendlines
import trendy

# Generate general support/resistance trendlines and show the chart
# winow < 1 is considered a fraction of the length of the data set
trendy.gentrends(x, window = 1.0/3, charts = True)

# Generate a series of support/resistance lines by segmenting the price history
trendy.segtrends(x, segments = 2, charts = True)  # equivalent to gentrends with window of 1/2
trendy.segtrends(x, segments = 5, charts = True)  # plots several S/R lines

# Generate smaller support/resistance trendlines to frame price over smaller periods
trendy.minitrends(x, window = 30, charts = True)

# Iteratively generate trading signals based on maxima/minima in given window
trendy.iterlines(x, window = 30, charts = True)  # buy at green dots, sell at red dots
        if (r_1 == (n / 2)) and (r_2 == (n / 2)):
            resistance.append(ltp[i + ((n / 2) - 1)])

        # local minima detection
        if (s_1 == (n / 2)) and (s_2 == (n / 2)):
            support.append(ltp[i + ((n / 2) - 1)])

    return support, resistance


###############################################################################

# test supres function
npArray = df['close'].as_matrix()

support, resistance = supres(npArray, 5)

###############################################################################

# test trendy
close = df['close']
close.index = df['date']

cutoff = '2000-08-01'
cut = close[close.index > cutoff]

trendy.gentrends(cut, window=1. / 3, charts=True)
trendy.segtrends(cut, segments=3, charts=True)
trendy.minitrends(cut, window=30, charts=True)
trendy.iterlines(cut, window=30, charts=True)