Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from binance.client import Client
- import matplotlib.pyplot as plt
- import numpy as np
- from datetime import datetime
- import talib as ta
- import os, sys
- 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)
- filename = 'credentials.txt'
- trader = Trader(filename)
- filtered_dips1 = []
- filtered_dips2 = []
- filtered_dips3 = []
- filtered_dips4 = []
- selected_dips = []
- selected_dipsCMO = []
- filtered_tops1 = []
- filtered_tops2 = []
- filtered_tops3 = []
- filtered_tops4 = []
- selected_tops = []
- selected_topsCMO = []
- trading_pairs = ['BTCBUSD','BNBBUSD','DOGEBUSD','AVAXBUSD', 'ADABUSD','SOLBUSD']
- def filter1(pair):
- interval = '1d'
- 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 daily 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_dips1.append(symbol)
- print('dip 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_dips1.append(symbol)
- print('dip 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_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
- filtered_tops1.append(symbol)
- print('top found')
- elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
- filtered_tops1.append(symbol)
- print('top found')
- def filter2(filtered_dips1):
- interval = '4h'
- symbol = filtered_dips1
- 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 " + 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_dips2.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_dips2.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 filter2x(filtered_tops1):
- interval = '4h'
- symbol = filtered_tops1
- 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 " + 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_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
- filtered_tops2.append(symbol)
- print('top found')
- elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
- filtered_tops2.append(symbol)
- print('top found')
- def filter3(filtered_dips2):
- interval = '1h'
- symbol = filtered_dips2
- 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_dips3.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 filter3x(filtered_tops2):
- interval = '1h'
- symbol = filtered_tops2
- 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)
- 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_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
- filtered_tops3.append(symbol)
- print('top found')
- elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
- filtered_tops3.append(symbol)
- print('top found')
- def filter4(filtered_dips3):
- interval = '15m'
- symbol = filtered_dips3
- 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_dips4.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 filter4x(filtered_tops3):
- interval = '15m'
- symbol = filtered_tops3
- 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)
- 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_line2[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
- filtered_tops4.append(symbol)
- print('top found')
- elif x[-1] > best_fit_line2[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
- filtered_tops4.append(symbol)
- print('top found')
- def momentum(filtered_dips4):
- interval = '5m'
- symbol = filtered_dips4
- 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_dips.append(symbol)
- selected_dipsCMO.append(real[-1])
- else:
- print('searching')
- def momentumx(filtered_tops4):
- interval = '5m'
- symbol = filtered_tops4
- 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('overbrought top found')
- selected_tops.append(symbol)
- selected_topsCMO.append(real[-1])
- else:
- print('searching')
- for i in trading_pairs:
- output = filter1(i)
- print(filtered_dips1)
- for i in trading_pairs:
- output = filter1(i)
- print(filtered_tops1)
- for i in filtered_dips1:
- output = filter2(i)
- print(filtered_dips2)
- for i in filtered_tops1:
- output = filter2x(i)
- print(filtered_tops2)
- for i in filtered_dips2:
- output = filter3(i)
- print(filtered_dips3)
- for i in filtered_tops2:
- output = filter3x(i)
- print(filtered_tops3)
- for i in filtered_dips3:
- output = filter4(i)
- print(filtered_dips4)
- for i in filtered_tops3:
- output = filter4x(i)
- print(filtered_tops4)
- for i in filtered_dips4:
- output = momentum(i)
- print(selected_dips)
- for i in filtered_tops4:
- output = momentumx(i)
- print(selected_tops)
- if len(selected_dips) > 1:
- print('dips are more then 1 oversold')
- print(selected_dips)
- #print(selected_dipsCMO)
- if min(selected_dipsCMO) in selected_dipsCMO:
- position = selected_dipsCMO.index(min(selected_dipsCMO))
- for id, value in enumerate(selected_dips):
- if id == position:
- print(selected_dips[id])
- elif len(selected_dips) == 1:
- print('1 dip found')
- print(selected_dips)
- print()
- if len(selected_tops) > 1:
- print('tops are more then 1 overbrought')
- print(selected_tops)
- if min(selected_topsCMO) in selected_topsCMO:
- position = selected_topsCMO.index(max(selected_topsCMO))
- for id, value in enumerate(selected_tops):
- if id == position:
- print(selected_tops[id])
- elif len(selected_tops) == 1:
- print('1 top found')
- print(selected_tops)
- print()
- sys.exit(0)
- sys.exit()
- exit()
Advertisement
Add Comment
Please, Sign In to add comment