.
  • Agregar a Technorati
  • Agregar a Del.icio.us
  • Agregar a DiggIt!
  • Agregar a Yahoo!
  • Agregar a Google
  • Agregar a Meneame
  • Agregar a Furl
  • Agregar a Reddit
  • Agregar a Magnolia
  • Agregar a Blinklist
  • Agregar a Blogmarks

import ccxt # Conexión con exchanges import pandas as pd import numpy as np from time import sleep class OTCTradingBot: def __init__(self, api_key: str, api_secret: str, par: str = "EUR/USD"): self.exchange = ccxt.ftx({ # Cambiar 'ftx' por el broker OTC que uses 'apiKey': api_key, 'secret': api_secret, 'enableRateLimit': True }) self.par = par self.niveles_grid = self._calcular_niveles_grid() self.max_perdida = 0.05 # Máximo 5% de pérdida por operación self.take_profit = 0.02 # Tomar ganancias del 2% def _calcular_niveles_grid(self) -> list: """Calcula los niveles de compra/venta basados en volatilidad.""" datos = self.exchange.fetch_ohlcv(self.par, '1d', limit=30) df = pd.DataFrame(datos, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume']) volatilidad = np.mean(df['high'] - df['low']) precio_actual = df['close'].iloc[-1] return [precio_actual * (1 + i * 0.005) for i in range(-10, 10)] # Grid de ±5% def ejecutar_orden(self, precio: float, cantidad: float, tipo: str) -> dict: """Envía órdenes limitadas de compra/venta.""" try: orden = self.exchange.create_order( symbol=self.par, type='limit', side=tipo, amount=cantidad, price=precio ) return orden except Exception as e: print(f"Error al enviar orden: {e}") return {} def ejecutar_estrategia(self): """Ejecuta el Grid Trading con gestión de riesgo.""" saldo = self.exchange.fetch_balance()['free']['USD'] tamaño_posicion = saldo * 0.01 # Riesgo del 1% por operación while True: precio_actual = self.exchange.fetch_ticker(self.par)['last'] for nivel in self.niveles_grid: if precio_actual <= nivel * 0.995: # Compra si baja un 0.5% self.ejecutar_orden(nivel, tamaño_posicion, 'buy') elif precio_actual >= nivel * 1.005: # Vende si sube un 0.5% self.ejecutar_orden(nivel, tamaño_posicion, 'sell') # Stop-loss dinámico (cierra operaciones con pérdidas >5%) posiciones = self.exchange.fetch_positions([self.par]) for pos in posiciones: if pos['unrealizedPnl'] < -self.max_perdida * saldo: self.exchange.create_order( symbol=self.par, type='market', side='sell' if pos['side'] == 'long' else 'buy', amount=pos['contracts'] ) sleep(60) # Espera 1 minuto entre iteraciones if __name__ == "__main__": bot = OTCTradingBot(api_key="TU_API_KEY", api_secret="TU_API_SECRET", par="EUR/USD") bot.ejecutar_estrategia()