def list_for_price_deviation(list, date='', duration=90, smooth=3): ''' For a collection of intrested stocks, plot the price deviation graph and save figures :param list: a list of stock code :param date: str, date :param duration: int, duration :return: None ''' h = al.PriceDeviation() valid_code = ms.get_stock_basics().index.values.tolist() for i in list: if i in valid_code: h.plot_difference(i, date, duration, type='save', smooth=smooth)
def __init__(self, code, date, duration, smooth, discover_mode=False): h = al.PriceDeviation() if discover_mode == True: try: self.origin_list = h.price_diff_list_load( code, date, duration, smooth) except: h.price_diff_list_save(code, date, duration, smooth) self.origin_list = h.price_diff_list_load( code, date, duration, smooth) else: self.origin_list = h.show_difference_list(code, date, duration, smooth) self.code = code self.smooth = smooth self.date = date self.criteria_list = [] self.action_list = []
def diff_line_double_duration(code_list, date_list, duration, smooth, amount=100000): for code in code_list: for date in date_list: try: p = al.PriceDeviation() s = p.__diff_lists_multi_smoothing__(code, smooth, date, duration) state = 'unhold' list = [] i_hold_fast = s[0]['smoothed difference %s'%smooth[0]] i_hold_slow = s[0]['smoothed difference %s'%smooth[1]] for i in s: if i['smoothed difference %s'%smooth[0]] >= i['smoothed difference %s'%smooth[1]]: if i_hold_fast < i_hold_slow and state == 'unhold': list.append({'date' : i['date'], 'signal': 'buy'}) state = 'hold' if i['smoothed difference %s'%smooth[0]] < i['smoothed difference %s'%smooth[1]]: if i_hold_fast > i_hold_slow and state == 'hold': list.append({'date' : i['date'], 'signal': 'sell'}) state = 'unhold' i_hold_fast = i['smoothed difference %s'%smooth[0]] i_hold_slow = i['smoothed difference %s'%smooth[1]] account = tr.StockAccount(time=at.next_opening_day(code, list[0]['date'])) account = tr.StockAccount(account.save()) account.deposit(amount, at.next_opening_day(code, list[0]['date'])) for i in range(len(list)): if list[i]['signal'] == 'buy' and list[i]['date'] != date: date_i = at.next_opening_day(code, list[i]['date'], default_shift=True) account.buy(code, 1, ms.get_stock_hist_data(code, date_i, 'open'), date_i, True) if list[i]['signal'] == 'sell' and list[i]['date'] != date: date_i = at.next_opening_day(code, list[i]['date'], default_shift=True) account.sell(code, 1, ms.get_stock_hist_data(code, date_i, 'open'), date_i) if list[i] == list[-1]: account.deposit(0.01, date) account.plot_performance_with_index(method='save', id=code) except: pass
def diff_line_strategy(code_list, date_list, duration, smooth, bbar=0.0, sbar=0.0, amount=100000, text_only=False): for code in code_list: for date in date_list: try: p = al.PriceDeviation() s = p.__diff_lists__(code, date=date, duration=duration, smooth=smooth) state = 'unhold' list = [] i_hold = 0.0 for i in s: if i['smoothed difference'] >= bbar: if i_hold < bbar and state == 'unhold': list.append({'date' : i['date'], 'signal': 'buy'}) state = 'hold' if i['smoothed difference'] < sbar: if i_hold >= sbar and state == 'hold': list.append({'date' : i['date'], 'signal': 'sell'}) state = 'unhold' i_hold = i['smoothed difference'] account = tr.StockAccount(time=at.next_opening_day(code, list[0]['date'])) account = tr.StockAccount(account.save()) account.deposit(amount, at.next_opening_day(code, list[0]['date'])) for i in range(len(list)): if list[i]['signal'] == 'buy' and list[i]['date'] != date: date_i = at.next_opening_day(code, list[i]['date'], default_shift=True) account.buy(code, 1, ms.get_stock_hist_data(code, date_i, 'open'), date_i, True) if list[i]['signal'] == 'sell' and list[i]['date'] != date: date_i = at.next_opening_day(code, list[i]['date'], default_shift=True) account.sell(code, 1, ms.get_stock_hist_data(code, date_i, 'open'), date_i) if list[i] == list[-1]: account.deposit(0.01, date) id = str(code) + '-' + str(smooth) + '-' + str(int(bbar*100)) + '-' + str(int(sbar*100)) account.plot_performance_with_index(method='save', id=id, text_only=text_only) except: pass
def diff_line_multi_line(code_list, date_list, buy_line=0.0, sell_line=0.0, exit_days=7, smooth=7, duration=300, amount=100000, text_only=False, discover_mode=False): h = al.PriceDeviation() for code in code_list: for date in date_list: try: if discover_mode == True: try: origin_list = h.price_diff_list_load(code, date, duration, smooth) except: h.price_diff_list_save(code, date, duration, smooth) origin_list = h.price_diff_list_load(code, date, duration, smooth) else: origin_list = h.show_difference_list(code, date, duration, smooth) hold = False through = False exit_count = 0 action_list = [] pre_value = 0.0 for i in origin_list: date = i['date'] value = i['smoothed difference'] exit_count += 1 if not hold: if pre_value <= buy_line and value >= buy_line: signal = 'buy' line = {'date': date, 'signal': signal} action_list.append(line) exit_count = 0 hold = True else: if not through and pre_value >= sell_line: through = True if not through: if value < buy_line: signal = 'sell' line = {'date': date, 'signal': signal} action_list.append(line) hold = False else: if value < sell_line: signal = 'sell' line = {'date': date, 'signal': signal} action_list.append(line) hold = False through = False ''' if exit_count >= exit_days: if value <= sell_line: signal = 'sell' line = {'date': date, 'signal': signal} action_list.append(line) hold = False ''' pre_value = value account = tr.StockAccount(time=at.next_opening_day(code, action_list[0]['date'])) account = tr.StockAccount(account.save()) account.deposit(amount, at.next_opening_day(code, action_list[0]['date'])) for i in range(len(action_list)): if action_list[i]['signal'] == 'buy' and action_list[i]['date'] != date: date_i = at.next_opening_day(code, action_list[i]['date'], default_shift=True) account.buy(code, 1, ms.get_stock_hist_data(code, date_i, 'open'), date_i, True) if action_list[i]['signal'] == 'sell' and action_list[i]['date'] != date: date_i = at.next_opening_day(code, action_list[i]['date'], default_shift=True) account.sell(code, 1, ms.get_stock_hist_data(code, date_i, 'open'), date_i) if action_list[i] == action_list[-1]: account.deposit(0.01, date) id = str(code) + '-' + str(smooth) + '-' + str(int(buy_line*100)) + '-' + str(int(sell_line*100)) account.plot_performance_with_index(method='save', id=id, text_only=text_only) except: pass
import messenger as ms import analyst as al import numpy as np import assistant as at import matplotlib.pyplot as plt h = al.PriceDeviation() h.price_diff_list_save('002415', date='2017-06-28', duration=600, smooth=3) origin_data = h.price_diff_list_load('002415', date='2017-06-28', duration=600, smooth=3) origin_data = origin_data[-200:] result = [] date_list = [] for i in range(30, len(origin_data)): date = origin_data[i]['date'] sub_list = origin_data[i - 30:i] diff_list = [i['smoothed difference'] for i in sub_list] theo_list = [i['smoothed theoretical'] for i in sub_list] corr = np.correlate(diff_list, theo_list) print(date, corr) result.append(corr) date_list.append(at.date_encoding(date)) plt.plot(date_list, result) plt.show()