def get_tick_data(code, from_datetime, until_datetime, db_collection): data = list(db_collection[code].find({'date': {'$gte': from_datetime, '$lte': until_datetime}})) converted_data = [] for td in data: converted = dt.cybos_stock_tick_convert(td) converted_data.append(converted) return converted_data
def handle_today_tick(code, d): d = dt.cybos_stock_tick_convert(d) vi.calculate_vi_price(code, d) ret = 0 ret |= ninethirty.handle_tick(code, d) ret |= today_bull.handle_today_bull(code, d) return ret
def get_tick_data(code, today, t): from_dt = datetime.combine( today, time(int(t / 10000), int(t % 10000 / 100), int(t % 100))) until_dt = datetime.combine(today, time(0)) + timedelta(days=1) tick_data = db_collection[code].find( {'date': { '$gt': from_dt, '$lt': until_dt }}) converted_data = [] for td in tick_data: converted = dt.cybos_stock_tick_convert(td) converted_data.append(converted) #print('get_tick_data', code, today, t, 'tick len', len(converted_data)) return converted_data
def get_tick_data(code, today, db_collection): from_datetime = datetime.combine(today, time(0, 0)) until_datetime = datetime.combine(today + timedelta(days=1), time(0, 0)) data = list(db_collection[code].find( {'date': { '$gte': from_datetime, '$lte': until_datetime }})) converted_data = [] for td in data: if code.endswith(message.BIDASK_SUFFIX): converted = dt.cybos_stock_ba_tick_convert(td) else: converted = dt.cybos_stock_tick_convert(td) converted_data.append(converted) return converted_data
def get_open_price(code, search_time, db_collection): open_tick_data = list(db_collection[code].find({ 'date': { '$gte': datetime.combine(search_time.date(), time(8, 58)), '$lte': datetime.combine(search_time.date(), time(9, 5)) } })) if len(open_tick_data) == 0: #print('no open tick data', code) return 0 converted_data = [] for td in open_tick_data: converted = dt.cybos_stock_tick_convert(td) converted_data.append(converted) open_price, _ = filter_in_market_tick(converted_data) return open_price
def tick_data_handler(self, code, data): if len(data) != 1: return tick_data = data[0] tick_data = dt.cybos_stock_tick_convert(tick_data) has_change = self.market_status.set_tick_data(tick_data) self.current_price = tick_data['current_price'] # for skipping first tick of in-market data if not has_change and self.market_status.is_in_market(): if self.open_price == 0 and tick_data['start_price'] != 0: self.open_price = tick_data['start_price'] self.tick_data.append(tick_data) if self.trader is not None: self.trader.tick_data_handler(tick_data)
def get_tick_data(code, tick_date): tick_date = tick_date if tick_date.__class__.__name__ == 'date' else tick_date.date( ) from_datetime = datetime.combine(tick_date, time(0, 0)) until_datetime = datetime.combine(tick_date + timedelta(days=1), time(0, 0)) data = list(get_collection()[code].find( {'date': { '$gte': from_datetime, '$lte': until_datetime }})) converted_data = [] for td in data: if code.endswith(message.BIDASK_SUFFIX): converted = dt.cybos_stock_ba_tick_convert(td) else: converted = dt.cybos_stock_tick_convert(td) converted_data.append(converted) return converted_data
def add_tick_data(self, datas, minute_handler): tick_data = [] for d in datas: tick_data.append(dt.cybos_stock_tick_convert(d)) if len(tick_data) == 0: return elif len(tick_data) > 1: print('tick data len is over 1') data = tick_data[0] hour = int(data['time_with_sec'] / 10000) min = int(data['time_with_sec'] % 10000 / 100) second = int(data['time_with_sec'] % 100) data['date'] = datetime.combine(self.today, time(hour, min, second)) if (data['market_type'] == dt.MarketType.IN_MARKET or (data['market_type'] == dt.MarketType.PRE_MARKET_EXP and self.start_time <= data['date'] <= self.close_time)): self.in_market_data.append(data) self.create_minute_data(minute_handler)
def handler(code, datas): global within_time_count global out_time_count tick_data = [] now = datetime.now() for d in datas: tick_data.append(dt.cybos_stock_tick_convert(d)) if len(tick_data) == 0: return elif len(tick_data) > 1: print('tick data len is over 1') data = tick_data[0] hour = int(data['time_with_sec'] / 10000) min = int(data['time_with_sec'] % 10000 / 100) second = int(data['time_with_sec'] % 100) data['date'] = datetime.combine(now.date(), time(hour, min, second)) if now - data['date'] > timedelta(seconds=30): print('delayed', (now - data['date']).seconds) out_time_count += 1 else: within_time_count += 1
def start_search(code, search_time, yesterday_list, price, qty, db_collection): print('start_search') code_dict = dict() no_open_price_count = 0 for progress, data in enumerate(yesterday_list): ycode = data['code'] open_price = get_open_price(ycode, search_time, db_collection) if open_price == 0: #print('cannot get open price', ycode) if ycode == code: print(code, 'cannot get open price') sys.exit(1) no_open_price_count += 1 print( 'get tick info', f'{progress+1-no_open_price_count}/{no_open_price_count}/{len(yesterday_list)}', end='\r') continue print( 'get tick info', f'{progress+1-no_open_price_count}/{no_open_price_count}/{len(yesterday_list)}', end='\r') # ex) 9:02 -> then find from 9:01 <= time < 9:03 tick_data = list(db_collection[ycode].find({ 'date': { '$gte': search_time - timedelta(seconds=60), '$lt': search_time + timedelta(seconds=60) } })) converted_data = [] for td in tick_data: converted = dt.cybos_stock_tick_convert(td) converted_data.append(converted) if (search_time.hour == 9 and search_time.minute < 2) or search_time.hour < 9: _, converted_data = filter_in_market_tick(converted_data) code_dict[ycode] = { 'tick': converted_data, 'yesterday_data': data, 'open': open_price } # loop ex) 9:02 -> from 9:02:00 to 9:02:59 loop by increment 1 second and search past 10 sec, 20 sec, 30 sec, 40 sec, 50 sec, 60 sec print('') until_time = search_time + timedelta(seconds=60) if code not in code_dict: print(code, 'not in code_dict') sys.exit(1) time_price_match = [] last_time = None target_code_tick = list( filter(lambda x: search_time <= x['date'] < until_time, code_dict[code]['tick'])) for tc in target_code_tick: if tc['current_price'] == price and tc['volume'] == qty: time_price_match.append(tc['date'].replace(microsecond=0)) time_price_match = list(dict.fromkeys(time_price_match)) print('matched time', time_price_match) while search_time < until_time: if not search_time in time_price_match: search_time += timedelta(seconds=1) continue time_interval = [60, 50, 40, 30, 20, 10, 5, 3] for t in time_interval: rank_list = [] for k, v in code_dict.items(): tick = code_dict[k]['tick'] tick = list( filter( lambda x: search_time - timedelta(seconds=t) <= x[ 'date'] < search_time, tick)) if len(tick) == 0: continue profit = (tick[-1]['current_price'] - tick[0]['current_price'] ) / tick[0]['current_price'] * 100 amount = sum([d['current_price'] * d['volume'] for d in tick]) open_price = code_dict[k]['open'] from_open = (tick[-1]['current_price'] - open_price) / open_price * 100 rank_list.append({ 'code': k, 'profit': profit, 'amount': amount, 'from_open': from_open }) rank_by_profit = sorted(rank_list, key=lambda x: x['profit'], reverse=True) rank_by_amount = sorted(rank_list, key=lambda x: x['amount'], reverse=True) print_rank_code(code, t, search_time, rank_by_profit, rank_by_amount) print('-' * 100) for rbp in rank_by_profit[:10]: print_code_info(rbp) print('*' * 100) for rba in rank_by_amount[:10]: print_code_info(rba) print('-' * 100) search_time += timedelta(seconds=1)
def test_cybos_stock_tick(): a = {'0': 'A005930', '1': 'Samsung'} new_a = dt.cybos_stock_tick_convert(a) assert dt.stock_tick[dt.CybosStockTick.CODE] in new_a.keys() assert dt.stock_tick[dt.CybosStockTick.COMPANY_NAME] in new_a.keys()