I used this signal generator code: # Load the market symbols url =…
- User
I used this signal generator code: # Load the market symbols url = f'https://api.binance.com/api/v3/ticker/price?symbol={symbol}' response = requests.get(url) ticker = response.json() current_price = float(ticker['price'])
def get_server_time(): ntp_client = ntplib.NTPClient() response = ntp_client.request('pool.ntp.org', version=3) now = time.time() offset = response.offset new_time = now + offset # Set the system clock to the new time os.system(f'sudo date -s @{int(new_time)}') print(f'New time: {dt.datetime.now()}')
params = { "timestamp": timestamp, "recvWindow": recv_window } try: markets = binance_futures.load_markets() except ccxt.BaseError as e: print(f'Error fetching markets: {e}') markets = []
if symbol in markets: print(f"{symbol} found in the market") else: print(f"{symbol} not found in the market")
Get server time and time difference
def get_server_time(exchange): return exchange.fetch_time()
def get_time_difference(): server_time = get_server_time(binance_futures) local_time = int(time.time() * 1000) time_difference = local_time - server_time return time_difference
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 ohlc = [] for d in data: timestamp = dt.datetime.fromtimestamp(d[0]/1000).strftime('%Y-%m-%d %H:%M:%S') ohlc.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(ohlc) df.set_index('Open time', inplace=True) return df df = get_klines(symbol, '1m', 44640)
def signal_generator(df): # Calculate EMA and MA lines df['EMA5'] = df['Close'].ewm(span=5, 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['MA10'] = df['Close'].rolling(window=10).mean() df['MA50'] = df['Close'].rolling(window=50).mean() df['MA100'] = df['Close'].rolling(window=100).mean()
# Calculate the last candlestick last_candle = df.iloc[-1] # Check for bullish signals if current_price > last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100']].iloc[-1].max() * 1.01: if last_candle[['EMA5', 'MA10', 'MA50', 'MA100']].iloc[-1].min() > current_price * 0.999: return 'buy' # Check for bearish signals elif current_price < last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100']].iloc[-1].min() * 0.99: if last_candle[['EMA5', 'MA10', 'MA50', 'MA100']].iloc[-1].max() < current_price * 1.001: return 'sell' # If no signal found, return an empty string return '' - Assistant
The error message indicates that the key 'price' is not present in the
tickerdictionary. This could be due to a few reasons:- The
responseobject returned by the API call did not contain the 'price' key. - There was an error in the API call and the
responseobject does not contain any data. - There was an error in the
response.json()call and thetickerdictionary is empty.
To debug this issue, you can print out the contents of the
responseobject and check if it contains the 'price' key. Additionally, you can try printing out thetickerdictionary to see if it is empty. If theresponseobject does not contain the 'price' key, you may need to modify your API call parameters to get the correct data. If theresponseobject is empty or thetickerdictionary is empty, there may be an issue with your API key or with the API itself. - The
- User
What I need to change in my code? Give me right cod as solution
- Assistant
- User
How I can print market current_price in my terminal ?
- Assistant
To print the
current_pricein your terminal, simply add a print statement to your code after thecurrent_pricevariable is assigned:# Load the market symbols url = f'https://api.binance.com/api/v3/ticker/price?symbol={symbol}' response = requests.get(url) ticker = response.json() try: current_price = float(ticker['price']) print(f"Current {symbol} price: {current_price}") except KeyError: print('Error fetching current price: price key not found in ticker data') current_price = NoneThis will print the current market price for the
symbolin your terminal. - User
I use your code, but I getting ERROR: 06/19/2023 05:18:29 Error fetching current price: price key not found in ticker data Error fetching markets: binance {"code":-1021,"msg":"Timestamp for this request was 1000ms ahead of the server's time."} BTC/USDT not found in the market Traceback (most recent call last): File "C:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 300, in signal = signal_generator(df) ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 190, in signal_generator
if current_price > last_candle[['EMA5', 'EMA20', 'EMA50', 'EMA100']].iloc[-1].max() * 1.01: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: '<' not supported between instances of 'float' and 'NoneType' - Assistant
- Assistant
Do you have any other questions or issues related to the code? I'll be happy to help!
- User
I used your code, but I getting ERROR: Error: Could not retrieve USDT balance from API response. 06/19/2023 07:16:06 Error fetching current price: price key not found in ticker data Traceback (most recent call last): File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\ntplib.py", line 318, in request response_packet, src_addr = s.recvfrom(256) ^^^^^^^^^^^^^^^ TimeoutError: timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 121, in synchronize_time() File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 112, in synchronize_time response = ntp_client.request('pool.ntp.org', version=3) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\ntplib.py", line 323, in request raise NTPException("No response received from %s." % host) ntplib.NTPException: No response received from pool.ntp.org.
- Assistant
The first error message indicates that the
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.