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 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
def get_pre_stock_data(self, stock_code, trade_date, pre_count): if pre_count is None or pre_count > 0: return None stock_datas = self.get_stock_data( stock_code, "", timeUtil.day_after_day(trade_date, -30), trade_date) key_index = -1 * pre_count if stock_datas is not None and len(stock_datas) > key_index: return stock_datas[key_index] return None
# def get_stock_realtime(self, stock_code): # resultList = self.pro.ts.get_realtime_quotes('000581').values.tolist() # # ts_code trade_date open high low close pre_close change pct_chg vol amount # if resultList is not None and len(resultList) > 0: # for one in resultList: # print(one) # 1 000004.SZ,20190722,20.36,20.36,19.60,19.60,20.35,-0.75,-3.69,4882.00,9651.82 # 2 000004.SZ,20190719,20.25,20.82,20.15,20.35,20.23,0.12,0.59,2515.00,5152.03 # 3 000004.SZ,20190718,20.24,20.30,19.67,20.23,20.35,-0.12,-0.59,2410.39,4855.03 if __name__ == '__main__': base = BaseStock("base") datas = base.get_stock_data('601318.SH', '国农科技', '20190302', timeUtil.day_after(0)) # # printUtil.p_list(datas) print(datas[0].get_trade_date()) ##TEST # # print(base.query_stock_data('000004.SZ', '国农科技', timeUtil.day_after(-1), timeUtil.day_after(-1))[0]) # stock = base.get_all_stock() # # count = 1 # for one in stock: # base.get_one_stock_daily(one, stock[one]) # count = count + 1 # if count > 20:
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)
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(",", " ") ]) for one in stock: shoot_stock = analysis_stock(one, stock[one], trade_data) if shoot_stock is not None: print(" %s(%s):%s" % (stock[one], one, shoot_stock.get_trade_date())) after_n_day_trade = base.get_stock_data( shoot_stock.get_ts_code(), stock[one],
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
from Analysis import Analysis from indexData.MofComData import MofComData from indexData.PigPrice365Day import PigData from tools.PrintUtil import * from indexData.BaseData import BaseData import tools.TimeUtil as tu import IndexConstants bd = BaseData('sample') mofComData = MofComData('MofComData') analysis = Analysis() next_month = tu.qi_huo_next_month() pre_year_date = tu.pre_year_date() today_date = tu.today() cycle_index = [1, 2, 5, 10, 30, 100, 200] # 猪肉 pigDate = PigData("猪肉") p_file(IndexConstants.date_file_path + 'qihuo' + today_date + '.txt', analysis.change_rate_day(pigDate.get_data(), cycle_index), pigDate.name) # 鸡蛋 p_file( IndexConstants.date_file_path + 'qihuo' + today_date + '.txt', analysis.change_rate_day( bd.get_qihuo_simaple('JD' + next_month + '.DCE', pre_year_date, today_date, '鸡蛋'), cycle_index), '鸡蛋' + tu.qi_huo_next_month()) # 铁矿石
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
def get_data(self): res_data = self.get_ts_qihuo("I" + tu.qi_huo_next_month() + '.DCE', tu.pre_year_date(), tu.today()) # res_data = self.get_ts_qihuo("I1906" + '.DCE', tu.pre_year_date(), tu.today()) self.return_data = {"name": "铁矿石", "week": 7, "data": res_data} return self.return_data
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' # p_file_list_with_no_format(stock_file_path, stock_datas_hfq) if index > 190: print("wait 1 minute for tushare rule.....") time.sleep(60) index = 0