def calculate_rsi(date, coin, screen): tickers_df = tickers.get_tickers(15, coin, date, screen) if len(tickers_df) >= 15: profit = [] loss = [] df = tickers_df.iloc[(len(tickers_df) - 15):len(tickers_df)] for i in range(len(df)): if i == 0: continue last_d = df.price.values[i] last_d1 = df.price.values[(i - 1)] dif = last_d - last_d1 if dif >= 0: profit.append(dif) else: loss.append(dif * (-1)) if len(profit) == 0: rs = 0 else: if len(loss) == 0: rs = 100 else: rs = np.mean(profit) / np.mean(loss) # RSI = 100 - 100 / (1 + RS) rsi_value = 100 - (100 / (1 + rs)) rsis.insert_rsi(date, coin, rsi_value, screen)
def calculate_macd(date, coin, screen): signal_line = np.NaN histogram = np.NaN tickers_df = tickers.get_tickers(26, coin, date, screen) if len(tickers_df) >= 26: ema_12 = tickers_df.iloc[len(tickers_df) - 12:len(tickers_df)].drop( ['date', 'coin'], axis=1).price.ewm(span=12, min_periods=12, adjust=True, ignore_na=False).mean() ema_26 = tickers_df.iloc[len(tickers_df) - 26:len(tickers_df)].drop( ['date', 'coin'], axis=1).price.ewm(span=26, min_periods=26, adjust=True, ignore_na=False).mean() ema12 = ema_12.iloc[11] ema26 = ema_26.iloc[25] macd_line = ema_12 - ema_26 macd_df = macds.get_macds(9, coin, date, screen) if len(macd_df) >= 9: signal_line = macd_df.iloc[len(macd_df) - 9:len(macd_df)].macd_line.ewm( span=9, min_periods=9, adjust=True, ignore_na=False).mean() signal_line = signal_line[0] histogram = macd_line[0] - signal_line macds.insert_macd(date, coin, ema12, ema26, macd_line[0], signal_line, histogram, screen)
def triple_screen(n, coin, screen): tickers_df = tickers.get_tickers(n, coin, datetime.now(), 0) if len(tickers_df) >= n: # last = tickers_df.price.mean() last = tickers_df.iloc[0].price tickers.insert_tickers(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), coin, last, screen)
def calculate_sma(date, coin, screen): tickers_df = tickers.get_tickers(20, coin, date, screen) if len(tickers_df) >= 5: sma5 = tickers_df.iloc[(len(tickers_df) - 5):len(tickers_df)].price.mean() sma5_theta = calculate_theta(date, coin, sma5, screen) if len(tickers_df) >= 20: sma20 = tickers_df.iloc[(len(tickers_df) - 20):len(tickers_df)].price.mean() smas.insert_smas(date, coin, sma5, sma20, sma5_theta, screen)
def get_boillinger_vote(date, coin, screen): vote = 0 boillinger_df = boillingers.get_boillinger(1, coin, date, screen) if not boillinger_df.empty: tickers_df = tickers.get_tickers(1, coin, date) if not tickers_df.empty: price = tickers_df.iloc[0].price lower_band = boillinger_df.iloc[0].lower_band upper_band = boillinger_df.iloc[0].upper_band if price < lower_band: vote = 1 elif price > upper_band: vote = -1 return vote
def calculate_boillinger(date, coin, screen): tickers_df = tickers.get_tickers(20, coin, date, screen) if len(tickers_df) >= 20: sma20 = tickers_df.iloc[(len(tickers_df) - 20):len(tickers_df)].price.mean() std20 = tickers_df.iloc[(len(tickers_df) - 20):len(tickers_df)].price.std() ema20 = emas.get_emas(1, coin, date, screen).iloc[0].ema20 # upper_band = sma20 + 2 * std20 # lower_band = sma20 - 2 * std20] #TODO: CHANGE PERCENTAGE TO A LIL BIT BIGGER upper_band = ema20 * (1 + 0.15) lower_band = ema20 * (1 - 0.15) height = upper_band - lower_band boillingers.insert_boillingers(date, coin, upper_band, lower_band, ema20, height, screen)
def calculate_ema(date, coin, screen): tickers_df = tickers.get_tickers(20, coin, date, screen) if len(tickers_df) >= 5: ema5 = tickers_df.iloc[(len(tickers_df) - 5):len(tickers_df)].price.ewm( span=5, min_periods=5, adjust=True, ignore_na=False).mean() ema5 = ema5.iloc[4] if len(tickers_df) >= 20: ema20 = tickers_df.iloc[(len(tickers_df) - 20):len(tickers_df)].price.ewm( span=20, min_periods=20, adjust=True, ignore_na=False).mean() ema20 = ema20.iloc[19] emas.insert_emas(date, coin, ema5, ema20, screen)
def trend_market(date, coin, ema_dif): from scipy.fftpack import ifft, fft from math import atan # 300 segundos = 5 min # 300 * 4h = 1200 h / 24 h = 50 dias # df = tickers.get_tickers(300, coin, date, 1)#TODO Change screen test df = df[df.date < date] data = df.price if len(df) < 4: vote = 0 return else: get_max_min(coin, df) yf = fft(data) wn = 18 yf[wn:-wn] = 0 iY = ifft(yf).real[::-1] theta = [atan((iY[0] - iY[1]) / 48), atan((iY[1] - iY[2]) / 48)] d_theta = (theta[0] - theta[1]) / theta[1] n = 2 macd_df_one = macds.get_macds(n, coin, date, 1) if len(macd_df_one) < n: return None vote = 0 current_ema26 = macd_df_one.iloc[1].ema_26 current_ema12 = macd_df_one.iloc[1].ema12 dif_current = np.log(current_ema12/current_ema26) base_ema26 = macd_df_one.iloc[0].ema_26 base_ema12 = macd_df_one.iloc[0].ema12 dif_base = np.log(base_ema12/base_ema26) delta_dif = (dif_current - dif_base)/dif_base vote_rsi = rsi_sign(coin, date) if vote_rsi is None: return None theta_b = features.features_signal_theta(theta[1]) theta_c = features.features_signal_theta(theta[0]) dif_b = features.features_signal_dif(dif_base) dif_c = features.features_signal_dif(dif_current) # if theta[0] > 0 and dif_current > 0: # vote = 1 # else: # vote = 0 filename = '../Notebooks/finalized_model.sav' loaded_model = pickle.load(open(filename, 'rb')) [dif_current, theta[0], rsi_sign(coin, date), ] result = loaded_model.predict(X_test, Y_test) print(result) insert_trend(coin, date, 1, dif_current, dif_base, delta_dif, theta[0], theta[1], d_theta, vote) return vote