def tickToBar(tick, granularity=180): #tick data to bar # global bars instrument_id = tick.InstrumentID action_day = tick.ActionDay update_time = tick.UpdateTime.replace(':', '') last_price = int( tick.LastPrice) if instrument_id[0:2] in int_instruments else float( tick.LastPrice) volume = tick.Volume # if volume == 0: # continue if update_time.find('.') != -1: dt = datetime.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S.%f") timestamp = time.mktime(dt.timetuple()) + (dt.microsecond / 1e6) else: timestamp = int( time.mktime( time.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S"))) date_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) ohlc_key = generate_ohlc_key(instrument_id=instrument_id, granularity=granularity, timestamp=timestamp) if ohlc_key not in nest: nest[ohlc_key] = { 'date_time': date_time, 'last_timestamp': timestamp, 'high': last_price, 'low': last_price, 'close': last_price, 'open': last_price } nest[ohlc_key]['last_timestamp'] = timestamp nest[ohlc_key]['date_time'] = date_time nest[ohlc_key]['close'] = last_price if last_price > nest[ohlc_key]['high']: nest[ohlc_key]['high'] = last_price elif last_price < nest[ohlc_key]['low']: nest[ohlc_key]['low'] = last_price if nest.__len__() > 1: for k, v in nest.items(): if k == ohlc_key: continue q_bar.put(nest[k]) del nest[k]
def login(): # 登录行情服务器 user = MyMdApi(instruments=inst, broker_id=BROKER_ID, investor_id=INVESTOR_ID, password=PASSWORD) user.Create("data") user.RegisterFront(ADDR_MD) user.Init() print('行情服务器登录成功') while True: if Utils.exit_flag: msg = 'Thread CTPDataCollectEngine say bye-bye' print(msg) logger.info(msg=msg) return try: payload = q_depth_market_data.get(timeout=1) print(payload) q_depth_market_data.task_done() instrument_id = payload.InstrumentID action_day = payload.ActionDay update_time = payload.UpdateTime.replace(':', '') last_price = payload.LastPrice volume = payload.Volume if volume == 0: continue if update_time.find('.') != -1: dt = datetime.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S.%f") timestamp = time.mktime(dt.timetuple()) + (dt.microsecond / 1e6) else: timestamp = int(time.mktime(time.strptime(' '.join([action_day, update_time]), "%Y%m%d %H%M%S"))) date_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) ohlc_key = generate_ohlc_key(instrument_id=instrument_id, granularity=granularity, timestamp=timestamp) if ohlc_key not in nest: nest[ohlc_key] = { 'date_time': date_time, 'last_timestamp': timestamp, 'high': last_price, 'low': last_price, 'close': last_price, 'open': last_price } nest[ohlc_key]['last_timestamp'] = timestamp nest[ohlc_key]['date_time'] = date_time nest[ohlc_key]['close'] = last_price if last_price > decimal.Decimal(nest[ohlc_key]['high']): nest[ohlc_key]['high'] = last_price elif last_price < decimal.Decimal(nest[ohlc_key]['low']): nest[ohlc_key]['low'] = last_price if nest.__len__() > 1: for k, v in list(nest.items()): if k == ohlc_key: continue data.append(nest[k]) # del nest[k] high = get_k_line_column(data=data, depth=20) ma_5 = ma(elements=high, step=5) ma_10 = ma(elements=high, step=10) # print high # print ma_5 # print ma_10 cu = cross(ma_5, ma_10) print(cu) far = be_apart_from(cu) print(far) print(nest) except queue.Empty as e: pass