def get_period_data_raw(code, start_date, end_date=0, period_type='m'): #print("Get Period data ", start_date, end_date, time_converter.datetime_to_intdate(start_date), time_converter.datetime_to_intdate(end_date)) data = [] conn = Connection() while conn.request_left_count() <= 0: print('Request Limit is reached', flush=True) time.sleep(1) chart_obj = win32com.client.Dispatch("CpSysDib.StockChart") chart_obj.SetInputValue(0, code) chart_obj.SetInputValue(1, ord('1')) if end_date == 0: chart_obj.SetInputValue(2, 0) else: chart_obj.SetInputValue(2, time_converter.datetime_to_intdate(end_date)) chart_obj.SetInputValue(3, time_converter.datetime_to_intdate(start_date)) chart_obj.SetInputValue(4, 10000) data_list = [0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 16, 17, 20, 21] chart_obj.SetInputValue(5, data_list) chart_obj.SetInputValue(6, ord(period_type)) chart_obj.SetInputValue(9, ord('1')) chart_obj.SetInputValue(10, ord('1')) chart_obj.BlockRequest() data_len = chart_obj.GetHeaderValue(3) for i in range(data_len): d = {} for j in range(len(data_list)): d[str(j)] = chart_obj.GetDataValue(j, i) data.append(d) return len(data), reversed(data)
def get_period_data_raw(code, start_date, end_date=0, period_type='m'): #print("Get Period data ", start_date, end_date, time_converter.datetime_to_intdate(start_date), time_converter.datetime_to_intdate(end_date)) data = [] conn = connection.Connection() conn.wait_until_available() chart_obj = win32com.client.gencache.EnsureDispatch("CpSysDib.StockChart") chart_obj.SetInputValue(0, code) chart_obj.SetInputValue(1, ord('1')) if end_date == 0: chart_obj.SetInputValue(2, 0) else: chart_obj.SetInputValue(2, time_converter.datetime_to_intdate(end_date)) chart_obj.SetInputValue(3, time_converter.datetime_to_intdate(start_date)) chart_obj.SetInputValue(4, 10000) data_list = [0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 16, 17, 20, 21] chart_obj.SetInputValue(5, data_list) chart_obj.SetInputValue(6, ord(period_type)) chart_obj.SetInputValue(9, ord('1')) chart_obj.SetInputValue(10, ord('1')) chart_obj.BlockRequest() data_len = chart_obj.GetHeaderValue(3) for i in range(data_len): d = {} for j in range(len(data_list)): d[str(j)] = chart_obj.GetDataValue(j, i) data.append(d) return len(data), reversed(data)
def set_target(self, target): code = target.split(':')[1] self.target_code = code day_code = code + '_D' stock = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] s = time_converter.datetime_to_intdate(self.from_date) f = time_converter.datetime_to_intdate(self.until_date) cursor = stock[day_code].find({'0': {'$gte': s, '$lte': f}}) self.data = list(cursor) logger.print(target, 'Length', len(self.data))
def get_day_past_highest(code, today, days): stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] yesterday = today - timedelta(days=1) while is_holidays(yesterday): yesterday -= timedelta(days=1) start = time_converter.datetime_to_intdate(yesterday - timedelta(days=days)) end = time_converter.datetime_to_intdate(yesterday) cursor = stock_db[code + '_D'].find({'0': {'$gte': start, '$lt': end}}) if cursor.count() == 0: return 0 return max([d['3'] for d in list(cursor)])
def get_day_minute_period_data_force_from_db(code, from_date: date, until_date: date, db_suffix='_M'): from_date = from_date if from_date.__class__.__name__ == 'date' else from_date.date( ) until_date = until_date if until_date.__class__.__name__ == 'date' else until_date.date( ) stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] return list(stock_db[code + db_suffix].find({ '0': { '$gte': time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date) } }))
def add_body(self, body, header): self.data.append(body) if len(body) == 0: stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] code = db.tr_code(header['code']) from_date = header['from'] until_date = header['until'] while from_date <= until_date: stock_db[code + '_V'].insert_one( {'0': time_converter.datetime_to_intdate(from_date)}) from_date += timedelta(days=1) #print('RECORD DATA to DB as EMPTY', header['method'], header['code'], header['from'], header['until']) else: stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] code = db.tr_code(header['code']) if header['method'] == message.DAY_DATA: stock_db[code + '_D'].insert_many(body) elif header['method'] == message.MINUTE_DATA: stock_db[code + '_M'].insert_many(body) elif header['method'] == message.INVESTOR_DATA: stock_db[code + '_INVESTOR'].insert_many(body) #print('RECORD DATA to DB', header['method'], header['code'], header['from'], header['until']) return len(self.data) == self.count
def evaluate_meet_goal(reader, today): meet_count = 0 cut_count = 0 today_d = time_converter.datetime_to_intdate(today) for code in market_code: if code_dict[code]['state'] != STATE_BOUGHT or code_dict[code][ 'buy_date'] == today: continue elif today_d not in code_dict[code]['data']: continue data = code_dict[code]['data'][today_d] buy_price = code_dict[code]['buy_price'] #print(buy_price, data['lowest_price'], (data['lowest_price'] - buy_price) / buy_price * 100 ) if (data['lowest_price'] - buy_price) / buy_price * 100 <= -10: cut_count += 1 report.append({ 'code': code, 'buy_price': buy_price, 'sell_price': data['lowest_price'], 'set_date': code_dict[code]['setdate'], 'buy_date': code_dict[code]['buy_date'], 'sell_date': today, 'profit': (data['lowest_price'] - buy_price) / buy_price * 100 }) #print('sell') code_dict[code]['state'] = STATE_NONE break elif (data['highest_price'] - buy_price) / buy_price * 100 >= 10: #print('good') meet_count += 1 report.append({ 'code': code, 'buy_price': buy_price, 'sell_price': data['lowest_price'], 'set_date': code_dict[code]['setdate'], 'buy_date': code_dict[code]['buy_date'], 'sell_date': today, 'profit': (data['highest_price'] - buy_price) / buy_price * 100 }) code_dict[code]['state'] = STATE_NONE break return meet_count, cut_count
def get_past_data(reader, code, from_date, until_date): past_data = stock_api.request_stock_day_data(reader, code, from_date, until_date) if trader_env.MAVG * 2 * 0.6 > len(past_data): #print('PAST DATA too short', len(past_data), code) return [] elif past_data[-1]['0'] != time_converter.datetime_to_intdate(until_date): #print(code, until_date, 'Cannot get last day data') return [] return past_data
def get_today_min_data(code, period_type='m'): result = get_today_data_raw(code, period_type) today_data = [] today_int = time_converter.datetime_to_intdate(datetime.now()) for r in result: if r['0'] == today_int: today_data.append(r) return len(today_data), today_data
def _get_data_from_db(code, from_date, until_date, db_suffix): from_date = from_date if from_date.__class__.__name__ == 'date' else from_date.date() until_date = until_date if until_date.__class__.__name__ == 'date' else until_date.date() now = datetime.now() from_date = _correct_date(from_date, now) until_date = _correct_date(until_date, now) stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] db_data = list(stock_db[code + db_suffix].find({'0': {'$gte':time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date)}})) print(code + db_suffix, time_converter.datetime_to_intdate(from_date), time_converter.datetime_to_intdate(until_date)) db_data = sort_db_data(db_data, db_suffix) days = [time_converter.intdate_to_datetime(d['0']).date() for d in db_data] days = list(dict.fromkeys(days)) working_days = get_working_days(from_date, until_date) vacancy_data = list(stock_db[code + '_V'].find({'0': {'$gte':time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date)}})) vacancy_days = [time_converter.intdate_to_datetime(d['0']).date() for d in vacancy_data] empties = _check_empty_date(days, vacancy_days, working_days) #print('DB DATA', len(db_data), 'EMPTIES', empties) return db_data, empties
def record_uni_data(): while True: now = datetime.now() result = morning_client.get_uni_day_data('A005930') if len(result) > 0 and result[-1][ 'date'] == time_converter.datetime_to_intdate(now.date()): break gevent.sleep(60) market_code = morning_client.get_all_market_code() for code in market_code: morning_client.get_uni_current_data(code) morning_client.get_uni_day_data(code)
def get_day_period_data(code, from_date: date, until_date: date, db_suffix='_D'): from_date = from_date if from_date.__class__.__name__ == 'date' else from_date.date( ) until_date = until_date if until_date.__class__.__name__ == 'date' else until_date.date( ) if from_date > datetime.now().date(): from_date = datetime.now().date() if until_date > datetime.now().date(): # Cannot exceed today until_date = datetime.now().date() stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] db_data = list(stock_db[code + db_suffix].find({ '0': { '$gte': time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date) } })) #print('DB LEN', len(db_data)) days = [time_converter.intdate_to_datetime(d['0']).date() for d in db_data] days = list(dict.fromkeys(days)) #print('DAYS:', days) working_days = get_working_days(from_date, until_date) #print('WORKING DAYS:', working_days) empties = _insert_day_data(stock_db, code, days, working_days, db_suffix) if len(empties) > 0: db_data = list(stock_db[code + db_suffix].find({ '0': { '$gte': time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date) } })) #print(db_data) return db_data
def check_investor_trend(code, start_date, end_date): conn = connection.Connection() obj = win32com.client.gencache.EnsureDispatch('CpSysDib.CpSvr7254') obj.SetInputValue(0, code) obj.SetInputValue(1, 0) obj.SetInputValue(2, time_converter.datetime_to_intdate(start_date)) obj.SetInputValue(3, time_converter.datetime_to_intdate(end_date)) obj.SetInputValue(4, ord('0')) obj.SetInputValue(5, 0) obj.SetInputValue(6, ord('1')) now = datetime.now() continue_request = True datas = [] prev = None while continue_request: conn.wait_until_available() obj.BlockRequest() count = obj.GetHeaderValue(1) if count == 0: break for i in range(count): d = {} for j in range(19): d[str(j)] = obj.GetDataValue(j, i) if prev != None and prev['0'] <= d['0']: continue_request = False break if now - time_converter.intdate_to_datetime(d['0']) > timedelta(days=365*5): continue_request = False prev = d datas.append(d) datas = sorted(datas, key=lambda x: x['0']) return datas
def get_highest_price(self, date, code, price): if self.use_cybos: from morning.cybos_api import stock_chart l, data = stock_chart.get_day_period_data(code, date, date) if l == 1: return data[0]['3'] else: stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] d = time_converter.datetime_to_intdate(date) cursor = stock_db[code + '_D'].find({'0': {'$gte':d, '$lte': d}}) if cursor.count() == 1: return (list(cursor))[0]['3'] return price
def get_window_data(reader, code, until_date): past_data = stock_api.request_stock_day_data(reader, code, until_date - timedelta(days=int(window_size*2)), until_date) if window_size > len(past_data): return [], [], [], [], [] elif past_data[-1]['0'] != time_converter.datetime_to_intdate(until_date): return [], [], [], [], [] past_data_c = [] for data in past_data[-90:]: past_data_c.append(dt.cybos_stock_day_tick_convert(data)) close_datas = [d['close_price'] for d in past_data_c] amount_datas = [d['amount'] for d in past_data_c] close_datas_recent = close_datas[-30:] amount_datas_recent = amount_datas[-30:] return past_data_c, close_datas, amount_datas, close_datas_recent, amount_datas_recent
def start_preload(dt, skip_ydata, skip_uni): global _market_codes, loading, _yesterday _LOGGER.info('Start Loading %s', dt) if len(_market_codes) == 0: # load infomration here, not depend on date _market_codes = morning_client.get_all_market_code() code_info.load_code_info(_market_codes) if not skip_uni: uni_current.load_uni_data(_market_codes) _yesterday_minute_data.clear() _yesterday_day_data.clear() yesterday = holidays.get_yesterday(dt) _yesterday = time_converter.datetime_to_intdate(yesterday) _LOGGER.info('Yesterday: %s', yesterday) if not skip_ydata: _get_yesterday_day_data(yesterday, _market_codes) #_get_yesterday_min_data(yesterday, _market_codes) loading = False
def query_uni_current_period_data(code, from_date, until_date): stock_db = MongoClient(db.HOME_MONGO_ADDRESS)['stock'] query = {'$gte': time_converter.datetime_to_intdate(from_date), '$lte': time_converter.datetime_to_intdate(until_date)} db_data = list(stock_db[code + message.UNI_CURRENT_SUFFIX].find({'27': query})) return db_data
def start_today_trading(reader, market_code, today, bull_profit, buy_profit): today_d = time_converter.datetime_to_intdate(today) for code in market_code: if today_d not in code_dict[code]['data']: continue elif code_dict[code]['state'] == STATE_BOUGHT: continue elif code_dict[code]['data'][today_d]['moving_average'] == 0: continue data = code_dict[code]['data'][today_d] if code_dict[code]['state'] == STATE_UNKNOWN: if data['start_price'] < data['moving_average'] and data[ 'close_price'] < data['moving_average']: code_dict[code]['state'] = STATE_NONE else: continue profit = (data['close_price'] - data['yesterday_close']) / data['yesterday_close'] * 100 code_dict[code]['today_bull'] = profit code_dict[code]['mvg'] = data['moving_average'] over_avg = data['start_price'] > data['moving_average'] and data[ 'close_price'] > data['moving_average'] if code_dict[code]['state'] == STATE_NONE: code_dict[code]['under_line_count'] = 0 bull_distance = ( today - code_dict[code]['last_bull_date'] ).days if code_dict[code]['last_bull_date'] is not None else 0 if profit >= bull_profit: if profit >= buy_profit and over_avg and data[ 'amount'] >= 30000000000 and bull_distance > 10: code_dict[code]['buy_price'] = data['start_price'] code_dict[code]['amount'] = data['amount'] code_dict[code]['state'] = STATE_BULL code_dict[code]['setdate'] = today code_dict[code]['highest'] = data['highest_price'] else: code_dict[code]['last_bull_date'] = today elif code_dict[code]['state'] == STATE_BULL: distance = (today - code_dict[code]['setdate']).days again_bull = profit >= bull_profit and data[ 'amount'] >= 30000000000 and data['amount'] > code_dict[code][ 'amount'] if distance > 6 and again_bull and data['close_price'] > code_dict[ code]['highest']: #print('again', data['start_price']) code_dict[code]['buy_price'] = data['start_price'] code_dict[code]['amount'] = data['amount'] code_dict[code]['setdate'] = today code_dict[code]['highest'] = data['highest_price'] elif data['lowest_price'] <= code_dict[code]['buy_price']: code_dict[code]['state'] = STATE_UNDER_LINE #code_dict[code]['buy_date'] = today if data['highest_price'] > code_dict[code]['highest']: code_dict[code]['highest'] = data['highest_price'] elif code_dict[code]['state'] == STATE_UNDER_LINE: #if code_dict[code]['buy_price'] < data['start_price'] and code_dict[code]['buy_price'] < data['close_price']: if code_dict[code]['buy_price'] > data['start_price'] and code_dict[ code]['buy_price'] < data['highest_price']: #code_dict[code]['buy_price'] = data['close_price'] code_dict[code]['buy_date'] = today code_dict[code]['state'] = STATE_BOUGHT else: code_dict[code]['under_line_count'] += 1 if code_dict[code]['under_line_count'] > 30: code_dict[code]['state'] = STATE_NONE
def get_year_high_by_datetime(code, dt): t = time_converter.datetime_to_intdate(dt) return get_year_high(code, t)
def get_uni_current_by_datetime(code, dt): t = time_converter.datetime_to_intdate(dt) return get_uni_current(code, t)