def process_pending_orders(self): print('当前订单队列共有%d个订单' % len(self.order_queue)) del_list = [] for i in range(len(self.order_queue)): old_order_id = self.order_queue[i] try: old_order_info = futureAPI.get_order_info( old_order_id, self.instrument_id) print('第%d个订单信息为:%s' % (i + 1, old_order_info)) status = old_order_info['status'] type = int(old_order_info['type']) last = float(old_order_info['price']) # 已撤单 if status == '-1': del_list.append(old_order_id) # 全部成交 elif status == '2': del_list.append(old_order_id) # 部分成交或等待成交 else: futureAPI.revoke_order(self.instrument_id, old_order_id) amount = int(old_order_info['size']) filled_amt = int(old_order_info['filled_qty']) unfilled = amount - filled_amt if unfilled >= 1: if type == 1: buyID = buyin_more(futureAPI, self.coin_name, self.instrument_id, False, unfilled) elif type == 2: buyID = buyin_less(futureAPI, self.coin_name, self.instrument_id, False, unfilled) else: continue if buyID: self.order_queue.append(buyID) else: del_list.append(old_order_id) except Exception as e: print(repr(e)) traceback.print_exc() del_list.append(old_order_id) continue # 删除已成交和已撤销订单 for to_del_id in del_list: self.order_queue.remove(to_del_id)
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, last_avg_price, buy_price, more, less, deque_3s, deque_10s, deque_min, \ deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, last_5min_macd, last_5min_macd_ts, moreless jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) if int(ts) - int(last_5min_macd_ts) >= 60: last_5min_macd_ts = int(ts) print(ts, last_5min_macd_ts) if more == 0 and less == 0 and moreless == 0: ret = futureAPI.get_specific_position(time_type) print(ret) if len(ret["holding"]) > 0: buy_available = int(ret["holding"][0]["long_avail_qty"]) sell_available = int(ret["holding"][0]["short_avail_qty"]) if buy_available > 0: thread.start_new_thread(ensure_sell_more, ( futureAPI, coin.name, time_type, latest_price, buy_price, )) if sell_available > 0: thread.start_new_thread(ensure_sell_less, ( futureAPI, coin.name, time_type, latest_price, buy_price, )) else: print("确认未持仓") 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_3s) handle_deque(deque_10s, deal_entity, ts, ind_10s) handle_deque(deque_min, deal_entity, ts, ind_1min) handle_deque(deque_5m, deal_entity, ts, ind_5m) # price_5m_ago = cal_price_ago(deque_5m) # print("price_5m_ago: %.4f" % price_5m_ago) avg_3s_price = ind_3s.cal_avg_price() avg_10s_price = ind_10s.cal_avg_price() avg_min_price = ind_1min.cal_avg_price() avg_5m_price = ind_5m.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_5m_change = cal_rate(avg_3s_price, avg_5m_price) # price_5m_ago_change = cal_rate(latest_price, price_5m_ago) if more == 1: # 盈利中,放宽卖出条件,盈利最大化 if latest_price > buy_price: if price_1m_change <= -0.1 and price_5m_change <= 0: if sell_more_batch(futureAPI, time_type, latest_price): more = 0 thread.start_new_thread(ensure_sell_more, ( futureAPI, coin.name, time_type, latest_price, buy_price, )) else: if price_1m_change <= -0.2 or price_5m_change <= 0: if sell_more_batch(futureAPI, time_type, latest_price): more = 0 thread.start_new_thread(ensure_sell_more, ( futureAPI, coin.name, time_type, latest_price, buy_price, )) elif less == 1: # 盈利中,放宽卖出条件,盈利最大化 if latest_price < buy_price: if price_1m_change >= 0.1 and price_5m_change >= 0: if sell_less_batch(futureAPI, time_type, latest_price): less = 0 thread.start_new_thread(ensure_sell_less, ( futureAPI, coin.name, time_type, latest_price, buy_price, )) else: if price_1m_change >= 0.2 or price_5m_change >= 0: if sell_less_batch(futureAPI, time_type, latest_price): less = 0 thread.start_new_thread(ensure_sell_less, ( futureAPI, coin.name, time_type, latest_price, buy_price, )) if price_1m_change >= 0.2 and price_5m_change >= 0.3: btc_change = coin_recent_change_rate("btc") eth_change = coin_recent_change_rate("eth") print('btc_change: %.2f%%, eth change: %.2f%%' % (btc_change, eth_change)) write_lines.append( 'btc_change: %.2f%%, eth change: %.2f%% \r\n' % (btc_change, eth_change)) if btc_change >= 0.5 and eth_change >= 0.5: if buyin_more(futureAPI, coin.name, time_type, latest_price + 0.01): more = 1 thread.start_new_thread(ensure_buyin_more, ( futureAPI, coin.name, time_type, latest_price, )) buy_price = latest_price info = u'发出做多信号!!!买入价格:' + str( buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif price_1m_change <= -0.2 and price_5m_change <= -0.3: btc_change = coin_recent_change_rate("btc") eth_change = coin_recent_change_rate("eth") print('btc_change: %.2f%%, eth change: %.2f%%' % (btc_change, eth_change)) write_lines.append( 'btc_change: %.2f%%, eth change: %.2f%% \r\n' % (btc_change, eth_change)) if btc_change <= -0.6 and eth_change <= -0.6: if buyin_less(futureAPI, coin.name, time_type, latest_price - 0.01): less = 1 thread.start_new_thread(ensure_buyin_less, ( futureAPI, coin.name, time_type, latest_price, )) buy_price = latest_price info = u'发出做空信号!!!买入价格:' + str( buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') last_avg_price = latest_price price_info = deal_entity.type + u' now_price: %.4f, 3s_price: %.4f, 10s_price: %.4f, 1m_price: %.4f, ' \ u'5min_price: %.4f' \ % (latest_price, avg_3s_price, avg_10s_price, avg_min_price, avg_5m_price) vol_info = u'cur_vol: %.3f, 3s vol: %.3f, 10s vol: %.3f, 1min vol: %.3f, ask_vol: %.3f, bid_vol: %.3f, 3s_ask_vol: %.3f, 3s_bid_vol: %.3f' \ % (deal_entity.amount, ind_3s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol, ind_3s.ask_vol, ind_3s.bid_vol) rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 5min_rate: %.2f%%' \ % (price_10s_change, price_1m_change, price_5m_change) write_info = 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(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, last_avg_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, lessmore, future_more_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 check_do_future_less( price_3m_change, price_1m_change, price_10s_change): latest_future_price = get_latest_future_price( futureAPI, future_instrument_id) if not latest_future_price: latest_future_price = latest_price future_buyin_less_order_id = buyin_less(futureAPI, coin.name, future_instrument_id, latest_future_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_future_price, future_buyin_less_order_id, )) future_buy_price = latest_price - 0.01 info = now_time + u' 发出做空信号!!future_buy_price: ' + str( future_buy_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') 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 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') time.sleep(5) # do more retry = 10 while retry > 0: retry -= 1 latest_future_price = get_latest_future_price( futureAPI, future_instrument_id) if not latest_future_price: latest_future_price = latest_price future_buyin_more_order_id = buyin_more( futureAPI, coin.name, future_instrument_id, latest_future_price, amount=None, lever_rate=20, taker=True) if future_buyin_more_order_id: lessmore = 1 thread.start_new_thread( ensure_buyin_more, ( futureAPI, coin.name, future_instrument_id, latest_future_price, future_buyin_more_order_id, )) future_more_buy_price = latest_price info = u'%s 反手做多,现货价格: %.3f' % (now_time, latest_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') break time.sleep(0.5) elif int(ts ) - future_buy_time >= 60 and price_10s_change > 0.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_buy_price), now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif latest_price > future_buy_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_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 if lessmore == 1: if new_macd < 0 or latest_price < future_more_buy_price * 0.995: if sell_more(futureAPI, future_instrument_id): lessmore = 0 thread.start_new_thread( ensure_sell_more, (futureAPI, coin.name, future_instrument_id, latest_price, future_more_buy_price)) info = u'做多止盈,盈利%.3f, time: %s' % ( future_more_buy_price - latest_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') holding_status = 'future_less: %d, spot_less: %d, future_more: %d' % ( lessless, less, lessmore) 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, last_avg_price, less, deque_3s, deque_10s, deque_min,\ deque_3m, ind_1s, ind_10s, ind_1min, ind_3m, write_lines, last_3min_macd_ts, new_macd, lessless,\ future_buy_time, buyin_price_spot, moremore, freeze_time 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 int( ts) - freeze_time > 180 and check_do_future_less( price_3m_change, price_1m_change, price_10s_change): latest_future_price = get_latest_future_price( futureAPI, future_instrument_id) if not latest_future_price: latest_future_price = latest_price future_buyin_less_order_id = buyin_less(futureAPI, coin.name, future_instrument_id, latest_future_price, amount=None, lever_rate=20, taker=True) if future_buyin_less_order_id: lessless = 1 future_buy_time = int(ts) buyin_price_spot = latest_price thread.start_new_thread(ensure_buyin_less, ( futureAPI, coin.name, future_instrument_id, latest_future_price, future_buyin_less_order_id, )) info = now_time + u' 发出做空信号!!future_buy_price: ' + str( latest_future_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') if moremore == 0 and int( ts) - freeze_time > 180 and check_do_future_more( price_3m_change, price_1m_change, price_10s_change): latest_future_price = get_latest_future_price( futureAPI, future_instrument_id) if not latest_future_price: latest_future_price = latest_price future_buyin_more_order_id = buyin_more(futureAPI, coin.name, future_instrument_id, latest_future_price, amount=None, lever_rate=20, taker=True) if future_buyin_more_order_id: moremore = 1 future_buy_time = int(ts) buyin_price_spot = latest_price thread.start_new_thread(ensure_buyin_more, ( futureAPI, coin.name, future_instrument_id, latest_future_price, future_buyin_more_order_id, )) info = now_time + u' 发出做多信号!!future_buy_price: ' + str( latest_future_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') if moremore == 1: if int(ts) - future_buy_time >= 60 and price_10s_change < -0.03: if sell_more(futureAPI, future_instrument_id): moremore = 0 thread.start_new_thread( ensure_sell_more, (futureAPI, coin.name, future_instrument_id, latest_price, buyin_price_spot)) info = u'做多止盈,盈利%.3f, time: %s' % ( latest_price - buyin_price_spot, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') freeze_time = int(ts) elif latest_price < buyin_price_spot * 0.99: if sell_more(futureAPI, future_instrument_id): moremore = 0 thread.start_new_thread( ensure_sell_more, (futureAPI, coin.name, future_instrument_id, latest_price, buyin_price_spot)) info = u'做多止损,亏损%.3f, time: %s' % ( buyin_price_spot - latest_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') freeze_time = int(ts) 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, buyin_price_spot)) freeze_time = int(ts) info = u'做空止盈,盈利%.3f, time: %s' % ( buyin_price_spot - 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 and price_10s_change > 0.03: if sell_less(futureAPI, future_instrument_id): lessless = 0 thread.start_new_thread( ensure_sell_less, (futureAPI, coin.name, future_instrument_id, latest_price, buyin_price_spot)) freeze_time = int(ts) info = u'做空止盈,盈利%.2f, time: %s' % ( (latest_price - buyin_price_spot), now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif latest_price > buyin_price_spot * 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, buyin_price_spot)) freeze_time = int(ts) info = u'做空止损,亏损%.2f, time: %s' % ( (latest_price - buyin_price_spot), now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') holding_status = 'future_less: %d, future_more: %d' % (lessless, moremore) 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, buy_price, more, less, write_lines, last_5min_macd_ts, \ btc_30s_change, eth_30s_change, ltc_30s_change, etc_30s_change, eos_30s_change, xrp_30s_change, \ btc_5min_change, eth_5min_change, ltc_5min_change, etc_5min_change, eos_5min_change, xrp_5min_change jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) try: if int(ts) - int(last_5min_macd_ts) >= 60: last_5min_macd_ts = int(ts) print(ts, last_5min_macd_ts) if more == 0 and less == 0: ret = futureAPI.get_specific_position(time_type) print(ret) if len(ret["holding"]) > 0: buy_available = int(ret["holding"][0]["long_avail_qty"]) sell_available = int(ret["holding"][0]["short_avail_qty"]) if buy_available > 0: thread.start_new_thread(ensure_sell_more, ( futureAPI, coin_eos.name, time_type, latest_price, buy_price, )) if sell_available > 0: thread.start_new_thread(ensure_sell_less, ( futureAPI, coin_eos.name, time_type, latest_price, buy_price, )) else: print("确认未持仓") each_message = jmessage[0] channel = each_message['channel'] jdata = each_message['data'][0] latest_price = float(jdata[1]) deal_entity = DealEntity(jdata[0], latest_price, round(float(jdata[2]), 3), ts, jdata[4]) if channel == 'ok_sub_spot_btc_usdt_deals': coin_btc.process_entity(deal_entity, ts) btc_30s_change = cal_rate(coin_btc.get_avg_price_3s(), coin_btc.get_avg_price_60s()) btc_5min_change = cal_rate(coin_btc.get_avg_price_3s(), coin_btc.get_avg_price_5min()) coin_change_info = 'btc 30s change: %.3f%%, 5min change: %.3f%%\r\n' % ( btc_30s_change, btc_5min_change) write_lines.append(coin_change_info) print(coin_change_info) elif channel == 'ok_sub_spot_etc_usdt_deals': coin_etc.process_entity(deal_entity, ts) etc_30s_change = cal_rate(coin_etc.get_avg_price_3s(), coin_etc.get_avg_price_60s()) etc_5min_change = cal_rate(coin_etc.get_avg_price_3s(), coin_etc.get_avg_price_5min()) coin_change_info = 'etc 30s change: %.3f%%, 5min change: %.3f%%\r\n' % ( etc_30s_change, etc_5min_change) write_lines.append(coin_change_info) print(coin_change_info) elif channel == 'ok_sub_spot_ltc_usdt_deals': coin_ltc.process_entity(deal_entity, ts) ltc_30s_change = cal_rate(coin_ltc.get_avg_price_3s(), coin_ltc.get_avg_price_60s()) ltc_5min_change = cal_rate(coin_ltc.get_avg_price_3s(), coin_ltc.get_avg_price_5min()) coin_change_info = 'ltc 30s change: %.3f%%, 5min change: %.3f%%\r\n' % ( ltc_30s_change, ltc_5min_change) write_lines.append(coin_change_info) print(coin_change_info) elif channel == 'ok_sub_spot_eos_usdt_deals': coin_eos.process_entity(deal_entity, ts) eos_30s_change = cal_rate(coin_eos.get_avg_price_3s(), coin_eos.get_avg_price_60s()) eos_5min_change = cal_rate(coin_eos.get_avg_price_3s(), coin_eos.get_avg_price_5min()) coin_change_info = 'eos 30s change: %.3f%%, 5min change: %.3f%%\r\n' % ( eos_30s_change, eos_5min_change) write_lines.append(coin_change_info) print(coin_change_info) elif channel == 'ok_sub_spot_eth_usdt_deals': coin_eth.process_entity(deal_entity, ts) eth_30s_change = cal_rate(coin_eth.get_avg_price_3s(), coin_eth.get_avg_price_60s()) eth_5min_change = cal_rate(coin_eth.get_avg_price_3s(), coin_eth.get_avg_price_5min()) coin_change_info = 'eth 30s change: %.3f%%, 5min change: %.3f%%\r\n' % ( eth_30s_change, eth_5min_change) write_lines.append(coin_change_info) print(coin_change_info) elif channel == 'ok_sub_spot_xrp_usdt_deals': coin_xrp.process_entity(deal_entity, ts) xrp_30s_change = cal_rate(coin_xrp.get_avg_price_3s(), coin_xrp.get_avg_price_60s()) xrp_5min_change = cal_rate(coin_xrp.get_avg_price_3s(), coin_xrp.get_avg_price_5min()) coin_change_info = 'xrp 30s change: %.3f%%, 5min change: %.3f%%\r\n' % ( xrp_30s_change, xrp_5min_change) write_lines.append(coin_change_info) print(coin_change_info) prices_30s = [ btc_30s_change, eth_30s_change, ltc_30s_change, etc_30s_change, eos_30s_change, xrp_30s_change ] prices_5min = [ btc_5min_change, eth_5min_change, ltc_5min_change, etc_5min_change, eos_5min_change, xrp_5min_change ] weights = [0.2, 0.15, 0.15, 0.15, 0.2, 0.15] weighted_30s_change = cal_weighted(prices_30s, weights) weighted_5min_change = cal_weighted(prices_5min, weights) if more == 1: if weighted_30s_change < -0.2 or weighted_5min_change < 0: if sell_more_batch(futureAPI, time_type, latest_price): more = 0 thread.start_new_thread(ensure_sell_more, ( futureAPI, coin_eos.name, time_type, latest_price, buy_price, )) elif less == 1: if weighted_30s_change > 0.2 or weighted_5min_change > 0: if sell_less_batch(futureAPI, time_type, latest_price): less = 0 thread.start_new_thread(ensure_sell_less, ( futureAPI, coin_eos.name, time_type, latest_price, buy_price, )) elif more == 0 and weighted_30s_change > 0.2 and weighted_5min_change > 0.3: if buyin_more(futureAPI, coin_eos.name, time_type): more = 1 thread.start_new_thread(ensure_buyin_more, ( futureAPI, coin_eos.name, time_type, latest_price, )) buy_price = latest_price info = u'发出做多信号!!!买入价格:' + str(buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif less == 0 and weighted_30s_change < -0.2 and weighted_5min_change < -0.3: if buyin_less(futureAPI, coin_eos.name, time_type): less = 1 thread.start_new_thread(ensure_buyin_less, ( futureAPI, coin_eos.name, time_type, latest_price, )) buy_price = latest_price info = u'发出做空信号!!!买入价格:' + str(buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') rate_info = u'weighted 30s_rate: %.3f%%, 5min_rate: %.3f%%' % ( weighted_30s_change, weighted_5min_change) write_info = 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(rate_info + u', ' + now_time) except Exception as e: print(repr(e)) traceback.print_exc()