qtrader icon indicating copy to clipboard operation
qtrader copied to clipboard

关于Portfolio里做空和做多的account_balance.cash计算疑惑

Open Junffzz opened this issue 1 year ago • 3 comments

代码如下:

    def update(self, deal: Deal):
        security = deal.security
        lot_size = security.lot_size
        price = deal.filled_avg_price
        quantity = deal.filled_quantity
        direction = deal.direction
        offset = deal.offset
        filled_time = deal.updated_time
        fee = self.market.fees(deal).total_fees
        # update balance
        self.account_balance.cash -= fee
        if direction == Direction.LONG:
            self.account_balance.cash -= price * quantity * lot_size
            if offset == Offset.CLOSE:  # pay interest when closing short 
                short_position = self.position.holdings[security][Direction.SHORT]
                short_interest = (
                        short_position.holding_price
                        * short_position.quantity
                        * (filled_time - short_position.update_time).days / 365
                        * self.market.SHORT_INTEREST_RATE
                )
                self.account_balance.cash -= short_interest
        elif direction == Direction.SHORT:
            self.account_balance.cash += price * quantity * lot_size

1.股票: direction=LONG开仓下是cash会减少,direction=SHORT卖出account_balance.cash余额增加,这是没有问题的。 2.期货: direction=LONG下如果offset=CLOSE为什么cash也用减法并且还有一个account_balance.cash -= short_interest。 direction=SHORT下如果offset=OPEN为什么cash还用加法呢(self.account_balance.cash += price * quantity * lot_size)。

问题是股票我能理解,买入和卖出的金额,但是期货的做多和做空,金额的变化不太理解。希望作者能帮忙解惑期权期货Portfolio里的update逻辑怎么解释。

Junffzz avatar May 29 '23 10:05 Junffzz

代码如下:

    def update(self, deal: Deal):
        security = deal.security
        lot_size = security.lot_size
        price = deal.filled_avg_price
        quantity = deal.filled_quantity
        direction = deal.direction
        offset = deal.offset
        filled_time = deal.updated_time
        fee = self.market.fees(deal).total_fees
        # update balance
        self.account_balance.cash -= fee
        if direction == Direction.LONG:
            self.account_balance.cash -= price * quantity * lot_size
            if offset == Offset.CLOSE:  # pay interest when closing short 
                short_position = self.position.holdings[security][Direction.SHORT]
                short_interest = (
                        short_position.holding_price
                        * short_position.quantity
                        * (filled_time - short_position.update_time).days / 365
                        * self.market.SHORT_INTEREST_RATE
                )
                self.account_balance.cash -= short_interest
        elif direction == Direction.SHORT:
            self.account_balance.cash += price * quantity * lot_size

1.股票: direction=LONG开仓下是cash会减少,direction=SHORT卖出account_balance.cash余额增加,这是没有问题的。 2.期货: direction=LONG下如果offset=CLOSE为什么cash也用减法并且还有一个account_balance.cash -= short_interest。 direction=SHORT下如果offset=OPEN为什么cash还用加法呢(self.account_balance.cash += price * quantity * lot_size)。

问题是股票我能理解,买入和卖出的金额,但是期货的做多和做空,金额的变化不太理解。希望作者能帮忙解惑期权期货Portfolio里的update逻辑怎么解释。

期貨short interest是零,股票做空有成本,short interest不為零。

josephchenhk avatar May 29 '23 12:05 josephchenhk

代码如下:

    def update(self, deal: Deal):
        security = deal.security
        lot_size = security.lot_size
        price = deal.filled_avg_price
        quantity = deal.filled_quantity
        direction = deal.direction
        offset = deal.offset
        filled_time = deal.updated_time
        fee = self.market.fees(deal).total_fees
        # update balance
        self.account_balance.cash -= fee
        if direction == Direction.LONG:
            self.account_balance.cash -= price * quantity * lot_size
            if offset == Offset.CLOSE:  # pay interest when closing short 
                short_position = self.position.holdings[security][Direction.SHORT]
                short_interest = (
                        short_position.holding_price
                        * short_position.quantity
                        * (filled_time - short_position.update_time).days / 365
                        * self.market.SHORT_INTEREST_RATE
                )
                self.account_balance.cash -= short_interest
        elif direction == Direction.SHORT:
            self.account_balance.cash += price * quantity * lot_size

1.股票: direction=LONG开仓下是cash会减少,direction=SHORT卖出account_balance.cash余额增加,这是没有问题的。 2.期货: direction=LONG下如果offset=CLOSE为什么cash也用减法并且还有一个account_balance.cash -= short_interest。 direction=SHORT下如果offset=OPEN为什么cash还用加法呢(self.account_balance.cash += price * quantity * lot_size)。 问题是股票我能理解,买入和卖出的金额,但是期货的做多和做空,金额的变化不太理解。希望作者能帮忙解惑期权期货Portfolio里的update逻辑怎么解释。

期貨short interest是零,股票做空有成本,short interest不為零。

作者你好,我理解期货做空,direction=SHORT,offset=OPEN,cash应该做减法,为什么Portfolio里计算cash做加法呢。

Junffzz avatar May 30 '23 02:05 Junffzz

你好,这里的cash是资产负债表的概念,做空时相当于把这个资产以当前价格卖出(所以cash增加)

josephchenhk avatar May 31 '23 09:05 josephchenhk