def analysis_stock(stock_code, stock_name, trade_data): base = BaseStock("base") avg = Avg() avg_result = avg.get_avg(stock_code, stock_name) trade_data_avg = None try: trade_data_avg = avg_result[trade_data] except: pass if trade_data_avg is None: return None current_stock_data = base.get_pre_stock_data(stock_code, trade_data, 0) pre_stock_data = base.get_pre_stock_data(stock_code, trade_data, -1) if pre_stock_data is None: return None pre_trade_data_avg = avg_result[pre_stock_data.get_trade_date()] if pre_stock_data is not None \ and trade_data_avg["5"] > trade_data_avg["10"] \ and current_stock_data.get_close() > trade_data_avg["5"] \ and pre_trade_data_avg["5"] < pre_trade_data_avg["10"] \ and pre_stock_data.get_close() < pre_trade_data_avg["10"] \ and pre_stock_data.get_close() > pre_trade_data_avg["5"]: return current_stock_data return None
def __init__(self): self.analysis_day = 20 self.base = BaseStock("base") self.up_track = [] self.avg_line = [] self.down_track = [] self.trade_date = []
def analysis_stock(stock_code, stock_name, trade_data, boll_result): base = BaseStock("base") datas = base.get_stock_data(stock_code, stock_name, timeUtil.day_after_day(trade_data, -30), trade_data) if datas is not None and len(datas) > 1: if datas[0].get_trade_date() != trade_data: return None # 比较时间交易记录 trade_time_data = datas[0] # 比较时间,前一天交易记录 trade_time_pre = datas[1] if boll_result is None: ball = Boll() boll_result = ball.get_boll(stock_code, stock_name) if boll_result[trade_data] is None: return None # 判断是否上穿Boll线中轨 if trade_time_data.get_close() >= boll_result[trade_time_data.get_trade_date()]["avg_line"] and trade_time_pre.get_close() <= \ boll_result[trade_time_pre.get_trade_date()]["avg_line"]: # 获取在Boll线中轨下的天数 continue_down_count = 0 for tt in range(1, len(datas)): if datas[tt].get_close() <= boll_result[ datas[tt].get_trade_date()]["avg_line"]: continue_down_count += 1 trade_time_data.set_down_line_day_count(continue_down_count) return trade_time_data else: return None
def __init__(self): self.base = BaseStock("base") self._ema_short = [] self._ema_lang = [] self._diff = [] self._eda = [] self._delta = [] self.trade_date = [] # 短时 self.short = 12 # 长时 self.long = 26 # DIF参数:9 self.dif = 9
class Boll(object): def __init__(self): self.analysis_day = 20 self.base = BaseStock("base") self.up_track = [] self.avg_line = [] self.down_track = [] self.trade_date = [] # { # '20190513': { # 'up_track': 20.895620489161818, # 'avg_line': 17.52700000000012, # 'down_track': 14.158379510838419 # }, # '20190514': { # 'up_track': 20.742542659338184, # 'avg_line': 17.343500000000116, # 'down_track': 13.94445734066205 # } # } def get_boll(self, stock_code, stock_name): day_len = -20000 date = timeUtil.today() stock_dates = self.base.get_stock_data( stock_code, stock_name, timeUtil.day_after_day(date, day_len), date) if stock_dates is not None and len(stock_dates) > 1: boll_result = {} stock_dates.reverse() # N天之内所有的开盘总和 day_count_sum = 0 for index in range(len(stock_dates)): self.trade_date.append(stock_dates[index].get_trade_date()) day_count_sum += stock_dates[index].get_close() if index < self.analysis_day: boll_result[stock_dates[index].get_trade_date()] = { "up_track": 0, "avg_line": 0, "down_track": 0, } else: day_count_sum = day_count_sum - stock_dates[ index - self.analysis_day].get_close() middle_value = day_count_sum / self.analysis_day md = 0.0 for i in range(index - self.analysis_day + 1, index + 1): md = md + math.pow( middle_value - stock_dates[i].get_close(), 2) md = md / self.analysis_day md = math.sqrt(md) boll_result[stock_dates[index].get_trade_date()] = { "up_track": middle_value + 2 * md, "avg_line": middle_value, "down_track": middle_value - 2 * md, } return boll_result else: return None
class Macd(object): def __init__(self): self.base = BaseStock("base") self._ema_short = [] self._ema_lang = [] self._diff = [] self._eda = [] self._delta = [] self.trade_date = [] # 短时 self.short = 12 # 长时 self.long = 26 # DIF参数:9 self.dif = 9 # { # '20190513': { # 'diff': 0.7732937859731557, # 'eda': 0.9415498112527317, # 'macd': -0.3365120505591521 # }, # '20190514': { # 'diff': 0.6013899635176436, # 'eda': 0.8735178417057141, # 'macd': -0.544255756376141 # } # } def get_macd(self, stock_code, stock_name): day_len = -20000 date = timeUtil.today() stock_dates = self.base.get_stock_data( stock_code, stock_name, timeUtil.day_after_day(date, day_len), date) if stock_dates is not None and len(stock_dates) > 1: stock_dates.reverse() macd_result = {} # 计算EMA EMA1 = [] EMA2 = [] for index in range(len(stock_dates)): self.trade_date.append(stock_dates[index].get_trade_date()) if index == 0: # 初始化短时EMA和长时EMA EMA1.append(stock_dates[index].get_close()) EMA2.append(stock_dates[index].get_close()) else: EMA1.append( 2 / (self.short + 1) * (stock_dates[index].get_close() - EMA1[index - 1]) + EMA1[index - 1]) EMA2.append( 2 / (self.long + 1) * (stock_dates[index].get_close() - EMA2[index - 1]) + EMA2[index - 1]) self._ema_short = EMA1 self._ema_lang = EMA2 self._diff = [] for index in range(len(EMA1)): self._diff.append(EMA1[index] - EMA2[index]) for index in range(len(EMA1)): if index == 0: self._eda.append(self._diff[index]) else: self._eda.append( 2 / (self.dif + 1) * (self._diff[index] - self._eda[index - 1]) + self._eda[index - 1]) # macd self._delta = [] for index in range(len(self._diff)): self._delta.append((self._diff[index] - self._eda[index]) * 2) for ii in range(0, len(self.trade_date)): macd_result[self.trade_date[ii]] = { "diff": self._diff[ii], "eda": self._eda[ii], "macd": self._delta[ii] } return macd_result else: print("No Data Find! %s[%s] on %s " % (stock_code, stock_name, date)) return None # macd = Macd() # result = macd.get_macd('000004.SZ', '国农科技') # print(result)
from stock.BaseStock import BaseStock import IndexConstants import sys from stock_mork_boll_line5_macd.StockMorkB5M import MorkB5N base = BaseStock("base") mork_ball = MorkB5N(20, -3, 9, 1, 1, -0.2, 99999) stock = base.get_all_60_stock() statistics_data = { "winTime": 0, "winMoney": 0, "down5LineTime": 0, "down5LineMoney": 0, "lostTime": 0, "lostMoney": 0, "otherTime": 0, "otherMoney": 0 } savedStdout = sys.stdout # 保存标准输出流 with open(IndexConstants.date_file_path + "MorkB5N20_3_9_1_1_Z_60.txt", 'at') as file: sys.stdout = file # 标准输出重定向至文件 for one in stock: mork_ball.mork_b5m(one, stock[one], statistics_data) sys.stdout = savedStdout # 恢复标准输出流 stock = base.get_all_30_stock() statistics_data = {
pre_trade_data_avg = avg_result[pre_stock_data.get_trade_date()] if pre_stock_data is not None \ and trade_data_avg["5"] > trade_data_avg["10"] \ and current_stock_data.get_close() > trade_data_avg["5"] \ and pre_trade_data_avg["5"] < pre_trade_data_avg["10"] \ and pre_stock_data.get_close() < pre_trade_data_avg["10"] \ and pre_stock_data.get_close() > pre_trade_data_avg["5"]: return current_stock_data return None if __name__ == '__main__': base = BaseStock("base") stock = base.get_all_stock() avg = Avg() ball = Boll() print("Save data in:" + constant.date_file_path) for i in range(5): analysis_date = "20190821" trade_data = timeUtil.day_after_day(analysis_date, -1 * i) print("Start---" + trade_data) result_file_path = constant.date_file_path + "Line5Up10_" + trade_data + ".txt" # result_file_path = constant.date_file_path + "Line5Up10.txt" pt.p_file_no_format_add(result_file_path, [ "code,date,O,H,L,C,P,Diff,R,count,sum,avg5,avg10,avg20,up_track,avg_line,down_track" .replace(",", " ") ])
def mork_bollBackDownLine(self, stock_code, stock_name, statistics_data): base = BaseStock("base") avg = Avg() avg_result = avg.get_avg(stock_code, stock_name) ball_class = Boll() boll_result = ball_class.get_boll(stock_code, stock_name) for i in range(0, 150): catch_stock = boll.analysis_stock( stock_code, stock_name, timeUtil.day_after_day(timeUtil.today(), i * -1), boll_result) if catch_stock is not None: after_datas = base.get_stock_data( stock_code, stock_name, timeUtil.day_after_day(catch_stock.get_trade_date(), 1), timeUtil.day_after_day(catch_stock.get_trade_date(), 20)) after_datas.reverse() if len(after_datas) >= 1: catch_down_line_day = catch_stock.get_down_line_day_count() if catch_down_line_day >= self._minDownDay and catch_down_line_day <= self._maxDownDay: print(stock_name + ":" + str(catch_stock)) print("%s 突破日股价 %.2f Boll下轨天数 %d" % (stock_code, catch_stock.get_close(), catch_down_line_day)) test_trade = { "stockCode": stock_code, "startBuy": 0, "startBuyDate": "", "endBuy": 0, "endBuyDate": "", # 止损率 "lostBuffRate": self._lostBuffRate, # 止盈利率 "winBuffRate": self._winBuffRate, "earn": 0, "earnRate": 0, "holdDay": 0, "forceSell": False, "forceSellType": "" } print("Line5[%.3f] - close[%.3f]" % (avg_result[catch_stock.get_trade_date()]["5"], catch_stock.get_close())) self.cal_print_avg_line(catch_stock, avg_result, after_datas, 0, test_trade) for cal_day in range(2, self._calDay + 1): if len(after_datas) >= cal_day: self.cal_print_avg_line( catch_stock, avg_result, after_datas, cal_day - 1, test_trade) if test_trade["startBuy"] > 0: print( "stockCode %s startBuy %.2f startBuyDate %s endBuy %.2f endBuyDate %s earn %.2f earnRate %.2f holdDay %d forceSell %s forceSellType %s" % (test_trade["stockCode"], test_trade["startBuy"], test_trade["startBuyDate"], test_trade["endBuy"], test_trade["endBuyDate"], test_trade["earn"], test_trade["earnRate"], test_trade["holdDay"], test_trade["forceSell"], test_trade["forceSellType"])) if test_trade["forceSellType"] == "down5Line": statistics_data[ "down5LineTime"] = statistics_data[ "down5LineTime"] + 1 statistics_data[ "down5LineMoney"] = statistics_data[ "down5LineMoney"] + test_trade[ "earnRate"] elif test_trade["forceSellType"] == "winBuffRate": statistics_data[ "winTime"] = statistics_data["winTime"] + 1 statistics_data["winMoney"] = statistics_data[ "winMoney"] + test_trade["earnRate"] elif test_trade["forceSellType"] == "lostBuffRate": statistics_data["lostTime"] = statistics_data[ "lostTime"] + 1 statistics_data["lostMoney"] = statistics_data[ "lostMoney"] + test_trade["earnRate"] else: statistics_data["otherTime"] = statistics_data[ "otherTime"] + 1 statistics_data[ "otherMoney"] = statistics_data[ "otherMoney"] + test_trade["earnRate"] print( "code %s winTime %d winMoney %.3f down5LineTime %d down5LineMoney %.3f lostTime %d lostMoney %.3f otherTime %d otherMoney %.3f" % (stock_code, statistics_data["winTime"], statistics_data["winMoney"], statistics_data["down5LineTime"], statistics_data["down5LineMoney"], statistics_data["lostTime"], statistics_data["lostMoney"], statistics_data["otherTime"], statistics_data["otherMoney"])) return statistics_data
if len(format_value) == 4: format_value = " " + format_value if len(format_value) == 5: format_value = " " + format_value return format_value stocks = [] fileInfo = open("D:" + os.sep + "analysisStock.txt") for line in fileInfo: stocks.append(line) avg = Avg() avg_clos = {} base = BaseStock("base") pingan_datas = base.get_stock_data('601318.SH', 'PingAn', '20190701', timeUtil.day_after(0)) lastest_trade_date = pingan_datas[0].get_trade_date() print("lastest_trade_date:" + lastest_trade_date) while True: dateStr = "" for oneStockInfo in stocks: oneStockInfo = oneStockInfo.strip('\n') oneStockInfo = oneStockInfo.strip(' ') try: if oneStockInfo[0] == '#': continue elif oneStockInfo == '000001': stock_code = oneStockInfo + ".SZ" oneStockInfo = 'sh' + oneStockInfo
def __init__(self): self.analysis_day = 20 self.base = BaseStock("base")
class Avg(object): def __init__(self): self.analysis_day = 20 self.base = BaseStock("base") def get_avg(self, stock_code, stock_name): day_len = -20000 date = timeUtil.today() stock_dates = self.base.get_stock_data(stock_code, stock_name, timeUtil.day_after_day(date, day_len), date) if stock_dates is not None and len(stock_dates) > 1: avg_result= {} stock_dates.reverse() day3_line = [] day4_line = [] day5_line = [] day6_line = [] day7_line = [] day8_line = [] day9_line = [] day10_line = [] day15_line = [] day20_line = [] day30_line = [] day60_line = [] day120_line = [] trade_date = [] day3 = 0 day4 = 0 day5 = 0 day6 = 0 day7 = 0 day8 = 0 day9 = 0 day10 = 0 day15 = 0 day20 = 0 day30 = 0 day60 = 0 day120 = 0 for index in range(len(stock_dates)): trade_date.append(stock_dates[index].get_trade_date()) # Day 3 day3 += stock_dates[index].get_close() if index < 3: day3_line.append(day3 / (index + 1)) else: day3 -= stock_dates[index - 3].get_close() day3_line.append(day3 / 3) # Day 4 day4 += stock_dates[index].get_close() if index < 4: day4_line.append(day4 / (index + 1)) else: day4 -= stock_dates[index - 4].get_close() day4_line.append(day4 / 4) # Day 5 day5 += stock_dates[index].get_close() if index < 5: day5_line.append(day5 / (index + 1)) else: day5 -= stock_dates[index - 5].get_close() day5_line.append(day5 / 5) # Day 6 day6 += stock_dates[index].get_close() if index < 6: day6_line.append(day6 / (index + 1)) else: day6 -= stock_dates[index - 6].get_close() day6_line.append(day6 / 6) # Day 7 day7 += stock_dates[index].get_close() if index < 7: day7_line.append(day7 / (index + 1)) else: day7 -= stock_dates[index - 7].get_close() day7_line.append(day7 / 7) # Day 8 day8 += stock_dates[index].get_close() if index < 8: day8_line.append(day8 / (index + 1)) else: day8 -= stock_dates[index - 8].get_close() day8_line.append(day8 / 8) # Day 9 day9 += stock_dates[index].get_close() if index < 9: day9_line.append(day9 / (index + 1)) else: day9 -= stock_dates[index - 9].get_close() day9_line.append(day9 / 9) # Day 10 day10 += stock_dates[index].get_close() if index < 10: day10_line.append(day10 / (index + 1)) else: day10 -= stock_dates[index - 10].get_close() day10_line.append(day10 / 10) # Day 15 day15 += stock_dates[index].get_close() if index < 15: day15_line.append(day15 / (index + 1)) else: day15 -= stock_dates[index - 15].get_close() day15_line.append(day15 / 15) # Day 20 day20 += stock_dates[index].get_close() if index < 20: day20_line.append(day20 / (index + 1)) else: day20 -= stock_dates[index - 20].get_close() day20_line.append(day20 / 20) # Day 30 day30 += stock_dates[index].get_close() if index < 30: day30_line.append(day30 / (index + 1)) else: day30 -= stock_dates[index - 30].get_close() day30_line.append(day30 / 30) # Day 60 day60 += stock_dates[index].get_close() if index < 60: day60_line.append(day60 / (index + 1)) else: day60 -= stock_dates[index - 60].get_close() day60_line.append(day60 / 60) # Day 120 day120 += stock_dates[index].get_close() if index < 120: day120_line.append(day120 / (index + 1)) else: day120 -= stock_dates[index - 120].get_close() day120_line.append(day120 / 120) # END LOOP------------------------------- for ii in range(0,len(trade_date)): avg_result[trade_date[ii]] = { "3": day3_line[ii], "4": day4_line[ii], "5": day5_line[ii], "6": day6_line[ii], "7": day7_line[ii], "8": day8_line[ii], "9": day9_line[ii], "10": day10_line[ii], "15": day15_line[ii], "20": day20_line[ii], "30": day30_line[ii], "60": day60_line[ii], "120": day120_line[ii] } return avg_result else: return None
from stock.BaseStock import BaseStock import tools.TimeUtil as timeUtil import IndexConstants as constant import time from tools.PrintUtil import * base = BaseStock("base") stock = base.get_all_stock() count = 1 index = 1 for one in stock: count = count + 1 if count < 3509: continue else: index = index + 1 index = index + 1 print(str(count) + "--" + one) stock_datas = base.query_stock_data(one, stock[one], timeUtil.day_after(-20000), timeUtil.today()) if stock_datas is not None: stock_file_path = constant.stock_file_path + one + '.txt' p_file_list_with_no_format(stock_file_path, stock_datas) # stock_datas_hfq = base.query_stock_data_hfq(one, stock[one], timeUtil.day_after(-20000), timeUtil.today()) # if stock_datas_hfq is not None: # stock_file_path = constant.stock_file_path_hfq + one + '.txt'