def on_message(ws, message): global buy_price, buy_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 bid_price > buy_price: spotAPI.revoke_order(instrument_id, old_order_id) #下单 if ask_price >= bid_price * 1.0008: new_order_id = spot_buy(spotAPI, instrument_id, amount, bid_price + 0.0001) if new_order_id: old_order_id = new_order_id buy_price = bid_price + 0.0001 buy_times += 1 bid_info = 'bids price: %.4f, buy_price: %.4f, buy_times: %d' % ( bid_price, buy_price, buy_times) print(bid_info)
def on_message(ws, message): global buy_price, sell_price, buy_queue, sell_queue, bid_price, ask_price, last_time_sec, latest_deal_price, last_time_account_sec message = bytes.decode(inflate(message), 'utf-8') # data decompress if 'pong' in message or 'addChannel' in message: return jmessage = json.loads(message) each_message = jmessage[0] channel = each_message['channel'] now_time_sec = float(time.time()) if channel == 'ok_sub_spot_%s_usdt_depth_5' % coin_name: data = each_message['data'] asks = data['asks'][::-1] bids = data['bids'] ask_price = float(asks[0][0]) bid_price = float(bids[0][0]) ask_price_2 = float(asks[1][0]) bid_price_2 = float(bids[1][0]) mid_price = (ask_price + bid_price) / 2 elif channel == ('ok_sub_spot_%s_usdt_deals' % coin_name): jdata = each_message['data'][0] latest_deal_price = float(jdata[1]) mid_price = (ask_price + bid_price) / 2 deal_entity = DealEntity(jdata[0], float(jdata[1]), round(float(jdata[2]), 3), now_time_sec, jdata[4]) handle_deque(deque_3s, deal_entity, now_time_sec, ind_3s) avg_3s_price = ind_3s.cal_avg_price() price_3s_change = cal_rate(latest_deal_price, avg_3s_price) spot_maker.timeLog( "最新成交价: %.4f, 中间价: %.4f, 买一价: %.4f, 卖一价: %.4f, 3秒平均价: %.4f, 波动: %.3f%%" % (latest_deal_price, mid_price, bid_price, ask_price, avg_3s_price, price_3s_change)) if price_3s_change > 0.03 or price_3s_change < -0.03: return elif latest_deal_price >= bid_price * 1.0003 and ask_price >= latest_deal_price * 1.0003: buy_price = bid_price + 0.0001 sell_price = ask_price - 0.0001 elif ask_price > bid_price * 1.0006: buy_price = bid_price + 0.0001 sell_price = ask_price - 0.0001 else: # 不操作 return try: buy_order_id = spot_buy(spotAPI, instrument_id, amount, buy_price) except Exception as e: buy_order_id = False traceback.print_exc() try: sell_order_id = spot_sell(spotAPI, instrument_id, amount, sell_price) except Exception as e: sell_order_id = False traceback.print_exc()
def process_message(message): global latest_price, last_avg_price, less, deque_3s, deque_10s, deque_min, future_buy_price,\ deque_3m, ind_3s, ind_10s, ind_1min, ind_3m, write_lines, last_3min_macd_ts, new_macd, lessless,\ future_buy_time, spot_buy_time, spot_sell_price, spot_buy_price, lessmore, future_more_buy_price ts = time.time() now_time = timestamp2string(ts) for each_message in message['data']: latest_price = float(each_message['price']) trade_id = each_message['trade_id'] size = each_message['size'] side = each_message['side'] deal_entity = DealEntity(trade_id, latest_price, size, ts, side) print('detail:' + deal_entity.detail()) handle_deque(deque_3s, deal_entity, ts, ind_1s) handle_deque(deque_10s, deal_entity, ts, ind_10s) handle_deque(deque_min, deal_entity, ts, ind_1min) handle_deque(deque_3m, deal_entity, ts, ind_3m) avg_3s_price = ind_1s.cal_avg_price() avg_10s_price = ind_10s.cal_avg_price() avg_min_price = ind_1min.cal_avg_price() avg_3m_price = ind_3m.cal_avg_price() price_10s_change = cal_rate(avg_3s_price, avg_10s_price) price_1m_change = cal_rate(avg_3s_price, avg_min_price) price_3m_change = cal_rate(avg_3s_price, avg_3m_price) # 做空 if less == 0 and check_do_future_less(price_3m_change, price_1m_change, price_10s_change): sell_id = sell_all_position(spotAPI, instrument_id, latest_price - 0.001) if sell_id: spot_buy_time = int(ts) time.sleep(1) sell_order_info = spotAPI.get_order_info( sell_id, instrument_id) if sell_order_info['status'] == 'filled' or sell_order_info[ 'status'] == 'part_filled': less = 1 spot_sell_price = float(sell_order_info['price']) info = now_time + u' 现货全部卖出!!!spot_sell_price:' + str( spot_sell_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: spotAPI.revoke_order_exception(instrument_id, sell_id) if less == 1: if price_1m_change > 0 and new_macd > 0: usdt_account = spotAPI.get_coin_account_info("usdt") usdt_available = float(usdt_account['available']) amount = math.floor(usdt_available / latest_price) if amount > 0: buy_id = spot_buy(spotAPI, instrument_id, amount, latest_price) if buy_id: time.sleep(3) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'filled': less = 0 spot_buy_price = order_info['price'] info = u'macd > 0, 买入现货止盈!!!买入价格:' + str( spot_buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: attempts = 5 while attempts > 0: attempts -= 1 spotAPI.revoke_order_exception( instrument_id, buy_id) time.sleep(1) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'cancelled': break else: less = 0 if int(ts) - spot_buy_time > 60: if latest_price > spot_sell_price and price_1m_change >= 0 and price_10s_change >= 0 \ and (ind_1min.bid_vol > ind_1min.ask_vol or price_3m_change >= 0): usdt_account = spotAPI.get_coin_account_info("usdt") usdt_available = float(usdt_account['available']) amount = math.floor(usdt_available / latest_price) if amount > 0: buy_id = spot_buy(spotAPI, instrument_id, amount, latest_price) if buy_id: time.sleep(3) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'filled': less = 0 spot_buy_price = order_info['price'] info = u'macd > 0, 买入现货止损!!!买入价格:' + str( spot_buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: attempts = 5 while attempts > 0: attempts -= 1 spotAPI.revoke_order_exception( instrument_id, buy_id) time.sleep(1) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'cancelled': break else: less = 0 holding_status = 'spot_less: %d' % less price_info = deal_entity.type + u' now_price: %.4f, 3s_price: %.4f, 10s_price: %.4f, 1m_price: %.4f, ' \ u'3min_price: %.4f' \ % (latest_price, avg_3s_price, avg_10s_price, avg_min_price, avg_3m_price) vol_info = u'cur_vol: %.3f, 3s vol: %.3f, 10s vol: %.3f, 1min vol: %.3f, ask_vol: %.3f, bid_vol: %.3f, ' \ u'3s_ask_vol: %.3f, 3s_bid_vol: %.3f, 3min vol: %.3f, 3min_ask_vol: %.3f, 3min_bid_vol: %.3f' \ % (deal_entity.amount, ind_1s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol, ind_1s.ask_vol, ind_1s.bid_vol, ind_3m.vol, ind_3m.ask_vol, ind_3m.bid_vol) rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 3min_rate: %.2f%%, new_macd: %.6f' \ % (price_10s_change, price_1m_change, price_3m_change, new_macd) write_info = holding_status + u', ' + price_info + u', ' + vol_info + u', ' + rate_info + u', ' + now_time + '\r\n' write_lines.append(write_info) if len(write_lines) >= 100: with codecs.open(file_deal, 'a+', 'UTF-8') as f: f.writelines(write_lines) write_lines = [] print(holding_status + '\r\n' + price_info + '\r\n' + vol_info + '\r\n' + rate_info + u', ' + now_time)
def on_message(ws, message): message = bytes.decode(inflate(message), 'utf-8') # data decompress if 'pong' in message or 'addChannel' in message: return global latest_price, less, deque_3s, deque_10s, deque_min, future_buy_price,\ deque_3m, ind_1s, ind_10s, ind_1min, ind_3m, write_lines, last_3min_macd_ts, new_macd, lessless,\ future_buy_time, spot_buy_time, spot_sell_price, spot_buy_price jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) if int(ts) - last_3min_macd_ts > 60: last_3min_macd_ts = int(ts) df = get_spot_macd(spotAPI, instrument_id, 300) diff = list(df['diff']) dea = list(df['dea']) new_macd = 2 * (diff[-1] - dea[-1]) with codecs.open(file_deal, 'a+', 'UTF-8') as f: f.writelines('update macd: %.6f\r\n' % new_macd) for each_message in jmessage: for jdata in each_message['data']: latest_price = float(jdata[1]) deal_entity = DealEntity(jdata[0], float(jdata[1]), round(float(jdata[2]), 3), ts, jdata[4]) handle_deque(deque_3s, deal_entity, ts, ind_1s) handle_deque(deque_10s, deal_entity, ts, ind_10s) handle_deque(deque_min, deal_entity, ts, ind_1min) handle_deque(deque_3m, deal_entity, ts, ind_3m) avg_3s_price = ind_1s.cal_avg_price() avg_10s_price = ind_10s.cal_avg_price() avg_min_price = ind_1min.cal_avg_price() avg_3m_price = ind_3m.cal_avg_price() price_10s_change = cal_rate(avg_3s_price, avg_10s_price) price_1m_change = cal_rate(avg_3s_price, avg_min_price) price_3m_change = cal_rate(avg_3s_price, avg_3m_price) # 做空 if lessless == 0 and ind_3m.vol > 500000 and ind_3m.ask_vol > 1.3 * ind_3m.bid_vol \ and ind_1min.vol > 300000 and ind_1min.ask_vol > 1.5 * ind_1min.bid_vol and -1.2 < price_1m_change \ and price_3m_change < price_1m_change < -0.4 and price_10s_change <= -0.05 and new_macd < 0: future_buyin_less_order_id = buyin_less(futureAPI, coin.name, future_instrument_id, latest_price, amount=None, lever_rate=20, taker=True) if future_buyin_less_order_id: lessless = 1 future_buy_time = int(ts) thread.start_new_thread(ensure_buyin_less, ( futureAPI, coin.name, future_instrument_id, latest_price, future_buyin_less_order_id, )) future_buy_price = latest_price - 0.01 info = u'发出做空信号!!买入时间: ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') if less == 0 and ind_3m.vol > 500000 and ind_3m.ask_vol > 1.3 * ind_3m.bid_vol \ and ind_1min.vol > 300000 and ind_1min.ask_vol > 1.5 * ind_1min.bid_vol and -1.2 < price_1m_change \ and price_3m_change < price_1m_change < -0.4 and price_10s_change <= -0.05 and new_macd < 0: sell_id = sell_all_position(spotAPI, instrument_id, latest_price - 0.001) if sell_id: spot_buy_time = int(ts) time.sleep(1) sell_order_info = spotAPI.get_order_info( sell_id, instrument_id) if sell_order_info['status'] == 'filled' or sell_order_info[ 'status'] == 'part_filled': less = 1 spot_sell_price = float(sell_order_info['price']) info = u'现货全部卖出!!!平均成交价格:' + spot_sell_price + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: spotAPI.revoke_order_exception(instrument_id, sell_id) if lessless == 1: if price_1m_change > 0 and new_macd > 0: if sell_less(futureAPI, future_instrument_id): lessless = 0 thread.start_new_thread( ensure_sell_less, (futureAPI, coin.name, future_instrument_id, latest_price, future_buy_price)) info = u'做空止盈,盈利%.3f, time: %s' % ( future_buy_price - latest_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif int(ts) - future_buy_time >= 60: if latest_price > spot_sell_price and price_3m_change >= 0 and price_1m_change >= 0 and price_10s_change >= 0: if sell_less(futureAPI, future_instrument_id): lessless = 0 thread.start_new_thread( ensure_sell_less, (futureAPI, coin.name, future_instrument_id, latest_price, future_buy_price)) info = u'做空止损,亏损%.2f, time: %s' % ( (latest_price - spot_buy_price), now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif latest_price > spot_sell_price * 1.01: if sell_less(futureAPI, future_instrument_id): lessless = 0 thread.start_new_thread( ensure_sell_less, (futureAPI, coin.name, future_instrument_id, latest_price, future_buy_price)) info = u'做空止损,亏损%.2f, time: %s' % ( (latest_price - spot_sell_price), now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') if less == 1: if price_1m_change > 0 and new_macd > 0: usdt_account = spotAPI.get_coin_account_info("usdt") usdt_available = float(usdt_account['available']) amount = math.floor(usdt_available / latest_price) if amount > 0: buy_id = spot_buy(spotAPI, instrument_id, amount, latest_price) if buy_id: time.sleep(3) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'filled': less = 0 spot_buy_price = order_info['price'] info = u'macd > 0, 买入现货止盈!!!买入价格:' + str( spot_buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: attempts = 5 while attempts > 0: attempts -= 1 spotAPI.revoke_order_exception( instrument_id, buy_id) time.sleep(1) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'cancelled': break else: less = 0 if int(ts) - spot_buy_time > 60: if latest_price > spot_sell_price and price_1m_change > 0 and price_3m_change > 0: usdt_account = spotAPI.get_coin_account_info("usdt") usdt_available = float(usdt_account['available']) amount = math.floor(usdt_available / latest_price) if amount > 0: buy_id = spot_buy(spotAPI, instrument_id, amount, latest_price) if buy_id: time.sleep(3) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'filled': less = 0 apot_buy_price = order_info['price'] info = u'macd > 0, 买入现货止盈!!!买入价格:' + str( apot_buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: attempts = 5 while attempts > 0: attempts -= 1 spotAPI.revoke_order_exception( instrument_id, buy_id) time.sleep(1) order_info = spotAPI.get_order_info( buy_id, instrument_id) if order_info['status'] == 'cancelled': break else: less = 0 holding_status = 'future_less: %d, spot_less: %d' % (lessless, less) price_info = deal_entity.type + u' now_price: %.4f, 3s_price: %.4f, 10s_price: %.4f, 1m_price: %.4f, ' \ u'3min_price: %.4f' \ % (latest_price, avg_3s_price, avg_10s_price, avg_min_price, avg_3m_price) vol_info = u'cur_vol: %.3f, 3s vol: %.3f, 10s vol: %.3f, 1min vol: %.3f, ask_vol: %.3f, bid_vol: %.3f, ' \ u'3s_ask_vol: %.3f, 3s_bid_vol: %.3f, 3min vol: %.3f, 3min_ask_vol: %.3f, 3min_bid_vol: %.3f' \ % (deal_entity.amount, ind_1s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol, ind_1s.ask_vol, ind_1s.bid_vol, ind_3m.vol, ind_3m.ask_vol, ind_3m.bid_vol) rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 3min_rate: %.2f%%, new_macd: %.6f' \ % (price_10s_change, price_1m_change, price_3m_change, new_macd) write_info = holding_status + u', ' + price_info + u', ' + vol_info + u', ' + rate_info + u', ' + now_time + '\r\n' write_lines.append(write_info) if len(write_lines) >= 100: with codecs.open(file_deal, 'a+', 'UTF-8') as f: f.writelines(write_lines) write_lines = [] print(holding_status + '\r\n' + price_info + '\r\n' + vol_info + '\r\n' + rate_info + u', ' + now_time)
def on_message(ws, message): global buy_price, sell_price, buy_queue, sell_queue, bid_price, ask_price, last_time_sec,latest_deal_price,last_time_account_sec message = bytes.decode(inflate(message), 'utf-8') # data decompress if 'pong' in message or 'addChannel' in message: return jmessage = json.loads(message) each_message = jmessage[0] channel = each_message['channel'] now_time_sec = float(time.time()) if channel == 'ok_sub_spot_%s_usdt_depth_5' % coin_name: data = each_message['data'] asks = data['asks'][::-1] bids = data['bids'] ask_price = float(asks[0][0]) bid_price = float(bids[0][0]) ask_price_2 = float(asks[1][0]) bid_price_2 = float(bids[1][0]) mid_price = (ask_price + bid_price) / 2 if now_time_sec > last_time_sec + 0.2: last_time_sec = now_time_sec print('撤单前sell_queue length: ', len(sell_queue)) new_queue = [] for order in sell_queue: is_revoke_suc = spot_maker.revoke_order(order.order_id, now_time_sec) if is_revoke_suc: if latest_deal_price >= ask_price * 0.9999: # 上涨行情, 追加买单 buy_price = mid_price sell_price = max(buy_price * 1.001, ask_price_2) buy_order_id = spot_maker.take_buy_order(2 * amount, buy_price) if buy_order_id: new_buy_order = Order(buy_order_id, buy_price, amount, 'buy', now_time_sec) buy_queue.append(new_buy_order) spot_maker.timeLog("挂买入单成功,时间:%s, 价格: %.4f, order_id: %s" % ( timestamp2string(now_time_sec), buy_price, buy_order_id)) continue else: sell_price = ask_price - 0.0001 if ask_price > bid_price * 1.0006: sell_order_id = spot_maker.take_sell_order(amount, sell_price) if sell_order_id: new_sell_order = Order(sell_order_id, sell_price, amount, 'sell', timestamp2string(now_time_sec)) new_queue.append(new_sell_order) spot_maker.timeLog("挂卖出单成功,时间:%s, 价格: %.4f, order_id: %s" % ( timestamp2string(now_time_sec), sell_price, sell_order_id)) else: # 撤单失败,retry spot_maker.timeLog('%s撤单失败,重试') spot_maker.revoke_order(order.order_id, now_time_sec) sell_queue = copy.deepcopy(new_queue) new_queue = [] print('撤单后sell_queue length: ', len(sell_queue)) print('撤单前buy_queue length: ', len(buy_queue)) for order in buy_queue: if spot_maker.revoke_order(order.order_id, now_time_sec): if latest_deal_price <= bid_price * 1.0001: # 下跌行情, 追加卖单 sell_price = mid_price buy_price = min(sell_price * 0.999, bid_price_2) sell_order_id = spot_maker.take_sell_order(amount, sell_price) if sell_order_id: new_sell_order = Order(sell_order_id, sell_price, 2 * amount, 'sell', timestamp2string(now_time_sec)) sell_queue.append(new_sell_order) spot_maker.timeLog("挂卖出单成功,时间:%s, 价格: %.4f, order_id: %s" % ( timestamp2string(now_time_sec), sell_price, sell_order_id)) continue else: buy_price = bid_price + 0.0001 if ask_price > bid_price * 1.0006: buy_order_id = spot_maker.take_buy_order(amount, buy_price) if buy_order_id: new_buy_order = Order(buy_order_id, buy_price, amount, 'buy', now_time_sec) new_queue.append(new_buy_order) spot_maker.timeLog("挂买入单成功,时间:%s, 价格: %.4f, order_id: %s" % ( timestamp2string(now_time_sec), buy_price, buy_order_id)) buy_queue = copy.deepcopy(new_queue) print('撤单后buy_queue length: ', len(buy_queue)) elif channel == ('ok_sub_spot_%s_usdt_deals' % coin_name): jdata = each_message['data'][0] latest_deal_price = float(jdata[1]) mid_price = (ask_price + bid_price) / 2 deal_entity = DealEntity(jdata[0], float(jdata[1]), round(float(jdata[2]), 3), now_time_sec, jdata[4]) handle_deque(deque_3s, deal_entity, now_time_sec, ind_3s) avg_3s_price = ind_3s.cal_avg_price() if now_time_sec > last_time_account_sec + 60: last_time_account_sec = now_time_sec spot_maker.get_account_money(coin_name) spot_maker.delete_overdue_order(latest_deal_price) price_3s_change = cal_rate(latest_deal_price, avg_3s_price) spot_maker.timeLog("最新成交价: %.4f, 中间价: %.4f, 买一价: %.4f, 卖一价: %.4f, 3秒平均价: %.4f, 波动: %.3f%%" % (latest_deal_price, mid_price, bid_price, ask_price, avg_3s_price, price_3s_change)) if price_3s_change > 0.03 or price_3s_change < -0.03: return elif latest_deal_price > bid_price * 1.0002 and ask_price > latest_deal_price * 1.0002 \ and buy_price != bid_price + 0.0001 and sell_price != ask_price - 0.0001: buy_price = bid_price + 0.0001 sell_price = ask_price - 0.0001 elif ask_price > bid_price * 1.0006 and buy_price != bid_price + 0.0001 and sell_price != ask_price - 0.0001: if latest_deal_price < mid_price: buy_price = bid_price + 0.0001 sell_price = buy_price * 1.0005 else: sell_price = ask_price - 0.0001 buy_price = ask_price * 0.9995 else: # 不操作 return try: buy_order_id = spot_buy(spotAPI, instrument_id, amount, buy_price) except Exception as e: buy_order_id = False traceback.print_exc() try: sell_order_id = spot_sell(spotAPI, instrument_id, amount, sell_price) except Exception as e: sell_order_id = False traceback.print_exc() if buy_order_id: buy_order = Order(buy_order_id, buy_price, amount, 'buy', timestamp2string(now_time_sec)) buy_queue.append(buy_order) spot_maker.timeLog("挂买入单成功,时间:%s, 价格: %.4f, order_id: %s" % ( timestamp2string(time.time()), buy_price, buy_order_id)) if sell_order_id: sell_order = Order(sell_order_id, sell_price, amount, 'sell', timestamp2string(now_time_sec)) sell_queue.append(sell_order) spot_maker.timeLog("挂卖出单成功,时间:%s, 价格: %.4f, order_id: %s" % ( timestamp2string(time.time()), sell_price, sell_order_id))