Hello,
I have developed a python script that does the following tasks:
- Gets the list of all USDT quoted pairs in binance futures using api endpoint: /fapi/v1/exchangeInfo
The function I am using for it is:
def get_symbol_names():
symbols_list = []
base_url = 'https://fapi.binance.com'
endpoint = '/fapi/v1/exchangeInfo'
params = {}
try:
response = requests.get(base_url + endpoint, params=params)
response.raise_for_status()
response_data = response.json()
for symbol in response_data["symbols"]:
if (symbol["quoteAsset"] == "USDT"):
symbols_list.append(symbol["symbol"])
return symbols_list
except requests.exceptions.ConnectionError:
print("Network error: Unable to connect to the internet or the API server.")
except requests.exceptions.HTTPError as http_err:
print(f"Server error: {http_err}")
except requests.exceptions.RequestException as err:
print(f"An unexpected error occurred: {err}")
return None # Return None if there was an error
- For each pair in the list, using a for loop, it then retrieved last 50 klines using api endpoint: /fapi/v1/klines
The function I am using for it is:
def get_kline_data(symbol, interval='4h', limit=50):
"""
Fetches kline (candlestick) data from the Binance API.
Parameters:
symbol (str): The symbol pair to fetch data for, e.g., 'SUIUSDT'.
interval (str): The time interval between klines, e.g., '4h'.
limit (int): The maximum number of data points to retrieve.
Returns:
list: JSON response containing the kline data if successful, otherwise None.
"""
base_url = 'https://fapi.binance.com'
endpoint = '/fapi/v1/klines'
params = {
'symbol': symbol,
'interval': interval,
'limit': limit
}
try:
response = requests.get(base_url + endpoint, params=params)
response.raise_for_status()
return response.json()
except requests.exceptions.ConnectionError:
print("Network error: Unable to connect to the internet or the API server.")
except requests.exceptions.HTTPError as http_err:
print(f"Server error: {http_err}")
except requests.exceptions.RequestException as err:
print(f"An unexpected error occurred: {err}")
return None # Return None if there was an error
- Perform analysis locally on the retrieved data, without connecting to the binance api for anything else.
The code for whole thing is:
import time
from get_data_from_api import get_symbol_names
from get_data_from_api import get_kline_data
from create_chart import create_chart
from analyze_kline_data import identify_areas_of_interest
from db_manager import check_areas_of_interest_table_in_db
from db_manager import clear_recent_breaches_table_in_db
from db_manager import update_areas_of_interest_in_db
from db_manager import update_recent_breaches_table
from analyze_aois_in_db import analyze_old_aois_in_db
from analyze_aois_in_db import filter_recent_breaches
# Call the function to get data
clear_recent_breaches_table_in_db()
#this function clears the old data in recent breaches table
#clearing old data is important because every time new analysis is run, the old data becomes reduntant
check_areas_of_interest_table_in_db()
symbols = get_symbol_names()
time.sleep(2)
if symbols:
for symbol in symbols:
data = get_kline_data(symbol,'4h',50)
if data:
areas_of_interest = identify_areas_of_interest(data)
combined_aois = analyze_old_aois_in_db(symbol,data,areas_of_interest) # this analyzes the old aois in db to check if they are breached during down-time and adds the ones that are breached with all (both breached and open) fresh ones so that db can be updated properly
recent_breaches = filter_recent_breaches(data,combined_aois)
if recent_breaches:
update_recent_breaches_table(symbol,recent_breaches)
print(f"Recent breaches updated in DB for {symbol}")
create_chart(data, areas_of_interest,symbol)
update_areas_of_interest_in_db(symbol,areas_of_interest)
print(f"Analysis completed, chart created and DB updated for {symbol}")
time.sleep(1)
print("Operation Complete")
As per my initial tests, the processing of retrieving data for all pairs takes around 5 minutes to complete. As per the usage weight listed in the api documentation, the endpoint ‘/fapi/v1/klines’ with a limit of 0-100 has a weight of 1. This means that my total usage weight over 5 minutes is 300-400
However, I got a 403 error when I ran this script today. (I have been running / testing it for past 3-4 days without any issues).
The information I am getting using this script forms a major part of my trading strategy and I plan on using it long term. What am I doing wrong here that got me the 403 error. I have tried going through the documentation to find the reason but it got me no where. I am open to sharing the complete code if needed.
Thanks