def on_message(ws, message): global sell_price, sell_times message = bytes.decode(inflate(message), 'utf-8') # data decompress if 'pong' in message or 'addChannel' in message: return jmessage = json.loads(message) for each_message in jmessage: data = each_message['data'] asks = data['asks'][::-1] bids = data['bids'] ask_price = float(asks[0][0]) bid_price = float(bids[0][0]) if ask_price < sell_price: spotAPI.revoke_order(instrument_id, old_order_id) #下单 if ask_price >= bid_price * 1.0008: new_order_id = spot_sell(spotAPI, instrument_id, amount, ask_price - 0.0001) if new_order_id: old_order_id = new_order_id sell_price = ask_price - 0.0001 sell_times += 1 bid_info = 'ask_price: %.4f, sell_price: %.4f, sell_times: %d' % ( ask_price, sell_price, sell_times) print(bid_info)
buy_price = float(ret['best_bid']) + 0.0001 sell_price = float(ret['best_ask']) - 0.0001 if status == 'filled': print("order %s 已完全成交" % str(old_order_id)) del_list.append(old_order_id) side = order_info['side'] if side == 'buy': more = 1 elif side == 'sell': more = 0 elif status == 'cancelled': del_list.append(old_order_id) elif int(ts) > string2timestamp( order_info['timestamp']) + 10 + 8 * 3600: spotAPI.revoke_order(instrument_id, old_order_id) for del_id in del_list: order_id_queue.remove(del_id) del_list = [] time.sleep(1) if int(ts) - last_minute_ts > 10: last_minute_ts = int(ts) if more == 1: print("持有做多单") else: print("未持有单") print("calculating macd...") try: df = get_spot_macd(spotAPI, instrument_id, 60)
if status == 'filled': print("order %s 已完全成交" % str(old_order_id)) del_list.append(old_order_id) if side == 'buy': more = 1 elif side == 'sell': more = 0 elif status == 'part_filled': if side == 'buy': more = 1 elif status == 'cancelled': del_list.append(old_order_id) elif int(ts) - (string2timestamp(order_info['timestamp']) + 8 * 3600) >= 3: print('撤单重挂,ts:%s, 真实时间:%s' % (timestamp2string(ts), timestamp2string(time.time()))) try: print('撤单结果: %s' % spotAPI.revoke_order(instrument_id, old_order_id)) except Exception as e: print(repr(e)) traceback.print_exc() del_list.append(old_order_id) continue ret = spotAPI.get_specific_ticker(instrument_id) print('当前行情: %s' % ret) if side == 'buy': buy_price = float(ret['best_ask']) try: order_id = buy_all_position(spotAPI, instrument_id, buy_price) if order_id: order_id_queue.append(order_id) except Exception as e: print(repr(e))