Exemplo n.º 1
0
    def market_open(self, event: Event, account: AbstractAccount, data_portal: DataPortal):
        dest_position = 0
        current_position = 0
        net_value = None

        # 等待直到获取到最新的股票价格
        current_price = None
        try:
            current_price = data_portal.current_price([self.code], event.visible_time)[self.code].price
        except:
            logging.error("没有获取到当天的开盘价,code:{}".format(self.code))
        if current_price:
            net_value = account.net_value({self.code: current_price})

        current_bid_ask = None
        try:
            current_bid_ask = data_portal.current_bid_ask([self.code])[self.code]
        except:
            logging.error("没有获取到最新的买卖价, code:{}".format(self.code))

        if self.last_close_price and current_price:
            if np.log(current_price / self.last_close_price) < 0.025:
                dest_position = int(net_value / current_price)

        if len(account.positions) > 0:
            current_position = account.positions[self.code]

        change = dest_position - current_position

        if change != 0:
            direction = OrderDirection.BUY if change > 0 else OrderDirection.SELL
            reason = "时间:{}, 当前持仓:{}, 总市值:{}, 目标持仓:{}, 昨日收盘价:{}, 今日开盘价:{}, 最新买卖价:{}, strategy:{}" \
                .format(event.visible_time, current_position, net_value, dest_position, self.last_close_price,
                        current_price, current_bid_ask.__dict__ if current_bid_ask else None, TestStrategy3.__doc__)
            if current_bid_ask:
                delta = 0.01
                limit_price = (current_bid_ask.bid_price + delta) if direction == OrderDirection.BUY else (
                        current_bid_ask.ask_price - delta)
                order = LimitOrder(self.code, direction, abs(change), event.visible_time, limit_price)
                order.with_reason(reason)
                account.place_order(order)
                # self.ensure_order_filled(account, data_portal, order, period=30, retry_count=3)
                self.ensure_order_filled_v2(account, data_portal, order, duration=60, delta=delta)
            else:
                order = MKTOrder(self.code, direction, abs(change), event.visible_time)
                order.with_reason(reason)
                account.place_order(order)
        else:
            msg = "不需要下单, 时间:{}, 当前持仓:{}, 总市值:{}, 目标持仓:{}, 昨日收盘价:{}, 今日开盘价:{}". \
                format(event.visible_time, current_position, net_value, dest_position, self.last_close_price,
                       current_price)
            logging.info(msg)
Exemplo n.º 2
0
    def market_close(self, event: Event, account: AbstractAccount, data_portal: DataPortal):
        dest_position = 0
        current_position = 0
        net_value = None

        # 等待直到获取到最新的股票价格
        current_price = None
        try:
            current_price = data_portal.current_price([self.code], event.visible_time)[self.code].price
        except:
            logging.error("没有获取到当天的开盘价,code:{}".format(self.code))
        if current_price:
            net_value = account.net_value({self.code: current_price})

        if current_price and self.last_close and current_price > self.last_close:
            dest_position = int(net_value / current_price)

        if len(account.positions) > 0:
            current_position = account.positions[self.code]

        change = dest_position - current_position
        if change != 0:
            direction = OrderDirection.BUY if change > 0 else OrderDirection.SELL
            reason = "时间:{}, 当前持仓:{}, 总市值:{}, 目标持仓:{}, 昨日收盘价:{}, 今日收盘价:{}, strategy:{}".format(event.visible_time,
                                                                                  current_position,
                                                                                  net_value, dest_position,
                                                                                  self.last_close,
                                                                                  current_price, TestStrategy2.__doc__)
            if current_price:
                order = LimitOrder(self.code, direction, abs(change), event.visible_time, current_price)
                order.with_reason(reason)
                account.place_order(order)
                self.ensure_order_filled(account, data_portal, order, 40, 1)
            else:
                order = MKTOrder(self.code, direction, abs(change), event.visible_time)
                order.with_reason(reason)
                account.place_order(order)
        else:
            logging.info("不需要下单, 时间:{}, 当前持仓:{}, 总市值:{}, 目标持仓:{}, 今日开盘价:{}, 今日收盘价:{}".
                         format(event.visible_time,
                                current_position,
                                net_value, dest_position,
                                self.last_open,
                                current_price))

        self.last_close = current_price
Exemplo n.º 3
0
 def on_tick(self, event: Event, account: AbstractAccount, data_portal: DataPortal):
     if not self.market_is_open:
         return
     if not isinstance(event.data, Tick):
         raise RuntimeError("wrong data")
     data = DataFrame([{'visible_time': event.data.visible_time, 'price': event.data.price}]).set_index('visible_time')
     self.daily_ticks.append(data)
     now = Timestamp.now(tz='Asia/Shanghai')
     start = now - self.time_span
     check_df = self.daily_ticks[start: now]
     this_price = event.data.price
     if len(account.get_open_orders()) <= 0:
         if len(account.positions) > 0:
             max_price = check_df['price'].max()
             change = abs((this_price - max_price) / max_price)
             if change > self.threshold:
                 reason = '当前价格:{}, 时间范围:{}的最高价为:{}, 当前持仓:{}'.\
                     format(this_price, self.time_span, max_price, account.positions)
                 order = LimitOrder(self.code, OrderDirection.SELL, quantity=abs(account.positions[self.code]),
                                    place_time=event.visible_time, limit_price=this_price)
                 order.with_reason(reason)
                 account.place_order(order)
                 self.ensure_order_filled(account, data_portal, order, period=10, retry_count=2)
             else:
                 logging.info("当前价格:{}, 时间范围:{}的最高价为:{}, 变动为:{}".
                              format(this_price, self.time_span, max_price, change))
         else:
             lowest_price = check_df['price'].min()
             change = abs((this_price - lowest_price) / lowest_price)
             if change > self.threshold:
                 reason = '当前价格:{}, 时间范围:{}的最低价为:{}, 当前持仓:{}'.\
                     format(this_price, self.time_span, lowest_price, account.positions)
                 quantity = int(account.cash / this_price)
                 order = LimitOrder(self.code, OrderDirection.BUY, quantity=quantity,
                                    place_time=event.visible_time, limit_price=this_price)
                 order.with_reason(reason)
                 account.place_order(order)
                 self.ensure_order_filled(account, data_portal, order, period=10, retry_count=2)
             else:
                 logging.info("当前价格:{}, 时间范围:{}的最低价为:{}, 变动为:{}".
                              format(this_price, self.time_span, lowest_price, change))
Exemplo n.º 4
0
    def market_close(self, event: Event, account: AbstractAccount,
                     data_portal: DataPortal):
        dest_position = 0
        current_position = 0
        net_value = None

        # 等待直到获取到最新的股票价格
        current_price = None
        try:
            current_price = data_portal.current_price(
                [self.code], event.visible_time)[self.code].price
        except:
            logging.error("没有获取到当天的开盘价,code:{}".format(self.code))
        if current_price:
            net_value = account.net_value({self.code: current_price})

        current_bid_ask = None
        try:
            current_bid_ask = data_portal.current_bid_ask([self.code
                                                           ])[self.code]
        except:
            logging.error("没有获取到最新的买卖价,code:{}".format(self.code))

        # if current_price and self.last_close and current_price > self.last_close:
        if True:
            dest_position = int(net_value * self.long_leverage / current_price)

        if len(account.positions) > 0:
            current_position = account.positions[self.code]

        change = dest_position - current_position
        if change != 0:
            direction = OrderDirection.BUY if change > 0 else OrderDirection.SELL
            reason = "时间:{}, 当前持仓:{}, 总市值:{}, 目标持仓:{}, 昨日收盘价:{}, 今日收盘价:{}, " \
                     "买卖价:{}, strategy:{}".format(event.visible_time,
                                                  current_position,
                                                  net_value, dest_position,
                                                  self.last_close,
                                                  current_price,
                                                  current_bid_ask.__dict__ if current_bid_ask else None,
                                                  SPCEStrategy.__doc__)
            if current_bid_ask:
                delta = 0.01
                limit_price = (current_bid_ask.bid_price +
                               delta) if direction == OrderDirection.BUY else (
                                   current_bid_ask.ask_price - delta)
                order = LimitOrder(self.code, direction, abs(change),
                                   event.visible_time, limit_price)
                order.with_reason(reason)
                account.place_order(order)
                # self.ensure_order_filled(account, data_portal, order, 40, 1)
                self.ensure_order_filled_v2(account, data_portal, order, 40,
                                            delta)
            else:
                order = MKTOrder(self.code, direction, abs(change),
                                 event.visible_time)
                order.with_reason(reason)
                account.place_order(order)
        else:
            logging.info(
                "不需要下单, 时间:{}, 当前持仓:{}, 总市值:{}, 目标持仓:{}, 今日开盘价:{}, 今日收盘价:{}".
                format(event.visible_time, current_position, net_value,
                       dest_position, self.last_open, current_price))

        self.last_close = current_price