Пример #1
0
    def update_position(self, current_position, order_amount, current_price,
                        order_type, timestamp):
        """

        :param timestamp:
        :type timestamp:
        :param current_position:
        :type current_position: Position
        :param order_amount:
        :type order_amount:
        :param current_price:
        :type current_price:
        :param order_type:
        :type order_type:
        """
        if order_type == ORDER_TYPE_LONG:
            need_money = (order_amount * current_price) * (1 + self.slippage +
                                                           self.buy_cost)
            if self.account.cash < need_money:
                if self.rich_mode:
                    self.input_money()
                else:
                    raise NotEnoughMoneyError()

            self.account.cash -= need_money

            # 计算平均价
            long_amount = current_position.long_amount + order_amount
            if long_amount == 0:
                current_position.average_long_price = 0
            current_position.average_long_price = (
                current_position.average_long_price *
                current_position.long_amount +
                current_price * order_amount) / long_amount

            current_position.long_amount = long_amount

            if current_position.trading_t == 0:
                current_position.available_long += order_amount

        elif order_type == ORDER_TYPE_SHORT:
            need_money = (order_amount * current_price) * (1 + self.slippage +
                                                           self.buy_cost)
            if self.account.cash < need_money:
                if self.rich_mode:
                    self.input_money()
                else:
                    raise NotEnoughMoneyError()

            self.account.cash -= need_money

            short_amount = current_position.short_amount + order_amount
            current_position.average_short_price = (
                current_position.average_short_price *
                current_position.short_amount +
                current_price * order_amount) / short_amount

            current_position.short_amount = short_amount

            if current_position.trading_t == 0:
                current_position.available_short += order_amount

        elif order_type == ORDER_TYPE_CLOSE_LONG:
            self.account.cash += (order_amount * current_price *
                                  (1 - self.slippage - self.sell_cost))
            # FIXME:如果没卖完,重新计算计算平均价

            current_position.available_long -= order_amount
            current_position.long_amount -= order_amount

        elif order_type == ORDER_TYPE_CLOSE_SHORT:
            self.account.cash += 2 * (order_amount *
                                      current_position.average_short_price)
            self.account.cash -= order_amount * current_price * (
                1 + self.slippage + self.sell_cost)

            current_position.available_short -= order_amount
            current_position.short_amount -= order_amount

        # save the order info to db
        order_id = '{}_{}_{}_{}'.format(
            self.trader_name, order_type, current_position.entity_id,
            to_time_str(timestamp, TIME_FORMAT_ISO8601))
        order = Order(id=order_id,
                      timestamp=to_pd_timestamp(timestamp),
                      trader_name=self.trader_name,
                      entity_id=current_position.entity_id,
                      order_price=current_price,
                      order_amount=order_amount,
                      order_type=order_type,
                      level=self.level.value,
                      status='success')
        self.session.add(order)
        self.session.commit()
Пример #2
0
    def update_position(self, current_position, order_amount, current_price,
                        order_type, timestamp):
        """

        :param timestamp:
        :type timestamp:
        :param current_position:
        :type current_position: Position
        :param order_amount:
        :type order_amount:
        :param current_price:
        :type current_price:
        :param order_type:
        :type order_type:
        """
        if order_type == ORDER_TYPE_LONG:
            need_money = (order_amount * current_price) * (1 + self.slippage +
                                                           self.buy_cost)
            if self.latest_account['cash'] < need_money:
                raise NotEnoughMoneyError()

            self.latest_account['cash'] -= need_money

            # 计算平均价
            long_amount = current_position['long_amount'] + order_amount
            current_position['average_long_price'] = (
                current_position['average_long_price'] *
                current_position['long_amount'] +
                current_price * order_amount) / long_amount

            current_position['long_amount'] = long_amount

            if current_position['trading_t'] == 0:
                current_position['available_long'] += order_amount

        elif order_type == ORDER_TYPE_SHORT:
            need_money = (order_amount * current_price) * (1 + self.slippage +
                                                           self.buy_cost)
            if self.latest_account['cash'] < need_money:
                raise NotEnoughMoneyError()

            self.latest_account['cash'] -= need_money

            short_amount = current_position['short_amount'] + order_amount
            current_position['average_short_price'] = (
                current_position['average_short_price'] *
                current_position['short_amount'] +
                current_price * order_amount) / short_amount

            current_position['short_amount'] = short_amount

            if current_position['trading_t'] == 0:
                current_position['available_short'] += order_amount

        elif order_type == ORDER_TYPE_CLOSE_LONG:
            self.latest_account['cash'] += (
                order_amount * current_price *
                (1 - self.slippage - self.sell_cost))

            current_position['available_long'] -= order_amount
            current_position['long_amount'] -= order_amount

        elif order_type == ORDER_TYPE_CLOSE_SHORT:
            self.latest_account['cash'] += 2 * (
                order_amount * current_position['average_short_price'])
            self.latest_account['cash'] -= order_amount * current_price * (
                1 + self.slippage + self.sell_cost)

            current_position['available_short'] -= order_amount
            current_position['short_amount'] -= order_amount

        # save the order info to db
        order_id = '{}_{}_{}_{}'.format(
            self.trader_name, order_type, current_position['entity_id'],
            to_time_str(timestamp, TIME_FORMAT_ISO8601))
        order = Order(id=order_id,
                      timestamp=to_pd_timestamp(timestamp),
                      trader_name=self.trader_name,
                      entity_id=current_position['entity_id'],
                      order_price=current_price,
                      order_amount=order_amount,
                      order_type=order_type,
                      level=self.level.value,
                      status='success')
        self.session.add(order)
        self.session.commit()