def segtrends(x, segments=2, charts=True, window=7): """ Turn minitrends to iterative process more easily adaptable to implementation in simple trading systems; allows backtesting functionality. :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 """ import numpy as np y = np.array(x) n=len(y) movy = movingaverage(y, window) # Implement trendlines and Find the indexes of these maxima in the data segments = int(segments) maxima = np.ones(segments) minima = np.ones(segments) x_maxima = np.ones(segments) x_minima = np.ones(segments) segsize = int(len(y)/segments) for i in range(1, segments+1): ind2 = i*segsize ind1 = ind2 - segsize seg = y[ind1:ind2] maxima[i-1] = max(seg) minima[i-1] = min(seg) x_maxima[i-1] = ind1 + (np.where(seg == maxima[i-1])[0][0]) x_minima[i-1] = ind1 + (np.where(seg == minima[i-1])[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') pass if charts: plt.plot(range(n), movy, 'b') plt.plot(x_maxima, maxima, 'g') plt.plot(x_minima, minima, 'r') plt.show() # OUTPUT return x_maxima, maxima, x_minima, minima
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
def segtrends(x, segments=2, window=7, charts=False, title=None): """ Turn minitrends to iterative process more easily adaptable to implementation in simple trading systems; allows backtesting functionality. :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 """ import numpy as np y = np.array(x) n=len(y) movy = movingaverage(y, window) # Implement trendlines and Find the indexes of these maxima in the data segments = int(segments) maxima = np.ones(segments) minima = np.ones(segments) x_maxima = np.ones(segments) x_minima = np.ones(segments) segsize = int(len(y)/segments) for i in range(1, segments+1): ind2 = i*segsize ind1 = ind2 - segsize seg = y[ind1:ind2] maxima[i-1] = max(seg) minima[i-1] = min(seg) x_maxima[i-1] = ind1 + (np.where(seg == maxima[i-1])[0][0]) x_minima[i-1] = ind1 + (np.where(seg == minima[i-1])[0][0]) 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: import matplotlib.pyplot as plt #plt.plot(range(n), movy, 'b') plt.plot(range(n), x, 'k') plt.plot(x_maxima, maxima, 'g') plt.plot(x_minima, minima, 'r') if title: plt.title(title) plt.show() # OUTPUT return x_maxima, maxima, x_minima, minima
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