def get_past_datas(code, today, t, code_dict): yesterday = holidays.get_yesterday(today) data = morning_client.get_past_day_data(code, yesterday, yesterday) if len(data) != 1: print('Cannot get yesterday data', code, yesterday) return code_dict[code]['yesterday_data'] = data[0] min_data = morning_client.get_minute_data(code, today, today) hour = int(t / 10000) minute = int((t % 10000) / 100) vi_datetime = datetime.combine(today, time(hour, minute)) before_vi_datetime = vi_datetime - timedelta(seconds=60) after_vi_datetime = vi_datetime + timedelta(seconds=120) before_datas = [] after_datas = [] for mdata in min_data: if mdata[ 'time'] <= before_vi_datetime.hour * 100 + before_vi_datetime.minute: before_datas.append(mdata) elif mdata[ 'time'] >= after_vi_datetime.hour * 100 + after_vi_datetime.minute: after_datas.append(mdata) code_dict[code]['today_min_data'] = before_datas code_dict[code]['today_min_after_vi'] = after_datas[:-1] for mdata in code_dict[code]['today_min_data']: if mdata['highest_price'] > code_dict[code]['vi_highest']: code_dict[code]['vi_highest'] = mdata['highest_price'] """
def GetPastMinuteData(self, request, context): _LOGGER.info('GetPastMinuteData %s', request) today = request.today.ToDatetime() yesterday = holidays.get_yesterday(today) from_date = holidays.get_date_by_previous_working_day_count( yesterday, request.count_of_days - 1) minute_datas = morning_client.get_minute_data(request.code, from_date, yesterday) protoc_converted = [] for m in minute_datas: protoc_converted.append( stock_provider_pb2.CybosDayData( date=m['0'], time=m['time'], start_price=int(m['start_price']), highest_price=int(m['highest_price']), lowest_price=int(m['lowest_price']), close_price=int(m['close_price']), volume=m['volume'], amount=m['amount'], cum_sell_volume=m['cum_sell_volume'], cum_buy_volume=m['cum_buy_volume'], foreigner_hold_volume=m['foreigner_hold_volume'], foreigner_hold_rate=m['foreigner_hold_rate'], institution_buy_volume=m['institution_buy_volume'], institution_cum_buy_volume=m['institution_cum_buy_volume']) ) return stock_provider_pb2.CybosDayDatas(day_data=protoc_converted)
def GetMinuteData(self, request, context): _LOGGER.info('GetMinuteData %s %s %s', request.code, request.from_datetime.ToDatetime() + timedelta(hours=9), request.until_datetime.ToDatetime() + timedelta(hours=9)) minute_datas = morning_client.get_minute_data( request.code, request.from_datetime.ToDatetime() + timedelta(hours=9), request.until_datetime.ToDatetime() + timedelta(hours=9)) protoc_converted = [] for m in minute_datas: protoc_converted.append( stock_provider_pb2.CybosDayData( date=m['0'], time=m['time'], start_price=int(m['start_price']), highest_price=int(m['highest_price']), lowest_price=int(m['lowest_price']), close_price=int(m['close_price']), volume=m['volume'], amount=m['amount'], cum_sell_volume=m['cum_sell_volume'], cum_buy_volume=m['cum_buy_volume'], foreigner_hold_volume=m['foreigner_hold_volume'], foreigner_hold_rate=m['foreigner_hold_rate'], institution_buy_volume=m['institution_buy_volume'], institution_cum_buy_volume=m['institution_cum_buy_volume']) ) return stock_provider_pb2.CybosDayDatas(day_data=protoc_converted)
def get_past_datas(code, today, yesterday, t): data = morning_client.get_past_day_data(code, yesterday, yesterday) if len(data) != 1: print('Cannot get yesterday data', code, yesterday) return code_dict[code]['yesterday_data'] = data[0] # get minute data time < (t / 100) min_data = morning_client.get_minute_data(code, today, today, int(t / 100)) code_dict[code]['today_min_data'] = min_data code_dict[code]['today_tick_data'] = get_tick_data(code, today, t) for mdata in code_dict[code]['today_min_data']: if mdata['highest_price'] > code_dict[code]['vi_highest']: code_dict[code]['vi_highest'] = mdata['highest_price']
def _get_yesterday_min_data(query_date, market_code): global _yesterday_minute_data fail_count = 0 _LOGGER.info('START collect yesterday min data') for progress, code in enumerate(market_code): if _request_stop: break #print('collect yesterday minute data', f'{progress+1}/{len(market_code)}/{fail_count}', end='\r') mdata = morning_client.get_minute_data(code, query_date, query_date) if len(mdata) > 0: _yesterday_minute_data[code] = mdata else: fail_count += 1 _LOGGER.info('DONE collect yesterday min data, fail cnt %d', fail_count)
def start_trading(tdate, codes): print('Start', tdate, len(codes)) yesterday = holidays.get_yesterday(tdate) start_time = datetime.now() yesterday_uni_data = get_yesterday_uni_day_data(yesterday, codes) print('Uni data count', len(yesterday_uni_data)) yesterday_uni_data = sorted(yesterday_uni_data, key=lambda x: x['market_close_profit'], reverse=True) codes = [d['code'] for d in yesterday_uni_data] yesterday_dict = get_yesterday_day_data(yesterday, codes) result = [] for ud in yesterday_uni_data: tdata = morning_client.get_minute_data(ud['code'], tdate, tdate) highest = 0 amount = 0 highest_time = None open_price = 0 for d in tdata: if open_price == 0: open_price = d['start_price'] if d['time'] > 931: break if d['highest_price'] > highest: highest = d['highest_price'] highest_time = d['time'] amount += d['amount'] if highest_time is not None: if ud['market_close'] == 0: print('close 0', ud) continue ud['date'] = d['0'] ud['today_open_price'] = open_price ud['today_open_profit'] = get_profit(open_price, ud['market_close']) ud['today_highest_time'] = highest_time ud['today_highest_price'] = highest ud['today_max_profit'] = get_profit(highest, ud['market_close']) ud['today_amount'] = amount ud['yesterday_amount'] = yesterday_dict[ud['code']]['amount'] result.append(ud) return result
def start_search(code, search_time, kosdaq_market_code): search_time = datetime.strptime(search_time, '%Y-%m-%d %H:%M') # Since kiwoom start minute from 900 but creon starts from 901 search_time_hr = search_time.hour * 100 + search_time.minute + 1 data_list = [] for kosdaq_code in kosdaq_market_code: data = morning_client.get_minute_data(kosdaq_code, search_time.date(), search_time.date()) fdata = list(filter(lambda x: x['time'] == search_time_hr, data)) if len(fdata) == 1: stock_data = fdata[0] profit = (stock_data['close_price'] - stock_data['start_price'] ) / stock_data['start_price'] * 100 data_list.append({ 'code': kosdaq_code, 'profit': profit, 'amount': stock_data['amount'] }) #else: # print(kosdaq_code, 'len', len(fdata), len(data)) by_profit = sorted(data_list, key=lambda x: x['profit'], reverse=True) print('*' * 100) for bp in enumerate(by_profit[:10]): print(bp) print('*' * 100) for i, bp in enumerate(by_profit): if bp['code'] == code: print('by profit rank', i + 1) print(bp) break by_amount = sorted(data_list, key=lambda x: x['amount'], reverse=True) print('*' * 100) for ba in enumerate(by_amount[:10]): print(ba) print('*' * 100) print('amount rank1', by_amount[0]['amount']) for i, ba in enumerate(by_amount): if ba['code'] == code: print('by amount rank', i + 1) print(ba) break
def get_yesterday_data(today, market_code): yesterday = holidays.get_yesterday(today) one_week_before = holidays.get_date_by_previous_working_day_count(today, 5) yesterday_list = [] for progress, code in enumerate(market_code): print('collect yesterday data', f'{progress+1}/{len(market_code)}', end='\r') data = morning_client.get_past_day_data(code, yesterday, yesterday) if len(data) == 1: data = data[0] data['code'] = code min_data = morning_client.get_minute_data(code, one_week_before, yesterday) if len(min_data) > 0: data['minute_max_volume'] = max( [d['volume'] for d in min_data]) else: data['minute_max_volume'] = 0 yesterday_list.append(data) print('') return yesterday_list
def start_trading(tdate, codes): print('Start', tdate, len(codes)) yesterday = holidays.get_yesterday(tdate) start_time = datetime.now() yesterday_data = get_yesterday_day_data(yesterday, codes) amount_passed = [] for data in yesterday_data: code = data['code'] if data['amount'] >= 3000000000: yesterday_close[code] = data['close_price'] yesterday_amount[code] = data['amount'] amount_passed.append(code) print('amount passed', len(amount_passed)) range_passed = [] for code in amount_passed: if is_price_in_range(yesterday, code): range_passed.append(code) print('range passed', len(range_passed)) meet_codes = [] for code in range_passed: state = STATE_START highest_in_today[code] = 0 tdata = morning_client.get_minute_data(code, tdate, tdate) ydata = morning_client.get_minute_data(code, yesterday, yesterday) if len(tdata) == 0: print(code, 'Today Min DATA is empty') continue current_data = [] current_data.extend(ydata) today_open_price[code] = tdata[0]['start_price'] today_amount = 0 for data in tdata: current_data.append(data) if data['time'] <= 930: today_amount += data['amount'] if data['highest_price'] > highest_in_today[code]: highest_in_today[code] = data['highest_price'] continue else: if yesterday_amount[code] > today_amount: break mavg = statistics.mean( [d['close_price'] for d in current_data[-60:]]) if state == STATE_START: if mavg < data['highest_price']: if data['highest_price'] > highest_in_today[code]: highest_in_today[code] = data['highest_price'] else: state = STATE_UNDER_MAVG if state == STATE_UNDER_MAVG: if data['close_price'] >= mavg * 1.01: state = STATE_MEET_MAVG if state == STATE_MEET_MAVG: meet_codes.append({ 'code': code, 'date': data['0'], 'time': data['time'], 'open profit': get_profit(today_open_price[code], yesterday_close[code]), 'current profit:': get_profit(data['close_price'], yesterday_close[code]), 'mavg': mavg, 'current_close': data['close_price'], 'current_highest': data['highest_price'], 'yesterday_amount': yesterday_amount[code], 'today_930_amount': today_amount }) print(meet_codes) break # calculate target price # or cut when meet under mavg / 0 % cut return meet_codes
def start_trading(tdate, codes): print('Start', tdate, len(codes)) yesterday = holidays.get_yesterday(tdate) get_day_data(yesterday, codes) for progress, code in enumerate(codes): if code not in yesterday_data or yesterday_data[code]['amount'] == 0: continue tdata = morning_client.get_minute_data(code, tdate, tdate) amount = 0 start_watch = False found_point = None previous_data = None start_data = None report = { 'date': tdate.year * 10000 + tdate.month * 100 + tdate.day, 'code': code, 'amount': 0, 'yesterday_amount': 0, 'open_profit': 0, 'pre_highest': 0, 'amount_x': 0, 'catch_highest_time': 0, 'buy_price': 0, 'highest_price': 0, 'over_time': 0, 'low_price': 0, 'low_time': 0 } for data in tdata: if start_data is None: start_data = data if data['time'] <= 930: amount += data['amount'] if data['highest_price'] > report['pre_highest']: report['pre_highest'] = data['highest_price'] continue elif not start_watch and data[ 'time'] > 930 and amount > yesterday_data[code][ 'amount'] and data['highest_price'] < report[ 'pre_highest']: start_watch = True report['amount'] = amount report['yesterday_amount'] = yesterday_data[code]['amount'] report['over_time'] = data['time'] print('found 1', code) if not start_watch: break if found_point is None: if data['time'] >= 1449: break else: if data['start_price'] > report['pre_highest']: print('found 2', code) report['catch_highest_time'] = data['time'] report['open_profit'] = float("{0:.2f}".format( (data['start_price'] - start_data['start_price']) / start_data['start_price'] * 100.0)) report['amount_x'] = float("{0:.2f}".format( amount / yesterday_data[code]['amount'])) report['buy_price'] = data['start_price'] found_point = datetime(tdate.year, tdate.month, tdate.day, int(data['time'] / 100), int(data['time'] % 100), 0) else: dt = datetime(tdate.year, tdate.month, tdate.day, int(data['time'] / 100), int(data['time'] % 100), 0) if dt - found_point >= timedelta(minutes=30): report['high_profit'] = float("{0:.2f}".format( (report['highest_price'] - report['buy_price']) / report['buy_price'] * 100.0)) report['low_profit'] = float("{0:.2f}".format( (report['low_price'] - report['buy_price']) / report['buy_price'] * 100.0)) result.append(report) print('report len', len(result)) break else: if data['highest_price'] > report['highest_price']: report['highest_price'] = data['highest_price'] report['post_high_time'] = data['time'] if report['low_price'] == 0 or data[ 'lowest_price'] < report['low_price']: report['low_price'] = data['lowest_price'] report['low_time'] = data['time'] """