def onCollectTrace(self, bar: BarData) -> CollectData:
        self.indicator.update_bar(bar)
        self.lasted15Bar[:-1] = self.lasted15Bar[1:]
        self.lasted3BarKdj[:-1] = self.lasted3BarKdj[1:]
        self.lasted3BarMacd[:-1] = self.lasted3BarMacd[1:]
        self.lasted3BarArron[:-1] = self.lasted3BarArron[1:]
        k, d, j = self.indicator.kdj(fast_period=9, slow_period=3)
        dif, dea, mBar = self.indicator.macd(fast_period=12,
                                             slow_period=26,
                                             signal_period=9)
        aroon_down, aroon_up = self.indicator.aroon(n=14)

        self.lasted15Bar[-1] = bar
        self.lasted3BarKdj[-1] = [k, d, j]
        self.lasted3BarMacd[-1] = [dif, dea, mBar]
        self.lasted3BarArron[-1] = [aroon_down, aroon_up]

        if self.indicator.count <= 15:
            return None

        #最近15天之内不含停牌数据
        if not BarUtils.isAllOpen(self.lasted15Bar):
            return None
        #交易日天数间隔超过5天的数据
        if BarUtils.getMaxIntervalDay(self.lasted15Bar) >= 5:
            return None

        timeKey = utils.to_start_date(bar.datetime)
        if self.mDateOccurCountMap.get(timeKey) is None:
            self.mDateOccurCountMap[timeKey] = 0

        if self.indicator.count >= 30:
            k0, d0, j0 = self.lasted3BarKdj[-2]
            k1, d1, j1 = self.lasted3BarKdj[-1]
            #金叉产生
            goldCross = k0 < d0 and k1 >= d1
            if not goldCross:
                return None
            kPatternValue = KPattern.encode3KAgo1(self.indicator)
            if not kPatternValue is None:
                self.mDateOccurCountMap[timeKey] += 1
                dimen = Dimension(type=TYPE_2KAGO1, value=kPatternValue)
                collectData = CollectData(dimen=dimen)
                collectData.occurBars = list(self.lasted15Bar[-3:])
                collectData.occurKdj = list(self.lasted3BarKdj)
                collectData.occurExtra['lasted3BarMacd'] = self.lasted3BarMacd
                collectData.occurExtra[
                    'lasted3BarArron'] = self.lasted3BarArron
                return collectData
        return None
Beispiel #2
0
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