from binance.client import Client import matplotlib.pyplot as plt import numpy as np from datetime import datetime import talib as ta #import statsmodels.api as sm import os, sys import asyncio class Trader: def __init__(self, file): self.connect(file) """ Creates Binance client """ def connect(self,file): lines = [line.rstrip('\n') for line in open(file)] key = lines[0] secret = lines[1] self.client = Client(key, secret) """ Gets all account balances """ def getBalances(self): prices = self.client.get_withdraw_history() return prices filename = 'credentials.txt' trader = Trader(filename) filtered_pairs1 = [] filtered_pairs2 = [] filtered_pairs3 = [] filtered_pairs4 = [] selected_pair = [] selected_pairCMO = [] trading_pairs = [ '1INCHUSDT','AAVEUSDT','ACAUSDT','ACHUSDT','ACMUSDT', 'ADAUSDT','ADXUSDT','AGLDUSDT','AIONUSDT','AKROUSDT', 'ALCXUSDT','ALGOUSDT','ALICEUSDT','ALPACAUSDT','ALPHAUSDT', 'ALPINEUSDT','AMPUSDT','ANCUSDT','ANKRUSDT','ANTUSDT', 'APEUSDT','API3USDT','APTUSDT','ARDRUSDT','ARPAUSDT','ARUSDT', 'ASRUSDT','ASTRUSDT','ATAUSDT','ATMUSDT','ATOMUSDT', 'AUCTIONUSDT','AUDIOUSDT','AUTOUSDT','AVAUSDT', 'AVAXUSDT','AXSUSDT','BADGERUSDT','BAKEUSDT','BALUSDT', 'BANDUSDT','BARUSDT','BATUSDT','BCHUSDT','BEAMUSDT', 'BELUSDT','BETAUSDT','BICOUSDT','BIFIUSDT','BLZUSDT','BNBUSDT', 'BNTUSDT','BNXUSDT','BONDUSDT','BSWUSDT', 'BTCUSDT','BTSUSDT','BTTCUSDT','BURGERUSDT', 'C98USDT','CAKEUSDT','CELOUSDT','CELRUSDT','CFXUSDT', 'CHESSUSDT','CHRUSDT','CHZUSDT','CITYUSDT','CKBUSDT', 'CLVUSDT','COCOSUSDT','COMPUSDT','COSUSDT','COTIUSDT', 'CRVUSDT','CTKUSDT','CTSIUSDT','CTXCUSDT','CVCUSDT', 'CVPUSDT','CVXUSDT','DARUSDT','DASHUSDT','DATAUSDT', 'DCRUSDT','DEGOUSDT','DENTUSDT','DEXEUSDT','DFUSDT', 'DGBUSDT','DIAUSDT','DOCKUSDT','DODOUSDT', 'DOGEUSDT','DOTUSDT','DREPUSDT','DUSKUSDT','DYDXUSDT', 'EGLDUSDT','ELFUSDT','ENJUSDT','ENSUSDT','EOSUSDT', 'EPXUSDT','ERNUSDT','ETCUSDT','ETHUSDT','FARMUSDT', 'FETUSDT','FIDAUSDT','FILUSDT','FIOUSDT','FIROUSDT', 'FISUSDT','FLMUSDT','FLOWUSDT','FLUXUSDT','FORTHUSDT', 'FORUSDT','FRONTUSDT','FTMUSDT','FUNUSDT', 'FXSUSDT','GALAUSDT','GALUSDT','GHSTUSDT','GLMRUSDT', 'GMTUSDT','GNOUSDT','GRTUSDT','GTCUSDT','GTOUSDT', 'HARDUSDT','HBARUSDT','HFTUSDT','HIGHUSDT','HIVEUSDT', 'HOTUSDT','ICPUSDT','ICXUSDT','IDEXUSDT','ILVUSDT', 'IMXUSDT','INJUSDT','IOSTUSDT','IOTAUSDT','IOTXUSDT', 'IRISUSDT','JASMYUSDT','JOEUSDT','JSTUSDT','JUVUSDT', 'KAVAUSDT','KDAUSDT','KEYUSDT','KLAYUSDT','KMDUSDT', 'KNCUSDT','KP3RUSDT','KSMUSDT','LAZIOUSDT','LDOUSDT','LEVERUSDT', 'LINAUSDT','LINKUSDT','LITUSDT','LOKAUSDT','LPTUSDT', 'LRCUSDT','LSKUSDT','LTCUSDT','LTOUSDT','LUNAUSDT','LUNCUSDT','MANAUSDT', 'MASKUSDT','MATICUSDT','MBLUSDT','MBOXUSDT','MCUSDT', 'MDTUSDT','MDXUSDT','MFTUSDT','MINAUSDT','MIRUSDT', 'MITHUSDT','MKRUSDT','MLNUSDT','MOBUSDT','MOVRUSDT', 'MTLUSDT','MULTIUSDT','NEBLUSDT','NEARUSDT','NEOUSDT', 'NEXOUSDT','NKNUSDT','NMRUSDT','NULSUSDT','OCEANUSDT', 'OGNUSDT','OGUSDT','OMGUSDT','OMUSDT','ONEUSDT', 'ONGUSDT','ONTUSDT','OOKIUSDT','ORNUSDT','OSMOUSDT','OXTUSDT', 'PAXGUSDT','PEOPLEUSDT','PERLUSDT','PERPUSDT','PHAUSDT','PHBUSDT', 'PLAUSDT','PNTUSDT','POLSUSDT','POLYXUSDT','PONDUSDT', 'PORTOUSDT','POWRUSDT','PSGUSDT','PUNDIXUSDT','PYRUSDT', 'QIUSDT','QNTUSDT','QTUMUSDT','QUICKUSDT','RADUSDT', 'RAREUSDT','RAYUSDT','REEFUSDT','REIUSDT', 'RENUSDT','REPUSDT','REQUSDT','RIFUSDT','RLCUSDT', 'RNDRUSDT','ROSEUSDT','RSRUSDT','RUNEUSDT','RVNUSDT', 'SANDUSDT','SANTOSUSDT','SCRTUSDT','SCUSDT','SFPUSDT', 'SHIBUSDT','SKLUSDT','SLPUSDT','SNXUSDT','SOLUSDT', 'SPELLUSDT','SRMUSDT','STEEMUSDT','STGUSDT','STMXUSDT','STORJUSDT','STPTUSDT', 'STRAXUSDT','STXUSDT','SUNUSDT','SUPERUSDT','SUSHIUSDT', 'SXPUSDT','SYSUSDT','TFUELUSDT','THETAUSDT', 'TKOUSDT','TLMUSDT','TOMOUSDT','TORNUSDT','TRBUSDT', 'TRIBEUSDT','TROYUSDT','TRUUSDT','TRXUSDT','TUSDT', 'TVKUSDT','TWTUSDT','UMAUSDT','UNFIUSDT','UNIUSDT', 'UTKUSDT','VETUSDT','VGXUSDT','VIDTUSDT','VITEUSDT', 'VOXELUSDT','VTHOUSDT','WANUSDT','WAVESUSDT','WAXPUSDT', 'WINGUSDT','WINUSDT','WNXMUSDT','WOOUSDT','WRXUSDT', 'WTCUSDT','XECUSDT','XEMUSDT','XLMUSDT','XMRUSDT', 'XNOUSDT','XRPUSDT','XTZUSDT','XVGUSDT','XVSUSDT', 'YFIIUSDT','YFIUSDT','YGGUSDT','ZECUSDT','ZENUSDT', 'ZILUSDT','ZRXUSDT' ] def filter1(pair): interval = '4h' symbol = pair klines = trader.client.get_klines(symbol=symbol,interval=interval) open_time = [int(entry[0]) for entry in klines] close = [float(entry[4]) for entry in klines] close_array = np.asarray(close) print("on 4h timeframe " + symbol) x = close y = range(len(x)) best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y) best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01 best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99 if x[-1] < best_fit_line3[-1] and best_fit_line1[0] <= best_fit_line1[-1]: filtered_pairs1.append(symbol) print('found') #plt.figure(figsize=(8,6)) #plt.grid(True) #plt.plot(x) #plt.plot(best_fit_line1, '--', color='r') #plt.plot(best_fit_line2, '--', color='r') #plt.plot(best_fit_line3, '--', color='r') #plt.show(block=False) #plt.pause(6) #plt.close() elif x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]: filtered_pairs1.append(symbol) print('found') #plt.figure(figsize=(8,6)) #plt.grid(True) #plt.plot(x) #plt.plot(best_fit_line1, '--', color='r') #plt.plot(best_fit_line2, '--', color='r') #plt.plot(best_fit_line3, '--', color='r') #plt.show(block=False) #plt.pause(6) #plt.close() else: print('searching') def filter2(filtered_pairs1): interval = '2h' symbol = filtered_pairs1 klines = trader.client.get_klines(symbol=symbol,interval=interval) open_time = [int(entry[0]) for entry in klines] close = [float(entry[4]) for entry in klines] close_array = np.asarray(close) print("on 2h " + symbol) x = close y = range(len(x)) best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y) best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01 best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99 if x[-1] < best_fit_line3[-1] and best_fit_line1[0] < best_fit_line1[-1]: filtered_pairs2.append(symbol) print('found') #plt.figure(figsize=(8,6)) #plt.grid(True) #plt.plot(x) #plt.plot(best_fit_line1, '--', color='r') #plt.plot(best_fit_line2, '--', color='r') #plt.plot(best_fit_line3, '--', color='r') #plt.show(block=False) #plt.pause(6) #plt.close() if x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]: filtered_pairs2.append(symbol) print('found') #plt.figure(figsize=(8,6)) #plt.grid(True) #plt.plot(x) #plt.plot(best_fit_line1, '--', color='r') #plt.plot(best_fit_line2, '--', color='r') #plt.plot(best_fit_line3, '--', color='r') #plt.show(block=False) #plt.pause(6) #plt.close() def filter3(filtered_pairs2): interval = '1h' symbol = filtered_pairs2 klines = trader.client.get_klines(symbol=symbol,interval=interval) open_time = [int(entry[0]) for entry in klines] close = [float(entry[4]) for entry in klines] close_array = np.asarray(close) print("on 1h timeframe " + symbol) #min = ta.MIN(close_array, timeperiod=30) #max = ta.MAX(close_array, timeperiod=30) #real = ta.HT_TRENDLINE(close_array) #wcl = ta.WCLPRICE(max, min, close_array) print(close[-1]) print() #print(min[-1]) #print(max[-1]) #print(real[-1]) x = close y = range(len(x)) best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y) best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01 best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99 if x[-1] < best_fit_line1[-1]: filtered_pairs3.append(symbol) print('found') #plt.figure(figsize=(8,6)) #plt.title(symbol) #plt.grid(True) #plt.plot(close) #plt.plot(best_fit_line1, '--', color='r') #plt.plot(best_fit_line2, '--', color='r') #plt.plot(best_fit_line3, '--', color='r') #plt.plot(close) #plt.plot(min) #plt.plot(max) #plt.plot(real) #plt.show(block=False) #plt.pause(5) #plt.close() else: print('searching') def filter4(filtered_pairs3): interval = '15m' symbol = filtered_pairs3 klines = trader.client.get_klines(symbol=symbol,interval=interval) open_time = [int(entry[0]) for entry in klines] close = [float(entry[4]) for entry in klines] close_array = np.asarray(close) print("on 15m timeframe " + symbol) #min = ta.MIN(close_array, timeperiod=30) #max = ta.MAX(close_array, timeperiod=30) #real = ta.HT_TRENDLINE(close_array) #wcl = ta.WCLPRICE(max, min, close_array) print(close[-1]) print() #print(min[-1]) #print(max[-1]) #print(real[-1]) x = close y = range(len(x)) best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y) best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01 best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99 if x[-1] < best_fit_line1[-1]: filtered_pairs4.append(symbol) print('found') #plt.figure(figsize=(8,6)) #plt.title(symbol) #plt.grid(True) #plt.plot(close) #plt.plot(best_fit_line1, '--', color='r') #plt.plot(best_fit_line2, '--', color='r') #plt.plot(best_fit_line3, '--', color='r') #plt.plot(close) #plt.plot(min) #plt.plot(max) #plt.plot(real) #plt.show(block=False) #plt.pause(5) #plt.close() else: print('searching') def momentum(filtered_pairs4): interval = '5m' symbol = filtered_pairs4 klines = trader.client.get_klines(symbol=symbol,interval=interval) open_time = [int(entry[0]) for entry in klines] close = [float(entry[4]) for entry in klines] close_array = np.asarray(close) print() print("on 5m timeframe " + symbol) real = ta.CMO(close_array, timeperiod=14) #print(real[-1]) if real[-1] < -50: print('oversold dip found') selected_pair.append(symbol) selected_pairCMO.append(real[-1]) else: print('searching') for i in trading_pairs: output = filter1(i) print(filtered_pairs1) for i in filtered_pairs1: output = filter2(i) print(filtered_pairs2) for i in filtered_pairs2: output = filter3(i) print(filtered_pairs3) for i in filtered_pairs3: output = filter4(i) print(filtered_pairs4) for i in filtered_pairs4: output = momentum(i) print(selected_pair) if len(selected_pair) > 1: print('dips are more then 1 oversold') print(selected_pair) #print(selected_pairCMO) if min(selected_pairCMO) in selected_pairCMO: #print(selected_pairCMO.index(min(selected_pairCMO))) position = selected_pairCMO.index(min(selected_pairCMO)) for id, value in enumerate(selected_pair): if id == position: print(selected_pair[id]) #sys.exit() elif len(selected_pair) == 1: print('1 dip found') print(selected_pair) #print(selected_pairCMO) #sys.exit() else: print('no oversold dips for the moment, rescan dips...') #print(selected_pair) #print(selected_pairCMO) #os.execl(sys.executable, sys.executable, *sys.argv) sys.exit(0) sys.exit() exit()