def ensure_sell_less(coin_name, time_type, lever_rate=20): sleep_time = 30 while sleep_time > 0: time.sleep(sleep_time) jRet = json.loads( okFuture.future_position_4fix(coin_name + "_usd", time_type, "1")) if len(jRet["holding"]) > 0: cancel_uncompleted_order(coin_name, time_type) time.sleep(1) jRet = json.loads( okFuture.future_position_4fix(coin_name + "_usd", time_type, "1")) sell_available = jRet["holding"][0]["sell_available"] okFuture.future_trade(coin_name + "_usd", time_type, '', sell_available, 4, 1, lever_rate) else: break ts = time.time() now_time = timestamp2string(ts) info = u'做空卖出成功!!!卖出价格:' + str(latest_price) + u', 收益: ' + str(buy_price - latest_price) \ + ', ' + now_time thread.start_new_thread(send_email, (info, )) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n')
def sell_more_batch(coin_name, time_type, latest_price, lever_rate = 20): global processing processing = True jRet = json.loads(okFuture.future_position_4fix(coin_name+"_usd", time_type, "1")) print(jRet) flag = True ret = u'没有做多订单' while len(jRet["holding"]) > 0: cancel_uncompleted_order(coin_name, time_type) if flag: flag = False amount = jRet["holding"][0]["buy_available"] order_data = gen_orders_data(latest_price, amount, 3, 5) ret = okFuture.future_batchTrade(coin_name + "_usd", time_type, order_data, lever_rate) else: buy_available = jRet["holding"][0]["buy_available"] ret = okFuture.future_trade(coin_name + "_usd", time_type, '', buy_available, 3, 1, lever_rate) if 'true' in ret: time.sleep(2) jRet = json.loads(okFuture.future_position_4fix(coin_name + "_usd", time_type, "1")) sell_more_suc() email_msg = "做多%s批量卖出成交, 时间: %s, 成交结果: %s" \ % (coin_name, timestamp2string(time.time()), ret) thread.start_new_thread(send_email, (email_msg,)) processing = False return True
def sell_more_batch(coin_name, time_type, latest_price, lever_rate=20): global processing, more2less processing = True jRet = json.loads( okFuture.future_position_4fix(coin_name + "_usd", time_type, "1")) print(jRet) flag = True ret = u'没有做多订单' while len(jRet["holding"]) > 0: cancel_uncompleted_order(coin_name, time_type) if flag: flag = False amount = jRet["holding"][0]["buy_available"] order_data = gen_orders_data(latest_price, amount, 3, 5) ret = okFuture.future_batchTrade(coin_name + "_usd", time_type, order_data, lever_rate) else: buy_available = jRet["holding"][0]["buy_available"] ret = okFuture.future_trade(coin_name + "_usd", time_type, '', buy_available, 3, 1, lever_rate) if 'true' in ret: time.sleep(5) jRet = json.loads( okFuture.future_position_4fix(coin_name + "_usd", time_type, "1")) sell_more_suc() if more2less == 1: if buyin_less(coin_name, time_type, latest_price): info = u'发出反手做空信号!!!买入价格:' + str(latest_price) with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') else: more2less = 0 email_msg = "做多%s批量卖出成交, 时间: %s, 成交结果: %s" \ % (coin_name, timestamp2string(time.time()), ret) thread.start_new_thread(send_email, (email_msg, )) processing = False return True
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, 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 exe_minute(ts): global last_minute_ts, buy_sell_ratio if int(ts) - int(last_minute_ts) >= 60: last_minute_ts = int(ts) ret = okFuture.future_orderinfo(coin.name + "_usd", time_type, -1, 1, None, None) for each_order in json.loads(ret)["orders"]: if each_order['price'] > latest_price * 1.005 or each_order[ 'price'] < latest_price * 0.995: okFuture.future_cancel(coin.name + "_usd", time_type, each_order['order_id']) ret = json.loads( okFuture.future_position_4fix("etc_usd", "quarter", "1")) print(ret) if len(ret["holding"]) > 0: buy_amount = ret["holding"][0]["buy_amount"] sell_amount = ret["holding"][0]["sell_amount"] if buy_amount > 0: buy_sell_ratio = buy_amount / (buy_amount + sell_amount) more_profit_ratio = float( ret["holding"][0]["buy_profit_lossratio"]) buy_available = ret["holding"][0]["buy_available"] print("多仓盈亏: %.2f%%, 多仓可平仓数量: %d" % (more_profit_ratio, buy_available)) if more_profit_ratio < -20: cancel_uncompleted_order(coin.name, time_type, 3) thread.start_new_thread(ensure_sell_more, (coin.name, time_type)) real_profit = float(buy_amount * more_profit_ratio / 20) info = "多仓止损,亏损: %.4fETC" % real_profit with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') print(info) elif more_profit_ratio > 3: cancel_uncompleted_order(coin.name, time_type, 3) pend_order_price(coin.name, time_type, 3, buy_amount, latest_price) elif buy_available > 0: buy_price_avg = ret["holding"][0]["buy_price_avg"] * 1.001 pend_order_price(coin.name, time_type, 3, buy_available, buy_price_avg) if sell_amount > 0: buy_sell_ratio = buy_amount / (buy_amount + sell_amount) less_profit_ratio = float( ret["holding"][0]["sell_profit_lossratio"]) sell_available = ret["holding"][0]["sell_available"] print("空仓盈亏: %.2f%%, 空仓可平仓数量: %d" % (less_profit_ratio, sell_available)) if less_profit_ratio < -20: cancel_uncompleted_order(coin.name, time_type, 4) thread.start_new_thread(ensure_sell_less, (coin.name, time_type)) real_profit = float(sell_amount * less_profit_ratio / 20) info = "空仓止损,亏损: %.4fETC" % real_profit with codecs.open(file_transaction, 'a+', 'utf-8') as f: f.writelines(info + '\n') print(info) if less_profit_ratio > 3: cancel_uncompleted_order(coin.name, time_type, 3) pend_order_price(coin.name, time_type, 3, buy_amount, latest_price) if sell_available > 0: sell_price_avg = ret["holding"][0]["sell_price_avg"] * 0.999 pend_order_price(coin.name, time_type, 4, sell_available, sell_price_avg) print("持仓多空比: %.2f" % buy_sell_ratio) else: print("确认未持仓")