Advertisement
xmd79

mtfz12

Mar 14th, 2024
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. from binance.client import Client
  2. import numpy as np
  3. import talib as ta
  4. import asyncio
  5. from binance.exceptions import BinanceAPIException
  6.  
  7. class Trader:
  8. def __init__(self, file):
  9. self.connect(file)
  10.  
  11. def connect(self, file):
  12. with open(file) as f:
  13. lines = f.readlines()
  14. key = lines[0].strip()
  15. secret = lines[1].strip()
  16. self.client = Client(key, secret)
  17.  
  18. def get_latest_price(self, symbol):
  19. ticker = self.client.get_symbol_ticker(symbol=symbol)
  20. return float(ticker['price'])
  21.  
  22. trader = Trader('credentials.txt')
  23.  
  24. filtered_pairs1 = []
  25. dips = []
  26. tops = []
  27.  
  28. async def filter_trading_pairs():
  29. trading_pairs = [symbol['symbol'] for symbol in trader.client.futures_exchange_info()['symbols'] if 'USDT' in symbol['symbol']]
  30. print("Trading pairs:", trading_pairs) # Print out trading pairs for debugging
  31. for pair in trading_pairs:
  32. await filter1(pair)
  33. return True
  34.  
  35. async def filter1(pair):
  36. interval = '4h'
  37. try:
  38. klines = trader.client.get_klines(symbol=pair, interval=interval)
  39. close = np.array([float(entry[4]) for entry in klines])
  40.  
  41. x = np.arange(len(close))
  42.  
  43. # Calculate polynomial fit
  44. poly_coefficients = np.polyfit(x, close, 1)
  45. poly_values = np.polyval(poly_coefficients, x)
  46.  
  47. # Find minimum and maximum of polynomial fit
  48. min_poly_value = np.min(poly_values)
  49. max_poly_value = np.max(poly_values)
  50.  
  51. # Find corresponding price values for min and max poly values
  52. min_poly_price = np.interp(min_poly_value, poly_values, close)
  53. max_poly_price = np.interp(max_poly_value, poly_values, close)
  54.  
  55. # Calculate current polynomial value for current price
  56. current_price = close[-1]
  57. current_poly_value = np.polyval(poly_coefficients, len(close))
  58.  
  59. # Calculate percentage deviation from min and max poly values
  60. dip_percentage = ((min_poly_value - current_price) / min_poly_value) * 100
  61. top_percentage = ((current_price - max_poly_value) / max_poly_value) * 100
  62.  
  63. # Calculate forecasted price for the next period
  64. forecast_period = 1 # Next period
  65. forecast_price = np.polyval(poly_coefficients, len(close) + forecast_period)
  66.  
  67. if close[-1] < min_poly_value:
  68. filtered_pairs1.append((pair, 'Dip', min_poly_price, min_poly_value, max_poly_price, max_poly_value, current_poly_value, forecast_price))
  69. print(f'Found dip on 4h timeframe for {pair}')
  70. print(f'Dip Percentage: {dip_percentage}%')
  71. print(f'Min Poly Value: {min_poly_value}, Corresponding Price: {min_poly_price}')
  72. print(f'Max Poly Value: {max_poly_value}, Corresponding Price: {max_poly_price}')
  73. print(f'Current Poly Value: {current_poly_value}, Current Price: {current_price}')
  74. print(f'Forecasted Price: {forecast_price}')
  75. dips.append((pair, dip_percentage, current_poly_value))
  76. elif close[-1] > max_poly_value:
  77. filtered_pairs1.append((pair, 'Top', min_poly_price, min_poly_value, max_poly_price, max_poly_value, current_poly_value, forecast_price))
  78. print(f'Found top on 4h timeframe for {pair}')
  79. print(f'Top Percentage: {top_percentage}%')
  80. print(f'Min Poly Value: {min_poly_value}, Corresponding Price: {min_poly_price}')
  81. print(f'Max Poly Value: {max_poly_value}, Corresponding Price: {max_poly_price}')
  82. print(f'Current Poly Value: {current_poly_value}, Current Price: {current_price}')
  83. print(f'Forecasted Price: {forecast_price}')
  84. tops.append((pair, top_percentage, current_poly_value))
  85. else:
  86. print(f'Searching for {pair}')
  87. except BinanceAPIException as e:
  88. if e.code == -1121:
  89. pass # Ignore invalid symbol error
  90. else:
  91. print(f"Error processing {pair}: {e}")
  92.  
  93. async def filter_1h_dips_tops():
  94. for pair, _, min_poly_value in sorted(dips, key=lambda x: x[1], reverse=True):
  95. await filter_tf(pair, min_poly_value, 'Dip', '1h')
  96. for pair, _, max_poly_value in sorted(tops, key=lambda x: x[1], reverse=True):
  97. await filter_tf(pair, max_poly_value, 'Top', '1h')
  98.  
  99. async def filter_15min_dips_tops():
  100. for pair, _, min_poly_value in sorted(dips, key=lambda x: x[1], reverse=True):
  101. await filter_tf(pair, min_poly_value, 'Dip', '15m')
  102. for pair, _, max_poly_value in sorted(tops, key=lambda x: x[1], reverse=True):
  103. await filter_tf(pair, max_poly_value, 'Top', '15m')
  104.  
  105. async def filter_5min_dips_tops():
  106. for pair, _, min_poly_value in sorted(dips, key=lambda x: x[1], reverse=True):
  107. await filter_tf(pair, min_poly_value, 'Dip', '5m')
  108. for pair, _, max_poly_value in sorted(tops, key=lambda x: x[1], reverse=True):
  109. await filter_tf(pair, max_poly_value, 'Top', '5m')
  110.  
  111. async def filter_tf(pair, poly_value, side, interval):
  112. try:
  113. klines = trader.client.get_klines(symbol=pair, interval=interval)
  114. close = np.array([float(entry[4]) for entry in klines])
  115.  
  116. x = np.arange(len(close))
  117.  
  118. # Calculate polynomial fit
  119. poly_coefficients = np.polyfit(x, close, 1)
  120. poly_values = np.polyval(poly_coefficients, x)
  121.  
  122. if side == 'Dip' and close[-1] < poly_value:
  123. print(f'Found dip on {interval} timeframe for {pair}')
  124. elif side == 'Top' and close[-1] > poly_value:
  125. print(f'Found top on {interval} timeframe for {pair}')
  126. else:
  127. print(f'Searching for {pair} on {interval} timeframe')
  128. except BinanceAPIException as e:
  129. if e.code == -1121:
  130. pass # Ignore invalid symbol error
  131. else:
  132. print(f"Error processing {pair} on {interval} timeframe: {e}")
  133.  
  134. async def main():
  135. await filter_trading_pairs()
  136. print(f"Number of Dips Found: {len(dips)}")
  137. print(f"Number of Tops Found: {len(tops)}")
  138.  
  139. sorted_dips = sorted(dips, key=lambda x: x[1])
  140. sorted_tops = sorted(tops, key=lambda x: x[1], reverse=True)
  141.  
  142. most_significant_dip = sorted_dips[0]
  143. most_significant_top = sorted_tops[0]
  144.  
  145. print(f"Related Poly Value for Most Significant Dip Asset: {most_significant_dip[2]}")
  146. print(f"Most Significant Dip Asset: {most_significant_dip[0]}, Dip Percentage: {most_significant_dip[1]}%")
  147. print(f"Related Poly Value for Most Significant Top Asset: {most_significant_top[2]}")
  148. print(f"Most Significant Top Asset: {most_significant_top[0]}, Top Percentage: {most_significant_top[1]}%")
  149.  
  150. await filter_1h_dips_tops()
  151. await filter_15min_dips_tops()
  152. await filter_5min_dips_tops()
  153.  
  154. asyncio.run(main())
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement