示例#1
0
    def update_tick(self, tick: TickData) -> None:
        """
        Update new tick data into generator.
        """
        new_bar = False

        # Filter tick data with 0 last price
        if not tick.last_price:
            return

        # Filter tick data with less intraday trading volume (i.e. older timestamp)
        if (self.last_tick and tick.volume
                and tick.volume < self.last_tick.volume):
            return

        if not self.bar:
            new_bar = True
        elif (self.bar.datetime.second != tick.datetime.second
              and not tick.datetime.second % self.window):
            self.on_bar(self.bar)

            new_bar = True

        if new_bar:
            self.bar = BarData(symbol=tick.symbol,
                               exchange=tick.exchange,
                               interval=Interval.MINUTE,
                               datetime=tick.datetime,
                               gateway_name=tick.gateway_name,
                               open_price=tick.last_price,
                               high_price=tick.last_price,
                               low_price=tick.last_price,
                               close_price=tick.last_price,
                               open_interest=tick.open_interest)
        else:
            self.bar.high_price = max(self.bar.high_price, tick.last_price)
            self.bar.low_price = min(self.bar.low_price, tick.last_price)
            self.bar.close_price = tick.last_price
            self.bar.open_interest = tick.open_interest
            self.bar.datetime = tick.datetime

        if self.last_tick:
            volume_change = tick.volume - self.last_tick.volume
            self.bar.volume += max(volume_change, 0)

        self.last_tick = tick
示例#2
0
    def update_tick(self, tick: TickData) -> None:
        """
        修改分钟合成逻辑,由原来的标准时间,修改成当前50秒到下个49秒
        """
        new_minute = False

        # Filter tick data with 0 last price
        if not tick.last_price:
            return

        if not self.bar:
            new_minute = True
        # elif self.bar.datetime.minute != tick.datetime.minute:
        #当前k线时间大于52 并且 存的K线小于 51 来切分一根K线的开始结束时间
        elif tick.datetime.second >= 52 and self.last_tick.datetime.second < 51:
            self.bar.datetime = self.bar.datetime.replace(second=0,
                                                          microsecond=0)
            self.on_bar(self.bar)

            new_minute = True

        if new_minute:
            self.bar = BarData(symbol=tick.symbol,
                               exchange=tick.exchange,
                               interval=Interval.MINUTE,
                               datetime=tick.datetime,
                               gateway_name=tick.gateway_name,
                               open_price=tick.last_price,
                               high_price=tick.last_price,
                               low_price=tick.last_price,
                               close_price=tick.last_price,
                               open_interest=tick.open_interest)
        else:
            self.bar.high_price = max(self.bar.high_price, tick.last_price)
            self.bar.low_price = min(self.bar.low_price, tick.last_price)
            self.bar.close_price = tick.last_price
            self.bar.open_interest = tick.open_interest
            self.bar.datetime = tick.datetime

        if self.last_tick:
            volume_change = tick.volume - self.last_tick.volume
            self.bar.volume += max(volume_change, 0)

        self.last_tick = tick
示例#3
0
 def compute_index_zz500_specifications(self, df):
     # 注意:所有的数据库数据和列表数据都按照日期的正序排序(从小到大)
     # 计算zz500指数的技术指标并入库
     """
     @ 入参:指数k线信息
     """
     am = ArrayManager(size=600)   
     for ix, row in df.iterrows():
         d = row.to_dict()
         LOG.info(d['trade_date'])   
         d['ts_code'] = d['ts_code'].replace('.', '_')
         bar = BarData(
                 gateway_name='ctp', symbol=d['ts_code'],
                 exchange=Exchange.SSE,
                 datetime=string_to_datetime(d['trade_date']))
         bar.symbol = d['ts_code']
         bar.open_price = d['open']
         bar.high_price = d['high']
         bar.low_price = d['low']
         bar.close_price = d['close']
         am.update_bar(bar)      
         rsi_20 = am.rsi(20)
         d['rsi_20'] = rsi_20
         try:
             d['ma_5'] = am.sma(5)
         except:
             traceback.print_exc()                    
             LOG.error('************************')
             LOG.error(d['ts_code'])
             LOG.error(d['trade_date'])
             LOG.error(bar)
         d['ma_10'] = am.sma(10)
         d['ma_20'] = am.sma(20)
         d['ma_30'] = am.sma(30)
         d['ma_60'] = am.sma(60)
         d['ma_120'] = am.sma(120)
         d['ma_250'] = am.sma(250)
         d['ma_500'] = am.sma(500)
         flt = {'trade_date': d['trade_date']}
         cl_index_zz500 = self.db[CL_INDEX_ZZ500]
         # cl_index_zz500.replace_one(flt, d, upsert=False)
         cl_index_zz500.update_one(flt, {'$setOnInsert': d}, upsert=True)   # 插入数据时,flt不存在则插入d,存在则不执行
示例#4
0
    def update_bar(self, bar: BarData):
        pre_price = bar.close_price
        if self.up is None:
            self.up = pre_price
            self.down = pre_price
            self.datetime = bar.datetime
            self.volume = bar.volume

            self.bar = BarData(
                symbol=bar.symbol,
                exchange=bar.exchange,
                datetime=bar.datetime,
                gateway_name=bar.gateway_name,
                open_price=pre_price,
                high_price=pre_price,
                low_price=pre_price,
                close_price=pre_price,
            )

        self.update_pre_price(pre_price)
        self.datetime = bar.datetime
        self.volume += bar.volume
示例#5
0
    def update_tick(self, tick: TickData):
        pre_price = tick.last_price

        if self.up is None:
            self.up = pre_price
            self.down = pre_price
            self.datetime = tick.datetime
            self.volume = tick.volume

            self.bar = BarData(
                symbol=tick.symbol,
                exchange=tick.exchange,
                datetime=tick.datetime,
                gateway_name=tick.gateway_name,
                open_price=pre_price,
                high_price=pre_price,
                low_price=pre_price,
                close_price=pre_price,
            )

        self.update_pre_price(pre_price)
        self.datetime = tick.datetime
        self.volume += tick.volume
示例#6
0
    def on_x_min(self, bar: BarData):
        self.am_x_min.update_bar(bar)
        if not self.am_x_min.inited:
            return

        if not self.base_high_price:
            self.base_high_price = bar.high_price
            self.past_high_price = bar.high_price
            self.base_low_price = bar.low_price
            self.past_low_price = bar.low_price

        bar.datetime = self.get_diff_datetime(bar.datetime)
        high_price = bar.high_price
        low_price = bar.low_price
        """分型处理"""
        # 上升
        if high_price > self.base_high_price and low_price > self.base_low_price:
            if self.brush_signal_special2 == -2 and self.is_jump:
                self.shape_dict[self.brush_datetime_special]["interval_down"] \
                    = self.shape_dict[self.brush_datetime_special]["interval_down_back"]
            self.brush_signal_special2 = 0

            if self.base_signal == 1 and low_price > self.base_high_price:
                self.is_jump = True

            self.handle_bars.append({
                "high_price": self.base_high_price,
                "low_price": self.base_low_price,
            })

            # 上升K线
            if self.base_signal == 1:
                pass

            # 底分型
            else:
                self.trade_signal = None
                self.brush_signal_special2 = 1
                self.brush_datetime_special = self.past_bar.datetime

                if self.brush_signal_special1 == 1:
                    _past_high_price = self.base_high_price
                else:
                    _past_high_price = self.past_high_price

                self.base_signal = 1
                self.shape_dict[self.past_bar.datetime] = {
                    "datetime":
                    self.past_bar.datetime,
                    "signal":
                    -1,
                    "value":
                    self.base_low_price,
                    "interval_up":
                    max(_past_high_price, self.base_high_price, high_price),
                    "interval_up_back":
                    max(_past_high_price, self.base_high_price),
                    "interval_down":
                    self.base_low_price,
                    "bar":
                    copy.deepcopy(self.past_bar),
                    "bars":
                    copy.deepcopy(self.bars),
                    "handle_bars":
                    copy.deepcopy(self.handle_bars),
                }
                """画笔处理"""
                # 确认上顶分型,暂定当前底分型
                self.brush_handle_bars = self.brush_handle_bars[:] + self.handle_bars[:]
                if self.brush_signal is None:
                    self.brush_signal = -1

                else:
                    if self.brush_signal == 1:
                        interval_up = self.shape_dict[
                            self.past_bar.datetime]["interval_up_back"]
                        interval_down = self.shape_dict[
                            self.past_bar.datetime]["interval_down"]
                        past_interval_up = interval_up if self.brush_past_datetime is None else \
                            self.shape_dict[self.brush_past_datetime]["interval_up"]
                        past_interval_down = interval_down if self.brush_past_datetime is None else \
                            self.shape_dict[self.brush_past_datetime]["interval_down"]

                        if (self.is_jump or (len(self.brush_bars) >= 4 and len(self.brush_handle_bars) > 2)) \
                                and not (interval_up < past_interval_up and interval_down > past_interval_down) \
                                and not (interval_up > past_interval_up and interval_down < past_interval_down):

                            # 确认上顶分型
                            if self.brush_past_datetime:

                                self.brush_dict[
                                    self.
                                    brush_past_datetime] = self.shape_dict[
                                        self.brush_past_datetime]
                                if self.brush_past_datetime in self.brush_datetime_list.keys(
                                ):
                                    self.brush_datetime_list[self.brush_past_datetime] = \
                                        self.shape_dict[self.brush_past_datetime]["value"]
                                    self.KLine_chart_dict.list_dict[
                                        "signal"] = list(
                                            self.brush_datetime_list.values())
                                """中枢处理"""
                                self.list_fixed_length(
                                    self.four_list, self.brush_past_datetime,
                                    4)
                                if self.center_signal is None:
                                    self.judge_center_signal()
                                else:
                                    zg, zd, gg, dd = self.get_center_info(
                                        is_skip_last=True)
                                    if self.shape_dict[
                                            self.
                                            brush_past_datetime]["value"] < zd:
                                        """交易信号判定:3卖"""
                                        if self.on_trade_signal3:
                                            self.on_trade_signal3("short", bar)
                                        current_value = self.brush_dict[
                                            self.brush_past_datetime]["value"]
                                        self.point_list.append({
                                            "name":
                                            "3卖",
                                            "color":
                                            "#53FA00",
                                            "datetime":
                                            self.brush_past_datetime,
                                            "y":
                                            current_value,
                                        })
                                        self.trade_signal_num3[
                                            self.brush_past_datetime] = {
                                                "datetime": bar.datetime,
                                                "value": bar.close_price,
                                                "shape":
                                                self.brush_past_datetime,
                                            }

                                        self.center_signal_true = -1
                                        self.center_list.append({
                                            "signal":
                                            self.center_signal_true,
                                            "shape_list":
                                            copy.deepcopy(
                                                self.center_shape_list),
                                            "out_length":
                                            self.shape_dict[
                                                self.center_shape_list[-2]]
                                            ["value"] - self.shape_dict[
                                                self.center_shape_list[-1]]
                                            ["value"],
                                            "zg":
                                            zg,
                                            "zd":
                                            zd,
                                            "gg":
                                            gg,
                                            "dd":
                                            dd,
                                            "trade_signal_num1":
                                            copy.deepcopy(
                                                self.trade_signal_num1),
                                            "trade_signal_num2":
                                            copy.deepcopy(
                                                self.trade_signal_num2),
                                            "trade_signal_num2_2":
                                            copy.deepcopy(
                                                self.trade_signal_num2_2),
                                            "trade_signal_num3":
                                            copy.deepcopy(
                                                self.trade_signal_num3),
                                        })
                                        self.area_list.append({
                                            "x": (self.center_shape_list[0],
                                                  self.center_shape_list[-2]),
                                            "y": (zg, zd),
                                            "border_color":
                                            "#ef232a"
                                            if self.center_signal_true == 1
                                            else "#14b143"
                                        })
                                        self.center_signal = None
                                        self.trade_signal_num1 = {}
                                        self.trade_signal_num2 = {}
                                        self.trade_signal_num2_2 = {}
                                        self.trade_signal_num3 = {}

                                    else:
                                        self.center_shape_list.append(
                                            self.brush_past_datetime)
                                """交易信号判定"""
                                if self.center_signal is not None:
                                    zg, zd, gg, dd = self.get_center_info()
                                    brush_dict_keys = list(
                                        self.brush_dict.keys())
                                    trade_signal_shape = self.brush_dict[
                                        self.brush_past_datetime]
                                    trade_signal_shape2 = self.brush_dict[
                                        brush_dict_keys[-2]]
                                    current_value = trade_signal_shape["value"]

                                    if self.center_signal_true == 1:
                                        """1卖"""
                                        compare_length = current_value - trade_signal_shape2[
                                            "value"]
                                        if compare_length < self.center_list[-1][
                                                "out_length"] and current_value >= gg:
                                            if self.on_trade_signal1:
                                                self.on_trade_signal1(
                                                    "short", bar)
                                            self.point_list.append({
                                                "name":
                                                "1卖",
                                                "color":
                                                "#336600",
                                                "datetime":
                                                self.brush_past_datetime,
                                                "y":
                                                current_value,
                                            })
                                            self.trade_signal_num1[
                                                self.brush_past_datetime] = {
                                                    "trade_datetime":
                                                    bar.datetime,
                                                    "trade_value":
                                                    bar.close_price,
                                                    "signal_datetime":
                                                    self.brush_past_datetime,
                                                }
                                        """2卖"""
                                        trade_signal_num1_keys = list(
                                            self.trade_signal_num1.keys())
                                        trade_signal_num2_keys = list(
                                            self.trade_signal_num2.keys())
                                        if len(trade_signal_num1_keys) - len(trade_signal_num2_keys) == 1 \
                                                and self.brush_past_datetime not in trade_signal_num1_keys \
                                                and current_value < self.brush_dict[trade_signal_num1_keys[-1]]["value"]:
                                            if self.on_trade_signal2:
                                                self.on_trade_signal2(
                                                    "short", bar)
                                            self.point_list.append({
                                                "name":
                                                "2卖",
                                                "color":
                                                "#4D9900",
                                                "datetime":
                                                self.brush_past_datetime,
                                                "y":
                                                current_value,
                                            })
                                            self.trade_signal_num2[
                                                self.brush_past_datetime] = {
                                                    "datetime": bar.datetime,
                                                    "value": bar.close_price,
                                                    "shape":
                                                    self.brush_past_datetime,
                                                }

                            self.brush_signal = -1
                            self.brush_past_datetime = self.past_bar.datetime
                            self.brush_bars = []
                            self.brush_handle_bars = []

                        else:
                            pass
                    else:
                        if self.brush_past_datetime is not None \
                                and self.base_low_price < self.shape_dict[self.brush_past_datetime]["value"]:
                            self.brush_past_datetime = self.past_bar.datetime
                            self.brush_bars = []
                            self.brush_handle_bars = []

                if low_price > self.base_high_price:
                    self.is_jump = True
                else:
                    self.is_jump = False

                self.bars = []
                self.handle_bars = []

            self.past_high_price = self.base_high_price
            self.past_low_price = self.base_low_price
            self.base_high_price = high_price
            self.base_low_price = low_price
            self.brush_signal_special1 = 0

        # 下降
        elif high_price < self.base_high_price and low_price < self.base_low_price:
            if self.brush_signal_special2 == -1 and self.is_jump:
                self.shape_dict[self.brush_datetime_special]["interval_up"] \
                    = self.shape_dict[self.brush_datetime_special]["interval_up_back"]
            self.brush_signal_special2 = 0

            if self.base_signal == -1 and high_price < self.base_low_price:
                self.is_jump = True

            self.handle_bars.append({
                "high_price": self.base_high_price,
                "low_price": self.base_low_price,
            })

            # 顶分型
            if self.base_signal == 1:
                self.trade_signal = None
                self.brush_signal_special2 = 2
                self.brush_datetime_special = self.past_bar.datetime

                if self.brush_signal_special1 == 2:
                    _past_low_price = self.base_low_price
                else:
                    _past_low_price = self.past_low_price

                self.base_signal = -1
                self.shape_dict[self.past_bar.datetime] = {
                    "datetime":
                    self.past_bar.datetime,
                    "signal":
                    1,
                    "value":
                    self.base_high_price,
                    "interval_up":
                    self.base_high_price,
                    "interval_down":
                    min(_past_low_price, self.base_low_price, low_price),
                    "interval_down_back":
                    min(_past_low_price, self.base_low_price),
                    "bar":
                    copy.deepcopy(self.past_bar),
                    "bars":
                    copy.deepcopy(self.bars),
                    "handle_bars":
                    copy.deepcopy(self.handle_bars),
                }
                """画笔处理"""
                # 确认上底分型,暂定当前顶分型
                self.brush_handle_bars = self.brush_handle_bars[:] + self.handle_bars[:]
                if self.brush_signal is None:
                    self.brush_signal = 1
                    self.brush_past_datetime = self.past_bar.datetime

                else:
                    if self.brush_signal == 1:
                        if self.brush_past_datetime is not None \
                                and self.base_high_price > self.shape_dict[self.brush_past_datetime]["value"]:
                            self.brush_past_datetime = self.past_bar.datetime
                            self.brush_bars = []
                            self.brush_handle_bars = []

                    else:
                        interval_up = self.shape_dict[
                            self.past_bar.datetime]["interval_up"]
                        interval_down = self.shape_dict[
                            self.past_bar.datetime]["interval_down_back"]
                        past_interval_up = interval_up if self.brush_past_datetime is None else \
                            self.shape_dict[self.brush_past_datetime]["interval_up"]
                        past_interval_down = interval_down if self.brush_past_datetime is None else \
                            self.shape_dict[self.brush_past_datetime]["interval_down"]

                        if (self.is_jump or (len(self.brush_bars) >= 4 and len(self.brush_handle_bars) > 2)) \
                                and not (interval_up < past_interval_up and interval_down > past_interval_down) \
                                and not (interval_up > past_interval_up and interval_down < past_interval_down):

                            if self.brush_past_datetime:

                                self.brush_dict[
                                    self.
                                    brush_past_datetime] = self.shape_dict[
                                        self.brush_past_datetime]
                                if self.brush_past_datetime in self.brush_datetime_list.keys(
                                ):
                                    self.brush_datetime_list[self.brush_past_datetime] \
                                        = self.shape_dict[self.brush_past_datetime]["value"]
                                    self.KLine_chart_dict.list_dict[
                                        "signal"] = list(
                                            self.brush_datetime_list.values())
                                """中枢处理"""
                                self.list_fixed_length(
                                    self.four_list, self.brush_past_datetime,
                                    4)
                                if self.center_signal is None:
                                    self.judge_center_signal()
                                else:
                                    zg, zd, gg, dd = self.get_center_info(
                                        is_skip_last=True)
                                    if self.shape_dict[
                                            self.
                                            brush_past_datetime]["value"] > zg:
                                        """交易信号判定:3买"""
                                        if self.on_trade_signal3:
                                            self.on_trade_signal3("long", bar)
                                        current_value = self.brush_dict[
                                            self.brush_past_datetime]["value"]
                                        self.point_list.append({
                                            "name":
                                            "3买",
                                            "color":
                                            "#FF4D82",
                                            "datetime":
                                            self.brush_past_datetime,
                                            "y":
                                            current_value,
                                        })
                                        self.trade_signal_num3[
                                            self.brush_past_datetime] = {
                                                "datetime": bar.datetime,
                                                "value": bar.close_price,
                                                "shape":
                                                self.brush_past_datetime,
                                            }

                                        self.center_signal_true = 1
                                        self.center_list.append({
                                            "signal":
                                            self.center_signal_true,
                                            "shape_list":
                                            copy.deepcopy(
                                                self.center_shape_list),
                                            "out_length":
                                            self.shape_dict[
                                                self.center_shape_list[-1]]
                                            ["value"] - self.shape_dict[
                                                self.center_shape_list[-2]]
                                            ["value"],
                                            "zg":
                                            zg,
                                            "zd":
                                            zd,
                                            "gg":
                                            gg,
                                            "dd":
                                            dd,
                                            "trade_signal_num1":
                                            copy.deepcopy(
                                                self.trade_signal_num1),
                                            "trade_signal_num2":
                                            copy.deepcopy(
                                                self.trade_signal_num2),
                                            "trade_signal_num2_2":
                                            copy.deepcopy(
                                                self.trade_signal_num2_2),
                                            "trade_signal_num3":
                                            copy.deepcopy(
                                                self.trade_signal_num3),
                                        })
                                        self.area_list.append({
                                            "x": (self.center_shape_list[0],
                                                  self.center_shape_list[-2]),
                                            "y": (zg, zd),
                                            "border_color":
                                            "#ef232a"
                                            if self.center_signal_true == 1
                                            else "#14b143"
                                        })
                                        self.center_signal = None
                                        self.trade_signal_num1 = {}
                                        self.trade_signal_num2 = {}
                                        self.trade_signal_num2_2 = {}
                                        self.trade_signal_num3 = {}

                                    else:
                                        self.center_shape_list.append(
                                            self.brush_past_datetime)
                                """交易信号判定 59B300 00F000 2EFF2E"""
                                if self.center_signal is not None:
                                    zg, zd, gg, dd = self.get_center_info()
                                    brush_dict_keys = list(
                                        self.brush_dict.keys())
                                    trade_signal_shape = self.brush_dict[
                                        self.brush_past_datetime]
                                    trade_signal_shape2 = self.brush_dict[
                                        brush_dict_keys[-2]]
                                    current_value = trade_signal_shape["value"]

                                    if self.center_signal_true == -1:
                                        """1买"""
                                        compare_length = trade_signal_shape2[
                                            "value"] - current_value
                                        if compare_length < self.center_list[-1][
                                                "out_length"] and current_value <= dd:
                                            if self.on_trade_signal1:
                                                self.on_trade_signal1(
                                                    "long", bar)
                                            self.point_list.append({
                                                "name":
                                                "1买",
                                                "color":
                                                "#CC003D",
                                                "datetime":
                                                self.brush_past_datetime,
                                                "y":
                                                current_value,
                                            })
                                            self.trade_signal_num1[
                                                self.brush_past_datetime] = {
                                                    "datetime": bar.datetime,
                                                    "value": bar.close_price,
                                                    "shape":
                                                    self.brush_past_datetime,
                                                }
                                        """2买"""
                                        trade_signal_num1_keys = list(
                                            self.trade_signal_num1.keys())
                                        trade_signal_num2_keys = list(
                                            self.trade_signal_num2.keys())
                                        if len(trade_signal_num1_keys) - len(trade_signal_num2_keys) == 1 \
                                                and self.brush_past_datetime not in trade_signal_num1_keys \
                                                and current_value > self.brush_dict[trade_signal_num1_keys[-1]]["value"]:
                                            if self.on_trade_signal2:
                                                self.on_trade_signal2(
                                                    "long", bar)
                                            self.point_list.append({
                                                "name":
                                                "2买",
                                                "color":
                                                "#FF3B0A",
                                                "datetime":
                                                self.brush_past_datetime,
                                                "y":
                                                current_value,
                                            })
                                            self.trade_signal_num2[
                                                self.brush_past_datetime] = {
                                                    "datetime": bar.datetime,
                                                    "value": bar.close_price,
                                                    "shape":
                                                    self.brush_past_datetime,
                                                }

                            self.brush_signal = 1
                            self.brush_past_datetime = self.past_bar.datetime
                            self.brush_bars = []
                            self.brush_handle_bars = []
                        else:
                            pass

                if high_price < self.base_low_price:
                    self.is_jump = True
                else:
                    self.is_jump = False

                self.bars = []
                self.handle_bars = []

            # 下降K线
            else:
                pass

            self.past_high_price = self.base_high_price
            self.past_low_price = self.base_low_price
            self.base_high_price = high_price
            self.base_low_price = low_price
            self.brush_signal_special1 = 0

        # 左包含
        elif high_price <= self.base_high_price and low_price >= self.base_low_price:
            if self.base_signal == 1:
                self.base_high_price = max(self.base_high_price, high_price)
                self.base_low_price = max(self.base_low_price, low_price)
            else:
                self.base_high_price = min(self.base_high_price, high_price)
                self.base_low_price = min(self.base_low_price, low_price)

            if self.base_signal == 1 and self.base_low_price > self.past_high_price:
                self.is_jump = True
                self.brush_signal_special1 = 2
            elif self.base_signal == -1 and self.base_high_price < self.past_low_price:
                self.is_jump = True
                self.brush_signal_special1 = 1

            if self.brush_signal_special2 > 0:
                self.brush_signal_special2 = -self.brush_signal_special2

        # 右包含
        elif high_price >= self.base_high_price and low_price <= self.base_low_price:
            if self.base_signal == 1:
                self.base_high_price = max(self.base_high_price, high_price)
                self.base_low_price = max(self.base_low_price, low_price)
            else:
                self.base_high_price = min(self.base_high_price, high_price)
                self.base_low_price = min(self.base_low_price, low_price)

            if self.base_signal == 1 and self.base_low_price > self.past_high_price:
                self.is_jump = True
                self.brush_signal_special1 = 2
            elif self.base_signal == -1 and self.base_high_price < self.past_low_price:
                self.is_jump = True
                self.brush_signal_special1 = 1

            if self.brush_signal_special2 > 0:
                self.brush_signal_special2 = -self.brush_signal_special2

        self.bars.append(copy.deepcopy(bar))
        self.brush_bars.append(copy.deepcopy(bar))

        if self.main_self.trading and self.KLine_chart_dict is not None and self.past_bar is not None:
            self.brush_datetime_list[self.past_bar.datetime] = None

            self.KLine_chart_dict.update_bar(
                self.past_bar,
                signal=None,
                datetime_str=self.past_bar.datetime,
                # signal=signal,
            )

        self.past_bar = copy.deepcopy(bar)
示例#7
0
 def _update_k_data(self, code, k_data):
     # 注意:所有的数据库数据和列表数据都按照日期的正序排序(从小到大)
     """
     更新股票,股指每日数据(行情,K线,市值等0)
     @:param code  股票(指数)代码                
     @:param k_data  ts中获取的最新df数据
     """        
     if len(k_data) != 0:
         k_data = k_data.sort_values(by='trade_date')            
         cl_stock_code = self.db[code]
         cl_stock_code.create_index([('trade_date', ASCENDING)], unique=True)
         # 更新k线数据
         # 1、新增日K线入库
         # 2、遍历数据库找出最近的500+22(必须保证更新数据操作在22天以内进行)条数据并更新最后的22条的ma和最高 最低价
         for ix, row in k_data.iterrows():
             d = row.to_dict()
             d['ts_code'] = d['ts_code'].replace('.', '_')
             if self._is_in_vnpy_db(d['ts_code'], update=True):
                 # 更新vnpy数据库数据                    
                 self._build_db_vnpy(d)
             flt = {'trade_date': d['trade_date']}
             cl_stock_code.replace_one(flt, d, upsert=True)
         rec = list(cl_stock_code.find({}).sort("trade_date", DESCENDING).limit(522))
         rec.reverse()
         am = ArrayManager(size=600)
         last_5_vol = deque([0.0] * 5)
         last_5_amount = deque([0.0] * 5)
         for d in rec:                
             if 0.0 not in last_5_vol:
                 vol_rate = d['vol'] / (sum(last_5_vol) / 5.0)               
                 amount_rate = d['amount'] / (sum(last_5_amount) / 5.0)   
                 d['vol_rate'] = vol_rate
                 d['amount_rate'] = amount_rate 
             else:
                 d['vol_rate'] = 0.0
                 d['amount_rate'] = 0.0
             last_5_vol.popleft()
             last_5_amount.popleft()
             last_5_vol.append(d['vol'])
             last_5_amount.append(d['amount'])                                       
             if d['ts_code'][-3:] == '_SH':
                 exchange = Exchange.SSE
                 d['exchange'] = 'SSE'
             else:
                 exchange = Exchange.SZSE
                 d['exchange'] = 'SZSE'                
             bar = BarData(
                 gateway_name='ctp', symbol=d['ts_code'],
                 exchange=exchange,
                 datetime=string_to_datetime(d['trade_date']))
             bar.symbol = d['ts_code']
             bar.volume = d['vol']
             bar.open_price = d['open']
             bar.high_price = d['high']
             bar.low_price = d['low']
             bar.close_price = d['close']
             am.update_bar(bar)
             if d['trade_date'] >= self.db_date:
                 d['ma_5'] = am.sma(5)
                 d['ma_10'] = am.sma(10)
                 d['ma_20'] = am.sma(20)
                 d['ma_30'] = am.sma(30)
                 d['ma_60'] = am.sma(60)
                 d['ma_120'] = am.sma(120)
                 d['ma_250'] = am.sma(250)
                 d['ma_500'] = am.sma(500)
                 d['high_5'] = np.max(am.high[-5:])
                 d['high_10'] = np.max(am.high[-10:])
                 d['high_20'] = np.max(am.high[-20:])
                 d['high_30'] = np.max(am.high[-30:])
                 d['high_60'] = np.max(am.high[-60:])
                 d['high_120'] = np.max(am.high[-120:])
                 d['high_250'] = np.max(am.high[-250:])
                 d['high_500'] = np.max(am.high[-500:])
                 d['low_5'] = np.min(am.low[-5:])
                 d['low_10'] = np.min(am.low[-10:])
                 d['low_20'] = np.min(am.low[-20:])
                 d['low_30'] = np.min(am.low[-30:])
                 d['low_60'] = np.min(am.low[-60:])
                 d['low_120'] = np.min(am.low[-120:])
                 d['low_250'] = np.min(am.low[-250:])
                 d['low_500'] = np.min(am.low[-500:])
                 flt = {'trade_date': d['trade_date']}
                 cl_stock_code.replace_one(flt, d, upsert=True)
示例#8
0
 def _init_k_data(self, code, k_data):
     # 注意:所有的数据库数据和列表数据都按照日期的正序排序(从小到大)
     """
     初始化股票数据库数据
     @:param code  股票(指数)代码                
     """     
     if len(k_data) != 0:
         last_5_vol = deque([0.0] * 5)
         last_5_amount = deque([0.0] * 5)
         k_data = k_data.sort_values(by='trade_date')            
         cl_stock_code = self.db[code]
         cl_stock_code.create_index([('trade_date', ASCENDING)], unique=True)
         am = ArrayManager(size=600)
         for ix, row in k_data.iterrows():
             d = row.to_dict()              
             d['ts_code'] = d['ts_code'].replace('.', '_')
             if 0.0 not in last_5_vol:
                 vol_rate = d['vol'] / (sum(last_5_vol) / 5.0)                
                 amount_rate = d['amount'] / (sum(last_5_amount) / 5.0)   
                 d['vol_rate'] = vol_rate
                 d['amount_rate'] = amount_rate 
             else:
                 d['vol_rate'] = 0.0
                 d['amount_rate'] = 0.0
             last_5_vol.popleft()
             last_5_amount.popleft()
             last_5_vol.append(d['vol'])
             last_5_amount.append(d['amount'])                   
             if self._is_in_vnpy_db(d['ts_code'], update=False):
                 # 构建vnpy股票数据库数据
                 self._build_db_vnpy(d)                               
             if d['ts_code'][-3:] == '_SH':
                 exchange = Exchange.SSE
                 d['exchange'] = 'SSE'
             else:
                 exchange = Exchange.SZSE                
                 d['exchange'] = 'SZSE'
             bar = BarData(
                 gateway_name='ctp', symbol=d['ts_code'],
                 exchange=exchange,
                 datetime=string_to_datetime(d['trade_date']))
             bar.symbol = d['ts_code']
             bar.volume = d['vol']
             bar.open_price = d['open']
             bar.high_price = d['high']
             bar.low_price = d['low']
             bar.close_price = d['close']
             am.update_bar(bar)
             try:
                 d['ma_5'] = am.sma(5)
             except:
                 traceback.print_exc()                    
                 LOG.error('************************')
                 LOG.error(d['ts_code'])
                 LOG.error(d['trade_date'])
                 LOG.error(bar)
             d['ma_10'] = am.sma(10)
             d['ma_20'] = am.sma(20)
             d['ma_30'] = am.sma(30)
             d['ma_60'] = am.sma(60)
             d['ma_120'] = am.sma(120)
             d['ma_250'] = am.sma(250)
             d['ma_500'] = am.sma(500)
             d['high_5'] = np.max(am.high[-5:])
             d['high_10'] = np.max(am.high[-10:])
             d['high_20'] = np.max(am.high[-20:])
             d['high_30'] = np.max(am.high[-30:])
             d['high_60'] = np.max(am.high[-60:])
             d['high_120'] = np.max(am.high[-120:])
             d['high_250'] = np.max(am.high[-250:])
             d['high_500'] = np.max(am.high[-500:])
             d['low_5'] = np.min(am.low[-5:])
             d['low_10'] = np.min(am.low[-10:])
             d['low_20'] = np.min(am.low[-20:])
             d['low_30'] = np.min(am.low[-30:])
             d['low_60'] = np.min(am.low[-60:])
             d['low_120'] = np.min(am.low[-120:])
             d['low_250'] = np.min(am.low[-250:])
             d['low_500'] = np.min(am.low[-500:])
             flt = {'trade_date': d['trade_date']}
             cl_stock_code.replace_one(flt, d, upsert=True)
示例#9
0
    def update_bar(self, bar: BarData):
        """
        自定义 由一分钟合成 N 分钟 K 线
        """
        # If not inited, creaate window bar object
        if not self.window_bar:
            # Generate timestamp for bar data
            if self.interval == Interval.MINUTE:
                dt = bar.datetime.replace(second=0, microsecond=0)
            else:
                dt = bar.datetime.replace(minute=0, second=0, microsecond=0)

            self.window_bar = BarData(symbol=bar.symbol,
                                      exchange=bar.exchange,
                                      datetime=dt,
                                      gateway_name=bar.gateway_name,
                                      open_price=bar.open_price,
                                      high_price=bar.high_price,
                                      low_price=bar.low_price)
        # Otherwise, update high/low price into window bar
        else:
            self.window_bar.high_price = max(self.window_bar.high_price,
                                             bar.high_price)
            self.window_bar.low_price = min(self.window_bar.low_price,
                                            bar.low_price)

        # Update close price/volume into window bar
        self.window_bar.close_price = bar.close_price
        self.window_bar.volume += int(bar.volume)
        self.window_bar.open_interest = bar.open_interest

        # Check if window bar completed
        finished = False

        if self.interval == Interval.MINUTE:
            # x-minute bar
            # if not (bar.datetime.minute + 1) % self.window:
            #     finished = True

            #把原来一分钟合成N分钟逻辑是被60整除,修改为被任意分钟整除
            if self.last_bar and bar.datetime.minute != self.last_bar.datetime.minute:
                self.interval_count += 1

                if not self.interval_count % self.window:
                    finished = True
                    self.interval_count = 0

        elif self.interval == Interval.HOUR:
            if self.last_bar and bar.datetime.hour != self.last_bar.datetime.hour:
                # 1-hour bar
                if self.window == 1:
                    finished = True
                # x-hour bar
                else:
                    self.interval_count += 1

                    if not self.interval_count % self.window:
                        finished = True
                        self.interval_count = 0

        if finished:
            self.on_window_bar(self.window_bar)
            self.window_bar = None

        # Cache last bar object
        self.last_bar = bar