Advertisement
xmd79

dips

Mar 22nd, 2024 (edited)
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.34 KB | None | 0 0
  1. from binance.client import Client
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from datetime import datetime
  5. import talib as ta
  6. import statsmodels.api as sm
  7. import os, sys
  8.  
  9. import asyncio
  10.  
  11.  
  12. class Trader:
  13.     def __init__(self, file):
  14.         self.connect(file)
  15.  
  16.     """ Creates Binance client """
  17.     def connect(self,file):
  18.         lines = [line.rstrip('\n') for line in open(file)]
  19.         key = lines[0]
  20.         secret = lines[1]
  21.         self.client = Client(key, secret)
  22.  
  23.     """ Gets all account balances """
  24.     def getBalances(self):
  25.         prices = self.client.get_withdraw_history()
  26.         return prices
  27.  
  28. filename = 'credentials.txt'
  29. trader = Trader(filename)
  30.  
  31.  
  32.  
  33. filtered_pairs1 = []
  34. filtered_pairs2 = []
  35. filtered_pairs3 = []
  36. selected_pair = []
  37. selected_pairCMO = []
  38.  
  39.  
  40. trading_pairs = []
  41.  
  42. # Assuming 'trader' and 'client' are predefined objects
  43. info = trader.client.get_exchange_info()
  44. symbols = info['symbols']
  45.  
  46. for s in symbols:
  47.     # Check if the symbol's quote asset is 'USDT' and is active for trading
  48.     if s['quoteAsset'] == 'USDT' and s['status'] == 'TRADING' and 'SPOT' in s['permissions']:
  49.         trading_pairs.append(s['symbol'])
  50.  
  51. with open('alts.txt', 'w') as f:
  52.     for pair in trading_pairs:
  53.         f.write(pair + '\n')
  54.  
  55.  
  56. def filter1(pair):
  57.  
  58.     interval = '2h'
  59.     symbol = pair
  60.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  61.     open_time = [int(entry[0]) for entry in klines]
  62.     close = [float(entry[4]) for entry in klines]
  63.     close_array = np.asarray(close)
  64.  
  65.     print("on 2h timeframe " + symbol)
  66.  
  67.     x = close
  68.     y = range(len(x))
  69.  
  70.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  71.     best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  72.     best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  73.  
  74.     if x[-1] < best_fit_line3[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  75.         filtered_pairs1.append(symbol)
  76.         print('found')
  77.  
  78.         #plt.figure(figsize=(8,6))
  79.         #plt.grid(True)
  80.         #plt.plot(x)
  81.         #plt.plot(best_fit_line1, '--', color='r')
  82.         #plt.plot(best_fit_line2, '--', color='r')
  83.         #plt.plot(best_fit_line3, '--', color='r')
  84.         #plt.show(block=False)
  85.         #plt.pause(6)
  86.         #plt.close()
  87.  
  88.     elif x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  89.         filtered_pairs1.append(symbol)
  90.         print('found')
  91.  
  92.         #plt.figure(figsize=(8,6))
  93.         #plt.grid(True)
  94.         #plt.plot(x)
  95.         #plt.plot(best_fit_line1, '--', color='r')
  96.         #plt.plot(best_fit_line2, '--', color='r')
  97.         #plt.plot(best_fit_line3, '--', color='r')
  98.         #plt.show(block=False)
  99.         #plt.pause(6)
  100.         #plt.close()
  101.  
  102.     else:
  103.         print('searching')
  104.  
  105. def filter2(filtered_pairs1):
  106.     interval = '15m'
  107.     symbol = filtered_pairs1
  108.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  109.     open_time = [int(entry[0]) for entry in klines]
  110.     close = [float(entry[4]) for entry in klines]
  111.     close_array = np.asarray(close)
  112.  
  113.     print("on 15min timeframe " + symbol)
  114.  
  115.     x = close
  116.     y = range(len(x))
  117.  
  118.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  119.     best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  120.     best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  121.  
  122.     if x[-1] < best_fit_line3[-1] and best_fit_line1[0] < best_fit_line1[-1]:
  123.         filtered_pairs2.append(symbol)
  124.         print('found')
  125.  
  126.         #plt.figure(figsize=(8,6))
  127.         #plt.grid(True)
  128.         #plt.plot(x)
  129.         #plt.plot(best_fit_line1, '--', color='r')
  130.         #plt.plot(best_fit_line2, '--', color='r')
  131.         #plt.plot(best_fit_line3, '--', color='r')
  132.         #plt.show(block=False)
  133.         #plt.pause(6)
  134.         #plt.close()
  135.  
  136.     if x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  137.         filtered_pairs2.append(symbol)
  138.         print('found')
  139.  
  140.         #plt.figure(figsize=(8,6))
  141.         #plt.grid(True)
  142.         #plt.plot(x)
  143.         #plt.plot(best_fit_line1, '--', color='r')
  144.         #plt.plot(best_fit_line2, '--', color='r')
  145.         #plt.plot(best_fit_line3, '--', color='r')
  146.         #plt.show(block=False)
  147.         #plt.pause(6)
  148.         #plt.close()
  149.  
  150. def filter3(filtered_pairs2):
  151.     interval = '5m'
  152.     symbol = filtered_pairs2
  153.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  154.     open_time = [int(entry[0]) for entry in klines]
  155.     close = [float(entry[4]) for entry in klines]
  156.     close_array = np.asarray(close)
  157.  
  158.     print("on 5m timeframe " + symbol)
  159.  
  160.     #min = ta.MIN(close_array, timeperiod=30)
  161.     #max = ta.MAX(close_array, timeperiod=30)
  162.  
  163.     #real = ta.HT_TRENDLINE(close_array)
  164.     #wcl = ta.WCLPRICE(max, min, close_array)
  165.    
  166.     print(close[-1])
  167.     print()
  168.     #print(min[-1])
  169.     #print(max[-1])
  170.     #print(real[-1])    
  171.  
  172.     x = close
  173.     y = range(len(x))
  174.  
  175.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  176.     best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  177.     best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  178.  
  179.     if x[-1] < best_fit_line1[-1]:
  180.         filtered_pairs3.append(symbol)
  181.         print('found')
  182.  
  183.         #plt.figure(figsize=(8,6))
  184.         #plt.title(symbol)
  185.         #plt.grid(True)
  186.         #plt.plot(close)
  187.         #plt.plot(best_fit_line1, '--', color='r')
  188.         #plt.plot(best_fit_line2, '--', color='r')
  189.         #plt.plot(best_fit_line3, '--', color='r')
  190.         #plt.plot(close)
  191.         #plt.plot(min)
  192.         #plt.plot(max)
  193.         #plt.plot(real)
  194.         #plt.show(block=False)
  195.         #plt.pause(5)
  196.         #plt.close()
  197.  
  198.     else:
  199.         print('searching')
  200.  
  201.  
  202. def momentum(filtered_pairs3):
  203.     interval = '1m'
  204.     symbol = filtered_pairs3
  205.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  206.     open_time = [int(entry[0]) for entry in klines]
  207.     close = [float(entry[4]) for entry in klines]
  208.     close_array = np.asarray(close)
  209.  
  210.     print("on 1m timeframe " + symbol)
  211.  
  212.     real = ta.CMO(close_array, timeperiod=14)
  213.     #print(real[-1])
  214.  
  215.     if real[-1] < -50:
  216.         print('oversold dip found')
  217.         selected_pair.append(symbol)
  218.         selected_pairCMO.append(real[-1])
  219.  
  220.     else:
  221.         print('searching')
  222.  
  223. for i in trading_pairs:
  224.     output = filter1(i)
  225.     print(filtered_pairs1)
  226.  
  227. for i in filtered_pairs1:
  228.     output = filter2(i)
  229.     print(filtered_pairs2)
  230.  
  231. for i in filtered_pairs2:
  232.     output = filter3(i)
  233.     print(filtered_pairs3)
  234.  
  235. for i in filtered_pairs3:
  236.     output = momentum(i)
  237.     print(selected_pair)
  238.  
  239. if len(selected_pair) > 1:
  240.     print('dips are more then 1 oversold')
  241.     print(selected_pair)
  242.     print(selected_pairCMO)
  243.    
  244.     if min(selected_pairCMO) in selected_pairCMO:
  245.         #print(selected_pairCMO.index(min(selected_pairCMO)))
  246.         position = selected_pairCMO.index(min(selected_pairCMO))
  247.  
  248.     for id, value in enumerate(selected_pair):
  249.         if id == position:
  250.             print(selected_pair[id])
  251.     #sys.exit()
  252.  
  253. elif len(selected_pair) == 1:
  254.     print('1 dip found')  
  255.     print(selected_pair)
  256.     print(selected_pairCMO)
  257.     #sys.exit()
  258.  
  259. else:
  260.     print('no oversold dips for the moment, restart script...')
  261.     print(selected_pair)
  262.     print(selected_pairCMO)
  263.     #os.execl(sys.executable, sys.executable, *sys.argv)
  264.  
  265. sys.exit(0)
  266. sys.exit()
  267. exit()
  268.  
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement