def check_order_in_db(symbol, buy_sell, price, db_program_id): cursor = db.cursor() #print(symbol, buy_sell, price) sql = "SELECT * FROM deal_table \ WHERE symbol='%s' and buy_sell=%d and program=%d" % (symbol, buy_sell, db_program_id) #print(sql) try: cursor.execute(sql) results = cursor.fetchall() for row in results: id = row[0] orderid = row[1] symbol = row[2] buy_sell = row[3] db_price = row[4] #amount = row[4] #print("id=%s, orderid=%s, symbol=%s, buy_sell=%s, price=%s, amount=%s" % \ # (id, orderid, symbol, buy_sell, price, amount)) if price == db_price: return orderid else: send_email("HB: " + "check_order_in_db error ", "price %.*f is different with db_price %.*f"% (price_print_format, price, price_print_format, db_price)) return 0 except: #print("Error: unable to fecth data") return 0
def callback(upd_event: 'OrderUpdateEvent'): print("---- order update : ----") #upd_event.print_object() try: b = __Autonomy__() current = sys.stdout sys.stdout = b upd_event.print_object() sys.stdout = current order_type = get_value("Order Type", b._buff) price = get_value("Trade Price", b._buff) order_state = get_value("Order State", b._buff) order_symbol = get_value("Symbol", b._buff) order_id = get_value("Order Id", b._buff) amount = get_value("Trade Volume", b._buff) order_type = get_first_value(order_type) order_state = get_first_value(order_state) order_symbol = get_first_value(order_symbol) price = get_first_value(price) price = price if price == "" else float(price) order_id = int(get_first_value(order_id)) amount = get_first_value(amount) amount = amount if amount == "" else float(amount) print(b._buff) if order_type == "sell-limit": order_type = "sell" elif order_type == "buy-limit": order_type = "buy" if order_state == "filled": order_state = "done" elif order_state == "partial-filled": order_state = "part done" if price or amount: order_info = "%s, %s, price=%s, Volume=%s" % ( order_type, order_state, price, amount) else: order_info = "%s, %s" % (order_type, order_state) print(order_info) current = sys.stdout a = __Autonomy__() sys.stdout = a print(b._buff) print("\n===== check SUBMITTED order =====\n") get_orders(order_symbol) sys.stdout = current send_email("HB: " + order_info, a._buff) except Exception as e: print("callback ExecuteError", e)
def loop_handle(): last_price = get_current_price('btcusdt') while (1): current_price = get_current_price('btcusdt') if current_price == 0: time.sleep(1) continue elif last_price - current_price >= 300: #report send_email( "BTC price: -- " + "%.1f <-- %.1f " % (current_price, last_price), "no content") last_price = current_price elif current_price - last_price >= 300: #report send_email( "BTC price: ++ " + "%.1f <-- %.1f " % (current_price, last_price), "no content") last_price = current_price print("btc price", current_price, last_price) time.sleep(loop_time)
def get_current_price(symbol): try: market_client = MarketClient() depth_size = 2 depth = market_client.get_pricedepth(symbol, DepthStep.STEP0, depth_size) #LogInfo.output("---- Top {size} bids ----".format(size=len(depth.bids))) i = 0 if not depth: return 0 for entry in depth.bids: i += 1 if i == 1: first_price = entry.price return first_price #LogInfo.output(str(i) + ": price: " + str(entry.price) + ", amount: " + str(entry.amount)) except Exception as e: print("ExecuteError in get_current_price", e) send_email( "HB: " + "get_current_price Exception ", "you can have a check if it is break or not after using pass!") pass return 0
symbol, each_amount) elif execute_result and not execute_num: created_success = create_my_order(buy_price, sell_price, symbol, each_amount) else: logger.info("most probably failed to execute here") logger.info( "each_amount = %f, current_prince=%f, buy price:%r, Let me sleep for one minute..." % (each_amount, current_price, price_points)) print_current_price_buy_sell_state() print("current price:", current_price) logger.info("len(state_points) = %d, len(price_points) = %d" % (len(state_points), len(price_points))) price_list = [] for price in price_points: if price not in state_points: price_list.append(price) if len(price_list): print("ISSUE: please check, no state price list:", price_list) send_email("HB: " + "ISSUE some price state goes wrong", ','.join('%s' % id for id in price_list)) print( "=======================================================================================================================" ) time.sleep(30)
def error_handler(exception: 'HuobiApiException'): send_email("HB: " + "subscribe error happened", exception)
def loop_handle(): q = queue.Queue() eq = queue.Queue() minute_price_q = queue.Queue() steps_dec = 10 q_accu_dec = 0 eq_accu_dec = 0 steps_inc = 10 q_accu_inc = 0 eq_accu_inc = 0 current_price = get_current_price(symbol) q_accu_inc_start = current_price q_accu_dec_start = current_price eq_accu_inc_start = current_price eq_accu_dec_start = current_price buying = 0 selling = 0 order_timeout_checking = 0 big_drop_flag = False big_drop_flag_time_out = 0 minute_control = 0 minute_percent = "" global big_change_threhold global diff_space_buy global diff_space_sell global deal_space global accu_buy global usdt_amount global price_list global buy_order_ongoing global sell_order_ongoing global f_trade_done global total_buy_times global just_buy_time_out cur_time = time.strftime("%Y-%m-%d", time.localtime()) log_file = "log/auto-hunter-price-big-change-%s-%s.txt"%(balance_name,cur_time) f_price_big_change = open(log_file, mode='a') while (1): lock.acquire() current_price = get_current_price(symbol) if current_price == 0: time.sleep(1) continue buy_price = get_previous_price(price_list) if buy_price !=0: cal_price = buy_price else: cal_price = current_price diff_space_sell_val = diff_space_sell*cal_price/100 deal_space_val = deal_space*cal_price/100 diff_space_buy_val = diff_space_buy*cal_price/100 accu_buy_val = accu_buy*cal_price/100 already_buy_times = len(price_list) put_into_queue(q, current_price, price_q_eq_size) q_order, q_even, q_diff = order_even_value(q) if minute_control == 0: put_into_queue(minute_price_q, q_even, minute_price_q_size) minute_control += 1 elif minute_control >= 20: minute_control = 0 else: minute_control += 1 put_into_queue(eq, q_even, price_q_eq_size) eq_order, eq_even, eq_diff = order_even_value(eq) big_change_str = "" showgap = "" qstr="" eqstr="" q_diff_percent = q_diff *100 /q_even eq_diff_percent = eq_diff *100/eq_even if big_drop_flag: if big_drop_flag_time_out: big_drop_flag_time_out = big_drop_flag_time_out -1 else: big_drop_flag = False if just_buy_time_out>0: just_buy_time_out = just_buy_time_out -1 if q_diff_percent > big_change_threhold: big_change_str = "Q-UPUPUP %.2f%% "%(q_diff_percent) output_to_log_file(f_price_big_change, "Q-UPUPUP: %.2f%%, q_even:%.*f, q_diff=%.*f"%(q_diff_percent, price_print_format, q_even, price_print_format,q_diff)) big_drop_flag = False elif q_diff_percent < -big_change_threhold: big_change_str = "Q-DOWNDOWN %.2f%% "%(q_diff_percent) big_drop_flag = True big_drop_flag_time_out = BIG_DROP_TIME output_to_log_file(f_price_big_change, "Q-DOWNDOWN: %.2f%%, q_even:%.*f, q_diff=%.*f"%(q_diff_percent, price_print_format, q_even, price_print_format,q_diff)) if eq_diff_percent > big_change_threhold: big_change_str += "EQ-UPUPUP %.2f%% "%(eq_diff_percent) output_to_log_file(f_price_big_change, "EQ-UPUPUP: %.2f%%, eq_even:%.*f, eq_diff=%.*f"%(eq_diff_percent, price_print_format,eq_even, price_print_format,eq_diff)) big_drop_flag = False elif eq_diff_percent < -big_change_threhold: big_change_str += "EQ-DOWNDOWN %.2f%% "%(eq_diff_percent) big_drop_flag = True big_drop_flag_time_out = BIG_DROP_TIME output_to_log_file(f_price_big_change, "EQ-DOWNDOWN: %.2f%%, eq_even:%.*f, eq_diff=%.*f"%(eq_diff_percent, price_print_format, eq_even, price_print_format, eq_diff)) if buy_order_ongoing: #orderid = check_order_in_db(symbol, , buying, db_program_id) if not orderid: printme_warning("buy %.*f is not found in local DB!!!"% (price_print_format, buying)) send_email("HB: " + "buy_order_ongoing error ", "buy %.*f is not found in local DB!!!"% (price_print_format, buying)) #buy_order_ongoing = False #好像有问题,这里会导致再次购买,所以把这个注释掉先 if order_timeout_checking > 1 : order_timeout_checking -= 1 else: order_timeout_checking = 0 printme("canceling buy order id %d"%orderid) cancel_order_and_restore_system(symbol, orderid) buy_order_ongoing = False buying = 0 if sell_order_ongoing: orderid = check_order_in_db(symbol, sell_db_flag, selling, db_program_id) if not orderid: printme_warning("sell %.*f is not found in local DB!!!"% (price_print_format, selling)) send_email("HB: " + "sell_order_ongoing error ", "sell %.*f is not found in local DB!!!"% (price_print_format, selling)) #sell_order_ongoing = False if order_timeout_checking > 1 : order_timeout_checking -= 1 else: order_timeout_checking = 0 printme("canceling sell order id %d"%orderid) cancel_order_and_restore_system(symbol, orderid) sell_order_ongoing = False selling = 0 if q_order == inc_order_flag: q_accu_inc += 1 if q_accu_inc == 1: q_accu_inc_start = get_last_second_value(q) elif q_accu_inc == 0: q_accu_inc_start = current_price qstr = "Q is ++ adiff:%.*f Qdiff:%.*f %.2f%%"%(price_print_format, current_price-q_accu_inc_start, price_print_format, q_diff, q_diff_percent) if just_buy_time_out == 0 and (q_accu_dec_start-current_price > accu_buy_val or buy_price > current_price + buy_space_times_if_cannot_meet_accu_buy*diff_space_buy_val) and already_buy_times < total_buy_times and not buy_order_ongoing and q_accu_dec >= steps_dec and (buy_price > current_price + diff_space_buy_val or buy_price==0): if (already_buy_times + last_several_times_restrict >= total_buy_times and (big_drop_flag or buy_price > current_price + buy_space_times_if_cannot_meet_big_drop*diff_space_buy_val)) or already_buy_times + last_several_times_restrict < total_buy_times: ret = handle_buy_order(symbol, current_price, usdt_amount) if ret: buy_order_ongoing = True buying = current_price order_timeout_checking = ORDER_TIMEOUT_LOOP_TIMES buy_policy_analyse(f_trade_done, "Q", q_accu_dec_start, big_drop_flag, current_price, already_buy_times, total_buy_times, buy_price, q_accu_dec, q_accu_inc, qstr, q, eq_accu_dec, eq_accu_inc, eqstr, eq,big_drop_flag_time_out, big_change_str, showgap, diff_space_sell_val, diff_space_sell, deal_space_val, deal_space, diff_space_buy_val, diff_space_buy, usdt_amount,accu_buy_val, accu_buy, price_list) elif already_buy_times + last_several_times_restrict >= total_buy_times and not big_drop_flag: output_to_log_file(f_trade_done, "CANCEL Q buy for last times %d/%d, no big drop. cur_price:%.*f diff:%.*f"%(already_buy_times, total_buy_times, price_print_format, current_price, price_print_format, buy_price - current_price)) if q_accu_inc >= 2: q_accu_dec = 0 elif q_order == dec_order_flag: q_accu_dec += 1 if q_accu_dec == 1: q_accu_dec_start = get_last_second_value(q) elif q_accu_dec == 0: q_accu_dec_start = current_price qstr = "Q is -- adiff:%.*f Qdiff:%.*f %.2f%%"%(price_print_format,current_price-q_accu_dec_start, price_print_format,q_diff, q_diff_percent) if not sell_order_ongoing and current_price-buy_price > diff_space_sell_val and buy_price: ret = handle_sell_order(symbol, current_price, usdt_amount, buy_price) if ret: sell_order_ongoing = True selling = current_price order_timeout_checking = ORDER_TIMEOUT_LOOP_TIMES sell_policy_analyse(f_trade_done, "Q", q_accu_inc_start, big_drop_flag, current_price, already_buy_times, total_buy_times, buy_price, q_accu_dec, q_accu_inc, qstr, q, eq_accu_dec, eq_accu_inc, eqstr, eq,big_drop_flag_time_out, big_change_str, showgap, diff_space_sell_val, diff_space_sell, deal_space_val, deal_space, diff_space_buy_val, diff_space_buy, usdt_amount,accu_buy_val, accu_buy, price_list) if q_accu_dec >= 2: q_accu_inc = 0 else: qstr = " Qdiff:%.*f %.2f%%"%(price_print_format,q_diff, q_diff_percent) if not sell_order_ongoing and current_price - buy_price > deal_space_val and buy_price: ret = handle_sell_order(symbol, current_price, usdt_amount, buy_price) if ret: sell_order_ongoing = True selling = current_price order_timeout_checking = ORDER_TIMEOUT_LOOP_TIMES if eq_accu_inc_start < q_accu_inc_start: inc_start = eq_accu_inc_start which_queue = "EQ" else: inc_start = q_accu_inc_start which_queue = "Q" sell_policy_analyse(f_trade_done, which_queue, inc_start, big_drop_flag, current_price, already_buy_times, total_buy_times, buy_price, q_accu_dec, q_accu_inc, qstr, q, eq_accu_dec, eq_accu_inc, eqstr, eq,big_drop_flag_time_out, big_change_str, showgap, diff_space_sell_val, diff_space_sell, deal_space_val, deal_space, diff_space_buy_val, diff_space_buy, usdt_amount,accu_buy_val, accu_buy, price_list) if eq_order == inc_order_flag: eq_accu_inc += 1 if eq_accu_inc == 1: eq_accu_inc_start = get_last_second_value(q) elif eq_accu_inc == 0: eq_accu_inc_start = current_price eqstr = "EQ is ++ adiff:%.*f EQdiff:%.*f %.2f%%" %(price_print_format, current_price-eq_accu_inc_start, price_print_format, eq_diff, eq_diff_percent) if just_buy_time_out == 0 and (eq_accu_dec_start-current_price > accu_buy_val or buy_price > current_price + buy_space_times_if_cannot_meet_accu_buy*diff_space_buy_val) and already_buy_times < total_buy_times and not buy_order_ongoing and eq_accu_dec >= steps_dec and (buy_price > current_price + diff_space_buy_val or buy_price==0): if (already_buy_times + last_several_times_restrict >= total_buy_times and (big_drop_flag or buy_price > current_price + buy_space_times_if_cannot_meet_big_drop*diff_space_buy_val)) or already_buy_times + last_several_times_restrict < total_buy_times: ret = handle_buy_order(symbol, current_price, usdt_amount) if ret: buy_order_ongoing = True buying = current_price order_timeout_checking = ORDER_TIMEOUT_LOOP_TIMES buy_policy_analyse(f_trade_done, "EQ", eq_accu_dec_start, big_drop_flag, current_price, already_buy_times, total_buy_times, buy_price, q_accu_dec, q_accu_inc, qstr, q, eq_accu_dec, eq_accu_inc, eqstr, eq,big_drop_flag_time_out, big_change_str, showgap, diff_space_sell_val, diff_space_sell, deal_space_val, deal_space, diff_space_buy_val, diff_space_buy, usdt_amount,accu_buy_val, accu_buy, price_list) elif already_buy_times + last_several_times_restrict >= total_buy_times and not big_drop_flag: output_to_log_file(f_trade_done, "CANCEL EQ buy for last times %d/%d, no big drop. cur_price:%.*f diff:%.*f"%(already_buy_times, total_buy_times, price_print_format, current_price, price_print_format, buy_price - current_price)) if eq_accu_inc >= 2: eq_accu_dec = 0 elif eq_order == dec_order_flag: eq_accu_dec += 1 if eq_accu_dec == 1: eq_accu_dec_start = get_last_second_value(q) elif eq_accu_dec == 0: eq_accu_dec_start = current_price eqstr = "EQ is -- adiff:%.*f EQdiff:%.*f %.2f%%"%(price_print_format, current_price-eq_accu_dec_start, price_print_format, eq_diff, eq_diff_percent) if not sell_order_ongoing and (current_price-buy_price) > diff_space_sell_val and buy_price: ret = handle_sell_order(symbol, current_price, usdt_amount, buy_price) if ret: sell_order_ongoing = True selling = current_price order_timeout_checking = ORDER_TIMEOUT_LOOP_TIMES sell_policy_analyse(f_trade_done, "EQ", eq_accu_inc_start, big_drop_flag, current_price, already_buy_times, total_buy_times, buy_price, q_accu_dec, q_accu_inc, qstr, q, eq_accu_dec, eq_accu_inc, eqstr, eq,big_drop_flag_time_out, big_change_str, showgap, diff_space_sell_val, diff_space_sell, deal_space_val, deal_space, diff_space_buy_val, diff_space_buy, usdt_amount,accu_buy_val, accu_buy, price_list) if eq_accu_dec >= 2: eq_accu_inc = 0 else: eqstr = " EQdiff:%.*f %.2f%%"%(price_print_format, eq_diff, eq_diff_percent) if not sell_order_ongoing and current_price-buy_price > deal_space_val and buy_price: ret = handle_sell_order(symbol, current_price, usdt_amount, buy_price) if ret: sell_order_ongoing = True selling = current_price order_timeout_checking = ORDER_TIMEOUT_LOOP_TIMES if eq_accu_inc_start < q_accu_inc_start: inc_start = eq_accu_inc_start which_queue = "EQ" else: inc_start = q_accu_inc_start which_queue = "Q" sell_policy_analyse(f_trade_done, which_queue, inc_start, big_drop_flag, current_price, already_buy_times, total_buy_times, buy_price, q_accu_dec, q_accu_inc, qstr, q, eq_accu_dec, eq_accu_inc, eqstr, eq,big_drop_flag_time_out, big_change_str, showgap, diff_space_sell_val, diff_space_sell, deal_space_val, deal_space, diff_space_buy_val, diff_space_buy, usdt_amount,accu_buy_val, accu_buy, price_list) showgap = "" if buy_price: showgap = "%.*f"%(price_print_format, current_price-buy_price) if buy_order_ongoing: printme("buying %.*f"%(price_print_format,buying)) if sell_order_ongoing: printme("selling %.*f"%(price_print_format,selling)) if qstr or eqstr: printme("--%d/++%d %s %r"%(q_accu_dec, q_accu_inc, qstr, q.queue)) printme("--%d/++%d %s %r"%(eq_accu_dec, eq_accu_inc, eqstr, eq.queue)) else: printme("cur_price=%.*f"%(price_print_format, current_price)) if big_drop_flag: printme("BIGDROP:%d %sgap:%s sell/deal=%.*f(%.3f%%)/%.*f(%.3f%%) buy_space=%.*f(%.3f%%) usdt:%d buy_times=%d/%d"%(big_drop_flag_time_out, big_change_str, showgap, price_print_format,diff_space_sell_val, diff_space_sell, price_print_format, deal_space_val, deal_space, price_print_format, diff_space_buy_val, diff_space_buy, usdt_amount,already_buy_times, total_buy_times)) else: printme("%sgap:%s sell/deal=%.*f(%.3f%%)/%.*f(%.3f%%) buy_space=%.*f(%.3f%%) usdt:%d buy_times=%d/%d"%(big_change_str, showgap, price_print_format, diff_space_sell_val, diff_space_sell, price_print_format, deal_space_val, deal_space, price_print_format, diff_space_buy_val, diff_space_buy, usdt_amount,already_buy_times, total_buy_times)) printme("cur_price=%.*f buy_timeout:%d accu_buy:%.*f/%.2f%% price list: %r"%(price_print_format, current_price, just_buy_time_out, price_print_format, accu_buy_val, accu_buy, price_list)) if minute_control == 1: minute_percent, five_percent, fifteen_percent, thirty_percent = cal_minute_change_percent(minute_price_q) printme("last_times:%d %s"%(last_several_times_restrict, minute_percent)) #if qstr or eqstr: printme("==================================================================================================================\n") lock.release() time.sleep(loop_time)
def callback(upd_event: 'OrderUpdateEvent'): print("---- order update : ----") #upd_event.print_object() try: b = __Autonomy__() current = sys.stdout sys.stdout = b upd_event.print_object() sys.stdout = current order_type = get_value("Order Type", b._buff) price = get_value("Trade Price", b._buff) order_state = get_value("Order State", b._buff) amount = get_value("Trade Volume", b._buff) if len(order_type): order_type = order_type[0] else: order_type = "" if len(order_state): order_state = order_state[0] else: order_state = "" if len(price): price = price[0] else: price = "" if len(amount): amount = amount[0] else: amount = "" print(b._buff) if order_type == "sell-limit": order_type = "sell" elif order_type == "buy-limit": order_type = "buy" if order_state == "filled": order_state = "done" elif order_state == "partial-filled": order_state = "part done" if price or amount: order_info = "%s, %s, price=%s, Volume=%s" % ( order_type, order_state, price, amount) else: order_info = "%s, %s" % (order_type, order_state) print(order_info) current = sys.stdout a = __Autonomy__() sys.stdout = a print(b._buff) print("\n===== check SUBMITTED order =====\n") get_orders(['eth3lusdt', 'dogeusdt', 'ethusdt']) sys.stdout = current if order_type == "sell" and order_state != "submitted" and order_state != "canceled": send_email("HB: " + order_info, a._buff) elif order_type == "buy" and order_state != "submitted" and order_state != "canceled": send_email("HB: " + order_info, a._buff) print() except: print("ExecuteError")