· 2 years ago · Mar 28, 2023, 11:30 AM
1import numpy as np
2from binance.client import Client
3from binance.enums import *
4import pandas as pd
5
6# Load API key and secret from credentials.txt
7with open('credentials.txt', 'r') as f:
8 lines = f.readlines()
9 api_key = lines[0].strip()
10 api_secret = lines[1].strip()
11
12# Connect to Binance Futures API
13client = Client(api_key, api_secret, testnet=False)
14
15# Retrieve price data for USDTM on all timeframes
16symbols = ['BTCUSDT']
17timeframes = ['1d', '4h', '1h', '15m', '5m']
18prices = {}
19for symbol in symbols:
20 for timeframe in timeframes:
21 klines = client.futures_klines(symbol=symbol, interval=timeframe, limit=1000)
22 df = pd.DataFrame(klines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_volume', 'trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'ignore'])
23 df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
24 df.set_index('timestamp', inplace=True)
25 prices[(symbol, timeframe)] = df
26
27# Perform polynomial fit analysis for all timeframes
28for symbol in symbols:
29 for timeframe in timeframes:
30 df = prices[(symbol, timeframe)]
31 x = df['close'].astype(float).values
32 y = np.arange(len(x)).astype(float)
33 best_fit_line1 = np.poly1d(np.polyfit(y, x, 1))(y)
34 best_fit_line2 = np.poly1d(np.polyfit(y, x, 1))(y) * 1.01
35 best_fit_line3 = np.poly1d(np.polyfit(y, x, 1))(y) * 0.99
36 # Determine if the close price is below the lowest line
37 if df['close'].astype(float)[-1] < min(best_fit_line1[-1], best_fit_line2[-1], best_fit_line3[-1]):
38 print(f"{symbol} {timeframe}: DIP")
39 else:
40 print(f"{symbol} {timeframe}: TOP")
41