def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() # Trade price trade.trade_price = float(str(raw[0])) # Trade volume trade.trade_volume = float(str(raw[1])) # Timestamp date_time = float(raw[2]) trade.date_time = datetime.utcfromtimestamp(date_time).strftime("%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = Trade.parse_side(raw[3]) # Trade id trade.trade_id = trade.date_time + '-' + str(instmt.get_exch_trade_id()) return trade
def parse_trade(cls, instmt, raws): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trades = [] for item in raws: trade = Trade() today = datetime.today().date() time = item[3] #trade.date_time = datetime.utcfromtimestamp(date_time/1000.0).strftime("%Y%m%d %H:%M:%S.%f") #Convert local time as to UTC. date_time = datetime(today.year, today.month, today.day, *list(map(lambda x: int(x), time.split(':'))), tzinfo=pytz.timezone('Asia/Shanghai') ) #trade.date_time = date_time.astimezone(pytz.utc).strftime('%Y%m%d %H:%M:%S.%f') trade.date_time = date_time.astimezone(pytz.utc).strftime('%Y%m%d %H:%M:%S.%f') # Trade side # Buy = 0 # Side = 1 trade.trade_side = Trade.parse_side(item[4]) # Trade id trade.trade_id = str(item[0]) # Trade price trade.trade_price = item[1] # Trade volume trade.trade_volume = item[2] trades.append(trade) return trades
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() # Trade price trade.trade_price = float(str(raw[0])) # Trade volume trade.trade_volume = float(str(raw[1])) # Timestamp date_time = float(raw[2]) trade.date_time = datetime.utcfromtimestamp(date_time).strftime( "%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = Trade.parse_side(raw[3]) # Trade id trade.trade_id = trade.date_time + '-' + str( instmt.get_exch_trade_id()) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) if cls.get_trade_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time date_time = float(raw[cls.get_trade_timestamp_field_name()]) trade.date_time = datetime.utcfromtimestamp(date_time).strftime("%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = 1 # Trade id trade.trade_id = str(raw[cls.get_trade_id_field_name()]) # Trade price trade.trade_price = float(str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float(str(raw[cls.get_trade_volume_field_name()])) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) # print(raw) if cls.get_trade_id_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time trade.date_time = datetime.utcnow().strftime("%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = Trade.parse_side(str(raw[cls.get_trade_side_field_name()])) # Trade id trade.trade_id = str(int(time.time()*1000)) # Trade price trade.trade_price = float(str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float(str(raw[cls.get_trade_volume_field_name()])) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time date_time = float(raw[cls.get_trades_timestamp_field_name()]) date_time = date_time / cls.get_timestamp_offset() trade.date_time = datetime.utcfromtimestamp(date_time).strftime("%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = Trade.parse_side(raw[cls.get_trade_side_field_name()]) # Trade price trade.trade_price = float(str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float(str(raw[cls.get_trade_volume_field_name()])) # Trade id trade.trade_id = trade.date_time + '-P' + str(trade.trade_price) + '-V' + str(trade.trade_volume) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ """ [{"date":1525079378,"date_ms":1525079378512,"amount":139.8153,"price":0.00197428,"type":"buy","tid":129688817}, {"date":1525079378,"date_ms":1525079378564,"amount":0.7217,"price":0.00197428,"type":"buy","tid":129688818}] """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_side_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time # date_time = raw[cls.get_trades_timestamp_field_name()] timestamp = float( raw[cls.get_order_book_timestamp_field_name()]) / 1000.0 trade.date_time = datetime.utcfromtimestamp(timestamp).strftime( "%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = 1 if raw[ cls.get_trade_side_field_name()] == 'buy' else 2 # Trade id trade.trade_id = str(raw[cls.get_trade_id_field_name()]) # Trade price trade.trade_price = float( str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float( str(raw[cls.get_trade_volume_field_name()])) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ """ {'id': 17592355736499, 'amount': 0.0972, 'price': 13802.66, 'direction': 'buy', 'ts': 1515800691239} """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_side_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time # date_time = raw[cls.get_trades_timestamp_field_name()] timestamp = float( raw[cls.get_order_book_timestamp_field_name()]) / 1000.0 trade.date_time = datetime.utcfromtimestamp(timestamp).strftime( "%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = 1 if raw[ cls.get_trade_side_field_name()] == 'buy' else 2 # Trade id trade.trade_id = str(raw[cls.get_trade_id_field_name()]) # Trade price trade.trade_price = float( str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float( str(raw[cls.get_trade_volume_field_name()])) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_side_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time date_time = raw[cls.get_trades_timestamp_field_name()] if len(date_time) == 19: date_time += '.' date_time += '0' * (26 - len(date_time)) date_time = datetime.strptime(date_time, cls.get_trades_timestamp_format()) trade.date_time = date_time.strftime("%Y%m%d %H:%M:%S.%f") # Trade side trade.trade_side = 1 if raw[ cls.get_trade_side_field_name()] == 'BUY' else 2 # Trade id trade.trade_id = str(raw[cls.get_trade_id_field_name()]) # Trade price trade.trade_price = float( str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float( str(raw[cls.get_trade_volume_field_name()])) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time date_time = raw[cls.get_trades_timestamp_field_name()] try: trade.date_time = datetime.strptime( date_time, '%Y-%m-%dT%H:%M:%S.%f').strftime('%Y%m%d %H:%M:%S.%f') except Exception as e: trade.date_time = datetime.strptime( date_time, '%Y-%m-%dT%H:%M:%S').strftime('%Y%m%d %H:%M:%S.%f') # Trade side trade.trade_side = Trade.parse_side( raw[cls.get_trade_side_field_name()]) # Trade id trade.trade_id = str(raw[cls.get_trade_id_field_name()]) # Trade price trade.trade_price = float( str(raw[cls.get_trade_price_field_name()])) # Trade volume trade.trade_volume = float( str(raw[cls.get_trade_volume_field_name()])) else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() trade_id = raw[0] trade_price = float(raw[1]) trade_volume = float(raw[2]) timestamp = raw[3] trade_side = raw[4] trade.trade_id = trade_id + timestamp trade.trade_price = trade_price trade.trade_volume = trade_volume trade.trade_side = Trade.parse_side(trade_side) return trade
def parse_trade(cls, instmt, raws): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trades = [] for raw in raws: trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_side_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time date_time = float(raw[cls.get_trades_timestamp_field_name()]) trade.date_time = datetime.utcfromtimestamp( date_time / 1000.0).strftime("%Y%m%d %H:%M:%S.%f") # Trade side # Buy = 0 # Side = 1 trade.trade_side = Trade.parse_side( raw[cls.get_trade_side_field_name()]) # Trade id trade.trade_id = str(raw[cls.get_trade_id_field_name()]) # Trade price trade.trade_price = raw[cls.get_trade_price_field_name()] # Trade volume trade.trade_volume = raw[cls.get_trade_volume_field_name()] else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) trades.append(trade) return trades
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() trade_id = raw[0] timestamp = raw[1] trade_price = raw[2] trade_volume = raw[3] trade.date_time = datetime.utcfromtimestamp(timestamp).strftime("%Y%m%d %H:%M:%S.%f") trade.trade_side = Trade.Side.BUY if trade_volume > 0 else Trade.Side.SELL trade.trade_volume = abs(trade_volume) trade.trade_id = str(trade_id) trade.trade_price = trade_price return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() trade_id = raw[0] trade_price = float(raw[1]) trade_volume = float(raw[2]) timestamp = raw[3] trade_side = raw[4] trade.trade_id = trade_id + timestamp trade.trade_price = trade_price trade.trade_volume = trade_volume trade.trade_side = Trade.parse_side(trade_side) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() trade_id = raw[0] timestamp = raw[1] trade_price = raw[2] trade_volume = raw[3] trade.date_time = datetime.utcfromtimestamp(timestamp).strftime( "%Y%m%d %H:%M:%S.%f") trade.trade_side = Trade.Side.BUY if trade_volume > 0 else Trade.Side.SELL trade.trade_volume = abs(trade_volume) trade.trade_id = str(trade_id) trade.trade_price = trade_price return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_side_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time timestamp = raw[cls.get_trades_timestamp_field_name()] timestamp = timestamp.replace('T', ' ').replace('Z', '').replace('-', '') trade.date_time = timestamp # Trade side trade.trade_side = Trade.parse_side( raw[cls.get_trade_side_field_name()]) # Trade id trade.trade_id = raw[cls.get_trade_id_field_name()] # Trade price trade.trade_price = raw[cls.get_trade_price_field_name()] # Trade volume trade.trade_volume = raw[cls.get_trade_volume_field_name()] else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade
def parse_trade(cls, instmt, raw): """ :param instmt: Instrument :param raw: Raw data in JSON :return: """ trade = Trade() keys = list(raw.keys()) if cls.get_trades_timestamp_field_name() in keys and \ cls.get_trade_id_field_name() in keys and \ cls.get_trade_side_field_name() in keys and \ cls.get_trade_price_field_name() in keys and \ cls.get_trade_volume_field_name() in keys: # Date time timestamp = raw[cls.get_trades_timestamp_field_name()] timestamp = timestamp.replace('T', ' ').replace('Z', '').replace('-' , '') trade.date_time = timestamp # Trade side trade.trade_side = Trade.parse_side(raw[cls.get_trade_side_field_name()]) # Trade id trade.trade_id = raw[cls.get_trade_id_field_name()] # Trade price trade.trade_price = raw[cls.get_trade_price_field_name()] # Trade volume trade.trade_volume = raw[cls.get_trade_volume_field_name()] else: raise Exception('Does not contain trade keys in instmt %s-%s.\nOriginal:\n%s' % \ (instmt.get_exchange_name(), instmt.get_instmt_name(), \ raw)) return trade