Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Junffzz opened this issue May 29, 2023 · 3 comments
Open

Comments

@Junffzz
Copy link

Junffzz commented May 29, 2023

代码如下:

    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逻辑怎么解释。

@josephchenhk
Copy link
Owner

代码如下:

    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不為零。

@Junffzz
Copy link
Author

Junffzz commented May 30, 2023

代码如下:

    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做加法呢。

@josephchenhk
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants