I used your code: import time from binance.client import Client from binance.enums…
- User
I used your 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 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 from ta import trend, momentum from ta.trend import SMAIndicator API_KEY = '' API_SECRET = ''
client = Client(API_KEY, API_SECRET)
Set the endpoint and parameters for the request
url = "https://fapi.binance.com/fapi/v2/account" timestamp = int(time.time() * 1000) recv_window = 5000
params = { "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 leverage = 100
Send the request using the requests library
response = requests.get(url, params=params, headers={'X-MBX-APIKEY': API_KEY}) account_info = response.json()
Get the USDT balance and calculate the max trade size based on the leverage
try: usdt_balance = next((item for item in account_info['accountBalance'] if item["asset"] == "USDT"), {"free": 0})['free'] except KeyError: usdt_balance = 0 print("Error: Could not retrieve USDT balance from API response.")
max_trade_size = float(usdt_balance) * leverage
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)
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': API_KEY, 'secret': API_SECRET, 'enableRateLimit': True, # enable rate limitation 'options': { 'defaultType': 'future', 'adjustForTimeDifference': True },'future': { 'sideEffectType': 'MARGIN_BUY', # MARGIN_BUY, AUTO_REPAY, etc… } })
Load the market symbols
def sync_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()}')
recv_window = 10000 params = { "timestamp": timestamp, "recvWindow": recv_window } try: markets = binance_futures.load_markets() except ccxt.BaseError as e: print(f'Error fetching markets: {e}') markets = []
- Assistant
It looks like the signal generated by the
signal_generatorfunction is always False, which means that the SMA indicator is not crossing above or below the previous value. You may need to adjust theindicator_windowparameter in thesignal_generatorfunction to a smaller or larger value depending on the time frame you are using (1 minute in this case). You can also try using a different technical indicator or a combination of indicators to generate signals.In addition, you need to call the
order_executionfunction with the correct signal (either "buy" or "sell") returned by thesignal_generatorfunction in order to place an order. You can use a conditional statement to check the signal and call the appropriate function:codesignal = signal_generator(df) if signal[0]: order_execution(symbol, "buy", step_size, leverage, order_type) elif signal[1]: order_execution(symbol, "sell", step_size, leverage, order_type) - User
What I need to change in signal_generator ?
- Assistant
In the
signal_generatorfunction, you can try adjusting theindicator_windowparameter to see if it improves the accuracy of the signals. You can try increasing or decreasing it to see which value works best for the time frame you are using.For example, if you are using a 1-minute time frame, you can try using a smaller value like 5 or 10 for
indicator_window. This will calculate the SMA indicator based on the last 5 or 10 minutes of data, which may generate more accurate signals in a faster-moving market.On the other hand, if you are using a longer time frame like 1 hour or 4 hours, you may need to use a larger value like 20, 50, or 100 for
indicator_windowto generate more reliable signals. - User
Give me only code
- Assistant