I used your code:import time import requests import hmac import hashlib import datetime…
- User
I used your code:import time import requests import hmac import hashlib import datetime as dt import pandas as pd API_KEY = '' API_SECRET = ''
print({dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')})
symbol = 'BCH/USDT' 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}?{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://www.mexc.com/open/api/v2/market/kline' params = { 'symbol': symbol, 'interval': interval, 'start_time': start_time, 'end_time': end_time } signature = generate_signature(API_KEY, API_SECRET, '/open/api/v2/market/kline', params) params['api_key'] = API_KEY params['sign'] = signature response = requests.get(url, params=params) if response.status_code == 200: data = response.json()['data'] df = pd.DataFrame(data, columns=['time', 'open', 'high', 'low', 'close', 'volume']) df['time'] = pd.to_datetime(df['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)
def signal_generator(df): # Calculate EMA and MA lines df['EMA5'] = df['close'].ewm(span=5, adjust=False).mean() df['EMA10'] = df['close'].ewm(span=10, adjust=False).mean() df['EMA20'] = df['close'].ewm(span=20, adjust=False).mean() df['EMA50'] = df['close'].ewm(span=50, adjust=False).mean() df['EMA100'] = df['close'].ewm(span=100, adjust=False).mean() df['EMA200'] = df['close'].ewm(span=200, adjust=False).mean() df['MA10'] = df['close'].rolling(window=10).mean() df['MA20'] = df['close'].rolling(window=20).mean() df['MA50'] = df['close'].rolling(window=50).mean() df['MA100'] = df['close'].rolling(window=100).mean() # Extract necessary prices from df open_price = df.open.iloc[-1] close_price = df.close.iloc[-1] previous_open = df.open.iloc[-2] previous_close = df.close.iloc[-2]
code - Assistant
- User
What did you changed ?
- Assistant
I made the following changes to the code:
- Added a condition in the
signal_generator()function to return an empty string if thedfparameter is None. This prevents the error from occurring when trying to access the DataFrame elements when it is not available.
pythondef signal_generator(df): if df is None: return '' ...- Modified the while loop to call the
get_historical_data()function before calling thesignal_generator()function. This ensures that you have the latest data in the DataFrame before generating the signal.
pythonwhile True: end_time = int(time.time() * 1000) start_time = end_time - lookback * 60 * 1000 df = get_historical_data(symbol, interval, start_time, end_time) signal = signal_generator(df) ...By implementing these changes, the code should now handle the case when the
get_historical_data()function returnsNoneand prevent the you were encountering. - Added a condition in the
- User
Will it solve my ERROR ?
- Assistant
Yes, the modifications I suggested should resolve the
TypeErroryou were encountering. By checking ifdfisNonein thesignal_generator()function and returning an empty string in that case, the code will be able to handle the situation where theget_historical_data()function returnsNone. This prevents the error when trying to access elements ofdfwhen it doesn't exist.Additionally, by calling
get_historical_data()beforesignal_generator()in the while loop, the code ensures that you have the latest data in the DataFrame before generating the signal. This helps avoid working with outdated or missing data.Please give it a try and let me know if you encounter any further issues or error messages.