Advertisement
xmd79

mtf dip finder

Nov 7th, 2024
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.37 KB | None | 0 0
  1. from binance.client import Client
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import talib as ta
  5. import os
  6. import sys
  7.  
  8. class Trader:
  9.     def __init__(self, file):
  10.         self.connect(file)
  11.  
  12.     """ Creates Binance client """
  13.     def connect(self, file):
  14.         lines = [line.rstrip('\n') for line in open(file)]
  15.         key = lines[0]
  16.         secret = lines[1]
  17.         self.client = Client(key, secret)
  18.  
  19.     """ Gets all account balances """
  20.     def getBalances(self):
  21.         prices = self.client.get_withdraw_history()
  22.         return prices
  23.  
  24.     """ Get all pairs traded against USDC """
  25.     def get_usdc_pairs(self):
  26.         exchange_info = self.client.get_exchange_info()
  27.         trading_pairs = [symbol['symbol'] for symbol in exchange_info['symbols'] if symbol['quoteAsset'] == 'USDC' and symbol['status'] == 'TRADING']
  28.         return trading_pairs
  29.  
  30. filename = 'credentials.txt'
  31. trader = Trader(filename)
  32.  
  33. filtered_pairs1 = []
  34. filtered_pairs2 = []
  35. filtered_pairs3 = []
  36. selected_pair = []
  37. selected_pairCMO = []
  38.  
  39. # Dynamically fetch trading pairs against USDC
  40. trading_pairs = trader.get_usdc_pairs()
  41.  
  42. def filter1(pair):
  43.     interval = '2h'
  44.     symbol = pair
  45.     klines = trader.client.get_klines(symbol=symbol, interval=interval)
  46.     close = [float(entry[4]) for entry in klines]
  47.  
  48.     if not close:
  49.         return  # Skip if no close price available
  50.  
  51.     print("on 2h timeframe " + symbol)
  52.  
  53.     x = close
  54.     y = range(len(x))
  55.  
  56.     best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  57.     best_fit_line2 = best_fit_line1 * 1.01
  58.     best_fit_line3 = best_fit_line1 * 0.99
  59.  
  60.     if x[-1] < best_fit_line3[-1]:
  61.         filtered_pairs1.append(symbol)
  62.         print('found')
  63.  
  64. def filter2(filtered_pairs1):
  65.     interval = '15m'
  66.     for symbol in filtered_pairs1:  # Loop through each symbol in the filtered pair list
  67.         klines = trader.client.get_klines(symbol=symbol, interval=interval)
  68.         close = [float(entry[4]) for entry in klines]
  69.  
  70.         if not close:
  71.             continue  # Skip if no close price available
  72.  
  73.         print("on 15min timeframe " + symbol)
  74.  
  75.         x = close
  76.         y = range(len(x))
  77.  
  78.         best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  79.         best_fit_line2 = best_fit_line1 * 1.01
  80.         best_fit_line3 = best_fit_line1 * 0.99
  81.  
  82.         if x[-1] < best_fit_line3[-1]:
  83.             filtered_pairs2.append(symbol)
  84.             print('found')
  85.  
  86. def filter3(filtered_pairs2):
  87.     interval = '5m'
  88.     for symbol in filtered_pairs2:  # Loop through each symbol in the filtered pair list
  89.         klines = trader.client.get_klines(symbol=symbol, interval=interval)
  90.         close = [float(entry[4]) for entry in klines]
  91.  
  92.         if not close:
  93.             continue  # Skip if no close price available
  94.  
  95.         print("on 5m timeframe " + symbol)
  96.        
  97.         x = close
  98.         y = range(len(x))
  99.  
  100.         best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
  101.         best_fit_line2 = best_fit_line1 * 1.01
  102.         best_fit_line3 = best_fit_line1 * 0.99
  103.  
  104.         if x[-1] < best_fit_line3[-1]:
  105.             filtered_pairs3.append(symbol)
  106.             print('found')
  107.  
  108. def momentum(filtered_pairs3):
  109.     interval = '1m'
  110.     for symbol in filtered_pairs3:  # Loop through each symbol in the filtered pair list
  111.         klines = trader.client.get_klines(symbol=symbol, interval=interval)
  112.         close = [float(entry[4]) for entry in klines]
  113.  
  114.         if not close:
  115.             continue  # Skip if no close price available
  116.  
  117.         print("on 1m timeframe " + symbol)
  118.  
  119.         close_array = np.asarray(close)
  120.  
  121.         real = ta.CMO(close_array, timeperiod=14)
  122.  
  123.         if real[-1] < -50:
  124.             print('mtf dip found')
  125.             selected_pair.append(symbol)
  126.             selected_pairCMO.append(real[-1])
  127.  
  128. # Run filters on trading pairs
  129. for i in trading_pairs:
  130.     filter1(i)
  131.  
  132. filter2(filtered_pairs1)
  133. filter3(filtered_pairs2)
  134. momentum(filtered_pairs3)
  135.  
  136. if len(selected_pair) > 1:
  137.     print('more mtf dips are found')
  138.     print(selected_pair)
  139.  
  140.     if min(selected_pairCMO) in selected_pairCMO:
  141.         position = selected_pairCMO.index(min(selected_pairCMO))
  142.  
  143.         for id, value in enumerate(selected_pair):
  144.             if id == position:
  145.                 print(selected_pair[id])
  146.  
  147. elif len(selected_pair) == 1:
  148.     print('1 mtf dip found')  
  149.     print(selected_pair)
  150.  
  151. sys.exit(0)
  152. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement