コード例 #1
0
 def market_close(self, event: Event, account: AbstractAccount,
                  data_portal: DataPortal):
     account.cancel_all_open_orders()
     for code in account.positions.keys():
         sell_order = MKTOrder(code, OrderDirection.SELL,
                               account.positions[code], event.visible_time)
         account.place_order(sell_order)
コード例 #2
0
    def order_status_change(self, order: Order, account: AbstractAccount):
        if order.status != OrderStatus.FILLED:
            return
        current_time = order.filled_end_time
        if current_time >= (
                self.scope.trading_calendar.next_close(current_time) -
                Timedelta(minutes=1)):
            # 收盘前一分钟不下单
            return
        # 取消当前所有的订单
        account.cancel_all_open_orders()

        # 挂上下两个网格的交易订单
        k = round((order.filled_avg_price - self.base_price) /
                  self.base_price / self.p)
        hold_quantity = 0 if self.code not in account.positions else account.positions[
            self.code]
        # 上一个格子的卖单
        if (k + 1) <= self.n:
            up_percentage = 0.5 - (k + 1) * (0.5 / self.n)
            up_price = self.base_price + self.base_price * self.p * (k + 1)
            up_net_val = hold_quantity * up_price + account.cash
            dest_quantity = int(up_net_val * up_percentage / up_price)
            sell_order = LimitOrder(self.code, OrderDirection.SELL,
                                    hold_quantity - dest_quantity,
                                    current_time, up_price)
            account.place_order(sell_order)
        # 下一个格子的买单
        if (k - 1) >= -self.n:
            down_percentage = 0.5 - (k - 1) * (0.5 / self.n)
            down_price = self.base_price + self.base_price * self.p * (k - 1)
            down_net_val = hold_quantity * down_price + account.cash
            dest_quantity = int(down_net_val * down_percentage / down_price)
            buy_order = LimitOrder(self.code, OrderDirection.BUY,
                                   dest_quantity - hold_quantity, current_time,
                                   down_price)
            account.place_order(buy_order)