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 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 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 save_period_tick_top_summary(code_list, date, duration, top=3, cut=None): path = os.path.join(os.getcwd(), 'tick_summary') if not os.path.exists(path): os.mkdir(path) count = 0 for code in code_list: count += 1 file_path = os.path.join(path, '%s-period-top-summary-%s-%s.txt'%(code, date, duration)) with open(file_path, 'w') as w: t = TickAnne(code, date, duration, cut) top_dict = t.top_statistics(top) for i in top_dict: w.writelines('%s\n'%i) w.writelines('\n') for j in top_dict[i]: price = j for k in top_dict[i][j]: percent = top_dict[i][j][k] text = k.replace('_', ' ') w.writelines('\tprice: %s\t%s: %s\n'%(price, text, percent)) w.writelines('\n' * 2) at.process_monitor(count / len(code_list) * 100)
def save_period_tick_summary(code_list, date, duration, cut=None): path = os.path.join(os.getcwd(), 'tick_summary') if not os.path.exists(path): os.mkdir(path) count = 0 for code in code_list: count += 1 file_path = os.path.join(path, '%s-period-summary-%s-%s.txt'%(code, date, duration)) with open(file_path, 'w') as w: t = TickAnne(code, date, duration, cut) dict = t.__combined_dict__ for i in sorted(dict): w.writelines('%s\n'%i) w.writelines('\n') w.writelines('\tbuy percent: %s\n' % dict[i]['buy_percent']) w.writelines('\tsell percent: %s\n' % dict[i]['sell_percent']) w.writelines('\tbuy effect: %s\n' % dict[i]['buy_effect']) w.writelines('\tsell effect: %s\n' % dict[i]['sell_effect']) w.writelines('\tstrength: %s\n' % dict[i]['strength']) w.writelines('\teffectiveness: %s\n' % dict[i]['effectiveness']) w.writelines('\toverpower: %s\n' % dict[i]['overpower']) w.writelines('\n') at.process_monitor(count / len(code_list) * 100)
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
def __diff_lists_multi_smoothing__(self, code, smooth, date='', duration=default_range, leading_smooth=leading_smooth, threads=threads_of_catch): ''' Generate a list of price differences along the date with multi-smoothing :param code: str, stock index :param date: str, date :param duration: int, the demanded duration :param smooth: list, the smooth method :param leading_smooth: int, the leading smooth period :param threads: int, the number of threads used :return: a list in form of (date, code, open, close, actual_percent, theoretical_percent, diff_percent, smoothed_actual, smoothed_theoretical, smoothed_diff) ''' def catch(code, date, outstanding, f, queue): content = f(code, date, outstanding) queue.put(content) # Generate a days list that is the integral multiple of the threads forwarding_days = (int((duration + leading_smooth) / threads + 1) * threads) days_list = (self.__opening_dates__(code, forwarding_days, date)) # Multi-threading with looping q = queue.Queue() origin_list = [] pointer = 0 outstanding = ms.get_stock_outstanding(code) while pointer <= len(days_list) - threads: thread = [] for i in days_list[pointer:pointer + threads]: t = threading.Thread(target=catch, args=(code, i, outstanding, self.__measure_diff__, q)) thread.append(t) for j in thread: j.start() for k in thread: k.join() at.process_monitor((pointer + threads) / len(days_list) * 100) pointer += threads while not q.empty(): origin_list.append(q.get()) origin_list = at.sort_list_by_date(origin_list) # Re-construct for the final output list if len(origin_list) > 0: percent_list = [] reference_h = origin_list[0]['reference'] for io in origin_list: # (date, code, open, close, reference, actual_change, theoretical_change, diff) c_date = io['date'] c_code = io['code'] open = io['open'] close = io['close'] high = io['high'] low = io['low'] volume = io['volume'] c_actual_change = self.__price_diff_percentage__( io['actual change'], reference_h) c_theoretical_change = self.__price_diff_percentage__( io['theoretical change'], reference_h) c_diff = self.__price_diff_percentage__( io['difference'], reference_h) percent_list_line = {'date': c_date, 'code': c_code, 'open': open, 'close': close, 'high': high, \ 'low': low, 'volume': volume, 'actual change': c_actual_change, \ 'theoretical change': c_theoretical_change, 'difference': c_diff} percent_list.append(percent_list_line) reference_h = io['reference'] smoothed_theoretical_holder = [ i['theoretical change'] for i in percent_list ] smoothed_actual = [i['actual change'] for i in percent_list] smoothed_change_holder = [i['difference'] for i in percent_list] smoothed_theoretical = [] smoothed_change = [] for ism in smooth: smoothed_theoretical.append( self.__smooth__(smoothed_theoretical_holder, ism)) smoothed_change.append( self.__smooth__(smoothed_change_holder, ism)) for ip in range(len(percent_list)): percent_list[ip]['smoothed actual'] = smoothed_actual[ip] for ismx in range(len(smoothed_theoretical)): percent_list[ip][ 'smoothed theoretical %s' % smooth[ismx]] = smoothed_theoretical[ismx][ip] percent_list[ip]['smoothed difference %s' % smooth[ismx]] = smoothed_change[ismx][ip] return percent_list[-duration:] else: return None