└── bot.py /bot.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import time 3 | import hmac 4 | import hashlib 5 | import json 6 | 7 | # Configuración 8 | API_KEY = 'TU_API_KEY' 9 | API_SECRET = 'TU_API_SECRET' 10 | BASE_URL = 'https://api.bitflex.com' 11 | 12 | # Parámetros de trading 13 | SYMBOL = 'BTCUSDT' # Par de trading 14 | BUY_THRESHOLD = 30000 # Comprar si el precio baja por debajo de este umbral 15 | SELL_THRESHOLD = 35000 # Vender si el precio sube por encima de este umbral 16 | TRADE_AMOUNT = 0.001 # Cantidad de BTC a comprar/vender 17 | 18 | # Función para generar la firma de la solicitud 19 | def generate_signature(params, secret): 20 | query_string = '&'.join([f"{key}={value}" for key, value in sorted(params.items())]) 21 | signature = hmac.new(secret.encode(), query_string.encode(), hashlib.sha256).hexdigest() 22 | return signature 23 | 24 | # Función para obtener el precio actual del mercado 25 | def get_market_price(symbol): 26 | url = f'{BASE_URL}/api/v1/market/ticker?symbol={symbol}' 27 | response = requests.get(url) 28 | data = response.json() 29 | return float(data['data']['lastPrice']) 30 | 31 | # Función para realizar una orden de compra 32 | def place_order(symbol, side, quantity): 33 | url = f'{BASE_URL}/api/v1/order' 34 | timestamp = int(time.time() * 1000) 35 | params = { 36 | 'symbol': symbol, 37 | 'side': side, 38 | 'type': 'LIMIT', 39 | 'quantity': quantity, 40 | 'price': get_market_price(symbol), 41 | 'timestamp': timestamp 42 | } 43 | signature = generate_signature(params, API_SECRET) 44 | headers = { 45 | 'X-MBX-APIKEY': API_KEY 46 | } 47 | params['signature'] = signature 48 | response = requests.post(url, headers=headers, data=params) 49 | return response.json() 50 | 51 | # Función principal del bot 52 | def trading_bot(): 53 | while True: 54 | try: 55 | price = get_market_price(SYMBOL) 56 | print(f'Precio actual: {price} USD') 57 | 58 | if price < BUY_THRESHOLD: 59 | print('El precio está bajo, comprando...') 60 | order = place_order(SYMBOL, 'BUY', TRADE_AMOUNT) 61 | print('Orden de compra ejecutada:', order) 62 | elif price > SELL_THRESHOLD: 63 | print('El precio está alto, vendiendo...') 64 | order = place_order(SYMBOL, 'SELL', TRADE_AMOUNT) 65 | print('Orden de venta ejecutada:', order) 66 | else: 67 | print('Esperando una mejor oportunidad...') 68 | 69 | # Esperar un tiempo antes de revisar nuevamente 70 | time.sleep(60) 71 | 72 | except Exception as e: 73 | print('Error:', e) 74 | time.sleep(60) 75 | 76 | # Ejecutar el bot 77 | if __name__ == '__main__': 78 | trading_bot() 79 | --------------------------------------------------------------------------------