def process(res, timestamp): global write_lines for i in res: if i == 'data': ts = time.time() trade_item = res[i][0] latest_price = float(trade_item['price']) trade_id = str(trade_item['trade_id']) size = float(trade_item['size']) side = str(trade_item['side']) deal_entity = DealEntity(trade_id, latest_price, size, ts, side) print('detail:' + deal_entity.detail()) 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_3m, deal_entity, ts, ind_3m) 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_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) 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) # print("price info: " + price_info) 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_3s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol, ind_3s.ask_vol, ind_3s.bid_vol, ind_3m.vol, ind_3m.ask_vol, ind_3m.bid_vol) # print("vol info: " + vol_info) rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 3min_rate: %.2f%%' \ % (price_10s_change, price_1m_change, price_3m_change) # print("rate info: " + rate_info) write_info = holding_status + u', ' + price_info + u', ' + vol_info + u', ' + rate_info + u', ' + timestamp + '\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', ' + str(timestamp))
def on_message(ws, message): message = bytes.decode(inflate(message), 'utf-8') # data decompress if 'pong' in message or 'addChannel' in message: return global deque_3s, deque_10s, deque_min, \ deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, time_flag_1min, time_flag_1s jmessage = json.loads(message) ts = time.time() if int(ts) - int(time_flag_1min) >= 60: time_flag_1min = int(ts) turtle.calc_unit() if int(ts) - int(time_flag_1s) >= 1: time_flag_1s = int(ts) turtle.build_position() turtle.add_position() 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) price_5m_ago = handle_deque(deque_5m, deal_entity, ts, ind_5m)
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 on_message(ws, message): global latest_price, deque_3s, deque_10s, deque_min, deque_3m, deque_15m, \ ind_3s, ind_10s, ind_1min, ind_3m, ind_15m, write_lines, swap_latest_price ts = time.time() now_time = timestamp2string(ts) message = bytes.decode(inflate(message), 'utf-8') # data decompress json_message = json.loads(message) for json_data in json_message['data']: ins_id = json_data['instrument_id'] if ins_id == swap_instrument_id: swap_latest_price = float(json_data['last']) continue latest_price = float(json_data['price']) deal_entity = DealEntity(json_data['trade_id'], latest_price, round(float(json_data['size']), 3), ts, json_data['side']) 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_3m, deal_entity, ts, ind_3m) handle_deque(deque_15m, deal_entity, ts, ind_15m) 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_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) price_change_3m_ago = cal_rate(latest_price, deque_3m[0].price) 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_3s.vol, ind_10s.vol, ind_1min.vol, ind_1min.ask_vol, ind_1min.bid_vol, ind_3s.ask_vol, ind_3s.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%%' \ % (price_10s_change, price_1m_change, price_3m_change) print_message = price_info + u', ' + vol_info + u', ' + rate_info + u', ' + now_time + '\r\n' write_lines.append(print_message) if len(write_lines) >= 100: with codecs.open(file_deal, 'a+', 'UTF-8') as f: f.writelines(write_lines) write_lines = [] print('less: %d, more: %d' % (less, more)) 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, 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, 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): 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, deque_3s, deque_10s, deque_min, count, \ deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, last_5min_macd, last_5min_macd_ts, prev_5m_price, prev_15m_price, \ prev_5min_price_less_15min, prev_5min_price_more_15min 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) last_5min_macd, tmp_ts = get_macd("etc_usd", time_type, "1min") print(last_5min_macd, last_5min_macd_ts) if more == 0 and less == 0: ret = json.loads( okFuture.future_position_4fix("etc_usd", "quarter", "1")) print(ret) if len(ret["holding"]) > 0: buy_available = ret["holding"][0]["buy_available"] sell_available = ret["holding"][0]["sell_available"] if buy_available > 0: thread.start_new_thread(ensure_sell_more, ( coin.name, time_type, )) if sell_available > 0: thread.start_new_thread(ensure_sell_less, ( coin.name, time_type, )) 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) handle_deque(deque_15m, deal_entity, ts, ind_15m) 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() avg_15m_price = ind_15m.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) if more == 1: if avg_5m_price < prev_5m_price: count += 1 if count > 3: if sell_more(coin.name, time_type, latest_price): more = 0 thread.start_new_thread(ensure_sell_more, ( coin.name, time_type, )) elif less == 1: if avg_5m_price > prev_5m_price: count += 1 if count > 3: if sell_less(coin.name, time_type, latest_price): less = 0 thread.start_new_thread(ensure_sell_less, ( coin.name, time_type, )) elif prev_5m_price <= prev_15m_price and avg_5m_price > avg_15m_price: if buyin_more_price(coin.name, time_type, latest_price - 0.002): more = 1 count = 0 buy_price = latest_price - 0.002 info = u'发出做多信号!!!买入价格:' + str( buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif prev_5m_price >= prev_15m_price and avg_5m_price < avg_15m_price: if buyin_less_price(coin.name, time_type, latest_price + 0.002): less = 1 count = 0 buy_price = latest_price + 0.002 info = u'发出做空信号!!!买入价格:' + str( buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') 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%%, 5min_macd: %.6f, 15min_price: %.4f' \ % (price_10s_change, price_1m_change, price_5m_change, last_5min_macd, avg_15m_price) print(price_info + '\r\n' + vol_info + '\r\n' + rate_info + u', ' + now_time) prev_5m_price = avg_5m_price prev_15m_price = avg_15m_price
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, last_avg_price, buy_price, more, less, deque_3s, deque_10s, deque_min, buy_sell_ratio, \ deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, last_minute_ts, last_second_ts, order_id_queue, now_time jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) for each_message in jmessage: for jdata in each_message['data']: latest_price = float(jdata[1]) exe_second(ts) exe_minute(ts) 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) 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) # 单边上涨行情持有做多单,达到卖出条件 if more == 1: if price_1m_change <= -0.2 or price_5m_change <= 0: if sell_more_batch(coin.name, time_type, latest_price): more = 0 thread.start_new_thread(ensure_sell_more, ( coin.name, time_type, )) # 单边下跌行情持有做空单,达到卖出条件 elif less == 1: if price_1m_change >= 0.2 or price_5m_change >= 0: if sell_less_batch(coin.name, time_type, latest_price): less = 0 thread.start_new_thread(ensure_sell_less, ( coin.name, time_type, )) # 单边上涨行情 if price_1m_change >= incr_1m_rate and price_5m_change >= incr_5m_rate: # 撤单开空、平多单 cancel_uncompleted_order(coin.name, time_type, 2) cancel_uncompleted_order(coin.name, time_type, 3) # 撤单平空单 cancel_uncompleted_order(coin.name, time_type, 4) # 空单全部卖出 if sell_less_batch(coin.name, time_type, latest_price): thread.start_new_thread(ensure_sell_less, ( coin.name, time_type, )) # 做多买入 if buyin_more(coin.name, time_type, latest_price): more = 1 thread.start_new_thread( ensure_buyin_more, (coin.name, time_type, latest_price)) # 单边下跌行情 elif price_1m_change < -incr_1m_rate and price_5m_change < -incr_5m_rate: # 撤单开多,平空单 cancel_uncompleted_order(coin.name, time_type, 1) cancel_uncompleted_order(coin.name, time_type, 4) # 撤单平多单 cancel_uncompleted_order(coin.name, time_type, 3) # 多单全部卖出 if sell_more_batch(coin.name, time_type, latest_price): thread.start_new_thread(ensure_sell_more, ( coin.name, time_type, )) # 做空买入 if buyin_less(coin.name, time_type, latest_price): less = 1 thread.start_new_thread( ensure_buyin_less, (coin.name, time_type, latest_price)) if -0.05 < price_1m_change < 0.05 and -0.02 < price_10s_change < 0.02 and ind_1min.vol < 6000 and ind_10s.vol < 1000: more_order_id, less_order_id = buyin_moreandless( coin.name, time_type, latest_price, 20, buy_sell_ratio) if more_order_id: order_id_queue.append(more_order_id) print('more order_id: %s' % more_order_id) if less_order_id: order_id_queue.append(less_order_id) print('less order_id: %s' % less_order_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, 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()
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))
def on_message(ws, message): global latest_price, last_avg_price, less, deque_3s, deque_10s, deque_min, instrument_id, \ deque_3m, ind_3s, ind_10s, ind_1min, ind_3m, write_lines, freeze_time, swap_less_time, \ swap_more_price, swap_more_time, more, swap_more_price, swap_less_price, swap_latest_price ts = time.time() now_time = timestamp2string(ts) message = bytes.decode(inflate(message), 'utf-8') # data decompress json_message = json.loads(message) for json_data in json_message['data']: ins_id = json_data['instrument_id'] if ins_id == swap_instrument_id: swap_latest_price = float(json_data['last']) continue latest_price = float(json_data['price']) deal_entity = DealEntity(json_data['trade_id'], latest_price, round(float(json_data['size']), 3), ts, json_data['side']) 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) price_change_3m_ago = cal_rate(latest_price, deque_3m[0].price) # 做空 if less == 0 and check_do_future_less_test(price_3m_change, price_1m_change, price_10s_change): i = 1 while True: swap_less_order_id = do_swap_less() write_info_into_file('第' + str(i) + '次做空尝试, id: ' + str(swap_less_order_id), file_transaction) i += 1 if swap_less_order_id: swap_order_info = swapAPI.get_order_info(swap_instrument_id, swap_less_order_id) write_info_into_file('做空 order info: ' + str(swap_order_info), file_transaction) if swap_order_info['state'] == '2': less = 1 swap_less_time = int(ts) swap_less_price = float(swap_order_info['price_avg']) thread.start_new_thread(send_email, ('合约做空,价格:' + str(swap_less_price),)) break elif swap_order_info['state'] == '-1': continue else: swapAPI.revoke_order(swap_less_order_id, swap_instrument_id) if more == 0 and check_do_future_more_test(price_3m_change, price_1m_change, price_10s_change): i = 1 while True: swap_more_order_id = do_swap_more() write_info_into_file('第' + str(i) + '次做多尝试, id: ' + str(swap_more_order_id), file_transaction) i += 1 if swap_more_order_id: swap_order_info = swapAPI.get_order_info(swap_instrument_id, swap_more_order_id) write_info_into_file('做多 order info: ' + str(swap_order_info), file_transaction) if swap_order_info['state'] == '2': more = 1 swap_more_time = int(ts) swap_more_price = float(swap_order_info['price_avg']) thread.start_new_thread(send_email, ('合约做多, 价格:' + str(swap_more_price),)) break elif swap_order_info['state'] == '-1': continue else: swapAPI.revoke_order(swap_more_order_id, swap_instrument_id) if less == 1: if int(ts) - swap_less_time > 60 and price_1m_change > 0.1: if stop_swap_less(): less = 0 elif more == 1: if int(ts) - swap_more_time > 60 and price_1m_change < -0.1: if stop_swap_more(): more = 0 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%%' \ % (price_10s_change, price_1m_change, price_3m_change) print_message = price_info + u', ' + vol_info + u', ' + rate_info + u', ' + now_time + '\r\n' write_lines.append(print_message) if len(write_lines) >= 100: with codecs.open(file_deal, 'a+', 'UTF-8') as f: f.writelines(write_lines) write_lines = [] print('less: %d, more: %d' % (less, more)) 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, buy_price, more, less, deque_3s, deque_10s, deque_min, moreless, lessmore, \ lessless, moremore, deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, last_5min_macd, last_5min_macd_ts, highest, \ lowest, vol_24h, new_macd, last_1min_ts, random_times jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) if int(ts) - int(last_1min_ts) >= 5: coin_list = ["etc", "btc", "eth", "eos", "ltc", "xrp"] weight_list = [0.15, 0.35, 0.15, 0.15, 0.1, 0.1] last_1min_ts = int(ts) rate_list = coin_avg_price_change(coin_list, 1) all_coins_1min_price_change = float( np.sum([ rate_list[i] * weight_list[i] for i in range(len(coin_list)) ])) avg_info = '' for i in range(len(coin_list)): avg_info += coin_list[i] + ': ' + str(rate_list[i]) + ' ' avg_info += 'avg: %.3f, time: %s\r\n' % (all_coins_1min_price_change, timestamp2string(ts)) print(avg_info) write_lines.append(avg_info) if all_coins_1min_price_change < -0.6 and lessless == 0 and moremore == 0: for i in range(random_times): if random.random() >= 0.9: 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) if ind_1min.ask_vol > 2 * ind_1min.bid_vol and ind_10s.ask_vol > 5 * ind_10s.bid_vol \ and price_10s_change < 0 and price_1m_change < -0.1 and price_5m_change < -0.3 \ and ind_1min.vol > 8 * vol_24h: if buyin_less(okFuture, coin.name, time_type, latest_price - 0.01): lessless = 1 thread.start_new_thread(ensure_buyin_less, ( okFuture, coin.name, time_type, latest_price, )) buy_price = latest_price - 0.01 info = u'发出做空信号!!!买入价格:' + str( buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') break random_times *= 2 elif all_coins_1min_price_change > 0.6 and lessless == 0 and moremore == 0: for i in range(random_times): if random.random() >= 0.9: 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_5m_change = cal_rate(avg_3s_price, avg_5m_price) price_10s_change = cal_rate(avg_3s_price, avg_10s_price) price_1m_change = cal_rate(avg_3s_price, avg_min_price) if ind_5m.ask_vol < ind_5m.bid_vol and ind_3m.ask_vol < ind_3m.bid_vol \ and 2 * ind_1min.ask_vol < ind_1min.bid_vol and 5 * ind_10s.ask_vol < ind_10s.bid_vol \ and price_10s_change > 0 and price_1m_change > 0.1 and price_5m_change > 0.3 \ and ind_1min > 8 * vol_24h: if buyin_more(okFuture, coin.name, time_type, latest_price + 0.01): moremore = 1 thread.start_new_thread(ensure_buyin_more, ( okFuture, coin.name, time_type, latest_price, )) buy_price = latest_price + 0.01 info = u'发出做多信号!!!买入价格:' + str( buy_price) + u', ' + now_time with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') break random_times *= 2 else: random_times = 1 if int(ts) - int(last_5min_macd_ts) >= 180: last_5min_macd_ts = int(ts) ticker = futureAPI.get_specific_ticker(coin.get_future_instrument_id()) vol_24h = int( int(ticker['volume_24h']) * 10 / 24 / 60 / float(ticker['last'])) symbol = coin.name + "_usd" contract_type = "quarter" df = get_future_Nval(okFuture, symbol, contract_type, "15min", 200, 16) highest = list(df['highest'])[-1] lowest = list(df['lowest'])[-1] df = get_macd(okFuture, coin.name + "_usd", "quarter", "5min", 300) diff = list(df['diff']) dea = list(df['dea']) new_macd = 2 * (diff[-1] - dea[-1]) if more == 0 and less == 0 and lessmore == 0 and moreless == 0 and lessless == 0 and moremore == 0: ret = json.loads( okFuture.future_position_4fix(coin.name + "_usd", "quarter", "1")) print(ret) if len(ret["holding"]) > 0: buy_available = ret["holding"][0]["buy_available"] sell_available = ret["holding"][0]["sell_available"] if buy_available > 0: thread.start_new_thread(ensure_sell_more, ( okFuture, coin.name, time_type, )) if sell_available > 0: thread.start_new_thread(ensure_sell_less, ( okFuture, coin.name, time_type, )) 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_3m, deal_entity, ts, ind_3m) handle_deque(deque_5m, deal_entity, ts, ind_5m) 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) if lessless == 1: if price_5m_change > 0 and new_macd > 0: if sell_less_batch(okFuture, coin.name, time_type, latest_price): lessless = 0 thread.start_new_thread(ensure_sell_less, ( okFuture, coin.name, time_type, )) info = u'做空止盈,盈利%.3f, time: %s' % ( buy_price - latest_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif price_5m_change > 0 and latest_price > buy_price - 0.02: if sell_less_batch(okFuture, coin.name, time_type, latest_price): lessless = 0 thread.start_new_thread(ensure_sell_less, ( okFuture, coin.name, time_type, )) info = u'做空止损,盈利%.3f, time: %s' % ( buy_price - latest_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif moremore == 1: if price_5m_change < 0 and new_macd < 0: if sell_more_batch(okFuture, coin.name, time_type, latest_price): moremore = 0 thread.start_new_thread(ensure_sell_more, ( okFuture, coin.name, time_type, )) info = u'做多止盈,盈利%.3f, time: %s' % (latest_price - buy_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') elif price_5m_change < 0 and latest_price < buy_price + 0.02: if sell_more_batch(okFuture, coin.name, time_type, latest_price): moremore = 0 thread.start_new_thread(ensure_sell_more, ( okFuture, coin.name, time_type, )) info = u'做多止损,盈利%.3f, time: %s' % (latest_price - buy_price, now_time) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') price_info = deal_entity.type + u' now_price: %.4f, highest: %.4f, lowest: %.4f, 3s_price: %.4f, 10s_price: %.4f, 1m_price: %.4f, ' \ u'5min_price: %.4f' \ % (latest_price, highest, lowest, 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, ' \ u'3s_ask_vol: %.3f, 3s_bid_vol: %.3f, 24h_vol: %.3f, 3min vol: %.3f, 3min_ask_vol: %.3f, 3min_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, vol_24h, ind_3m.vol, ind_3m.ask_vol, ind_3m.bid_vol) rate_info = u'10s_rate: %.2f%%, 1min_rate: %.2f%%, 5min_rate: %.2f%%, 5min_macd: %.6f' \ % (price_10s_change, price_1m_change, price_5m_change, new_macd) 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): global latest_price, last_avg_price, less, deque_3s, deque_10s, deque_min, instrument_id, \ deque_3m, ind_3s, ind_10s, ind_1min, ind_3m, write_lines, freeze_time, lever_sell_time, lever_sell_price, \ lever_more_price, lever_more_time, more ts = time.time() now_time = timestamp2string(ts) message = bytes.decode(inflate(message), 'utf-8') # data decompress json_message = json.loads(message) for json_data in json_message['data']: latest_price = float(json_data['price']) deal_entity = DealEntity(json_data['trade_id'], latest_price, round(float(json_data['size']), 3), ts, json_data['side']) 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) price_change_3m_ago = cal_rate(latest_price, deque_3m[0].price) print('price change 3m ago: %.3f%%' % price_change_3m_ago) # 做空 if less == 0 and price_change_3m_ago > -5 \ and check_do_future_less(price_3m_change, price_1m_change, price_10s_change): if do_lever_less(): less = 1 lever_sell_time = int(ts) if less == 1: print('做空时间:%s,价格:%.3f' % (timestamp2string(lever_sell_time), lever_sell_price)) if price_1m_change > 0 and int(ts) - lever_sell_time > 60: if stop_lever_less(): less = 0 elif int(ts) - lever_sell_time > 30 and latest_price > lever_sell_price: if stop_lever_less(): less = 0 elif more == 1: print('做多时间:%s,价格:%.3f' % (timestamp2string(lever_more_time), lever_more_price)) if price_1m_change < 0 and int(ts) - lever_sell_time > 120: if stop_lever_more(): more = 0 elif int(ts) - lever_sell_time > 60 and latest_price < lever_more_price: if stop_lever_more(): more = 0 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%%' \ % (price_10s_change, price_1m_change, price_3m_change) print_message = price_info + u', ' + vol_info + u', ' + rate_info + u', ' + now_time + '\r\n' write_lines.append(print_message) if len(write_lines) >= 100: with codecs.open(file_deal, 'a+', 'UTF-8') as f: f.writelines(write_lines) write_lines = [] print('less: %d, more: %d' % (less, more)) print(price_info + '\r\n' + vol_info + '\r\n' + rate_info + u', ' + now_time)
def on_message(ws, message): if 'pong' in message or 'addChannel' in message: return print(message) global latest_price, last_avg_price, buy_price, last_last_price, more, less, deque_3s, deque_10s, deque_min, \ deque_5m, ind_3s, ind_10s, ind_1min, ind_5m, write_lines, less2more, more2less jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) 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) 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) if more == 1 and not processing: if avg_3s_price <= 1.001 * avg_5m_price or price_1m_change <= -incr_1m_rate: # 反手做空 if price_1m_change <= -incr_1m_rate: more2less = 1 sell_more_batch(coin.name, time_type, latest_price) elif less == 1 and not processing: if avg_3s_price >= 0.999 * avg_5m_price or price_1m_change >= incr_1m_rate: # 反手做多 if price_1m_change >= incr_1m_rate: less2more = 1 sell_less_batch(coin.name, time_type, latest_price) elif more2less == 1 and not processing: if price_1m_change >= 0: sell_less_batch(coin.name, time_type, latest_price) elif less2more == 1 and not processing: if price_1m_change <= 0: sell_more_batch(coin.name, time_type, latest_price) elif check_vol(): if latest_price > avg_3s_price > avg_10s_price > last_avg_price > last_last_price \ and incr_1m_rate <= price_1m_change < price_5m_change and incr_5m_rate <= price_5m_change < 1.5 \ and 0.05 <= price_10s_change <= 0.2\ and ind_1min.bid_vol > float(2 * ind_1min.ask_vol) \ and ind_3s.bid_vol > float(5 * ind_3s.ask_vol): if buyin_more(coin.name, time_type, latest_price): more = 1 thread.start_new_thread(ensure_buyin_more, ( 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 latest_price < avg_3s_price < avg_10s_price < last_avg_price < last_last_price \ and -1.5 < price_5m_change <= -incr_5m_rate and price_5m_change < price_1m_change <= -incr_1m_rate \ and -0.2 <= price_10s_change <= -0.05 \ and ind_1min.ask_vol > float(4 * ind_1min.bid_vol) and ind_3s.ask_vol > float(5 * ind_3s.bid_vol): if buyin_less(coin.name, time_type, latest_price): less = 1 thread.start_new_thread(ensure_buyin_less, ( 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') if last_avg_price != avg_10s_price: last_last_price = last_avg_price last_avg_price = avg_10s_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 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, 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 jmessage = json.loads(message) ts = time.time() now_time = timestamp2string(ts) if int(ts) - int(last_5min_macd_ts) >= 300: last_5min_macd_ts = int(ts) print(last_5min_macd_ts) if more == 0 and less == 0: ret = json.loads( okFuture.future_position_4fix("etc_usd", "quarter", "1")) print(ret) if len(ret["holding"]) > 0: buy_available = ret["holding"][0]["buy_available"] sell_available = ret["holding"][0]["sell_available"] if buy_available > 0: thread.start_new_thread(ensure_sell_more, ( coin.name, time_type, )) if sell_available > 0: thread.start_new_thread(ensure_sell_less, ( coin.name, time_type, )) 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) 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) if more == 1: if price_10s_change < 0: cancel_uncompleted_order(coin.name, time_type) if sell_more_batch(coin.name, time_type, latest_price): more = 0 thread.start_new_thread(ensure_sell_more, ( coin.name, time_type, )) elif less == 1: if price_10s_change > 0: cancel_uncompleted_order(coin.name, time_type) if sell_less_batch(coin.name, time_type, latest_price): less = 0 thread.start_new_thread(ensure_sell_less, ( coin.name, time_type, )) elif check_vol(): if price_10s_change > incr_10s_rate: if ind_3s.bid_vol >= vol_3s_bal * ind_3s.ask_vol and ind_1min.bid_vol >= vol_1m_bal * ind_1min.ask_vol: latest_order_id = buyin_more_price( coin.name, time_type, latest_price) if latest_order_id: more = 1 thread.start_new_thread(pend_order, ( coin.name, time_type, latest_order_id, 'more', )) 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_10s_change < -incr_10s_rate: if ind_3s.ask_vol >= vol_3s_bal * ind_3s.bid_vol and ind_1min.ask_vol >= vol_1m_bal * ind_1min.bid_vol: latest_order_id = buyin_less_price( coin.name, time_type, latest_price) if latest_order_id: less = 1 thread.start_new_thread(pend_order, ( coin.name, time_type, latest_order_id, 'less', )) 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') 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%%, 5min_macd: %.6f' \ % (price_10s_change, price_1m_change, price_5m_change, last_5min_macd) 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_3s, ind_10s, ind_1min, ind_3m, write_lines, 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) price_change_3m_ago = cal_rate(latest_price, deque_3m[0]) write_info("3min ago change: %.2f%%" % price_change_3m_ago) # 做空 if less == 0 and price_change_3m_ago > -5 and check_do_future_less( price_3m_change, price_1m_change, price_10s_change) and price_change_3m_ago: lever_less_order_id = do_lever_less() if lever_less_order_id: lever_less_time = int(ts) time.sleep(1) sell_order_info = leverAPI.get_order_info( lever_less_order_id, instrument_id) # 1:部分成交 2:完全成交 if sell_order_info['state'] == '1' or sell_order_info[ 'state'] == '2': less = 1 lever_sell_price = float(sell_order_info['price_avg']) info = now_time + u' 杠杆卖出!!!卖出均价:' + str( lever_sell_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: leverAPI.revoke_order_exception( instrument_id, lever_less_order_id) if less == 1: if price_1m_change > 0 and int( ts ) - lever_less_time > 120 and ind_1min.bid_vol > ind_1min.ask_vol: if stop_lever_less(latest_price): less = 0 if int( ts ) - lever_less_time > 60 and latest_price > lever_sell_price: if stop_lever_less(latest_price): 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%%' \ % (price_10s_change, price_1m_change, price_3m_change) info = holding_status + u',' + price_info + u',' + vol_info + u',' + rate_info + u',' + now_time + '\r\n' write_lines.append(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)