You didn't finish your message : Give me full updated code please Certainly! Here’s the…
- User
You didn't finish your message : Give me full updated code please Certainly! Here’s the full updated code with the modifications to accommodate a lookback of 44640 minutes:
import time import requests import hmac import hashlib import datetime as dt import pandas as pd
API_KEY = ‘Your API Key’ API_SECRET = ‘Your API Secret’
symbol = ‘BCHUSDT’ interval = ‘1m’ lookback = 44640
def generate_signature(api_key, api_secret, endpoint, params): params_string = ‘&’.join([f’{key}={params[key]}’ for key in params]) payload = f’{endpoint}\n{params_string}' signature = hmac.new(api
- Assistant
Sorry, but I can't provide the full updated code as it is incomplete. Please provide the missing code or specify the modifications you would like to make so that I can help you further.
- User
import time import requests import hmac import hashlib import datetime as dt import pandas as pd
API_KEY = ‘Your API Key’ API_SECRET = ‘Your API Secret’
symbol = ‘BCHUSDT’ interval = ‘1m’ lookback = 44640
def generate_signature(api_key, api_secret, endpoint, params): params_string = ‘&’.join([f’{key}={params[key]}’ for key in params]) payload = f’{endpoint}\n{params_string}' signature = hmac.new(api
- Assistant
Apologies, but there seems to be an indentation issue in the code you provided. Here's the corrected and updated code:
import time import requests import hmac import hashlib import datetime as dt import pandas as pd API_KEY = 'Your API Key' API_SECRET = 'Your API Secret' symbol = 'BCHUSDT' interval = '1m' lookback = 44640 def generate_signature(api_key, api_secret, endpoint, params): params_string = '&'.join([f'{key}={params[key]}' for key in params]) payload = f'{endpoint}\n{params_string}' signature = hmac.new(api_secret.encode('utf-8'), payload.encode('utf-8'), hashlib.sha256).hexdigest() return signature def get_historical_data(symbol, interval, start_time, end_time): url = 'https://api.binance.com/api/v3/klines' params = { 'symbol': symbol, 'interval': interval, 'startTime': start_time, 'endTime': end_time } headers = { 'X-MBX-APIKEY': API_KEY } signature = generate_signature(API_KEY, API_SECRET, '/api/v3/klines', params) params['signature'] = signature response = requests.get(url, params=params, headers=headers) if response.status_code == 200: data = response.json() 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']) df['Open time'] = pd.to_datetime(df['Open time'], unit='ms') df['Close time'] = pd.to_datetime(df['Close time'], unit='ms') df['Volume'] = df['Volume'].astype(float) return df else: return None end_time = int(time.time() * 1000) start_time = end_time - lookback * 60 * 1000 df = get_historical_data(symbol, interval, start_time, end_time) print(df) - User
How I can set lookback ?
- Assistant
To set the lookback period, you can change the value of the
lookbackvariable in the code. Thelookbackvariable represents the number of minutes you want to go back in time to fetch historical data.For example, if you want to set the lookback period to 1 day (24 hours), you can change the
lookbackvariable to1440:lookback = 1440Similarly, if you want to set the lookback period to 1 week (7 days), you can change the
lookbackvariable to10080:lookback = 10080Feel free to adjust the
lookbackvariable according to your desired time period. - User
- Assistant
- User
Wait! What did you change ?
- Assistant
Apologies for the confusion. In the code that you have provided, I made the following changes:
- Moved the definition of the
signal_generatorfunction before the main loop that calls it. - Added a
while Trueloop to continuously fetch historical data and generate signals. - Moved the calculation of
end_timeandstart_timeinside the main loop to fetch the latest data for each iteration. - Updated the condition to check if the returned
dffromget_historical_datais notNonebefore proceeding with signal generation. - Added a delay of 5 seconds using
time.sleep(5)between each API request to avoid making too many requests in a short period. - Removed the duplicate line
df = get_historical_data(symbol, interval, start_time, end_time).
With these changes, the code will continuously fetch historical data, generate signals based on the strategy, and print the signal with the current timestamp.
Please note that you still need to replace the empty strings
API_KEYandAPI_SECRETwith your actual Binance API key and secret for it to work properly. - Moved the definition of the
- User
I used your code , but for whcih one exchange it does working binance or MEXC
- Assistant
The code provided is specifically written for the Binance exchange. The API endpoints and request structure are designed for the Binance API.
If you want to use it with the MEXC exchange, you will need to make some modifications to the code