def generate_body(pair_name, price, amount, order_type): return { "command": order_type, "currencyPair": pair_name, "rate": float_to_str(price), "amount": float_to_str(amount), "nonce": generate_nonce() }
def generate_body(pair_name, price, amount, order_type): return { "pair": pair_name, "type": order_type, "ordertype": "limit", "price": float_to_str(price), "volume": float_to_str(amount), "nonce": generate_nonce() }
def log_arbitrage_determined_volume_not_enough(sell_order_book, buy_order_book, msg_queue): msg = """analyse order book - DETERMINED volume of deal is not ENOUGH {pair_name}: first_exchange: {first_exchange} first exchange volume: <b>{vol1}</b> second_exchange: {second_exchange} second_exchange_volume: <b>{vol2}</b>""".format( pair_name=get_pair_name_by_id(sell_order_book.pair_id), first_exchange=get_exchange_name_by_id(sell_order_book.exchange_id), second_exchange=get_exchange_name_by_id(buy_order_book.exchange_id), vol1=float_to_str(sell_order_book.bid[FIRST].volume), vol2=float_to_str(buy_order_book.ask[LAST].volume)) print_to_console(msg, LOG_ALL_MARKET_NETWORK_RELATED_CRAP) log_to_file(msg, DEBUG_LOG_FILE_NAME) if get_logging_level() >= LOG_ALL_TRACE: msg_queue.add_message(DEBUG_INFO_MSG, msg)
def log_arbitrage_heart_beat(sell_order_book, buy_order_book, difference): msg = """check_highest_bid_bigger_than_lowest_ask: \tFor pair - {pair_name} \tExchange1 - {exch1} BID = {bid} \tExchange2 - {exch2} ASK = {ask} \tDIFF = {diff}""".format(pair_name=get_pair_name_by_id(sell_order_book.pair_id), exch1=get_exchange_name_by_id(sell_order_book.exchange_id), bid=float_to_str(sell_order_book.bid[FIRST].price), exch2=get_exchange_name_by_id(buy_order_book.exchange_id), ask=float_to_str(buy_order_book.ask[LAST].price), diff=difference) print_to_console(msg, LOG_ALL_MARKET_NETWORK_RELATED_CRAP) log_to_file(msg, DEBUG_LOG_FILE_NAME)
def log_too_small_volume(order, max_volume, min_volume, msg_queue): msg = """<b> !!! NOT ENOUGH VOLUME !!! </b> Balance is not enough to place order {o} Determined volume is: {v} Minimum volume from recent tickers: {mv} so we going to ABANDON and FORGET about this order. """.format(o=order, v=float_to_str(max_volume), mv=float_to_str(min_volume)) print_to_console(msg, LOG_ALL_ERRORS) msg_queue.add_message(DEAL_INFO_MSG, msg) log_to_file(msg, ERROR_LOG_FILE_NAME)
def add_sell_order_huobi_url(key, pair_name, price, amount): final_url = SELL_URL + generate_url(key, HUOBI_API_ONLY, HUOBI_SELL_ORDER) params = json.dumps({ 'amount': float_to_str(amount), 'price': float_to_str(price), 'symbol': pair_name, 'source': 'api', 'type': 'sell-limit', 'account-id': get_huobi_account(key), }) res = PostRequestDetails(final_url, HUOBI_POST_HEADERS, params) return res
def log_arbitrage_determined_price_not_enough(sell_price, sell_price_order_book, buy_price, buy_price_order_book, difference, final_difference, pair_id, msg_queue): msg = """analyse order book - adjusted prices below 0.2 hardcoded threshold: \tfinal_sell: {sell_price} initial_sell: {i_sell} \tfinal_buy: {final_buy} initial_buy: {i_buy} \tfinal_diff: {final_diff} original_diff: {diff} \tfor pair_id = {p_name}""".format(sell_price=float_to_str(sell_price), i_sell=float_to_str(sell_price_order_book), final_buy=float_to_str(buy_price), i_buy=float_to_str(buy_price_order_book), final_diff=final_difference, p_name=get_pair_name_by_id(pair_id), diff=difference) print_to_console(msg, LOG_ALL_MARKET_NETWORK_RELATED_CRAP) log_to_file(msg, DEBUG_LOG_FILE_NAME) msg_queue.add_message(DEBUG_INFO_MSG, msg)
def add_buy_order_huobi_url(key, pair_name, price, amount): final_url = BUY_URL + generate_url(key, HUOBI_API_ONLY, HUOBI_BUY_ORDER) params = json.dumps({ "account-id": get_huobi_account(key), "amount": float_to_str(amount), "price": float_to_str(price), "source": "api", "symbol": pair_name, "type": "buy-limit" }) res = PostRequestDetails(final_url, HUOBI_POST_HEADERS, params) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_buy_order_huobi: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def add_sell_order_bittrex_url(key, pair_name, price, amount): # https://bittrex.com/api/v1.1/market/selllimit?apikey=API_KEY&market=BTC-LTC&quantity=1.2&rate=1.3 final_url = BITTREX_SELL_ORDER + key.api_key + "&nonce=" + str(generate_nonce()) body = { "market": pair_name, "quantity": float_to_str(amount), "rate": float_to_str(price) } final_url += _urlencode(body) headers = {"apisign": signed_string(final_url, key.secret)} res = PostRequestDetails(final_url, headers, body) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_sell_order_bittrex: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def __str__(self): str_repr = """Trade #{num} at timest1: {timest1} timest2: {timest2} type: {type} {deal1} {deal2} Current profit - {bakshish} """.format(num=self.id, timest1=self.timest1, timest2=self.timest2, type=get_order_type_by_id(self.deal_type), deal1=str(self.deal_1), deal2=str(self.deal_2), bakshish=float_to_str(self.current_profit)) return str_repr
def add_buy_order_binance_url(key, pair_name, price, amount): # curl -H "X-MBX-APIKEY: vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A" # -X POST 'https://api.binance.com/api/v3/order' -d 'symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1 # &price=0.1&recvWindow=6000000×tamp=1499827319559&signature=c8db56825ae71d6d79447849e617115f4a920fa2acdcab2b053c4b2838bd6b71' body = { "symbol": pair_name, "side": "BUY", "type": "LIMIT", "timeInForce": "GTC", "recvWindow": 5000, "timestamp": get_now_seconds_utc_ms(), "quantity": amount, "price": float_to_str(price) } res = generate_post_request(BINANCE_BUY_ORDER, body, key) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_buy_order_binance: {res}".format(res=res) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def add_sell_order_binance_url(key, pair_name, price, amount): final_url = BINANCE_SELL_ORDER body = { "symbol": pair_name, "side": "SELL", "type": "LIMIT", "timeInForce": "GTC", "recvWindow": 5000, "timestamp": get_now_seconds_utc_ms(), "quantity": amount, "price": float_to_str(price) } res = generate_post_request(final_url, body, key) if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP: msg = "add_sell_order_binance: url - {url} headers - {headers} body - {body}".format( url=res.final_url, headers=res.headers, body=res.body) print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP) log_to_file(msg, "market_utils.log") return res
def __str__(self): return """Profit in {coin_name}: {profit_coin} Profit in {base_name}: {profit_base} """.format( coin_name=get_currency_name_by_id(self.dst_currency_id), profit_coin=float_to_str(self.profit_in_coin), base_name=get_currency_name_by_id(self.base_currency_id), profit_base=float_to_str(self.profit_in_base_currency))
def save_report(start_time, end_time, profit_by_base, profit_details, missing_orders, failed_orders, loss_details, loss_by_base, orders, history_trades, file_name="what_we_have_at_the_end.log"): msg = "Profit report for time period of {t1} - {t2}".format( t1=ts_to_string_local(start_time), t2=ts_to_string_local(end_time)) log_to_file(msg, file_name) msg = "Epoch format: {t1} - {t2}".format(t1=start_time, t2=end_time) log_to_file(msg, file_name) orders_by_arbitrage_id = group_orders_by_arbitrage_id(orders) log_to_file( "Total number of arbitrage events - {p}".format( p=len(orders_by_arbitrage_id)), file_name) log_to_file("For them we registered - {p} orders".format(p=len(orders)), file_name) log_to_file( "Resulted number of trades - {p}".format(p=len(history_trades)), file_name) for base_currency_id in profit_details: log_to_file( "\t\tTotal profit by {cn} - {nn}".format( cn=get_currency_name_by_id(base_currency_id), nn=float_to_str(profit_by_base[base_currency_id])), file_name) for details_by_pair_id in profit_details[base_currency_id]: log_to_file(details_by_pair_id, file_name) total_number_missing = 0 for entry in missing_orders: total_number_missing += len(missing_orders[entry]) msg = "Total number of orders without trades (Expired?): {n}".format( n=total_number_missing) log_to_file(msg, file_name) for exchange_id in missing_orders: msg = "\t{exch} Number of orders without trades: {n}".format( exch=get_exchange_name_by_id(exchange_id), n=len(missing_orders[exchange_id])) log_to_file(msg, file_name) for exchange_id in missing_orders: msg = "Missing orders for {exch}".format( exch=get_exchange_name_by_id(exchange_id)) log_to_file(msg, "missing_orders.log") for x in missing_orders[exchange_id]: log_to_file(x, "missing_orders.log") total_number_failed = 0 for exchange_id in failed_orders: total_number_failed += len(failed_orders[exchange_id]) msg = "Total number of orders without order_id (Failed?): {n}".format( n=total_number_failed) log_to_file(msg, file_name) for exchange_id in failed_orders: msg = "\t{exch} Number of orders without trades: {n}".format( exch=get_exchange_name_by_id(exchange_id), n=len(failed_orders[exchange_id])) log_to_file(msg, file_name) for exchange_id in failed_orders: msg = "Failed orders for {exch}".format( exch=get_exchange_name_by_id(exchange_id)) log_to_file(msg, "failed_orders.log") for x in failed_orders[exchange_id]: log_to_file(x, "failed_orders.log") log_to_file("\t\tLOSS DETAILS", file_name) for base_currency_id in loss_details: log_to_file( "\t\tLoss details by {cn} - {nn}".format( cn=get_currency_name_by_id(base_currency_id), nn=float_to_str(loss_by_base[base_currency_id])), file_name) for details_by_pair_id in loss_details[base_currency_id]: log_to_file(details_by_pair_id, file_name)
def init_deals_with_logging_speedy(trade_pairs, difference, file_name, processor, msg_queue): # FIXME move after deal placement ? global overall_profit_so_far overall_profit_so_far += trade_pairs.current_profit base_currency_id, dst_currency_id = split_currency_pairs( trade_pairs.deal_1.pair_id) msg = """We try to send following deals to exchange. <b>Expected profit in {base_coin}:</b> <i>{cur}</i>. <b>Overall:</b> <i>{tot}</i> <b>Difference in percents:</b> <i>{diff}</i> Deal details: {deal} """.format(base_coin=get_currency_name_by_id(base_currency_id), cur=float_to_str(trade_pairs.current_profit), tot=float_to_str(overall_profit_so_far), diff=difference, deal=str(trade_pairs)) msg_queue.add_message(DEAL_INFO_MSG, msg) log_to_file(msg, file_name) if not YES_I_KNOW_WHAT_AM_I_DOING: die_hard("init_deals_with_logging_speedy called for {f}".format( f=trade_pairs)) parallel_deals = [] for order in [trade_pairs.deal_1, trade_pairs.deal_2]: method_for_url = dao.get_method_for_create_url_trade_by_exchange_id( order) # key, pair_name, price, amount key = get_key_by_exchange(order.exchange_id) pair_name = get_currency_pair_name_by_exchange_id( order.pair_id, order.exchange_id) post_details = method_for_url(key, pair_name, order.price, order.volume) constructor = return_with_no_change wu = WorkUnit(post_details.final_url, constructor, order) wu.add_post_details(post_details) parallel_deals.append(wu) res = processor.process_async_post(parallel_deals, DEAL_MAX_TIMEOUT) if res is None: log_to_file( "For TradePair - {tp} result is {res}".format(tp=trade_pairs, res=res), file_name) log_to_file( "For TradePair - {tp} result is {res}".format(tp=trade_pairs, res=res), ERROR_LOG_FILE_NAME) return # check for errors only for entry in res: json_response, order = entry if "ERROR" in json_response: msg = """ <b>ERROR: </b>NONE During deal placement: {u1} Details: {err_msg} """.format(u1=order, err_msg=json_response) msg_queue.add_order(FAILED_ORDERS_MSG, order) else: msg = """ For trade {trade} Response is {resp} """.format(trade=order, resp=json_response) print_to_console(msg, LOG_ALL_ERRORS) msg_queue.add_message(DEBUG_INFO_MSG, msg) log_to_file(msg, file_name) for order in [trade_pairs.deal_1, trade_pairs.deal_2]: msg_queue.add_order(ORDERS_MSG, order)