xmd79

custom mtf algo dip finder with time series momentum

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