def createEarnHoldBarsImg(): from earnmi_demo.strategy_demo.holdbars.HoldBarAnanysic import macd, kdj sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) chart = Chart() indictor = macd() computeCount = 0 for code in lists: bars = sw.getSW2Daily(code, start, end) # print(f"bar.size = {bars.__len__()}") chart.run(bars, indictor) holdBarList = indictor.getHoldBars() for holdBar in holdBarList: barList = holdBar.bars if (HoldBarUtils.isEarnBarList(barList)): tiemStr = holdBar.start_time.strftime('%Y%m%d') chart.show(barList, savefig=f'imgs\\earn-{code}-{tiemStr}.png') # print(f"code:{code},cost_pct = %.2f%%" % (total_cost_pct*100)) computeCount = computeCount + 1 if computeCount > 10: break
def computeAll(): from earnmi.data.SWImpl import SWImpl from earnmi.chart.Chart import Chart, IndicatorItem, Signal sw = SWImpl() lists = sw.getSW2List() start = datetime(2018, 5, 1) end = datetime(2020, 8, 17) dataSet = {} class DataItem(object): pass for code in lists: #for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator() for bar in barList: ##先识别形态 rets = KPattern.matchIndicator(indicator) size = len(rets) if size > 0: """有形态识别出来 """ for item in rets: name = item.name value = item.value dataItem = None if dataSet.__contains__(name): dataItem = dataSet[name] else: dataItem = DataItem() dataItem.values = [] ##形态被识别的值。 dataItem.pcts = [] ##识别之后第二天的盈利情况 dataSet[name] = dataItem ##第二天的收益 pct = (bar.close_price - bar.open_price) / bar.open_price ##收录当前形态 dataItem.values.append(value) dataItem.pcts.append(pct) pass indicator.update_bar(bar) ##打印当前形态 print(f"总共识别出{len(dataSet)}个形态") for key, dataItem in dataSet.items(): values = np.array(dataItem.values) pcts = np.array(dataItem.pcts) * 100 print( f"{key}: len={len(dataItem.values)},values:{values.mean()},pcts:%.2f%%,pcts_std=%.2f" % (pcts.mean(), pcts.std()))
def collectKPattherAndShowChart(): from earnmi.data.SWImpl import SWImpl from vnpy.trader.constant import Exchange from vnpy.trader.constant import Interval sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) bars = [] limitSize = 0 chart = Chart() for code in lists: # for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator(40) preBar = None yestodayIsMatch = False for i in range(0, len(barList)): bar = barList[i] indicator.update_bar(bar) patternValue = KPattern.encode1KAgo1(indicator) todayIsMatch = 9 == patternValue if todayIsMatch: if indicator.count > 20: chart.show( indicator.makeBars(), savefig=f"imgs/collectKPattherAndShowChart_{limitSize}" ) limitSize += 1 if (limitSize > 50): break pass if yestodayIsMatch: pass preBar = bar yestodayIsMatch = todayIsMatch if (limitSize > 50): break pass
class SWDataSource(BarDataSource): def __init__(self, start: datetime, end: datetime): self.index = 0 from earnmi.data.SWImpl import SWImpl self.sw = SWImpl() self.start = start self.end = end def nextBars(self) -> Tuple[Sequence['BarData'], str]: # if self.index > 2: # return None,None sw_code_list = self.sw.getSW2List() if self.index < len(sw_code_list): code = sw_code_list[self.index] self.index += 1 return self.sw.getSW2Daily(code, self.start, self.end), code return None, None
def crateHoldbarsImg(): sw = SWImpl() lists = sw.getSW2List() start = datetime(2019, 5, 1) end = datetime(2020, 8, 17) chart = Chart() for code in lists: bars = sw.getSW2Daily(code, start, end) # print(f"bar.size = {bars.__len__()}") indictor = kdj() chart.run(bars, indictor) holdBarList = indictor.getHoldBars() barList = utils.to_bars(holdBarList) chart.show(barList, item=indictor, savefig=f'imgs\\{code}.png') #print(f"code:{code},cost_pct = %.2f%%" % (total_cost_pct*100)) break
from datetime import datetime from earnmi.data.MarketImpl import MarketImpl from earnmi.data.SWImpl import SWImpl import time sw = SWImpl() swCode = sw.getSW2List()[0] stockslist = sw.getSW2Stocks(swCode) print(f"stockslist:{len(stockslist)}") startDate = datetime(2015, 4, 1) endDate = datetime(2020, 5, 1) market = MarketImpl() market.setToday(endDate) market.addNotice('002852') bars = market.getHistory().getKbars('002852', 300 * 3) for code in stockslist: market.addNotice(code) print(f"getBars:{code}") bars = market.getHistory().getKbars(code, 300 * 3) #time.sleep(2) print(f"getBars size:{len(bars)}")
def findKPatternThatIn3Day(first_day_pct: float = 3, targe_pct=3): sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) dataSet = {} total_count = 0 for code in lists: # for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator(40) traceItems: ['TraceIn3DayItem'] = [] for bar in barList: ###跟踪数据 toDeleteList = [] for traceItem in traceItems: traceItem.onTraceBar(bar) if traceItem.isFinished(): toDeleteList.append(traceItem) if traceItem.isWanted(): ###归纳到统计里面 dataItem: CountItem = dataSet.get(traceItem.kPattern) if dataItem is None: dataItem = CountItem() dataSet[traceItem.kPattern] = dataItem pct = traceItem.current_sell_pct total_count += 1 dataItem.count_total += 1 dataItem.pct_total += pct if traceItem.isSuccess(): dataItem.count_earn += 1 dataItem.pct_earn += pct pass for traceItem in toDeleteList: traceItems.remove(traceItem) indicator.update_bar(bar) kEncodeValue = KPattern.encode2KAgo1(indicator) if kEncodeValue is None: continue traceItem = TraceIn3DayItem(kEncodeValue, bar) traceItems.append(traceItem) ##打印当前形态 occur_count = 0 print(f"总共分析{total_count}个形态,识别出{len(dataSet)}个形态,有意义的形态有:") max_succ_rate = 0 min_succ_rate = 100 ret_list = [] for key, dataItem in dataSet.items(): success_rate = 100 * dataItem.count_earn / dataItem.count_total if dataItem.count_total < 300: continue if success_rate < 40: continue ret_list.append(key) if dataItem.count_earn > 0: earn_pct = dataItem.pct_earn / dataItem.count_earn else: earn_pct = 0 avg_pct = dataItem.pct_total / dataItem.count_total occur_count += dataItem.count_total occur_rate = 100 * dataItem.count_total / total_count max_succ_rate = max(success_rate, max_succ_rate) min_succ_rate = min(success_rate, min_succ_rate) print( f"{key}: total={dataItem.count_total},suc=%.2f%%,occur_rate=%.2f%%,earn_pct:%.2f%%,avg_pct:%.2f%%)" % (success_rate, occur_rate, earn_pct, avg_pct)) total_occur_rate = 100 * occur_count / total_count print(f"总共:occur_rate=%.2f%%, min_succ_rate=%.2f%%, max_succ_rate=%.2f%%" % (total_occur_rate, min_succ_rate, max_succ_rate)) print(f"{ret_list}")
def printKPatterMoreDetail(kPatters=[ 535, 359, 1239, 1415, 1072, 712, 1412, 1240, 1413, 888, 2823, 706, 1414, 1064 ]): from vnpy.trader.constant import Exchange from vnpy.trader.constant import Interval sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) pct_split = [-7, -5, -3, -1.5, -0.5, 0.5, 1.5, 3, 5, 7] #pct_split = [-7, -5, -3, -1.0, 0, 1, 3, 5, 7] pct_split = [2] pctEncoder = FloatEncoder(pct_split) kPattersMap = {} for value in kPatters: kPattersMap[value] = True class InnerData(object): kValue: int ## sell_disbute = np.zeros(pctEncoder.mask()) ##卖方力量分布情况 buy_disbute = np.zeros(pctEncoder.mask()) #买方力量分布情况 pass dataSet = {} occurDayMap = {} allTrayDay = 1 for code in lists: # for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator(40) traceItems: ['TraceIn3DayItem'] = [] allTrayDay = max(allTrayDay, len(barList)) for bar in barList: ###跟踪数据 toDeleteList = [] for traceItem in traceItems: traceItem.onTraceBar(bar) if traceItem.isFinished(): toDeleteList.append(traceItem) if traceItem.isWanted(): occurBar = traceItem.firstBar dayKey = occurBar.datetime.year * 13 * 35 + occurBar.datetime.month * 13 + occurBar.datetime.day occurDayMap[dayKey] = True ###归纳到统计里面 innerData: InnerData = dataSet.get(traceItem.kPattern) if innerData is None: innerData = InnerData() innerData.kValue = traceItem.kPattern dataSet[traceItem.kPattern] = innerData sell_pct = traceItem.current_sell_pct buy_pct = traceItem.current_buy_pct innerData.buy_disbute[pctEncoder.encode(buy_pct)] += 1 innerData.sell_disbute[pctEncoder.encode( sell_pct)] += 1 pass for traceItem in toDeleteList: traceItems.remove(traceItem) indicator.update_bar(bar) kEncodeValue = KPattern.encode2KAgo1(indicator) if kEncodeValue is None or kPattersMap.get(kEncodeValue) is None: continue traceItem = TraceIn3DayItem(kEncodeValue, bar) traceItems.append(traceItem) print(f"所有交易日中,有意义的k线形态出现占比:%.2f%%" % (100 * len(occurDayMap) / allTrayDay)) for kValue, dataItem in dataSet.items(): total_count1 = 0 total_count2 = 0 for cnt in dataItem.sell_disbute: total_count1 += cnt for cnt in dataItem.buy_disbute: total_count2 += cnt assert total_count1 == total_count2 assert total_count1 > 0 print(f"\n\nk:%6d, " % (kValue)) print(f" 卖方价格分布:") for encode in range(0, len(dataItem.sell_disbute)): occurtRate = 100 * dataItem.sell_disbute[encode] / total_count1 print(f" {pctEncoder.descriptEncdoe(encode)}:%.2f%%" % (occurtRate)) print(f" 买方价格分布:") for encode in range(0, len(dataItem.buy_disbute)): occurtRate = 100 * dataItem.buy_disbute[encode] / total_count1 print(f" {pctEncoder.descriptEncdoe(encode)}:%.2f%%" % (occurtRate)) pass
def ganerateKPatternTrainData(): class MyPattherCollector(KPatternCollector): # 收集指定的k线 collectKPatternOnly = True KPattern: [] = [712] pct_split = [-7, -5, -3, -1.5, -0.5, 0.5, 1.5, 3, 5, 7] # pct_split = [-7, -5, -3, -1.0, 0, 1, 3, 5, 7] # pct_split = [-0.5,0.5] pctEncoder = FloatEncoder(pct_split) def __init__(self): self.kPattersMap = {} self.sw = SWImpl() self.dataSet = [] for value in self.KPattern: self.kPattersMap[value] = True def onStart(self, code: str) -> bool: self.indicator = Indicator(40) self.traceCode = code self.traceName = self.sw.getSw2Name(code) return True """ 检查是否追踪某个k线形态,是的话,创建一个最终值对象。 """ def checkIfTrace(self, newBar: BarData) -> TraceIn3DayItem: self.indicator.update_bar(newBar) kEncodeValue = KPattern.encode2KAgo1(self.indicator) if kEncodeValue is None: return None if self.collectKPatternOnly and self.kPattersMap.get( kEncodeValue) is None: return None return TraceIn3DayItem(kEncodeValue, newBar) def onTraceFinish(self, traceItem: TraceIn3DayItem): if traceItem.isWanted(): ##收集数据。 data = [] data.append(self.traceCode) data.append(self.traceName) data.append(traceItem.kPattern) data.append(traceItem.first_buy_pct) data.append(traceItem.fisrt_sell_pct) data.append(traceItem.current_sell_pct) data.append(traceItem.current_buy_pct) self.dataSet.append(data) def onDestroy(self): import pandas as pd cloumns = [ "code", "name", "kPattern", "buy_price", "sell_price", "label_sell_price", "label_buy_price" ] wxl = pd.DataFrame(self.dataSet, columns=cloumns) writer = pd.ExcelWriter('files/sw_train_data_sample.xlsx') wxl.to_excel(writer, sheet_name="sample", index=False) writer.save() writer.close() print(f"dataSize = {len(self.dataSet)}") collector: KPatternCollector = MyPattherCollector() sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) collector.onCreate() for code in lists: collector.onStart(code) barList = sw.getSW2Daily(code, start, end) traceItems = [] for bar in barList: toDeleteList = [] for traceItem in traceItems: traceItem.onTraceBar(bar) if traceItem.isFinished(): toDeleteList.append(traceItem) collector.onTraceFinish(traceItem) for traceItem in toDeleteList: traceItems.remove(traceItem) traceItem = collector.checkIfTrace(bar) if traceItem is None: continue traceItems.append(traceItem) collector.onEnd(code) collector.onDestroy()
def generateSWTrainData(kPatterns: [], start: datetime, end: datetime) -> pd.DataFrame: sw = SWImpl() lists = sw.getSW2List() cloumns = [ "code", "name", "kPattern", "k", "d", "dif", "dea", "macd", "open", "short", "long" ] datas = [] kPatternMap = {} for kPatternValues in kPatterns: kPatternMap[kPatternValues] = True macd_list = [] for code in lists: # for code in lists: name = sw.getSw2Name(code) barList = sw.getSW2Daily(code, start, end) indicator = Indicator(34) preBar = None for bar in barList: ##先识别形态 kEncodeValue = None if indicator.inited: tmpKEncodeValue = KPattern.encode3KAgo1(indicator) if kPatternMap.__contains__(tmpKEncodeValue): kEncodeValue = tmpKEncodeValue if kEncodeValue is None: indicator.update_bar(bar) preBar = bar continue ##昨天的kdj k, d, j = indicator.kdj(array=False) dif, dea, macd = indicator.macd(fast_period=12, slow_period=26, signal_period=9, array=False) ##第二天的收益 short_pct = 100 * ((bar.high_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price long_pct = 100 * ((bar.low_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price open_pct = 100 * (bar.open_price - preBar.close_price) / preBar.close_price item = [] item.append(code) item.append(name) item.append(kEncodeValue) item.append(k) item.append(d) item.append(dif) item.append(dea) item.append(macd) #下个k线数据 item.append(open_pct) item.append(short_pct) item.append(long_pct) datas.append(item) macd_list.append(macd) indicator.update_bar(bar) preBar = bar macd_list = np.array(macd_list) print( f"total size : {len(datas)},mean ={macd_list.mean()},max={macd_list.max()},min={macd_list.min()}" ) wxl = pd.DataFrame(datas, columns=cloumns) return wxl
def compute_SW_KEncode_parseAlgro1_split( pct_split=[-7, -5, -3, -1.5, -0.5, 0.5, 1.5, 3, 5, 7], extra_split=[1, 2, 3]): from earnmi.data.SWImpl import SWImpl sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) dataSet = {} pct_split = [-7, -5, -3, -1.5, -0.5, 0.5, 1.5, 3, 5, 7] extra_split = [0.5, 1.0, 1.5, 2.0, 2.5, 2.0] total_count = 0 pct_code_count = np.zeros(len(pct_split) + 1) high_extra_pct_code_count = np.zeros(len(extra_split) + 1) low_extra_pct_code_count = np.zeros(len(extra_split) + 1) for code in lists: #for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator(40) preBar = None for i in range(0, len(barList)): bar = barList[i] indicator.update_bar(bar) if (i > 10): k_code, pct_code, high_extra_pct_code, low_extra_pct_code = KEncode.parseAlgro1( indicator.close[-2], indicator.open[-1], indicator.high[-1], indicator.low[-1], indicator.close[-1], pct_split, extra_split) high_extra_pct_code_count[ high_extra_pct_code] = high_extra_pct_code_count[ high_extra_pct_code] + 1 low_extra_pct_code_count[ low_extra_pct_code] = low_extra_pct_code_count[ low_extra_pct_code] + 1 pct_code_count[pct_code] = pct_code_count[pct_code] + 1 total_count += 1 preBar = bar ##打印当前形态 print(f"pct_split: {pct_split}") print(f"extra_split: {extra_split}") count_list = pct_code_count print(f"应该服从正太分布") print(f"\n:pct_code_count分布,avg = %.4f%%" % (100 / len(count_list))) for codeId in range(0, len(count_list)): item_count = count_list[codeId] item_occur_rate = 100 * item_count / total_count print(f"\t{codeId}: %.4f %% count:%d" % (item_occur_rate, item_count)) count_list = high_extra_pct_code_count print(f"\n:high_extra_pct_code_count分布,avg = %.4f%%" % (100 / len(count_list))) for codeId in range(0, len(count_list)): item_count = count_list[codeId] item_occur_rate = 100 * item_count / total_count print(f"\t{codeId}: %.4f %% count:%d" % (item_occur_rate, item_count)) count_list = low_extra_pct_code_count print(f"\n:low_extra_pct_code_count分布,avg = %.4f%%" % (100 / len(count_list))) for codeId in range(0, len(count_list)): item_count = count_list[codeId] item_occur_rate = 100 * item_count / total_count print(f"\t{codeId}: %.4f %% count:%d" % (item_occur_rate, item_count))
def compute_SW_KPattern_data(): from earnmi.data.SWImpl import SWImpl from earnmi.chart.Chart import Chart, IndicatorItem, Signal sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) dataSet = {} total_count = 0 for code in lists: #for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator() preBar = None for bar in barList: ##先识别形态 rets = KPattern.matchIndicator(indicator) size = len(rets) if size > 0 and not preBar is None: """有形态识别出来 """ for item in rets: name = item.name value = item.value total_count += 1 dataItem = None if dataSet.__contains__(name): dataItem = dataSet[name] else: dataItem = CountItem() dataItem.values = [] ##形态被识别的值。 dataItem.pcts = [] ##识别之后第二天的盈利情况 dataSet[name] = dataItem ##第二天的收益 short_pct = ((bar.high_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price #long_pct = ((bar.high_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price ###pct = (bar.close_price - preBar.close_price) / preBar.close_price ##收录当前形态 dataItem.count_total += 1 dataItem.pct_total += short_pct if short_pct > 0.000001: dataItem.count_earn += 1 dataItem.pct_earn += short_pct dataItem.values.append(value) dataItem.pcts.append(short_pct) pass indicator.update_bar(bar) preBar = bar ##打印当前形态 print(f"总共分析{total_count}个形态,识别出{len(dataSet)}个形态,有意义的形态有:") for key, dataItem in dataSet.items(): if dataItem.count_total < 1000: continue success_rate = 100 * dataItem.count_earn / dataItem.count_total if abs(int(success_rate - 50)) < 5: continue values = np.array(dataItem.values) pcts = np.array(dataItem.pcts) * 100 count = len(values) long_values = [] short_value = [] long_pcts = [] long_ok_cnt = 0 short_pcts = [] short_ok_cnt = 0 for i in range(0, count): v = values[i] if v > 0: long_values.append(v) long_pcts.append(pcts[i]) if pcts[i] >= 0.000001: long_ok_cnt = long_ok_cnt + 1 else: short_value.append(v) short_pcts.append(pcts[i]) if pcts[i] <= -10.000001: short_ok_cnt = short_ok_cnt + 1 long_values = np.array(long_values) short_value = np.array(short_value) long_pcts = np.array(long_pcts) short_pcts = np.array(short_pcts) long_pct = 0 long_std = math.nan long_success = math.nan short_pct = 0 short_std = math.nan short_success = math.nan if len(long_values) > 0: long_pct = long_pcts.mean() long_std = long_pcts.std() long_success = long_ok_cnt / len(long_values) if len(short_value) > 0: short_pct = short_pcts.mean() short_std = short_pcts.std() short_success = short_ok_cnt / len(short_value) print( f"{key}: count={count},suc_reate=%.2f%%,long(size:{len(long_values)},suc=%.2f%%,pcts:%.2f%%,std=%.2f),short(size:{len(short_value)},suc=%.2f%%,pcts:%.2f%%,std=%.2f)" % (success_rate, long_success * 100, long_pct, long_std, short_success * 100, short_pct, short_std)) print("-----------具体情况-----------") outputKeys = ["CDLADVANCEBLOCK"] for key in outputKeys: dataItem = dataSet[key] values = np.array(dataItem.values) pcts = np.array(dataItem.pcts) * 100 count = len(dataItem.values) print( f"{key}: count={count},values:{values.mean()},pcts:%.2f%%,pcts_std=%.2f" % (pcts.mean(), pcts.std())) itemSize = 10 size = int(count / itemSize) if count % itemSize > 0: size = size + 1 for i in range(0, size): lineStr = "" start = itemSize * i end = min(start + itemSize, count) for j in range(start, end): lineStr = lineStr + (f" %4d->%.2f%%," % (values[j], pcts[j])) print(lineStr)
def printKPatterMoreDetail(kPatters=[ 6, 3, 17, 81, 7, 5, 4, 82, 159, 16, 28, 83, 15, 84, 18, 27, 93, 104, 158, 92, 160, 236, 157, 94, 85, 80, 14, 8, 161, 9, 29, 170, 26, 19, 38, 2, 79 ]): from earnmi.data.SWImpl import SWImpl from vnpy.trader.constant import Exchange from vnpy.trader.constant import Interval sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) pct_split = [-7, -5, -3, -1.5, -0.5, 0.5, 1.5, 3, 5, 7] pct_split = [-7, -5, -3, -1.0, 0, 1, 3, 5, 7] pct_split = [-0.5, 0.5] pctEncoder = FloatEncoder(pct_split) kPattersMap = {} for value in kPatters: kPattersMap[value] = True class InnerData(object): kValue: int ## sell_disbute = np.zeros(pctEncoder.mask()) ##卖方力量分布情况 buy_disbute = np.zeros(pctEncoder.mask()) #买方力量分布情况 pass dataSet = {} occurDayMap = {} allTrayDay = 1 for code in lists: # for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator(40) preBar = None previousIsMatch = False previousPatternVaule = None allTrayDay = max(allTrayDay, len(barList)) for i in range(0, len(barList)): bar = barList[i] indicator.update_bar(bar) patternValue = KPattern.encode1KAgo1(indicator) todayIsMatch = False if not patternValue is None: todayIsMatch = kPattersMap.__contains__(patternValue) if todayIsMatch: dayKey = bar.datetime.year * 13 * 35 + bar.datetime.month * 13 + bar.datetime.day occurDayMap[dayKey] = True pass if previousIsMatch: innerData: InnerData = dataSet.get(previousIsMatch) if innerData is None: innerData = InnerData() innerData.kValue = previousIsMatch dataSet[previousPatternVaule] = innerData sell_pct = 100 * ((bar.high_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price buy_pct = 100 * ((bar.low_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price innerData.buy_disbute[pctEncoder.encode(buy_pct)] += 1 innerData.sell_disbute[pctEncoder.encode(sell_pct)] += 1 pass preBar = bar previousIsMatch = todayIsMatch previousPatternVaule = patternValue print(f"所有交易日中,有意义的k线形态出现占比:%.2f%%" % (100 * len(occurDayMap) / allTrayDay)) for kValue, dataItem in dataSet.items(): total_count1 = 0 total_count2 = 0 for cnt in dataItem.sell_disbute: total_count1 += cnt for cnt in dataItem.buy_disbute: total_count2 += cnt assert total_count1 == total_count2 assert total_count1 > 0 print(f"\n\nk:%6d, " % (kValue)) print(f" 卖方价格分布:") for encode in range(0, len(dataItem.sell_disbute)): occurtRate = 100 * dataItem.sell_disbute[encode] / total_count1 print(f" {pctEncoder.descriptEncdoe(encode)}:%.2f%%" % (occurtRate)) print(f" 买方价格分布:") for encode in range(0, len(dataItem.buy_disbute)): occurtRate = 100 * dataItem.buy_disbute[encode] / total_count1 print(f" {pctEncoder.descriptEncdoe(encode)}:%.2f%%" % (occurtRate)) pass
def compute_SW_KEncode_data(): from earnmi.data.SWImpl import SWImpl sw = SWImpl() lists = sw.getSW2List() start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) dataSet = {} total_count = 0 occurKPattenDayMap = {} kBarListTotalDay = 0 for code in lists: #for code in lists: barList = sw.getSW2Daily(code, start, end) indicator = Indicator(40) preBar = None kBarListTotalDay = len(barList) for bar in barList: ##先识别形态 kEncodeValue = KPattern.encode1KAgo1(indicator) if kEncodeValue is None: indicator.update_bar(bar) preBar = bar continue total_count += 1 dataItem: CountItem = None if dataSet.__contains__(kEncodeValue): dataItem = dataSet[kEncodeValue] else: dataItem = CountItem() dataSet[kEncodeValue] = dataItem ##第二天的收益 pct = ((bar.high_price + bar.close_price) / 2 - preBar.close_price) / preBar.close_price ##收录当前形态 #dataItem.values.append(value) dataItem.count_total += 1 dataItem.pct_total += pct if pct > 0.000001: dataItem.count_earn += 1 dataItem.pct_earn += pct indicator.update_bar(bar) preBar = bar occurDayKey = preBar.datetime.year * 13 * 35 + preBar.datetime.month * 35 + preBar.datetime.day occurKPattenDayMap[occurDayKey] = True ##打印当前形态 occur_count = 0 print(f"总共分析{total_count}个形态,识别出{len(dataSet)}个形态,有意义的形态有:") max_succ_rate = 0 min_succ_rate = 100 ret_list = [] for key, dataItem in dataSet.items(): success_rate = 100 * dataItem.count_earn / dataItem.count_total if dataItem.count_total < 500: continue if abs(int(success_rate - 50)) < 10: continue ret_list.append(key) earn_pct = 100 * dataItem.pct_earn / dataItem.count_earn if success_rate < 50: earn_pct = 100 * (dataItem.pct_total - dataItem.pct_earn) / ( dataItem.count_total - dataItem.count_earn) avg_pct = 100 * dataItem.pct_total / dataItem.count_total occur_count += dataItem.count_total occur_rate = 100 * dataItem.count_total / total_count max_succ_rate = max(success_rate, max_succ_rate) min_succ_rate = min(success_rate, min_succ_rate) print( f"{key}: total={dataItem.count_total},suc=%.2f%%,occur_rate=%.2f%%,earn_pct:%.2f%%,avg_pct:%.2f%%)" % (success_rate, occur_rate, earn_pct, avg_pct)) total_occur_rate = 100 * occur_count / total_count total_occur_in_day_rate = 100 * len( occurKPattenDayMap) / kBarListTotalDay ##在所有交易日中,k线形态日出占比: print(f"总共:occur_rate=%.2f%%, min_succ_rate=%.2f%%, max_succ_rate=%.2f%%" f"\n所有交易日中,k线形态日出占比:%.2f%%" % (total_occur_rate, min_succ_rate, max_succ_rate, total_occur_in_day_rate)) print(f"{ret_list}")
if __name__ == "__main__": import pickle from earnmi.model.CoreEngineImpl import CoreEngineImpl from earnmi.data.SWImpl import SWImpl def saveCollectData(bars: []): fileName = "files/testSaveCollectData.bin" with open(fileName, 'wb') as fp: pickle.dump(bars, fp, -1) def loadCollectData(): bars = None fileName = "files/testSaveCollectData.bin" with open(fileName, 'rb') as fp: bars = pickle.load(fp) return bars start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) sw = SWImpl() code = sw.getSW2List()[3] bars = sw.getSW2Daily(code, start, end) #saveCollectData(bars) bars2 = loadCollectData() assert bars == bars2 assert len(bars) == len(bars2) and len(bars2) != 0
start = datetime(2014, 5, 1) end = datetime(2020, 8, 17) # code = "600196" # # # market = MarketImpl() # market.addNotice(code) # market.setToday(end) # # # bars = market.getHistory().getKbarFrom(code,start) sw = SWImpl() lists = sw.getSW2List() chart = Chart() for code in lists: bars = sw.getSW2Daily(code, start, end) print(f"bar.size = {bars.__len__()}") item = AroonItem() chart.run(bars, item) print(f"holdbars = {len(item.getHoldBars())}") barList = [] close_price = None for holdBar in item.getHoldBars(): if close_price is None: barList.append(holdBar.toBarData())
def computeHoldBarIndictor(indictor:IndicatorItem)->HoldBarData: sw = SWImpl() lists = sw.getSW2List() chart = Chart() total_cost_pcts = []#收益 avg_eran_cost_pcts = []#每个盈利holdbard的平均盈利 total_days = [] total_eran_days = [] total_holdbars = [] total_holdbars_earn = [] max_cost_pcts =[] #最大收益 min_cost_pcts = [] for code in lists: if len(sw.getSW2Stocks(code)) < 10: continue bars = sw.getSW2Daily(code, start, end) #print(f"bar.size = {bars.__len__()}") chart.run(bars, indictor) holdbarList = indictor.getHoldBars() #holdbarList = HoldBarUtils.filterHoldBar(holdbarList) data = HoldBarUtils.computeHoldBarIndictor(holdbarList); if data is None: continue total_cost_pcts.append(data.total_cost_pct) max_cost_pcts.append(data.max_cost_pct) min_cost_pcts.append(data.min_cost_pct) total_days.append(data.total_day) total_holdbars.append(data.total_holdbar) total_holdbars_earn.append(data.total_holdbar_earn) avg_eran_cost_pcts.append(data.avg_eran_cost_pct) total_eran_days.append(data.total_earn_day) ret = HoldBarData() total_cost_pcts = np.array(total_cost_pcts) total_days = np.array(total_days) max_cost_pcts = np.array(max_cost_pcts) min_cost_pcts = np.array(min_cost_pcts) total_holdbars = np.array(total_holdbars) total_holdbars_earn = np.array(total_holdbars_earn) avg_eran_cost_pcts = np.array(avg_eran_cost_pcts) total_eran_days = np.array(total_eran_days) ret.total_min_cost_pct = total_cost_pcts.min() ret.total_max_cost_pct = total_cost_pcts.max() ret.total_cost_pct = total_cost_pcts.mean() ret.total_cost_pct_std = np.std(total_cost_pcts) ret.total_day = total_days.mean() ret.max_cost_pct = max_cost_pcts.mean() ret.min_cost_pct = min_cost_pcts.mean() ret.total_holdbar = total_holdbars.mean() ret.total_holdbar_earn = total_holdbars_earn.mean() ret.avg_eran_cost_pct = avg_eran_cost_pcts.mean() ret.total_earn_day = total_eran_days.mean() return ret