Which Logic is used to identify zones: import requests import pandas as pd import numpy…
- User
Which Logic is used to identify zones: import requests import pandas as pd import numpy as np import time import mplfinance as mpf # For plotting import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from matplotlib.dates import date2num import matplotlib.dates as mdates
List of cryptocurrency pairs and timeframes
symbols = [ 'BTCUSDT', 'ETHUSDT', 'XRPUSDT', 'DOGEUSDT', 'SOLUSDT', 'BNBUSDT', 'SUIUSDT', 'CKBUSDT', 'FTMUSDT', 'RUNEUSDT', 'UNIUSDT', 'AAVEUSDT', 'BCHUSDT', 'KEYUSDT', '1INCHUSDT' ]
timeframes = ['30m', '1h', '2h', '4h']
interval_mapping = { '30m': '30m', '1h': '1h', '2h': '2h', '4h': '4h' }
def get_klines(symbol, interval, limit=150): """ Fetches historical kline data from Binance API. """ base_url = 'https://api.binance.com' endpoint = '/api/v3/klines' params = { 'symbol': symbol, 'interval': interval, 'limit': limit } response = requests.get(base_url + endpoint, params=params) data = response.json() if response.status_code != 200: print(f"Error fetching data for {symbol} at {interval}: {data}") return None df = pd.DataFrame(data, columns=[ 'Open Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close Time', 'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume', 'Ignore' ]) # Convert timestamp to datetime df['Date'] = pd.to_datetime(df['Open Time'], unit='ms') df.set_index('Date', inplace=True) # Convert numeric columns to float numeric_cols = ['Open', 'High', 'Low', 'Close', 'Volume', 'Quote Asset Volume', 'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume'] df[numeric_cols] = df[numeric_cols].astype(float) return df
def find_zones(df, N=5): """ Identifies potential demand and supply zones by finding swing lows and highs. """ # Identify swing highs df['Swing_High'] = df['High'][ (df['High'] == df['High'].rolling(window=2N+1, center=True).max()) & (df['High'].shift(1) != df['High']) # Exclude flat tops ] # Identify swing lows df['Swing_Low'] = df['Low'][ (df['Low'] == df['Low'].rolling(window=2N+1, center=True).min()) & (df['Low'].shift(1) != df['Low']) # Exclude flat bottoms ]
code# Clean up NaN values in swings swing_highs = df.dropna(subset=['Swing_High'])[['Swing_High']] swing_lows = df.dropna(subset=['Swing_Low'])[['Swing_Low']] return swing_highs, swing_lowsdef filter_fresh_zones(swing_highs, swing_lows, df): """ Filters swing highs and swing lows to keep only fresh (unviolated) zones. """ fresh_swing_highs = [] fresh_swing_lows = []
code - Assistant