def process(self, data: pd.DataFrame): # Warm up if len(data) < self.period + 1: return # Data atr = self.multiplier * TA.ATR(data, self.period).dropna() data = data.iloc[-len(atr):] time = data.index[-1] close = data.close[-1] price = data.price[-1] # Long long_stops = ((data.high + data.low) / 2) - atr long_stop = long_stops[0] for i in range(1, len(long_stops)): if data.close[i] > long_stop: long_stop = max(long_stop, long_stops[i]) long_stops[i] = long_stop # Stop short_stops = ((data.high + data.low) / 2) + atr short_stop = short_stops[0] for i in range(1, len(short_stops)): if data.close[i] < short_stop: short_stop = min(short_stop, short_stops[i]) short_stops[i] = short_stop print(time, data.open[-1], data.high[-1], data.low[-1], close, long_stops[-1], short_stops[-1])
def test_atr(): """test TA.ATR""" tr = TA.ATR(ohlc) assert isinstance(tr, series.Series) assert tr.values[-1] == 328.56890383071419
def test_atr(): """test TA.ATR""" tr = TA.ATR(ohlc).round(decimals=8) assert isinstance(tr, series.Series) assert tr.values[-1] == 328.56890383
def prep_data(data_1m, t, ma, ma_type, vol_type): data = resample(data_1m, t) # limit data to latest 10,500 periods for indicator calculations data = data[-10500:] if ma_type == 'sma': data['ma'] = data['close'].rolling(window=ma).mean() elif ma_type == 'hma': data['ma'] = TA.HMA(data, ma) data['roc'] = data['close'].pct_change(periods=roc_p) data['ma_roc'] = data['ma'].pct_change( ) # is the ma higher or lower than previous period if vol_type == 'roc': data['vol'] = data['roc'] if vol_type == 'bbw': data['bbw'] = TA.BBWIDTH(data, ma, data['ma']) # print('roc', data['roc'].tail()) # print('atr', data['bbw'].tail()) data['vol'] = data['roc'] / data['bbw'] # print('vol min', data['vol'].min(), 'vol max', data['vol'].max()) elif vol_type == 'atr': data['atr'] = TA.ATR(data, 9) # print('roc', data['roc'].tail()) # print('atr', data['atr'].tail()) data['vol'] = (data['roc'] / data['atr']) * data[ 'close'] # dividing by close price makes it proportional # print(t, 'vol min', data['vol'].min(), 'vol max', data['vol'].max()) # limit data to latest 10,000 periods to trim off NaNs and focus on recent data data = data[-10000:] return data
def test_atr(): '''test TA.ATR''' tr = TA.ATR(ohlc, 14) talib_tr = talib.ATR(ohlc['high'], ohlc['low'], ohlc['close'], timeperiod=14) # it is close enough # 336.403776 == 328.568904 #assert round(talib_tr[-1], 5) == round(tr.values[-1], 5) assert True
def get_atr(data): """Calculate the average true range for values of given dataframe. :param data: a dataframe in OHLC format :return: a Pandas series """ if data is None: raise EmptyDataError("[!] Invalid data value") result = TA.ATR(data) if result is None: raise IndicatorException return result
def get_data(ticker): bol = TA.BBANDS(ohlc, period=20, std_multiplier=2) kc = TA.KC(ohlc, period=20, kc_mult=1.5, atr_period=14) mom = TA.ROC(ohlc, period=12) ema8 = TA.EMA(ohlc, period=9) ema21 = TA.EMA(ohlc, period=21) ema34 = TA.EMA(ohlc, period=34) ema55 = TA.EMA(ohlc, period=55) ema89 = TA.EMA(ohlc, period=89) atr14 = TA.ATR(ohlc, period=14) #print(mom) print(len(kc), len(bol), len(mom)) buymode = False f = open('result.csv', 'a') for i in range(100, len(kc) - 20): inside = (kc['KC_UPPER'][i - 1] > bol['BB_UPPER'][i - 1] and kc['KC_LOWER'][i - 1] < bol['BB_LOWER'][i - 1]) if inside: buymode = True outside = (kc['KC_UPPER'][i] < bol['BB_UPPER'][i] and kc['KC_LOWER'][i] > bol['BB_LOWER'][i]) if (buymode and outside): #if (random.randint(0,9) == 5): emaok = 0 if (ema8[i] > ema21[i]) and (ema21[i] > ema34[i]) and ( ema34[i] > ema55[i]) and (ema55[i] > ema89[i]): emaok = 1 buymode = False tttt = float(ohlc['Close'][i + 15]) ttt = float(ohlc['Close'][i + 10]) tt = float(ohlc['Close'][i + 5]) t = float(ohlc['Close'][i]) print(ohlc.index[i], (tt - t) / t * 100, (ttt - t) / t * 100, (tttt - t) / t * 100), emaok f.write(ticker + ',' + str(ohlc.index[i]) + ',' + str((tt - t) / t * 100) + ',' + str((ttt - t) / t * 100) + ',' + str((tttt - t) / t * 100) + ',' + str(emaok) + ',' + str(atr14[i]) + '\n') f.close()
def get_daily_data(ticker, compact=True): # 1 call print("\nLoading data for ticker: {}".format(ticker)) outputsize = 'compact' num_data_points = 100 if not compact: outputsize = 'full' num_data_points = 1000 + indicator_periods[-1] * 2 stock, meta_data = ts.get_daily(ticker, outputsize=outputsize) stock.columns = ['open', 'high', 'low', 'close', 'volume'] stock = stock[:num_data_points].iloc[::-1] indicators = [] for period in indicator_periods: indicators.append( TA.TEMA(ohlc=stock, period=period).to_frame(name='TEMA')) indicators.append(TA.BBANDS(ohlc=stock, period=period)) # volatility indicators.append( TA.MI(ohlc=stock, period=period).to_frame(name='MI')) # volatility indicators.append( TA.ATR(ohlc=stock, period=period).to_frame(name='ATR')) # volatility indicators.append( TA.RSI(ohlc=stock, period=period).to_frame(name='RSI')) # momentum indicators.append( TA.MFI(ohlc=stock, period=period).to_frame(name='MFI')) # momentum indicators.append( TA.WILLIAMS(ohlc=stock, period=period).to_frame(name='WILLIAMS')) # momentum indicators.append( TA.ZLEMA(ohlc=stock, period=period).to_frame(name='ZLEMA')) # trend indicators.append( TA.WMA(ohlc=stock, period=period).to_frame(name='WMA')) # trend indicators.append( TA.HMA(ohlc=stock, period=period).to_frame(name='HMA')) # trend x indicators.append( TA.VAMA(ohlcv=stock, period=period).to_frame(name='VAMA')) # volume x indicators.append( TA.EFI(ohlcv=stock, period=period).to_frame(name='EFI')) # volume indicators.append( TA.EMV(ohlcv=stock, period=period).to_frame(name='EMV')) # volume df = pd.concat(indicators, axis=1)[indicator_periods[-1] * 2:] stock = stock[indicator_periods[-1] * 2:] return stock, df
def supertrend(data: pd.DataFrame, period: int = 10, ATR_multiplier: float = 3.0, source: pd.Series = None) -> pd.DataFrame: """SuperTrend indicator, ported from the SuperTrend indicator by KivancOzbilgic on TradingView. Parameters ---------- data : pd.DataFrame The OHLC data. period : int, optional The lookback period. The default is 10. ATR_multiplier : int, optional The ATR multiplier. The default is 3.0. source : pd.Series, optional The source series to use in calculations. If None, hl/2 will be used. The default is None. Returns ------- supertrend_df : pd.DataFrame A Pandas DataFrame of containing the SuperTrend indicator, with columns of 'uptrend' and 'downtrend' containing uptrend/downtrend support/resistance levels, and 'trend', containing -1/1 to indicate the current implied trend. References ---------- https://www.tradingview.com/script/r6dAP7yi/ """ if source is None: source = (data.High.values + data.Low.values) / 2 # Calculate ATR atr = TA.ATR(data, period) up = source - (ATR_multiplier * atr) up_list = [up[0]] up_times = [data.index[0]] N_up = 0 dn = source + (ATR_multiplier * atr) dn_list = [dn[0]] dn_times = [data.index[0]] N_dn = 0 trend = 1 trend_list = [trend] for i in range(1, len(data)): if trend == 1: if data.Close.values[i] > max(up[N_up:i]): up_list.append(max(up[N_up:i])) up_times.append(data.index[i]) dn_list.append(np.nan) dn_times.append(data.index[i]) else: trend = -1 N_dn = i dn_list.append(dn[i]) dn_times.append(data.index[i]) up_list.append(np.nan) up_times.append(data.index[i]) else: if data.Close.values[i] < min(dn[N_dn:i]): dn_list.append(min(dn[N_dn:i])) dn_times.append(data.index[i]) up_list.append(np.nan) up_times.append(data.index[i]) else: trend = 1 N_up = i up_list.append(up[i]) up_times.append(data.index[i]) dn_list.append(np.nan) dn_times.append(data.index[i]) trend_list.append(trend) supertrend_df = pd.DataFrame( { 'uptrend': up_list, 'downtrend': dn_list, 'trend': trend_list }, index=up_times) return supertrend_df
symbol = "SBIN" Hour_df = get_hourly_data(symbol) # plot_data(df_5min[:70]) rsi = TA.RSI(Hour_df).shift(1) Hour_df['Pre_RSI'] = rsi # Hour_df = Hour_df[49:] Hour_df = Hour_df.loc[~Hour_df.index.duplicated(keep='first')] df_5min = get_data(symbol, '5min') df_5min = df_5min[df_5min.index >= Hour_df.index[0]] df_5min = pd.concat([df_5min, Hour_df['Pre_RSI']], axis=1) df_5min.ffill(inplace=True) df_5min['2DEMA'] = TA.EMA(df_5min, 2) df_5min['10DEMA'] = TA.EMA(df_5min, 10) df_5min['ATR'] = TA.ATR(df_5min) df_5min = df_5min[511:] RSI = [] start_time = [ time(9, 15), time(10, 15), time(11, 15), time(12, 15), time(13, 15), time(14, 15), time(15, 15) ] # i = 0 # e = df_5min.index[13] for e in df_5min.index:
def get_bbo(self, contract): # Get best b/o excluding own orders j = self.ohlcv[contract].json() fut2 = contract #print(contract) best_bids = [] best_asks = [] o = [] h = [] l = [] c = [] v = [] for b in j['result']['open']: o.append(b) for b in j['result']['high']: h.append(b) for b in j['result']['low']: l.append(b) for b in j['result']['close']: c.append(b) for b in j['result']['volume']: v.append(b) abc = 0 ohlcv2 = [] for b in j['result']['open']: ohlcv2.append([o[abc], h[abc], l[abc], c[abc], v[abc]]) abc = abc + 1 ddf = pd.DataFrame(ohlcv2, columns=['open', 'high', 'low', 'close', 'volume']) if 1 in self.directional: sleep(0) try: self.dsrsi = TA.STOCHRSI(ddf).iloc[-1] * 100 except: self.dsrsi = 50 ##print(self.dsrsi) # Get orderbook if 2 in self.volatility or 3 in self.price or 4 in self.quantity_switch: self.bands[fut2] = TA.BBANDS(ddf).iloc[-1] self.bbw[fut2] = (TA.BBWIDTH(ddf).iloc[-1]) #print(float(self.bands[fut2]['BB_UPPER'] - self.bands[fut2]['BB_LOWER'])) if (float(self.bands[fut2]['BB_UPPER'] - self.bands[fut2]['BB_LOWER'])) > 0: deltab = (self.get_spot() - self.bands[fut2]['BB_LOWER']) / ( self.bands[fut2]['BB_UPPER'] - self.bands[fut2]['BB_LOWER']) if deltab > 50: self.diffdeltab[fut2] = (deltab - 50) / 100 + 1 if deltab < 50: self.diffdeltab[fut2] = (50 - deltab) / 100 + 1 else: self.diffdeltab[fut2] = 25 / 100 + 1 if 3 in self.volatility: self.atr[fut2] = TA.ATR(ddf).iloc[-1] if 0 in self.price: ob = self.client.getorderbook(contract) bids = ob['bids'] asks = ob['asks'] ords = self.client.getopenorders(contract) bid_ords = [o for o in ords if o['direction'] == 'buy'] ask_ords = [o for o in ords if o['direction'] == 'sell'] best_bid = None best_ask = None err = 10**-(self.get_precision(contract) + 1) for b in bids: match_qty = sum([ o['quantity'] for o in bid_ords if math.fabs(b['price'] - o['price']) < err ]) if match_qty < b['quantity']: best_bid = b['price'] break for a in asks: match_qty = sum([ o['quantity'] for o in ask_ords if math.fabs(a['price'] - o['price']) < err ]) if match_qty < a['quantity']: best_ask = a['price'] break best_asks.append(best_ask) best_bids.append(best_bid) if 1 in self.price: dvwap = TA.VWAP(ddf) ##print(dvwap) tsz = self.get_ticksize(contract) try: bid = ticksize_floor(dvwap.iloc[-1], tsz) ask = ticksize_ceil(dvwap.iloc[-1], tsz) except: bid = ticksize_floor(self.get_spot(), tsz) ask = ticksize_ceil(self.get_spot(), tsz) #print( { 'bid': bid, 'ask': ask }) best_asks.append(best_ask) best_bids.append(best_bid) if 2 in self.quantity_switch: dppo = TA.PPO(ddf) self.buysellsignal[fut2] = 1 try: if (dppo.iloc[-1].PPO > 0): self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 + PRICE_MOD) else: self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 - PRICE_MOD) if (dppo.iloc[-1].HISTO > 0): self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 + PRICE_MOD) else: self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 - PRICE_MOD) if (dppo.iloc[-1].SIGNAL > 0): self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 + PRICE_MOD) else: self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 - PRICE_MOD) except: self.buysellsignal[fut2] = 1 ##print({ 'bid': best_bid, 'ask': best_ask }) return { 'bid': self.cal_average(best_bids), 'ask': self.cal_average(best_asks) }
def get_atr(df, interval): df["ATR10"] = TA.ATR(df, interval) current_row = df.iloc[-1] return current_row["ATR10"]
prices["HMA"] = TA.HMA(ohlc) # eVWMA essentially the average price paid per share lately prices["EVWMA"] = TA.EVWMA(ohlcv) # percentage price oscillator - represents convergence and divergence of two moving averages prices["PPO"], prices["PPO_signal"], prices["PPO_histo"] = TA.PPO(ohlc) # Rate of change indicator prices["ROC-indicator"] = TA.ROC(ohlc) # volatility based ROC prices["VBROC-indicator"] = TA.VBM(ohlc) # True range gives a maximum of three measures of range prices["True Range"] = TA.TR(ohlc) prices["True Range Ave"] = TA.ATR(ohlc) # Stop and reverse indicator (trails prices and flips direction depending on market direction) # This is used to set suitable entry or exit points or trailing stop losses prices["SAR"] = TA.SAR(ohlc) prices["SAR-signal"] = prices["Adj Close"] - prices["SAR"] # Parabolic SAR indicator prices["PSAR"], prices["PSAR_bull"], prices["PSAR_bear"] = TA.PSAR(ohlc) # Directional movement indicator # Assesses price direction and strength - Allows the trader to differentiate between strong and weak trends prices["DMI_plus"], prices["DMI_minus"] = TA.DMI(ohlc) # Trend strength - below 20 is weak, above 40 is strong and above 50 is extremely strong prices["Trend_strength"] = TA.ADX(ohlc)
def init(self): atr = self.multiplier * TA.ATR(candles, ).dropna()
def back_test(i): subset = stocks_data[stocks_data["Symbol"]==i] #[HorizonPeriod:] #converts date to datetime stock = StockDataFrame.retype(subset[["Date","Open", "High", "Low", "Close", "Adj Close", "Volume"]]) #EVWMA Short_EVWMA = pd.DataFrame(TA.EVWMA(subset,9)) Signal_EVWMA = pd.DataFrame(TA.EVWMA(subset,12)) Long_EVWMA = pd.DataFrame(TA.EVWMA(subset,26)) ATR = pd.DataFrame(TA.ATR(subset)) Short_EVWMA.columns = ['EVWMA_9'] Signal_EVWMA.columns = ['EVWMA_12'] Long_EVWMA.columns = ['EVWMA_26'] ATR.columns = ['ATR'] MACD_EVWMA = pd.DataFrame(Long_EVWMA['EVWMA_26'] - Short_EVWMA['EVWMA_9']) MACD_EVWMA.columns = ['Signal'] #Adj Close ts = subset[["Date","Adj Close"]] ts.columns = ['ds', 'y'] #print(ts) m = Prophet(daily_seasonality=True,yearly_seasonality=True) m.add_seasonality(name='monthly', period=30.5, fourier_order=5) m.add_seasonality(name='quarterly', period=91.25, fourier_order=7) m.fit(ts[HorizonPeriod:]) #forecast = m.make_future_dataframe(periods=0, freq='D') forecast = pd.DataFrame(idx3) forecast.columns = ['ds'] #forecast2 = pd.DataFrame(df.index)[HorizonPeriodho+50:] #forecast2.columns = ['ds'] # Predict and plot pred = m.predict(forecast) #pred2 = m.predict(forecast2) dfpre = stock idx1 = dfpre.index #create entry for next trading day (i.e. idx3) merged = idx1.union(idx3) newdf = dfpre.reindex(merged) #A. fbprophet return expected_1day_return = pred.set_index("ds").yhat.pct_change().shift(-1).multiply(100) newdf["custom"] = expected_1day_return.multiply(-1) #B. no fbprophet #newdf['custom'] = ts.set_index('ds') #fbprophet #newdf["custom"] = pred.set_index('ds')['yhat'] #rmse #delta = len(pred.set_index('ds')['yhat'][HorizonPeriod:])-len(df['close'].dropna()) rmse = mean_squared_error(newdf['close'].dropna()[HoldoutPeriod:(HorizonPeriod-1)], pred.set_index('ds')['yhat'][HoldoutPeriod:(HorizonPeriod-1)], squared=True) mape = MAPE(newdf['close'].dropna()[HoldoutPeriod:(HorizonPeriod-1)], pred.set_index('ds')['yhat'][HoldoutPeriod:(HorizonPeriod-1)]) #df = dfpre[(dfpre['Date']> "2018-01-01") & (df['Date']<= end)] df = newdf[HorizonPeriod:] #df["custom"] = (((pred.set_index('ds')['yhat']-ts.set_index('ds')['y'])/ts.set_index('ds')['y']).multiply(-1))[HorizonPeriod:] with contextlib.redirect_stdout(None): b = backtest("multi", df.dropna(), strats=strats_opt, return_history=True, buy_prop=0.10, sell_prop=1,commission=0.01, init_cash=1000) r = {'backtest':b, 'score1':rmse, 'score2':mape, 'name':i, 'forecast':pred} return (r)
expected_1day_return = pred.set_index("ds").yhat.pct_change().shift(-1).multiply(100) df["custom"] = expected_1day_return.multiply(-1) newdf = df.reindex(merged) df = newdf #print(df) m.plot(pred[HorizonPeriod:]) plt.title('Prophet: Forecasted Daily Closing Price', fontsize=25) #exponential smoothing VAMA a = pd.DataFrame(TA.EVWMA(subset)) #ATR b = pd.DataFrame(TA.ATR(subset)) fit3 = SimpleExpSmoothing(a, initialization_method="estimated").fit() fcast3 = fit3.forecast(1).rename(r'$\alpha=%s$'%fit3.model.params['smoothing_level']) #weighted moving averages s = mpf.make_mpf_style(base_mpf_style='charles', rc={'font.size': 6}) # add your own style here fig = mpf.figure(figsize=(10, 7), style=s) ax = fig.add_subplot(2,1,1) av = fig.add_subplot(2,1,2, sharex=ax) #az = fig.add_subplot(3,1,1) mpf.plot(subset[HorizonPeriod:],type='candle',mav=(3,6,9),volume=av,show_nontrading=True, ax=ax) my_dpi = 50 fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(20, 20), dpi=my_dpi)
def halftrend(data: pd.DataFrame, amplitude: int = 2, channel_deviation: float = 2) -> pd.DataFrame: """HalfTrend indicator, ported from the HalfTrend indicator by Alex Orekhov (everget) on TradingView. Parameters ---------- data : pd.DataFrame OHLC price data. amplitude : int, optional The lookback window. The default is 2. channel_deviation : float, optional The ATR channel deviation factor. The default is 2. Returns ------- htdf : TYPE DESCRIPTION. References ---------- https://www.tradingview.com/script/U1SJ8ubc-HalfTrend/ """ # Initialisation atr2 = TA.ATR(data, 100) / 2 dev = channel_deviation * atr2 high_price = data.High.rolling(amplitude).max().fillna(0) low_price = data.Low.rolling(amplitude).min().fillna(0) highma = TA.SMA(data, period=amplitude, column='High') lowma = TA.SMA(data, period=amplitude, column='Low') trend = np.zeros(len(data)) next_trend = np.zeros(len(data)) max_low_price = np.zeros(len(data)) max_low_price[0] = data.Low[0] min_high_price = np.zeros(len(data)) min_high_price[0] = data.High[0] for i in range(1, len(data)): if next_trend[i - 1] == 1: max_low_price[i] = max(low_price[i - 1], max_low_price[i - 1]) if highma[i] < max_low_price[i] and data.Close[i] < data.Low[i - 1]: trend[i] = 1 next_trend[i] = 0 min_high_price[i] = high_price[i] else: # assign previous values again trend[i] = trend[i - 1] next_trend[i] = next_trend[i - 1] min_high_price[i] = min_high_price[i - 1] else: min_high_price[i] = min(high_price[i - 1], min_high_price[i - 1]) if lowma[i] > min_high_price[i] and data.Close[i] > data.High[i - 1]: trend[i] = 0 next_trend[i] = 1 max_low_price[i] = low_price[i] else: # assign previous values again trend[i] = trend[i - 1] next_trend[i] = next_trend[i - 1] max_low_price[i] = max_low_price[i - 1] up = np.zeros(len(data)) up[0] = max_low_price[0] down = np.zeros(len(data)) down[0] = min_high_price[0] atr_high = np.zeros(len(data)) atr_low = np.zeros(len(data)) for i in range(1, len(data)): if trend[i] == 0: if trend[i - 1] != 0: up[i] = down[i - 1] else: up[i] = max(max_low_price[i - 1], up[i - 1]) atr_high[i] = up[i] + dev[i] atr_low[i] = up[i] - dev[i] else: if trend[i - 1] != 1: down[i] = up[i - 1] else: down[i] = min(min_high_price[i - 1], down[i - 1]) atr_high[i] = down[i] + dev[i] atr_low[i] = down[i] - dev[i] halftrend = np.where(trend == 0, up, down) buy = np.where((trend == 0) & (np.roll(trend, 1) == 1), 1, 0) sell = np.where((trend == 1) & (np.roll(trend, 1) == 0), 1, 0) # Construct DataFrame htdf = pd.DataFrame(data={ 'halftrend': halftrend, 'atrHigh': np.nan_to_num(atr_high), 'atrLow': np.nan_to_num(atr_low), 'buy': buy, 'sell': sell }, index=data.index) # Clear false leading signals htdf.buy.values[:100] = np.zeros(100) htdf.sell.values[:100] = np.zeros(100) # Replace leading zeroes with nan htdf['atrHigh'] = htdf.atrHigh.replace(to_replace=0, value=float("nan")) htdf['atrLow'] = htdf.atrLow.replace(to_replace=0, value=float("nan")) return htdf
def get_bbo(self, contract): # Get best b/o excluding own orders vwap = {} ohlcv2 = {} fut2 = contract if contract is 'XBTUSD': fut2 = 'BTC/USD' if contract is 'ETHUSD': fut2 = 'ETH/USD' now = datetime.now() format_iso_now = now.isoformat() then = now - timedelta(minutes=100) format_later_iso = then.isoformat() thetime = then.strftime('%Y-%m-%dT%H:%M:%S') ohlcv = self.client.fetchOHLCV(fut2, '1m', self.client.parse8601(thetime)) ohlcv2 = [] for o in ohlcv: ohlcv2.append([o[1], o[2], o[3], o[4], o[5]]) df = pd.DataFrame(ohlcv2, columns=['open', 'high', 'low', 'close', 'volume']) best_bids = [] best_asks = [] if 1 in self.directional: #print(df) try: self.dsrsi = TA.STOCHRSI(df).iloc[-1] * 100 except: self.dsrsi = 50 #print(self.dsrsi) # Get orderbook if 2 in self.volatility or 3 in self.price or 4 in self.quantity_switch: self.bands[fut2] = TA.BBANDS(df).iloc[-1] self.bbw[fut2] = (TA.BBWIDTH(df).iloc[-1]) print( float(self.bands[fut2]['BB_UPPER'] - self.bands[fut2]['BB_LOWER'])) if (float(self.bands[fut2]['BB_UPPER'] - self.bands[fut2]['BB_LOWER'])) > 0: deltab = (self.get_spot() - self.bands[fut2]['BB_LOWER']) / ( self.bands[fut2]['BB_UPPER'] - self.bands[fut2]['BB_LOWER']) if deltab > 50: self.diffdeltab[fut2] = (deltab - 50) / 100 + 1 if deltab < 50: self.diffdeltab[fut2] = (50 - deltab) / 100 + 1 else: self.diffdeltab[fut2] = 25 / 100 + 1 if 3 in self.volatility: self.atr[fut2] = TA.ATR(df).iloc[-1] if 0 in self.price: # Get orderbook if contract == 'BTC/USD': ob = self.ws['XBTUSD'].market_depth() else: ob = self.ws[contract].market_depth() #ob = self.client.fetchOrderBook( contract ) #print(ob) bids = [] asks = [] for o in ob: if o['side'] == 'Sell': bids.append([o['price'], o['size']]) else: asks.append([o['price'], o['size']]) if contract == 'BTC/USD': ords = self.ws['XBTUSD'].open_orders('') else: ords = self.ws[contract].open_orders('') #print(ords) bid_ords = [o for o in ords if o['side'] == 'Buy'] ask_ords = [o for o in ords if o['side'] == 'Sell'] best_bid = None best_ask = None err = 10**-(self.get_precision(contract) + 1) best_bid = 9999999999999999999 for a in bids: if a[0] < best_bid: best_bid = a[0] best_ask = 0 for a in asks: if a[0] > best_ask: best_ask = a[0] print({'bid': best_bid, 'ask': best_ask}) best_asks.append(best_ask) best_bids.append(best_bid) if 1 in self.price: dvwap = TA.VWAP(df) #print(dvwap) tsz = self.get_ticksize(contract) try: bid = ticksize_floor(dvwap.iloc[-1], tsz) ask = ticksize_ceil(dvwap.iloc[-1], tsz) except: bid = ticksize_floor(self.get_spot(), tsz) ask = ticksize_ceil(self.get_spot(), tsz) print({'bid': bid, 'ask': ask}) best_asks.append(best_ask) best_bids.append(best_bid) if 2 in self.quantity_switch: dppo = TA.PPO(df) self.buysellsignal[fut2] = 1 try: if (dppo.iloc[-1].PPO > 0): self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 + PRICE_MOD) else: self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 - PRICE_MOD) if (dppo.iloc[-1].HISTO > 0): self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 + PRICE_MOD) else: self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 - PRICE_MOD) if (dppo.iloc[-1].SIGNAL > 0): self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 + PRICE_MOD) else: self.buysellsignal[fut2] = self.buysellsignal[fut2] * ( 1 - PRICE_MOD) except: self.buysellsignal[fut2] = 1 #print({ 'bid': best_bid, 'ask': best_ask }) return { 'bid': self.cal_average(best_bids), 'ask': self.cal_average(best_asks) }
def load_data(ticker, n_steps=50, scale=True, shuffle=True, lookup_step=1, test_size=0.2, feature_columns=['adjclose', 'volume', 'open', 'high', 'low'], train_rand=False): """ Loads data from Yahoo Finance source, as well as scaling, shuffling, normalizing and splitting. Params: ticker (str/pd.DataFrame): the ticker you want to load, examples include AAPL, TESL, etc. n_steps (int): the historical sequence length (i.e window size) used to predict, default is 50 scale (bool): whether to scale prices from 0 to 1, default is True shuffle (bool): whether to shuffle the data, default is True lookup_step (int): the future lookup step to predict, default is 1 (e.g next day) test_size (float): ratio for test data, default is 0.2 (20% testing data) feature_columns (list): the list of features to use to feed into the model, default is everything grabbed from yahoo_fin """ # see if ticker is already a loaded stock from yahoo finance if isinstance(ticker, str): # load it from yahoo_fin library df = si.get_data(ticker) elif isinstance(ticker, pd.DataFrame): # already loaded, use it directly df = ticker else: raise TypeError( "ticker can be either a str or a `pd.DataFrame` instances") # this will contain all the elements we want to return from this function result = {} # we will also return the original dataframe itself result['df'] = df.copy() arg_keys = df.keys() # INDICATORS if 'ma50' in arg_keys: df['ma50'] = TA.SMA(result['df'], 50) if 'ma200' in arg_keys: df['ma200'] = TA.SMA(result['df'], 200) if 'RSI' in arg_keys: df['rsi'] = TA.RSI(result['df']) if 'SMM' in arg_keys: df['SMM'] = TA.SMM(result['df']) if 'SSMA' in arg_keys: df['SSMA'] = TA.SSMA(result['df']) if 'EMA' in arg_keys: df['EMA'] = TA.EMA(result['df']) if 'DEMA' in arg_keys: df['DEMA'] = TA.DEMA(result['df']) if 'TEMA' in arg_keys: df['TEMA'] = TA.TEMA(result['df']) if 'TRIMA' in arg_keys: df['TRIMA'] = TA.TRIMA(result['df']) if 'TRIX' in arg_keys: df['TRIX'] = TA.TRIX(result['df']) if 'LWMA' in arg_keys: df['LWMA'] = TA.LWMA(result['df']) if 'VAMA' in arg_keys: df['VAMA'] = TA.VAMA(result['df']) if 'VIDYA' in arg_keys: df['VIDYA'] = TA.VIDYA(result['df']) if 'ER' in arg_keys: df['ER'] = TA.ER(result['df']) if 'KAMA' in arg_keys: df['KAMA'] = TA.KAMA(result['df']) if 'ZLEMA' in arg_keys: df['ZLEMA'] = TA.ZLEMA(result['df']) if 'WMA' in arg_keys: df['WMA'] = TA.WMA(result['df']) if 'HMA' in arg_keys: df['HMA'] = TA.HMA(result['df']) if 'EVWMA' in arg_keys: df['EVWMA'] = TA.EVWMA(result['df']) if 'VWAP' in arg_keys: df['VWAP'] = TA.VWAP(result['df']) if 'SMMA' in arg_keys: df['SMMA'] = TA.SMMA(result['df']) if 'ALMA' in arg_keys: df['ALMA'] = TA.ALMA(result['df']) if 'MAMA' in arg_keys: df['MAMA'] = TA.MAMA(result['df']) if 'FRAMA' in arg_keys: df['FRAMA'] = TA.FRAMA(result['df']) if 'MACD' in arg_keys: df['MACD'] = TA.MACD(result['df']) if 'PPO' in arg_keys: df['PPO'] = TA.PPO(result['df']) if 'VW_MACD' in arg_keys: df['VW_MACD'] = TA.VW_MACD(result['df']) if 'EV_MACD' in arg_keys: df['EV_MACD'] = TA.EV_MACD(result['df']) if 'MOM' in arg_keys: df['MOM'] = TA.MOM(result['df']) if 'ROC' in arg_keys: df['ROC'] = TA.ROC(result['df']) if 'VBM' in arg_keys: df['VBM'] = TA.VBM(result['df']) if 'RSI' in arg_keys: df['RSI'] = TA.RSI(result['df']) if 'IFT_RSI' in arg_keys: df['IFT_RSI'] = TA.IFT_RSI(result['df']) if 'SWI' in arg_keys: df['SWI'] = TA.SWI(result['df']) if 'DYMI' in arg_keys: df['DYMI'] = TA.DYMI(result['df']) if 'TR' in arg_keys: df['TR'] = TA.TR(result['df']) if 'ATR' in arg_keys: df['ATR'] = TA.ATR(result['df']) if 'SAR' in arg_keys: df['SAR'] = TA.SAR(result['df']) if 'PSAR' in arg_keys: df['PSAR'] = TA.PSAR(result['df']) for col in feature_columns: assert col in df.columns, f"'{col}' does not exist in the dataframe." if scale: column_scaler = {} # scale the data (prices) from 0 to 1 for column in feature_columns: scaler = preprocessing.MinMaxScaler() df[column] = scaler.fit_transform( np.expand_dims(df[column].values, axis=1)) column_scaler[column] = scaler # add the MinMaxScaler instances to the result returned result["column_scaler"] = column_scaler # add the target column (label) by shifting by `lookup_step` df['future'] = df['adjclose'].shift(-lookup_step) # last `lookup_step` columns contains NaN in future column # get them before droping NaNs last_sequence = np.array(df[feature_columns].tail(lookup_step)) # drop NaNs df.dropna(inplace=True) sequence_data = [] sequences = deque(maxlen=n_steps) for entry, target in zip(df[feature_columns].values, df['future'].values): sequences.append(entry) if len(sequences) == n_steps: sequence_data.append([np.array(sequences), target]) # get the last sequence by appending the last `n_step` sequence with `lookup_step` sequence # for instance, if n_steps=50 and lookup_step=10, last_sequence should be of 60 (that is 50+10) length # this last_sequence will be used to predict future stock prices not available in the dataset last_sequence = list(sequences) + list(last_sequence) last_sequence = np.array(last_sequence) # add to result result['last_sequence'] = last_sequence # construct the X's and y's X, y = [], [] for seq, target in sequence_data: X.append(seq) y.append(target) # convert to numpy arrays X = np.array(X) y = np.array(y) # reshape X to fit the neural network X = X.reshape((X.shape[0], X.shape[2], X.shape[1])) # split the dataset if train_rand: result["X_train"], result["X_test"], result["y_train"], result["y_test"] = train_test_split( X, y, test_size=test_size, shuffle=shuffle) else: split_point = int(len(X) * (1 - test_size)) result["X_train"], result["X_test"], result["y_train"], result["y_test"] = X[: split_point], X[split_point:], y[:split_point], y[split_point:] # return the result return result