Advertisement
xmd79

mtf dip finder with time series filter

May 27th, 2022 (edited)
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.06 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. import pandas as pd
  9.  
  10. class Trader:
  11.     def __init__(self, file):
  12.         self.connect(file)
  13.  
  14.     """ Creates Binance client """
  15.     def connect(self,file):
  16.         lines = [line.rstrip('\n') for line in open(file)]
  17.         key = lines[0]
  18.         secret = lines[1]
  19.         self.client = Client(key, secret)
  20.  
  21.     """ Gets all account balances """
  22.     def getBalances(self):
  23.         prices = self.client.get_withdraw_history()
  24.         return prices
  25.  
  26. filename = 'credentials.txt'
  27. trader = Trader(filename)
  28.  
  29.  
  30. # request info on all spot symbols
  31. # exchange_info = trader.get_exchange_info()  
  32. # trading_pairs = [info['symbol'] for info in exchange_info['symbols']]
  33.  
  34. trading_pairs = [
  35.     '1INCHUSDT','AAVEUSDT','ACAUSDT','ACHUSDT','ACMUSDT',
  36.     'ADAUSDT','ADXUSDT','AGLDUSDT','AIONUSDT','AKROUSDT',
  37.     'ALCXUSDT','ALGOUSDT','ALICEUSDT','ALPACAUSDT','ALPHAUSDT',
  38.     'ALPINEUSDT','AMPUSDT','ANCUSDT','ANKRUSDT','ANTUSDT',
  39.     'APEUSDT','API3USDT','ARDRUSDT','ARPAUSDT','ARUSDT',
  40.     'ASRUSDT','ASTRUSDT','ATAUSDT','ATMUSDT','ATOMUSDT',
  41.     'AUCTIONUSDT','AUDIOUSDT','AUTOUSDT','AVAUSDT',
  42.     'AVAXUSDT','AXSUSDT','BADGERUSDT','BAKEUSDT','BALUSDT',
  43.     'BANDUSDT','BARUSDT','BATUSDT','BCHUSDT','BEAMUSDT',
  44.     'BELUSDT','BETAUSDT','BICOUSDT','BLZUSDT','BNBUSDT',
  45.     'BNTUSDT','BNXUSDT','BONDUSDT','BSWUSDT','BTCSTUSDT',
  46.     'BTCUSDT','BTGUSDT','BTSUSDT','BTTCUSDT','BURGERUSDT',
  47.     'C98USDT','CAKEUSDT','CELOUSDT','CELRUSDT','CFXUSDT',
  48.     'CHESSUSDT','CHRUSDT','CHZUSDT','CITYUSDT','CKBUSDT',
  49.     'CLVUSDT','COCOSUSDT','COMPUSDT','COSUSDT','COTIUSDT',
  50.     'CRVUSDT','CTKUSDT','CTSIUSDT','CTXCUSDT','CVCUSDT',
  51.     'CVPUSDT','CVXUSDT','DARUSDT','DASHUSDT','DATAUSDT',
  52.     'DCRUSDT','DEGOUSDT','DENTUSDT','DEXEUSDT','DFUSDT',
  53.     'DGBUSDT','DIAUSDT','DNTUSDT','DOCKUSDT','DODOUSDT',
  54.     'DOGEUSDT','DOTUSDT','DREPUSDT','DUSKUSDT','DYDXUSDT',
  55.     'EGLDUSDT','ELFUSDT','ENJUSDT','ENSUSDT','EOSUSDT',
  56.     'EPXUSDT','ERNUSDT','ETCUSDT','ETHUSDT','FARMUSDT',
  57.     'FETUSDT','FIDAUSDT','FILUSDT','FIOUSDT','FIROUSDT',
  58.     'FISUSDT','FLMUSDT','FLOWUSDT','FLUXUSDT','FORTHUSDT',
  59.     'FORUSDT','FRONTUSDT','FTMUSDT','FTTUSDT','FUNUSDT',
  60.     'FXSUSDT','GALAUSDT','GALUSDT','GHSTUSDT','GLMRUSDT',
  61.     'GMTUSDT','GNOUSDT','GRTUSDT','GTCUSDT','GTOUSDT',
  62.     'HARDUSDT','HBARUSDT','HIGHUSDT','HIVEUSDT','HNTUSDT',
  63.     'HOTUSDT','ICPUSDT','ICXUSDT','IDEXUSDT','ILVUSDT',
  64.     'IMXUSDT','INJUSDT','IOSTUSDT','IOTAUSDT','IOTXUSDT',
  65.     'IRISUSDT','JASMYUSDT','JOEUSDT','JSTUSDT','JUVUSDT',
  66.     'KAVAUSDT','KDAUSDT','KEYUSDT','KLAYUSDT','KMDUSDT',
  67.     'KNCUSDT','KP3RUSDT','KSMUSDT','LAZIOUSDT','LDOUSDT',
  68.     'LINAUSDT','LINKUSDT','LITUSDT','LOKAUSDT','LPTUSDT',
  69.     'LRCUSDT','LSKUSDT','LTCUSDT','LTOUSDT','MANAUSDT',
  70.     'MASKUSDT','MATICUSDT','MBLUSDT','MBOXUSDT','MCUSDT',
  71.     'MDTUSDT','MDXUSDT','MFTUSDT','MINAUSDT','MIRUSDT',
  72.     'MITHUSDT','MKRUSDT','MLNUSDT','MOBUSDT','MOVRUSDT',
  73.     'MTLUSDT','MULTIUSDT','NBSUSDT','NEARUSDT','NEOUSDT',
  74.     'NEXOUSDT','NKNUSDT','NMRUSDT','NULSUSDT','OCEANUSDT',
  75.     'OGNUSDT','OGUSDT','OMGUSDT','OMUSDT','ONEUSDT',
  76.     'ONGUSDT','ONTUSDT','OOKIUSDT','ORNUSDT','OXTUSDT',
  77.     'PAXGUSDT','PEOPLEUSDT','PERLUSDT','PERPUSDT','PHAUSDT',
  78.     'PLAUSDT','PNTUSDT','POLSUSDT','POLYUSDT','PONDUSDT',
  79.     'PORTOUSDT','POWRUSDT','PSGUSDT','PUNDIXUSDT','PYRUSDT',
  80.     'QIUSDT','QNTUSDT','QTUMUSDT','QUICKUSDT','RADUSDT',
  81.     'RAMPUSDT','RAREUSDT','RAYUSDT','REEFUSDT','REIUSDT',
  82.     'RENUSDT','REPUSDT','REQUSDT','RIFUSDT','RLCUSDT',
  83.     'RNDRUSDT','ROSEUSDT','RSRUSDT','RUNEUSDT','RVNUSDT',
  84.     'SANDUSDT','SANTOSUSDT','SCRTUSDT','SCUSDT','SFPUSDT',
  85.     'SHIBUSDT','SKLUSDT','SLPUSDT','SNXUSDT','SOLUSDT',
  86.     'SPELLUSDT','SRMUSDT','STMXUSDT','STORJUSDT','STPTUSDT',
  87.     'STRAXUSDT','STXUSDT','SUNUSDT','SUPERUSDT','SUSHIUSDT',
  88.     'SXPUSDT','SYSUSDT','TCTUSDT','TFUELUSDT','THETAUSDT',
  89.     'TKOUSDT','TLMUSDT','TOMOUSDT','TORNUSDT','TRBUSDT',
  90.     'TRIBEUSDT','TROYUSDT','TRUUSDT','TRXUSDT','TUSDT',
  91.     'TVKUSDT','TWTUSDT','UMAUSDT','UNFIUSDT','UNIUSDT',
  92.     'UTKUSDT','VETUSDT','VGXUSDT','VIDTUSDT','VITEUSDT',
  93.     'VOXELUSDT','VTHOUSDT','WANUSDT','WAVESUSDT','WAXPUSDT',
  94.     'WINGUSDT','WINUSDT','WNXMUSDT','WOOUSDT','WRXUSDT',
  95.     'WTCUSDT','XECUSDT','XEMUSDT','XLMUSDT','XMRUSDT',
  96.     'XNOUSDT','XRPUSDT','XTZUSDT','XVGUSDT','XVSUSDT',
  97.     'YFIIUSDT','YFIUSDT','YGGUSDT','ZECUSDT','ZENUSDT',
  98.     'ZILUSDT','ZRXUSDT'
  99.     ]
  100.  
  101.  
  102.  
  103. filtered_pairs1 = []
  104. filtered_pairs2 = []
  105. filtered_pairs3 = []
  106. selected_pair = []
  107. selected_pairSINE = []
  108.  
  109. def filter1(pair):
  110.     interval = '2h'
  111.     symbol = pair
  112.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  113.     open_time = [int(entry[0]) for entry in klines]
  114.     close = [float(entry[4]) for entry in klines]
  115.     close_array = np.asarray(close)
  116.  
  117.     print("on 2h timeframe " + symbol)
  118.     print()
  119.    
  120.    
  121.     x = close
  122.     y = range(len(x))
  123.  
  124.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  125.     best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  126.     best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  127.  
  128.     if x[-1] < best_fit_line3[-1] and best_fit_line1[0] <= best_fit_line1[-1]:
  129.         filtered_pairs1.append(symbol)
  130.         print('found')
  131.  
  132.         #plt.figure(figsize=(8,6))
  133.         #plt.grid(True)
  134.         #plt.plot(x)
  135.         #plt.plot(best_fit_line1, '--', color='r')
  136.         #plt.plot(best_fit_line2, '--', color='r')
  137.         #plt.plot(best_fit_line3, '--', color='r')
  138.         #plt.show(block=False)
  139.         #plt.pause(6)
  140.         #plt.close()
  141.  
  142.     elif x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  143.         filtered_pairs1.append(symbol)
  144.         print('found')
  145.  
  146.         #plt.figure(figsize=(8,6))
  147.         #plt.grid(True)
  148.         #plt.plot(x)
  149.         #plt.plot(best_fit_line1, '--', color='r')
  150.         #plt.plot(best_fit_line2, '--', color='r')
  151.         #plt.plot(best_fit_line3, '--', color='r')
  152.         #plt.show(block=False)
  153.         #plt.pause(6)
  154.         #plt.close()
  155.  
  156.     else:
  157.         print('searching')
  158.  
  159. def filter2(filtered_pairs1):
  160.     interval = '1h'
  161.     symbol = filtered_pairs1
  162.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  163.     open_time = [int(entry[0]) for entry in klines]
  164.     close = [float(entry[4]) for entry in klines]
  165.     close_array = np.asarray(close)
  166.  
  167.     print("on 1h timeframe " + symbol)
  168.     print()
  169.    
  170.     x = close
  171.     y = range(len(x))
  172.  
  173.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  174.     best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  175.     best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  176.  
  177.     if x[-1] < best_fit_line3[-1] and best_fit_line1[0] < best_fit_line1[-1]:
  178.         filtered_pairs2.append(symbol)
  179.         print('found')
  180.  
  181.         #plt.figure(figsize=(8,6))
  182.         #plt.grid(True)
  183.         #plt.plot(x)
  184.         #plt.plot(best_fit_line1, '--', color='r')
  185.         #plt.plot(best_fit_line2, '--', color='r')
  186.         #plt.plot(best_fit_line3, '--', color='r')
  187.         #plt.show(block=False)
  188.         #plt.pause(6)
  189.         #plt.close()
  190.  
  191.     if x[-1] < best_fit_line3[-1] and best_fit_line1[0] >= best_fit_line1[-1]:
  192.         filtered_pairs2.append(symbol)
  193.         print('found')
  194.  
  195.         #plt.figure(figsize=(8,6))
  196.         #plt.grid(True)
  197.         #plt.plot(x)
  198.         #plt.plot(best_fit_line1, '--', color='r')
  199.         #plt.plot(best_fit_line2, '--', color='r')
  200.         #plt.plot(best_fit_line3, '--', color='r')
  201.         #plt.show(block=False)
  202.         #plt.pause(6)
  203.         #plt.close()
  204.  
  205. def filter3(filtered_pairs2):
  206.     interval = '15m'
  207.     symbol = filtered_pairs2
  208.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  209.     open_time = [int(entry[0]) for entry in klines]
  210.     close = [float(entry[4]) for entry in klines]
  211.     close_array = np.asarray(close)
  212.  
  213.     print("on 15m timeframe " + symbol)
  214.  
  215.     #min = ta.MIN(close_array, timeperiod=30)
  216.     #max = ta.MAX(close_array, timeperiod=30)
  217.  
  218.     #real = ta.HT_TRENDLINE(close_array)
  219.     #wcl = ta.WCLPRICE(max, min, close_array)
  220.    
  221.     #print(close[-1])
  222.     print()
  223.     #print(min[-1])
  224.     #print(max[-1])
  225.     #print(real[-1])    
  226.  
  227.     x = close
  228.     y = range(len(x))
  229.  
  230.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  231.     best_fit_line2 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 1.01
  232.     best_fit_line3 = (np.poly1d(np.polyfit(y, x, 1))(y)) * 0.99
  233.  
  234.     if x[-1] < best_fit_line1[-1]:
  235.         filtered_pairs3.append(symbol)
  236.         print('found')
  237.  
  238.         #plt.figure(figsize=(8,6))
  239.         #plt.title(symbol)
  240.         #plt.grid(True)
  241.         #plt.plot(close)
  242.         #plt.plot(best_fit_line1, '--', color='r')
  243.         #plt.plot(best_fit_line2, '--', color='r')
  244.         #plt.plot(best_fit_line3, '--', color='r')
  245.         #plt.plot(close)
  246.         #plt.plot(min)
  247.         #plt.plot(max)
  248.         #plt.plot(real)
  249.         #plt.show(block=False)
  250.         #plt.pause(5)
  251.         #plt.close()
  252.  
  253.     else:
  254.         print('searching')
  255.  
  256.  
  257. def momentum(filtered_pairs3):
  258.     interval = '5m'
  259.     symbol = filtered_pairs3
  260.     klines = trader.client.get_klines(symbol=symbol,interval=interval)
  261.     open_time = [int(entry[0]) for entry in klines]
  262.     close = [float(entry[4]) for entry in klines]
  263.     close_array = np.asarray(close)
  264.  
  265.     print("on 5m timeframe " + symbol)
  266.  
  267.     #real = ta.CMO(close_array, timeperiod=14)
  268.     #print(real[-1])
  269.  
  270.     sine, leadsine = ta.HT_SINE(close_array)
  271.    
  272.     if sine[-1] <= -0.5:
  273.         print("found dip on time series sinewave")
  274.         selected_pair.append(symbol)
  275.         selected_pairSINE.append(sine[-1])
  276.         print(sine[-1])
  277.        
  278.         real = ta.CMO(close_array, timeperiod=14)
  279.         print(real[-1])
  280.    
  281.         if real[-1] < -50:
  282.             print('oversold dip found')
  283.             selected_pair.append(symbol)
  284.             selected_pairCMO.append(real[-1])
  285.     else:
  286.         print('searching')
  287.  
  288.  
  289. for i in trading_pairs:
  290.     output = filter1(i)
  291.     print(filtered_pairs1)
  292.  
  293. for i in filtered_pairs1:
  294.     output = filter2(i)
  295.     print(filtered_pairs2)
  296.  
  297. for i in filtered_pairs2:
  298.     output = filter3(i)
  299.     print(filtered_pairs3)
  300.  
  301. for i in filtered_pairs3:
  302.     output = momentum(i)
  303.     print(selected_pair)
  304.  
  305. if len(selected_pair) > 1:
  306.     print('dips are more then 1 on time series')
  307.     print(selected_pair)
  308.    
  309.     if min(selected_pair) in selected_pairSINE:
  310.         #print(selected_pairSINE.index(min(selected_pairSINE)))
  311.         position = selected_pairSINE.index(min(selected_pairSINE))
  312.  
  313.         for id, value in enumerate(selected_pair):
  314.             if id == position:
  315.                 print(selected_pair[id])
  316.         #print(selected_pair[id])
  317.     #sys.exit()
  318.  
  319.  
  320. elif len(selected_pair) == 1:
  321.     print('1 dip found')  
  322.     print(selected_pair)
  323.  
  324.  
  325.     #sys.exit()
  326.  
  327. else:
  328.     print('no more dips for the moment, restart script...')
  329.     print(selected_pair)
  330.     #print(selected_pairCMO)
  331.     #os.execl(sys.executable, sys.executable, *sys.argv)
  332.    
  333.  
  334.  
  335. sys.exit(0)
  336. sys.exit()
  337. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement