codigo bot
codigo bot
import time
from kucoin.client import Market
from kucoin.client import Trade
from kucoin.client import User
import pandas as pd
import talib
import requests
import datetime
import csv
url = 'https://api.kucoin.com'
api_key = config['api_key']
api_secret = config['api_secret']
api_passphrase = config['api_passphrase']
# Conexion
market = Market(url=url)
# Archivo csv
csv_path = 'orders.csv'
# Definir el encabezado
header = ['Order ID', 'Fecha Open', 'Ticker', 'Order Type', 'Order Size', 'Price
Open', 'Price Closed', 'Comision',
'Fecha Cierre',
'Profit/Loss']
vela_duration = 3600
while True:
try:
accounts = user.get_account_list()
for account in accounts:
if account['id'] == '644c128b585d420001f6fa02':
available_BTC = float(account['available'])
print('_' * 100)
print(f'Balance en BTC de: {available_BTC} BTC')
print('_' * 100)
if account['id'] == '644c11cf333b69000107406e':
available_USDT = float(account['available'])
print('_' * 100)
print(f'Balance en USDT de: {available_USDT} USDT')
print('_' * 100)
df.set_index('time', inplace=True)
df = df[::-1] # Reverse the DataFrame
df = df.astype(float)
last_close = df['close'].iloc[-2]
bmsb_mayor = df['bmsb_mayor'].iloc[-2]
bmsb_menor = df['bmsb_menor'].iloc[-2]
previous_close = df['close'].iloc[-3]
previous_bmsb_mayor = df['bmsb_mayor'].iloc[-3]
previous_bmsb_menor = df['bmsb_menor'].iloc[-3]
ticker = market.get_ticker(symbol)
price_actual = float(ticker['price'])
fecha_actual = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if buySignal:
if sellOpen:
try:
ordersellcancelled = trade.get_order_details(check_venta)
# Datos a extraer de orden de cancelelada de venta
size = ordersellcancelled['size']
fee_venta = float(ordersellcancelled['fee'])
# Fecha de cierre
closing_time = datetime.datetime.now().strftime('%Y-%m-%d %H:
%M:%S')
print(f'Orden de venta cerrada con un size de: {size}')
comision = fee_compra + fee_venta
sellOpen = False
limitVenta = False
add_order_to_csv(order_id=order_id, opening_time=opening_time,
ticker=symbol, order_type='sell',
order_size=size, price_open=price,
price_closed=price_closed,
comision=comision, closing_time=closing_time,
profit_loss=profit_or_loss)
except Exception as e:
print(f'Error placing order: {e}')
try:
accounts = user.get_account_list()
for account in accounts:
if account['id'] == '644c128b585d420001f6fa02':
available_BTC = float(account['available'])
print(f"El valor disponible para la cuenta en BTC es:
{available_BTC}")
size_to = round((available_BTC * 30 / 100), 6)
print(f'El size es de: {size_to}')
orderbuy = trade.create_market_order(symbol, 'buy',
size=size_to)
except Exception as e:
print(f'Error placing order: {e}')
time.sleep(2)
try:
check_compra_total =
trade.get_order_details(orderId=orderbuy['orderId'])
check_compra = check_compra_total['id']
try:
ticker = market.get_ticker(symbol)
price = float(ticker['price'])
except Exception as e:
price = 'Error'
opening_time = datetime.datetime.now().strftime('%Y-%m-%d %H:
%M:%S')
print('_' * 100)
print(f'El id de la compra es: {check_compra}, con un size de
{size_to}, en el precio: {price}')
print(f'Fecha de apertura: {opening_time}')
print('_' * 100)
buyOpen = True
limitCompra = True
except Exception as e:
print(f'Error while checking order status: {e}')
time.sleep(2)
try:
check_compra_total =
trade.get_order_details(orderId=orderbuy['orderId'])
check_compra = check_compra_total['id']
try:
ticker = market.get_ticker(symbol)
price = float(ticker['price'])
except Exception as e:
price = 'Error'
opening_time = datetime.datetime.now().strftime('%Y-%m-%d %H:
%M:%S')
print('_' * 100)
print(f'El id de la compra es: {check_compra}, con un size de
{size_to}, en el precio: {price}')
print(f'Fecha de apertura: {opening_time}')
print('_' * 100)
buyOpen = True
limitCompra = True
except Exception as e:
print(f'Error while checking order status: {e}')
if sellSignal:
if buyOpen:
try:
orderbuycancelled = trade.get_order_details(check_compra)
# Datos a extraer de orden de cancelada de compra
size = orderbuycancelled['size']
fee_compra = float(orderbuycancelled['fee'])
# Fecha de cierre
closing_time = datetime.datetime.now().strftime('%Y-%m-%d %H:
%M:%S')
print(f'Orden de compra cerrada con un size de: {size}')
comision = fee_compra + fee_venta
buyOpen = False
limitCompra = False
add_order_to_csv(order_id=order_id, opening_time=opening_time,
ticker=symbol, order_type='buy',
order_size=size, price_open=price,
price_closed=price_closed,
comision=comision, closing_time=closing_time,
profit_loss=profit_or_loss)
except Exception as e:
print(f'Error placing order: {e}')
try:
accounts = user.get_account_list()
for account in accounts:
if account['id'] == '644c128b585d420001f6fa02':
available_BTC = float(account['available'])
print(f"El valor disponible para la cuenta en BTC es:
{available_BTC}")
size_to = round((available_BTC * 30 / 100), 6)
print(f'El size es de: {size_to}')
ordersell = trade.create_market_order(symbol, 'sell',
size=size_to)
except Exception as e:
print(f'Error placing order: {e}')
time.sleep(2)
try:
check_venta_total =
trade.get_order_details(orderId=ordersell['orderId'])
check_venta = check_venta_total['id']
try:
ticker = market.get_ticker(symbol)
price = float(ticker['price'])
except Exception as e:
price = 'Error'
opening_time = datetime.datetime.now().strftime('%Y-%m-%d %H:
%M:%S')
print('_' * 100)
print(f'El id de la venta es: {check_venta}, con un size de
{size_to}, en el precio: {price}')
print(f'Fecha de apertura: {opening_time}')
print('_' * 100)
sellOpen = True
limitVenta = True
except Exception as e:
print(f'Error while checking order status: {e}')
time.sleep(2)
try:
check_venta_total =
trade.get_order_details(orderId=ordersell['orderId'])
check_venta = check_venta_total['id']
try:
ticker = market.get_ticker(symbol)
price = float(ticker['price'])
except Exception as e:
price = 'Error'
except Exception as e:
print(f'Error:{e}')
for i in range(sleep_time):
# Imprime el carácter actual del símbolo de carga
try:
ticker = market.get_ticker(symbol)
price_actual = float(ticker['price'])
except Exception as e:
price_actual = 'Error'
current_time = time.time()