def handle_data(context, data): analysis = pd.DataFrame(index = data.index) analysis['price'] = data['CLS_PRC'] analysis['volume'] = data['VOLUME'] analysis['sma_f'] = data.CLS_PRC.rolling(window=SMA_FAST,center=False).mean() analysis['sma_s'] = data.CLS_PRC.rolling(window=SMA_SLOW,center=False).mean() analysis['rsi'] = ta.RSI(data.CLS_PRC.values, RSI_PERIOD) analysis['sma_r'] = analysis.rsi.rolling(window=RSI_AVG_PERIOD,center=False).mean() #analysis['macd'], analysis['macdSignal'], analysis['macdHist'] = ta.MACD(data.CLS_PRC.as_matrix(), fastperiod=MACD_FAST, slowperiod=MACD_SLOW, signalperiod=MACD_SIGNAL) analysis['stoch_k'], analysis['stoch_d'] = ta.STOCH(data.HIGH_PRC.values, data.LOW_PRC.values, data.CLS_PRC.values, fastk_period=14, slowk_period=3, slowd_period=3) #analysis['stoch_k'], analysis['stoch_d'] = ta.STOCHRSI(data.CLS_PRC.values, timeperiod=14, fastk_period=3, fastd_period=3) # analysis['stoch_k'], analysis['stoch_d'] = indic.STO(data.HIGH_PRC, data.LOW_PRC, data.CLS_PRC, nK=14, nD=3, nS=3) analysis['VOL_CHG'] = data['VOLUME'].pct_change(fill_method='ffill') analysis['CDL3INSIDE'] = ta.CDL3INSIDE(data.OPEN_PRC, data.HIGH_PRC, data.LOW_PRC, data.CLS_PRC) analysis['ZIGZAG'] = zigzag.peak_valley_pivots(np.array(data['CLS_PRC']), 0.01, -0.01) analysis['ZIGZAG_STOCH'] = zigzag.peak_valley_pivots(np.array(analysis['stoch_k']), 0.01, -0.01) #analysis['sma'] = np.where(analysis.sma_f > analysis.sma_s, 1, 0) #analysis['macd_test'] = np.where((analysis.macd > analysis.macdSignal), 1, 0) #analysis['stoch_k_test'] = np.where((analysis.stoch_k < 50) & (analysis.stoch_k > analysis.stoch_k.shift(1)), 1, 0) #analysis['rsi_test'] = np.where((analysis.rsi < 50) & (analysis.rsi > analysis.rsi.shift(1)), 1, 0) #print(analysis) return analysis
def onBars(self, bars): # Wait for enough bars to be available to calculate a SMA. bar = bars[self.__instrument] #getfeed barDs = self.getFeed().getDataSeries("orcl") closeDs = self.getFeed().getDataSeries("orcl").getCloseDataSeries() volumeDs = self.getFeed().getDataSeries("orcl").getVolumeDataSeries() #Long entry (2): engulfing signal: engulfing where 100 for bullish engulfing; -100 for bearish engulfing; 0 for no engulfing engulfing = indicator.CDLENGULFING(barDs,50000) #recognize decreasing trend by previous 3 price and 3 volume trenddropprice = indicator.ROC(closeDs,50000,timeperiod=3) trenddropvol = indicator.ROC(volumeDs,50000,timeperiod=2) #Long exit (2): zigzag function to recognize divergence ##zigzag for Price pivots = peak_valley_pivots(closeDs, 0.2, -0.2) zigPrice = compute_segment_returns(np.array(closeDs), pivots) ##zigzag for Volume pivotV = peak_valley_pivots(volumeDs, 0.2, -0.2) zigVolume = compute_segment_returns(np.array(volumeDs), pivots) ##zigzag for RSI ###create rsi series if self.__rsi[-1] is None: self.x=np.append(self.x,[50]) else: self.x=np.append(self.x,self.__rsi[-1]) pivotRSI = peak_valley_pivots(self.x, 0.2, -0.2) zigVolume = compute_segment_returns(np.array(volumeDs), pivots) zigRSI = compute_segment_returns(self.x, pivots) #Long exit (1) action: Edit the 90% price if price goes up if self.__position is not None and not self.__position.exitActive() and bar.getPrice()> (self.stoplossprice/0.9): self.stoplossprice=bar.getPrice()*0.9 # If a position was not opened, check if we should enter a long position. if self.__position is None: # Long entry (2) action: [candle stick (try bullish engulfing first)] AND [rsi below 50] AND [price increase with volume high] # Enter a buy market order for 10 shares. The order is good till canceled. if engulfing[-1]==100 and trenddropprice[-2]<-2 and self.__rsi[-1]<50 and trenddropvol[-1]>0: self.__position = self.enterLong(self.__instrument, 10, True) self.stoplossprice= bar.getPrice()*0.9 #Stop Long loss self.longprice=bar.getPrice() #calculate profit only # Check if we have to exit the position. # Long exit (1) action AND Stop Long loss Long exit (2) elif not self.__position.exitActive() and (bar.getPrice() < self.stoplossprice or (zigPrice[-1]>0 and zigVolume[-1]<=0)): self.__position.exitMarket()
def test_strictly_increasing(self): data = np.linspace(1, 10, 10) result = zigzag.peak_valley_pivots(data, 0.1, -0.1) expected_result = np.zeros_like(data) expected_result[0], expected_result[-1] = VALLEY, PEAK assert_array_equal(result, expected_result)
def test_strictly_decreasing_but_less_than_threshold(self): data = np.linspace(1.05, 1.0, 10) result = zigzag.peak_valley_pivots(data, 0.1, -0.1) expected_result = np.zeros_like(data) expected_result[0], expected_result[-1] = PEAK, VALLEY assert_array_equal(result, expected_result)
def dataXY(ticker, alpha=0.02): data =fetch(ticker, start=datetime.datetime(2012, 1, 1)) data['pivots'] = peak_valley_pivots(data.Close, alpha, -alpha) rd = data.reset_index() X = rd[rd.pivots!=0][['Close', 'pivots']].reset_index().values X = np.dstack( [X[i:i+5, :] for i in range( X.shape[0] -4)] ) X[:,1,:] = X[:,1,:]/X[0,1,:] # covert to return axis F = map(lambda i: genFeature(X[..., i]), range(X.shape[-1])) F = np.array(F) return F, data
def plot_with_pivots(X, levels, zigzag_percent=1, only_good=False, path=None): pivots = peak_valley_pivots(X, zigzag_percent / 100, -zigzag_percent / 100) plt.xlim(0, len(X)) plt.ylim(X.min() * 0.995, X.max() * 1.005) plt.plot(np.arange(len(X)), X, 'k-', alpha=0.9) plt.plot(np.arange(len(X))[pivots != 0], X[pivots != 0], 'k:', alpha=0.5) plt.scatter(np.arange(len(X))[pivots == 1], X[pivots == 1], color='g') plt.scatter(np.arange(len(X))[pivots == -1], X[pivots == -1], color='r') _plot_levels(plt, levels, only_good) if path: plt.savefig(path) else: plt.show() plt.close()
def __init__(self, dataframe): try: dataframe.df['index'] = to_datetime(dataframe.df.index) sub = Series(dataframe.df.high, index=dataframe.df.index) pivots = peak_valley_pivots(sub, 0.1, -0.1) self.points = {} i = 0 while i < len(dataframe.df): if (pivots[i] != 0): self.points[dataframe.df.index[i]] = pivots[i] i = i + 1 except ValueError: data = sys.exc_info()[0] except: data = sys.exc_info()[0]
def main(ticker, idx=0, alpha = 0.02): data =fetch(ticker) data['pivots'] = peak_valley_pivots(data.Close, alpha, -alpha) rd = data.reset_index() X = rd[rd.pivots!=0][['Close', 'pivots']].reset_index().values X = np.dstack( [X[i:i+5, :] for i in range( X.shape[0] -4)] ) features = [] for i in range(X.shape[-1]): x = X[:,:,i] x[:, 1] = x[:, 1]/x[0,1] a1, b1 = trend(x[::4, 0], x[::4, 1]) a2, b2 = trend(x[1::2, 0], x[1::2, 1]) xm = a1*x[2,0] + b1 dy = xm - x[2,1] features.append([idx, x[0,2], a1, a2, dy]) print idx, x[0,2], a1, a2, dy #import ipdb; ipdb.set_trace() raw = rd[int(x[0,0]): int(x[-1,0])] ret = raw.Close/raw.Close.iloc[0] ry1 = a1 * x[:,0] + b1 ry2 = a2 * x[:,0] + b2 fig = plt.figure(figsize=(6,3)) ax = fig.add_subplot(111) ret.plot(style='g.', ax=ax) ax.plot(x[:,0], x[:,1], '--') ax.plot(x[:,0], ry1, 'r-', lw=2) ax.plot(x[:,0], ry2, 'b-', lw=2) ax.plot(x[::2, 0], x[::2, 1], 'ro') ax.plot(x[1::2, 0], x[1::2, 1], 'bo') ax.axhline(y=1.0, lw=1, c='k') ax.annotate("%.3f%%"%(100*a1), (x[1,0], a1*x[1,0]+b1), color='r') ax.annotate("%.3f%%"%(100*a2), (x[3,0], a2*x[3,0]+b2), color='b') ax.annotate("%.3f%%"%(100*dy), (x[2,0], x[2,1]), color='k') ax.set_title("#%s %s, %.3f%%, %.3f%%, %.3f%%"%(idx, x[0,2], 100*a1, 100*a2, 100*dy)) ax.set_xlim([x[0,0]-5, x[-1,0]+5]) ax.set_ylim([x[:,1].min()-0.03, x[:,1].max()+0.03 ]) plt.savefig("figs/%s.png"%(idx)) idx += 1 with open('data.csv', 'a') as fs: writer = csv.writer(fs) writer.writerows(features) return idx
def find_support_and_resistance(self): percent = 0.1 def zig_zag_cluster_levels(ppd, md, mp, mb, pk): return ZigZagClusterLevels(peak_percent_delta=ppd, merge_distance=md, merge_percent=mp, min_bars_between_peaks=mb, peaks=pk) zig = zig_zag_cluster_levels(percent, None, round(365.25 / 12 / 2) - 1, round(365.25 / 12) + 1, 'Low') if zig.levels is None: percent = 0.25 zig = zig_zag_cluster_levels(percent, None, 0.25, 100, 'Low') self.kline[self.kline.columns[1:5]] = \ self.kline[self.kline.columns[1:5]].astype('float') zig.fit(self.kline) val = self.kline.close.values piv = peak_valley_pivots(val, percent / 100, -percent / 100) return zig.levels, piv
def create_binary_zig_zag_class(X, target_quote, pts_variation, plot=True, lasttrend=False): pivots = peak_valley_pivots(X[target_quote].values, pts_variation, -pts_variation) spivots = pd.Series(pivots, index=X.index) spivots = spivots[ pivots != 0] # just the pivots point, when it changes to up or down # remove the last segment of zigzag from training, can not be used it # is a misleading trend?? is it? if not lasttrend: spivots.drop(spivots.tail(1).index, inplace=True) # remove the last point it is not real?? X['du'] = spivots if plot: f, axr = plt.subplots(2, sharex=True, figsize=(15, 4)) f.subplots_adjust(hspace=0) X[target_quote].plot(ax=axr[0]) X.loc[spivots.index, target_quote].plot(ax=axr[0], style='.r-') X['du'].fillna(method='ffill', inplace=True) # fill nans with last valid value # turn in [0-1] # 1 is up trend, 0 is down trend X['du'] = -X['du'] X.loc[X['du'] < 0, 'du'] = 0 # where is smaller than zero put 0 if plot: X['du'].plot(style='-b', ax=axr[1]) plt.ylim(-0.15, 1.15) plt.ylabel('up=1 down=0') return spivots
def test_Valleys(self): s = st("AAPL", "2014-08-04", 200) s.df['index'] = pd.to_datetime(s.df.index) sub = pd.Series(s.df.close, index=s.df.index) pivots = peak_valley_pivots(sub, 0.01, -0.01) points = {} i = 0 while i < len(s.df): if (pivots[i] != 0): #Dato = -1/1 #points[s.df.index[i]] = pivots[i] #indexnummer = -1/1 #points[i] = pivots[i] points[i] = s.df.iloc[i]['close'] i = i + 1 print(points)
def test_Valleys(self): s = st("AAPL", "2014-08-04", 200) s.df['index'] = pd.to_datetime(s.df.index) sub = pd.Series(s.df.close, index=s.df.index) pivots = peak_valley_pivots(sub, 0.01, -0.01) points = {} i = 0 while i < len(s.df): if (pivots[i] != 0): #Dato = -1/1 #points[s.df.index[i]] = pivots[i] #indexnummer = -1/1 #points[i] = pivots[i] points[i] = s.df.iloc[i]['close'] i=i+1 print(points)
def get_pip (frame, column, error): out_frame = frame out_frame["direction"] = peak_valley_pivots(out_frame[column], error, -1*error) return out_frame[out_frame["direction"].isin([1,-1])]
def test_single_valleyed(self): data = np.array([1.0, 0.9, 1.2]) result = zigzag.peak_valley_pivots(data, 0.1, -0.1) expected_result = np.array([PEAK, VALLEY, PEAK]) assert_array_equal(result, expected_result)
import matplotlib.pyplot as plt import numpy as np import pandas as pd import zigzag as zz from pandas_datareader import get_data_yahoo df = pd.read_csv('data/PHR.csv', index_col=0, parse_dates=['Date']) df = df.ix[len(df) - 252:len(df) - 1] df = df.reset_index(drop=True) pivots = zz.peak_valley_pivots(df.Close.values, 0.05, -0.01) ts_pivots = pd.Series(df.Close, index=df.index) ts_pivots = ts_pivots[pivots != 0] df.Close.plot() ts_pivots.plot(style='g-o') plt.show()
def test_strictly_decreasing(self): data = np.linspace(100.0, 1.0, 10) pivots = zigzag.peak_valley_pivots(data, 0.1, -0.1) assert_array_almost_equal(zigzag.compute_segment_returns(data, pivots), np.array([-0.99]))
def _find_potential_level_prices(self, X): pivots = peak_valley_pivots(X, self._peak_percent_delta, -self._peak_percent_delta) indexes = self._get_pivot_indexes(pivots) pivot_prices = X[indexes] return pivot_prices
def test_decreasing_kinked(self): data = np.array([1.0, 1.01, 0.9]) result = zigzag.peak_valley_pivots(data, 0.1, -0.1) expected_result = np.array([VALLEY, PEAK, VALLEY]) assert_array_equal(result, expected_result)
# p_open = open_a[i - 1] # p_close = close_a[i - 1] # p_low = low_a[i - 1] # p_high = high_a[i - 1] # p_ohlcv4 = ohlcv4[i - 1] # if p_ohlcv4 < ohlcv4: high_low.append(max(ohlcv4_a[i], ohlcv4_a[i - 1])) # if ohlcv4_a[i - 1] < ohlcv4_a[i]: # high_low.append(high) # else: # high_low.append(low) X = np.array(high_low) pivots = zigzag.peak_valley_pivots(X, 0.02, -0.01) """ 위 변곡점: 1 아래 변곡점: -1 나머지: 0 swsong 위 꼭지점은 -1 아래 꼭지점은 1 번갈아 나오면 0점. """ actions = [] action = None for pivot in pivots:
def test_rise_fall_rise(self): data = np.array([1.0, 1.05, 1.1, 1.0, 0.9, 1.5]) pivots = zigzag.peak_valley_pivots(data, 0.1, -0.1) assert_array_almost_equal(zigzag.compute_segment_returns(data, pivots), np.array([0.1, -0.181818, 0.6666666]))
def objetivo(self, datos): close = datos["close"].as_matrix() cambio = datos["close"].pct_change().abs().mean() * self.cb pivots = zigzag.peak_valley_pivots(close, cambio, cambio * -1) movimiento = zigzag.pivots_to_modes(pivots) return movimiento
high_low.append(high if open < close else low) continue p_open = open_a[i - 1] p_close = close_a[i - 1] p_low = low_a[i - 1] p_high = high_a[i - 1] p_ohlcv4 = p_open + p_high + p_low + p_close / 4 if p_ohlcv4 < ohlcv4: high_low.append(high) else: high_low.append(low) X = np.array(high_low) pivots = zigzag.peak_valley_pivots(X, 0.03, -0.03) """ 위 변곡점: 1 아래 변곡점: -1 나머지: 0 """ actions = [] action = None for pivot in pivots: if pivot == 1: action = 'D' elif pivot == -1: action = 'U' actions.append(action)
def zig(data): pivots = peak_valley_pivots(data.Close, 0.02, -0.02) return pivots