I used this code: import time from binance.client import Client from binance.enums…
- User
I used this code: import time from binance.client import Client from binance.enums import * from binance.exceptions import BinanceAPIException from binance.helpers import round_step_size import pandas as pd import requests import json import numpy as np import pytz import datetime as dt import ccxt
Get the current time and timestamp
now = dt.datetime.now() date = now.strftime("%m/%d/%Y %H:%M:%S") print(date)
timestamp = int(time.time() * 1000)
API keys and other configuration
API_KEY = '' API_SECRET = ''
client = Client(API_KEY, API_SECRET)
STOP_LOSS_PERCENTAGE = -50 TAKE_PROFIT_PERCENTAGE = 100 MAX_TRADE_QUANTITY_PERCENTAGE = 100 POSITION_SIDE_SHORT = 'SELL' POSITION_SIDE_LONG = 'BUY' quantity = 1 symbol = 'BTC/USDT' order_type = 'market' leverage = 100 max_trade_quantity_percentage = 1 binance_futures = ccxt.binance({ 'apiKey': '', 'secret': '', 'enableRateLimit': True, # enable rate limitation 'options': { 'defaultType': 'future', 'adjustForTimeDifference': True },'future': { 'sideEffectType': 'MARGIN_BUY', # MARGIN_BUY, AUTO_REPAY, etc… } })
binance_futures = ccxt.binance({ 'apiKey': API_KEY, 'secret': API_SECRET, 'enableRateLimit': True, # enable rate limitation 'options': { 'defaultType': 'future', 'adjustForTimeDifference': True } })
Load the market symbols
markets = binance_futures.load_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): server_time = exchange.fetch_currencies() return server_time['timestamp']
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' } try: 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 except requests.exceptions.RequestException as e: print(f'Error in get_klines: {e}') return None df = get_klines(symbol, '1m', 89280)
- Assistant
It would be helpful if you could provide the specific error message that you are receiving. Without that information, it is difficult to accurately identify the issue.
- User
ERROR: 06/06/2023 19:24:20 BTC/USDT found in the market The signal time is: 2023-06-06 19:24:29 : Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 256, in
order_execution(symbol, signal, MAX_TRADE_QUANTITY_PERCENTAGE, leverage, order_type) File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 243, in order_execution response = binance_futures.create_order(**order_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: binance.create_order() got an unexpected keyword argument 'closePosition'
PS C:\Users\Alan.vscode\jew_bot> - Assistant
The error message indicates that the
binance_futures.create_order()method was called with an unexpected keyword argumentclosePosition. This could be due to a few reasons:- The
binance_futuresexchange object does not support the use ofclosePositionas a parameter for order creation. - The version of the
ccxtlibrary being used does not support theclosePositionparameter for thecreate_order()method.
To resolve the issue, you could try removing the
closePositionparameter from theorder_paramsdictionary being passed to thebinance_futures.create_order()method and see if that resolves the issue. Here's what the updatedorder_paramsdictionary would look like:order_params = { "type": "MARKET" if signal == "buy" else "MARKET", "side": "BUY" if signal == "buy" else "SELL", "amount": quantity, "price": price, "newClientOrderId": 'bot', "type":order_type.upper(), "responseType": "RESULT", "timeInForce": "GTC", "workingType": "MARK_PRICE", "priceProtect": False, "leverage": leverage } - The
- User
ERROR: The signal time is: 2023-06-06 19:27:15 : Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 255, in
order_execution(symbol, signal, MAX_TRADE_QUANTITY_PERCENTAGE, leverage, order_type) File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 242, in order_execution response = binance_futures.create_order(**order_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: binance.create_order() got an unexpected keyword argument 'newClientOrderId' - Assistant
The error message states that the
binance_futures.create_order()method was called with an unexpected keyword argumentnewClientOrderId. This error can occur if theccxtversion being used does not support the included parameter.To resolve the issue, you could try removing the
newClientOrderIdparameter from theorder_paramsdictionary. Here's what the updatedorder_paramsdictionary would look like:order_params = { "type": "MARKET" if signal == "buy" else "MARKET", "side": "BUY" if signal == "buy" else "SELL", "amount": quantity, "price": price, "type":order_type.upper(), "responseType": "RESULT", "timeInForce": "GTC", "workingType": "MARK_PRICE", "priceProtect": False, "leverage": leverage }Note that the
newClientOrderIdparameter has been removed from theorder_paramsdictionary.