ricequant / rqalpha

A extendable, replaceable Python algorithmic backtest && trading framework supporting multiple securities
http://rqalpha.io
Other
5.41k stars 1.62k forks source link

Postion类中self._prev_close和self._last_price如何赋值? #875

Closed codermichele closed 7 months ago

codermichele commented 7 months ago

Postion类的初始化方法

def __init__(self, order_book_id, direction, init_quantity=0, init_price=None):
        self._env = Environment.get_instance()
        self._order_book_id = order_book_id
        self._instrument = self._env.data_proxy.instrument(order_book_id)  # type: Instrument
        self._direction = direction

        self._quantity = init_quantity
        self._old_quantity = init_quantity
        self._logical_old_quantity = 0

        self._avg_price: float = init_price or 0
        self._trade_cost: float = 0
        self._transaction_cost: float = 0
        self._prev_close: Optional[float] = init_price
        self._last_price: Optional[float] = init_price

        self._direction_factor = 1 if direction == POSITION_DIRECTION.LONG else -1

在Account中的

def get_position(self, order_book_id: str, direction: POSITION_DIRECTION = POSITION_DIRECTION.LONG) -> Position:
        """
        获取某个标的的持仓对象

        :param order_book_id: 标的编号
        :param direction: 持仓方向
        """
        try:
            return self._positions[order_book_id][direction]
        except KeyError:
            return Position(order_book_id, direction)

方法中实例化Position时“return Position(order_book_id, direction)”,并没有传入init_price,self._prev_close和self._last_price:应该都是None才对,可在调试时发现这两个变量都有实际的值,我想了解下这个值是怎么赋值进去的?

Cuizi7 commented 7 months ago

在 last_price 和 prev_close 两个 property 中第一次赋值。