def timeseries2seqs(data, timestamps, length=3, T=48): raw_ts = copy(timestamps) if type(timestamps[0]) != pd.Timestamp: timestamps = string2timestamp(timestamps, T=T) offset = pd.DateOffset(minutes=24 * 60 // T) breakpoints = [0] for i in range(1, len(timestamps)): if timestamps[i - 1] + offset != timestamps[i]: print(timestamps[i - 1], timestamps[i], raw_ts[i - 1], raw_ts[i]) breakpoints.append(i) breakpoints.append(len(timestamps)) X = [] Y = [] for b in range(1, len(breakpoints)): print('breakpoints: ', breakpoints[b - 1], breakpoints[b]) idx = range(breakpoints[b - 1], breakpoints[b]) for i in range(len(idx) - length): x = np.vstack(data[idx[i:i + length]]) y = data[idx[i + length]] X.append(x) Y.append(y) X = np.asarray(X) Y = np.asarray(Y) print("X shape: ", X.shape, "Y shape:", Y.shape) return X, Y
def __init__(self, data, timestamps, T=48, CheckComplete=True): super(STMatrix, self).__init__() assert len(data) == len(timestamps) self.data = data self.timestamps = timestamps self.T = T self.pd_timestamps = string2timestamp(timestamps, self.T) if CheckComplete: self.check_complete() self.make_index()
def timeseries2seqs_peroid_trend(data, timestamps, length=3, T=48, peroid=pd.DateOffset(days=7), peroid_len=2): raw_ts = copy(timestamps) if type(timestamps[0]) != pd.Timestamp: timestamps = string2timestamp(timestamps, T=T) # timestamps index timestamp_idx = dict() for i, t in enumerate(timestamps): timestamp_idx[t] = i offset = pd.DateOffset(minutes=24 * 60 // T) breakpoints = [0] for i in range(1, len(timestamps)): if timestamps[i - 1] + offset != timestamps[i]: print(timestamps[i - 1], timestamps[i], raw_ts[i - 1], raw_ts[i]) breakpoints.append(i) breakpoints.append(len(timestamps)) X = [] Y = [] for b in range(1, len(breakpoints)): print('breakpoints: ', breakpoints[b - 1], breakpoints[b]) idx = range(breakpoints[b - 1], breakpoints[b]) for i in range(len(idx) - length): # period target_timestamp = timestamps[i + length] legal_idx = [] for pi in range(1, 1 + peroid_len): if target_timestamp - peroid * pi not in timestamp_idx: break legal_idx.append(timestamp_idx[target_timestamp - peroid * pi]) # print("len: ", len(legal_idx), peroid_len) if len(legal_idx) != peroid_len: continue legal_idx += idx[i:i + length] # trend x = np.vstack(data[legal_idx]) y = data[idx[i + length]] X.append(x) Y.append(y) X = np.asarray(X) Y = np.asarray(Y) print("X shape: ", X.shape, "Y shape:", Y.shape) return X, Y
def delete_overdue_order(self, latest_deal_price): now_ts = int(time.time()) self.timeLog('Start to delete pending orders..., %s' % timestamp2string(now_ts)) orders_list = self.spotAPI.get_orders_list('open', instrument_id) self.timeLog('before delete, pending orders num: %d' % len(orders_list)) for order in orders_list[0]: o_price = float(order['price']) o_time = string2timestamp(order['timestamp']) if o_time < now_ts - 60 or o_price < latest_deal_price * 0.995 or o_price or o_price > 1.005 * latest_deal_price: order_id = order['order_id'] self.revoke_order(order_id, now_ts) self.timeLog('Finish delete pending orders')
order_info = spotAPI.get_order_info( old_order_id, instrument_id) except Exception as e: print(repr(e)) traceback.print_exc() del_list.append(old_order_id) continue print('order_id: %s, order_info: %s' % (old_order_id, order_info)) # 完全成交的订单或已经撤单的订单 status = order_info['status'] side = order_info['side'] print('now timestamp: %d, order created time: %d' % (int(ts), string2timestamp(order_info['created_at']) + 8 * 3600)) if status == 'filled': print("order %s 已完全成交" % str(old_order_id)) del_list.append(old_order_id) if side == 'buy': more = 1 elif side == 'sell': more = 0 elif status == 'cancelled': del_list.append(old_order_id) elif int(ts) - (string2timestamp(order_info['created_at']) + 8 * 3600) >= 3: print('撤单重挂') try:
status = order_info['status'] ret = spotAPI.get_specific_ticker(instrument_id) buy_price = float(ret['best_bid']) + 0.0001 sell_price = float(ret['best_ask']) - 0.0001 if status == 'filled': print("order %s 已完全成交" % str(old_order_id)) del_list.append(old_order_id) side = order_info['side'] if side == 'buy': more = 1 elif side == 'sell': more = 0 elif status == 'cancelled': del_list.append(old_order_id) elif int(ts) > string2timestamp( order_info['timestamp']) + 10 + 8 * 3600: spotAPI.revoke_order(instrument_id, old_order_id) for del_id in del_list: order_id_queue.remove(del_id) del_list = [] time.sleep(1) if int(ts) - last_minute_ts > 10: last_minute_ts = int(ts) if more == 1: print("持有做多单") else: print("未持有单") print("calculating macd...") try:
# 完全成交的订单或已经撤单的订单 status = order_info['status'] side = order_info['side'] if status == 'filled': print("order %s 已完全成交" % str(old_order_id)) del_list.append(old_order_id) if side == 'buy': more = 1 elif side == 'sell': more = 0 elif status == 'part_filled': if side == 'buy': more = 1 elif status == 'cancelled': del_list.append(old_order_id) elif int(ts) - (string2timestamp(order_info['timestamp']) + 8 * 3600) >= 3: print( '撤单重挂,ts:%s, 真实时间:%s' % (timestamp2string(ts), timestamp2string(time.time()))) try: print( '撤单结果: %s' % spotAPI.revoke_order(instrument_id, old_order_id)) except Exception as e: print(repr(e)) traceback.print_exc() del_list.append(old_order_id) continue ret = spotAPI.get_specific_ticker(instrument_id) print('当前行情: %s' % ret)