here is the code:
import json
from binance.client import Client
from binance.exceptions import BinanceAPIException
Charger les clés API depuis le fichier de configuration
try:
with open(â/Users/brandonmbambi/Documents/BOT/bot280524/config.jsonâ) as config_file:
config = json.load(config_file)
except FileNotFoundError:
print(âErreur : Le fichier config.json est introuvable.â)
exit()
api_key = config[âapi_keyâ]
api_secret = config[âapi_secretâ]
Vérifiez les clés API
print(f"API Key: {api_key}â)
print(f"API Secret: {api_secret}â)
Initialisation de lâinstance Binance Futures avec python-binance
client = Client(api_key, api_secret)
Vérifiez les permissions de la clé API
try:
account_info = client.futures_account()
print(âLes clĂ©s API sont valides et les permissions sont correctes.â)
except BinanceAPIException as e:
print(f"Erreur de lâAPI Binance: {e}")
exit()
VĂ©rifiez les symboles disponibles pour les futures USD-M et COIN-M
try:
exchange_info = client.futures_exchange_info()
usd_m_symbols = [symbol[âsymbolâ] for symbol in exchange_info[âsymbolsâ] if symbol[âcontractTypeâ] == âPERPETUALâ]
coin_m_symbols = [symbol[âsymbolâ] for symbol in exchange_info[âsymbolsâ] if symbol[âcontractTypeâ] in [âCURRENT_QUARTERâ, âNEXT_QUARTERâ]]
print(âSymboles valides pour les futures USD-M :â)
print(usd_m_symbols)
print(âSymboles valides pour les futures COIN-M :â)
print(coin_m_symbols)
except BinanceAPIException as e:
print(f"Erreur lors de la rĂ©cupĂ©ration des informations dâĂ©change: {e}")
exit()
Choisir le bon symbole (modifiez ici si nécessaire)
symbol = âBTCUSDTâ # Changez pour âBTCUSD_PERPâ si vous utilisez les futures COIN-M
VĂ©rifiez si le symbole est valide
if symbol not in usd_m_symbols and symbol not in coin_m_symbols:
print(f"Erreur : Le symbole {symbol} nâest pas valide.")
exit()
leverage = 100
risk_percentage = 10.0
take_profit_multiplier = 3.0
magic_number = 123456 # Identifiant unique pour les ordres
def set_leverage(symbol, leverage):
try:
client.futures_change_leverage(symbol=symbol, leverage=leverage)
print(f"Leverage set to {leverage} for {symbol}â)
except BinanceAPIException as e:
print(f"Erreur lors de la dĂ©finition de lâeffet de levier: {e}â)
def calculate_position_size(balance, entry_price, leverage):
effective_balance = balance * (risk_percentage / 100)
position_size = (effective_balance * leverage) / entry_price
return position_size
def place_order(symbol, side, position_size, stop_loss_price, take_profit_price):
try:
order = client.futures_create_order(
symbol=symbol,
side=side.upper(),
type=âMARKETâ,
quantity=position_size,
newClientOrderId=magic_number
)
stop_loss_order = client.futures_create_order(
symbol=symbol,
side=âSELLâ if side.upper() == âBUYâ else âBUYâ,
type=âSTOP_MARKETâ,
stopPrice=stop_loss_price,
quantity=position_size,
)
take_profit_order = client.futures_create_order(
symbol=symbol,
side=âSELLâ if side.upper() == âBUYâ else âBUYâ,
type=âLIMITâ,
price=take_profit_price,
quantity=position_size,
timeInForce=âGTCâ
)
return order, stop_loss_order, take_profit_order
except BinanceAPIException as e:
print(f"Erreur lors de la crĂ©ation de lâordre: {e}")
def manage_open_orders():
try:
orders = client.futures_get_open_orders(symbol=symbol)
for order in orders:
print(f"Open order: {order}â)
except BinanceAPIException as e:
print(f"Erreur lors de la gestion des ordres ouverts: {e}â)
def fibonacci_levels(high, low):
diff = high - low
levels = {
âretracement_50â: high - (diff * 0.5),
âretracement_618â: high - (diff * 0.618),
}
return levels
def main():
set_leverage(symbol, leverage)
balance = 1000 # Exemple de balance en USDT
HH = 50000 # Exemple de HH
HL = 49500 # Exemple de HL
LH = 50500 # Exemple de LH
LL = 49000 # Exemple de LL
levels = fibonacci_levels(HH, HL)
if levels[âretracement_618â] <= LH <= levels[âretracement_50â]:
entry_price = LH
stop_loss_price = HH
take_profit_price = HL - (HH - HL) * take_profit_multiplier
position_size = calculate_position_size(balance, entry_price, leverage)
place_order(symbol, âSELLâ, position_size, stop_loss_price, take_profit_price)
levels = fibonacci_levels(LL, LH)
if levels[âretracement_618â] <= HL <= levels[âretracement_50â]:
entry_price = HL
stop_loss_price = LL
take_profit_price = LH + (LH - LL) * take_profit_multiplier
position_size = calculate_position_size(balance, entry_price, leverage)
place_order(symbol, âBUYâ, position_size, stop_loss_price, take_profit_price)
manage_open_orders()
if name == âmainâ:
main()