I used this code : import time from binance.client import Client from binance.exceptions…
- User
I used this code : import time from binance.client import Client from binance.exceptions import BinanceAPIException from binance.helpers import round_step_size import pandas as pd import json import numpy as np import pytz import datetime as dt import ccxt from decimal import Decimal import requests import hmac import hashlib import ntplib import os import ta import ta.volatility API_KEY = '' API_SECRET = ''
client = Client(API_KEY, API_SECRET)
def sync_time(): server_time = requests.get('https://api.binance.com/api/v3/time').json()['serverTime'] local_time = int(time.time() * 1000) time_difference = server_time - local_time # Set the system clock to the new time os.system(f'sudo date -s @{int(server_time/1000)}') print(f'New time: {dt.datetime.now()}')
Sync your local time with the server time
sync_time()
Set the endpoint and parameters for the request
url = "https://fapi.binance.com/fapi/v1/klines" symbol = 'BCH/USDT' interval = '1m' lookback = 44640 timestamp = int(time.time() * 1000) - 500 # subtract 500ms from local time to account for clock-drift recv_window = 60000 # increased recv_window value params = { "symbol": symbol.replace("/", ""), "interval": interval, "startTime": int((time.time() - lookback * 60) * 1000), "endTime": int(time.time() * 1000), "timestamp": timestamp, "recvWindow": recv_window }
Sign the message using the Client’s secret key
message = "&".join([f"{k}={v}" for k, v in params.items()]) signature = hmac.new(API_SECRET.encode(), message.encode(), hashlib.sha256).hexdigest() params[signature] = signature
Send the request using the requests library
response = requests.get(url, params=params, headers={'X-MBX-APIKEY': API_KEY})
Check for errors in the response
response.raise_for_status() API_KEY_MEXC = '' API_SECRET_MEXC = ''
Initialize MEXC client
mexc_futures = ccxt.mexc({ 'apiKey': API_KEY_MEXC, 'secret': API_SECRET_MEXC, 'enableRateLimit': True, 'options': { 'defaultType': 'future', } }) time.sleep(1) def get_klines(symbol, interval, lookback): url = "https://fapi.binance.com/fapi/v1/klines" end_time = int(time.time() * 1000) # end time is now start_time = end_time - (lookback * 60 * 1000) # start time is lookback minutes ago symbol = symbol.replace("/", "") # remove slash from symbol query_params = f"?symbol={symbol}&interval={interval}&startTime={start_time}&endTime={end_time}" headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' } response = requests.get(url + query_params, headers=headers) response.raise_for_status() data = response.json() if not data: # if data is empty, return None print('No data found for the given timeframe and symbol') return None ohlcv = [] for d in data: timestamp = dt.datetime.fromtimestamp(d[0]/1000).strftime('%Y-%m-%d %H:%M:%S') ohlcv.append({ 'Open time': timestamp, 'Open': float(d[1]), 'High': float(d[2]), 'Low': float(d[3]), 'Close': float(d[4]), 'Volume': float(d[5]) }) df = pd.DataFrame(ohlcv) df.set_index('Open time', inplace=True) return df df = get_klines(symbol, interval, lookback)
- Assistant
The code provided is a Python script that uses the Binance API and the CCXT library to perform technical analysis on cryptocurrency trading pairs. Here's a breakdown of what the code does:
- It imports necessary libraries and modules.
- It sets up the Binance API credentials.
- It defines a function,
sync_time(), to synchronize the local time with the Binance server time. - It calls the
sync_time()function to synchronize the time. - It sets up the endpoint and parameters for the Binance API request to retrieve Kline data.
- It signs the request using the API secret key and sends the request using the
requestslibrary. - It checks for errors in the response.
- It sets up the API credentials for the MEXC exchange using the CCXT library.
- It defines a function,
get_klines(), to retrieve Kline data from the Binance API.
- User
- Assistant
- User
But is it your code will give me signal every second ?
- Assistant
Apologies for the confusion. The code provided will indeed generate trading signals every second as it is currently implemented in a while loop with a sleep duration of 1 second at the end:
while True: df = get_klines(symbol, '1m