Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from binance.client import Client
- import numpy as np
- import talib as ta
- import asyncio
- from binance.exceptions import BinanceAPIException
- class Trader:
- def __init__(self, file):
- self.connect(file)
- def connect(self, file):
- with open(file) as f:
- lines = f.readlines()
- key = lines[0].strip()
- secret = lines[1].strip()
- self.client = Client(key, secret)
- def get_latest_price(self, symbol):
- ticker = self.client.get_symbol_ticker(symbol=symbol)
- return float(ticker['price'])
- trader = Trader('credentials.txt')
- filtered_pairs1 = []
- dips = []
- tops = []
- async def filter_trading_pairs():
- trading_pairs = [symbol['symbol'] for symbol in trader.client.futures_exchange_info()['symbols'] if 'USDT' in symbol['symbol']]
- print("Trading pairs:", trading_pairs) # Print out trading pairs for debugging
- for pair in trading_pairs:
- await filter1(pair)
- return True
- async def filter1(pair):
- interval = '4h'
- try:
- klines = trader.client.get_klines(symbol=pair, interval=interval)
- close = np.array([float(entry[4]) for entry in klines])
- x = np.arange(len(close))
- # Calculate polynomial fit
- poly_coefficients = np.polyfit(x, close, 1)
- poly_values = np.polyval(poly_coefficients, x)
- # Find minimum and maximum of polynomial fit
- min_poly_value = np.min(poly_values)
- max_poly_value = np.max(poly_values)
- # Find corresponding price values for min and max poly values
- min_poly_price = np.interp(min_poly_value, poly_values, close)
- max_poly_price = np.interp(max_poly_value, poly_values, close)
- # Calculate current polynomial value for current price
- current_price = close[-1]
- current_poly_value = np.polyval(poly_coefficients, len(close))
- # Calculate percentage deviation from min and max poly values
- dip_percentage = ((min_poly_value - current_price) / min_poly_value) * 100
- top_percentage = ((current_price - max_poly_value) / max_poly_value) * 100
- # Calculate forecasted price for the next period
- forecast_period = 1 # Next period
- forecast_price = np.polyval(poly_coefficients, len(close) + forecast_period)
- if close[-1] < min_poly_value:
- filtered_pairs1.append((pair, 'Dip', min_poly_price, min_poly_value, max_poly_price, max_poly_value, current_poly_value, forecast_price))
- print(f'Found dip on 4h timeframe for {pair}')
- print(f'Dip Percentage: {dip_percentage}%')
- print(f'Min Poly Value: {min_poly_value}, Corresponding Price: {min_poly_price}')
- print(f'Max Poly Value: {max_poly_value}, Corresponding Price: {max_poly_price}')
- print(f'Current Poly Value: {current_poly_value}, Current Price: {current_price}')
- print(f'Forecasted Price: {forecast_price}')
- dips.append((pair, dip_percentage, current_poly_value))
- elif close[-1] > max_poly_value:
- filtered_pairs1.append((pair, 'Top', min_poly_price, min_poly_value, max_poly_price, max_poly_value, current_poly_value, forecast_price))
- print(f'Found top on 4h timeframe for {pair}')
- print(f'Top Percentage: {top_percentage}%')
- print(f'Min Poly Value: {min_poly_value}, Corresponding Price: {min_poly_price}')
- print(f'Max Poly Value: {max_poly_value}, Corresponding Price: {max_poly_price}')
- print(f'Current Poly Value: {current_poly_value}, Current Price: {current_price}')
- print(f'Forecasted Price: {forecast_price}')
- tops.append((pair, top_percentage, current_poly_value))
- else:
- print(f'Searching for {pair}')
- except BinanceAPIException as e:
- if e.code == -1121:
- pass # Ignore invalid symbol error
- else:
- print(f"Error processing {pair}: {e}")
- async def filter_1h_dips_tops():
- for pair, _, min_poly_value in sorted(dips, key=lambda x: x[1], reverse=True):
- await filter_tf(pair, min_poly_value, 'Dip', '1h')
- for pair, _, max_poly_value in sorted(tops, key=lambda x: x[1], reverse=True):
- await filter_tf(pair, max_poly_value, 'Top', '1h')
- async def filter_15min_dips_tops():
- for pair, _, min_poly_value in sorted(dips, key=lambda x: x[1], reverse=True):
- await filter_tf(pair, min_poly_value, 'Dip', '15m')
- for pair, _, max_poly_value in sorted(tops, key=lambda x: x[1], reverse=True):
- await filter_tf(pair, max_poly_value, 'Top', '15m')
- async def filter_5min_dips_tops():
- for pair, _, min_poly_value in sorted(dips, key=lambda x: x[1], reverse=True):
- await filter_tf(pair, min_poly_value, 'Dip', '5m')
- for pair, _, max_poly_value in sorted(tops, key=lambda x: x[1], reverse=True):
- await filter_tf(pair, max_poly_value, 'Top', '5m')
- async def filter_tf(pair, poly_value, side, interval):
- try:
- klines = trader.client.get_klines(symbol=pair, interval=interval)
- close = np.array([float(entry[4]) for entry in klines])
- x = np.arange(len(close))
- # Calculate polynomial fit
- poly_coefficients = np.polyfit(x, close, 1)
- poly_values = np.polyval(poly_coefficients, x)
- if side == 'Dip' and close[-1] < poly_value:
- print(f'Found dip on {interval} timeframe for {pair}')
- elif side == 'Top' and close[-1] > poly_value:
- print(f'Found top on {interval} timeframe for {pair}')
- else:
- print(f'Searching for {pair} on {interval} timeframe')
- except BinanceAPIException as e:
- if e.code == -1121:
- pass # Ignore invalid symbol error
- else:
- print(f"Error processing {pair} on {interval} timeframe: {e}")
- async def main():
- await filter_trading_pairs()
- print(f"Number of Dips Found: {len(dips)}")
- print(f"Number of Tops Found: {len(tops)}")
- sorted_dips = sorted(dips, key=lambda x: x[1])
- sorted_tops = sorted(tops, key=lambda x: x[1], reverse=True)
- most_significant_dip = sorted_dips[0]
- most_significant_top = sorted_tops[0]
- print(f"Related Poly Value for Most Significant Dip Asset: {most_significant_dip[2]}")
- print(f"Most Significant Dip Asset: {most_significant_dip[0]}, Dip Percentage: {most_significant_dip[1]}%")
- print(f"Related Poly Value for Most Significant Top Asset: {most_significant_top[2]}")
- print(f"Most Significant Top Asset: {most_significant_top[0]}, Top Percentage: {most_significant_top[1]}%")
- await filter_1h_dips_tops()
- await filter_15min_dips_tops()
- await filter_5min_dips_tops()
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement