def __series_of_data__(self, code, days=9, start_date=''): ''' Generate a series of data for the duration demanded :param code: str, stock index :param days: int, the duration :param start_date: str, the start date :return: a list contains the data for each days ''' days_list = at.opening_days(code, days, start_date) data_list = [] thread = [] q = queue.Queue() def catch(code, date, queque): catch = ms.get_stock_hist_data(code, date) queque.put(catch) for i in days_list: t = threading.Thread(target=catch, args=(code, i, q)) thread.append(t) for j in thread: j.start() for k in thread: k.join() while not q.empty(): data_list.append(q.get()) data_list = at.sort_list_by_date(data_list) return data_list
def periodic_auction_scanner(code, days, start_date='', progress_bar=1): ''' Scanner the periodic auction volume for multiple days :param code: the stock index :param days: the duration :param start_date: specifying the start date if needed :return: a list of (date, ratio) pairs ''' try: outstanding = ms.get_stock_outstanding(code) content_list = [] days_list = at.opening_days(code, days, start_date) count = 1 for i in days_list: volume = al.periodic_auction_volume(code, i) ratio = float(volume / outstanding) ratio = round(ratio * 100, 5) pair = (i, ratio) content_list.append(pair) if progress_bar == 1: at.process_monitor(count / len(days_list) * 100) count += 1 return content_list except: print(error_message)
def kdj(self, code, date='', method=9, smooth=30): ''' Return the (k, d, j) given a specific time :param code: str, stock index :param date: str, the date needed :param method: the range took into the calculation, default 9 days :param smooth: the smooth period, default 30 days :return: the value, (k, d, j) ''' rsv_list = [] days_list = at.opening_days(code, smooth, date) count = 1 for i in days_list: rsv_h = self.__rsv__(code, i, method) rsv_list.append(rsv_h) at.process_monitor(count / len(days_list) * 100) count += 1 k_line = [50] d_line = [50] for j in rsv_list: k_hold = j / 3 + 2 * k_line[-1] / 3 k_line.append(k_hold) d_hold = k_line[-1] / 3 + 2 * d_line[-1] / 3 d_line.append(d_hold) k = k_line[-1] d = d_line[-1] j = 3 * k - 2 * d if j > 100: j = 100 return (k, d, j)
def get_stock_hist_data_yesterday(code, date_t, type=''): ''' Get the stock's data of the previous day of a specific day :param code: str, stock index :param date: str, date :param type: str, specify the value returned :return: a list of relevant information ''' date = at.opening_days(code, 2, date_t)[0] return ms.get_stock_hist_data(code, date, type)
def __opening_dates__(self, code, days, date=''): ''' Get the opening trading days :param code: str, stock index :param days: int, the duration :param date: str, start date :return: a list of opening days ''' days = at.opening_days(code, days, date) if len(days) == 1: return days[0] else: return days
def periodic_auction_volume(code, date): ''' Get the volume during the periodic auction period :param code: string, stock index :param date: string, date in format '2016-10-11' :return: int, the volume ''' tick_data = ms.get_tick_data(code, date) while str(tick_data.ix[0].tolist()[-1]) == 'nan': date = at.opening_days(days=2)[0] tick_data = ms.get_tick_data(code, date) volume = tick_data[-1:].volume.values[0] if math.isnan(volume): volume = 0 volume = 100 * volume volume = int(volume) return volume
def save_tick_summary(code_list, date, duration, detail=True, cut=None): path = os.path.join(os.getcwd(), 'tick_summary') if not os.path.exists(path): os.mkdir(path) for code in code_list: date_list = at.opening_days(code, duration, date) file_path = os.path.join(path, '%s-%s-%s.txt'%(code, date, duration)) count = 0 with open(file_path, 'w') as w: for date in date_list: count += 1 w.writelines('%s\n'%date) w.writelines('\n') t = TickAna(code, date, cut) aggre_summary = t.aggregate_summary() w.writelines('\t%s\n' % 'Summary') w.writelines('\t\tbuy amount: %s\n' % aggre_summary['buy_amount']) w.writelines('\t\tsell amount: %s\n' % aggre_summary['sell_amount']) w.writelines('\t\tbuy effect: %s\n' % aggre_summary['buy_effect']) w.writelines('\t\tsell effect: %s\n' % aggre_summary['sell_effect']) w.writelines('\t\tstrength: %s\n' % aggre_summary['strength']) w.writelines('\t\teffectiveness: %s\n' % aggre_summary['effectiveness']) w.writelines('\t\toverpower: %s\n' % aggre_summary['overpower']) w.writelines('\n') if detail == True: summary = t.summary() for i in summary: w.writelines('\t%s\n' % i) w.writelines('\t\tbuy amount: %s\n' % summary[i]['buy_amount']) w.writelines('\t\tsell amount: %s\n' % summary[i]['sell_amount']) w.writelines('\t\tbuy effect: %s\n' % summary[i]['buy_effect']) w.writelines('\t\tsell effect: %s\n' % summary[i]['sell_effect']) w.writelines('\t\tstrength: %s\n' % summary[i]['strength']) w.writelines('\t\teffectiveness: %s\n' % summary[i]['effectiveness']) w.writelines('\t\toverpower: %s\n' % summary[i]['overpower']) w.writelines('\n') w.writelines('\n') at.process_monitor(count / len(date_list) * 100)
def kdj_of_a_period(self, code, duration, start_date="", method=9, smooth=30): ''' Calculate a series of kdj in a relatively less consuming manner :param code: str, stock index :param duration: int, the duration of the period demanded :param start_date: str, the start date :param method: the range took into calculation, default 9 days :param smooth: the smooth period, default 30 days :return: a series of kdj in a form of (date, (k, d, j)) ''' rsv_list = [] days_list = at.opening_days(code, smooth + duration, start_date) count = 1 for i in days_list: rsv_h = self.__rsv__(code, i, method) rsv_list.append(rsv_h) at.process_monitor(count / len(days_list) * 100) count += 1 k_line = [50] d_line = [50] for j in rsv_list: k_hold = j / 3 + 2 * k_line[-1] / 3 k_line.append(k_hold) d_hold = k_line[-1] / 3 + 2 * d_line[-1] / 3 d_line.append(d_hold) output_list = [] for io in range(duration): date = days_list[io - duration] k = k_line[io - duration] d = d_line[io - duration] j = 3 * k - 2 * d if j > 100: j = 100 output_list.append((date, (k, d, j))) return output_list
def __close_price_list__(self, code, date, smooth, multi_threads=20): ''' Get the price needed for calculation and return both days list and price list :param code: str, stock index :param date: str, the date :param smooth: int, the period taken into calculation :return: days list and price list ''' days = (int(smooth / multi_threads) + 1) * multi_threads days_list = at.opening_days(code, days, date) close_price_list = [] count = 1 q = queue.Queue() start_pointer = 0 def catch(code, date, stack): hold = ms.get_stock_hist_data(code, date) stack.put(hold) while start_pointer <= len(days_list) - multi_threads: thread = [] for i in days_list[start_pointer:start_pointer + multi_threads]: t = threading.Thread(target=catch, args=(code, i, q)) thread.append(t) for j in thread: j.start() for k in thread: k.join() at.process_monitor(count / (int(smooth / multi_threads) + 1) * 100) count += 1 start_pointer += multi_threads while not q.empty(): close_price_list.append(q.get()) close_price_list = at.sort_list_by_date(close_price_list) close_price_list = close_price_list[-smooth:] return close_price_list