kungfu-origin / kungfu

Kungfu Trader
Apache License 2.0
3.4k stars 1.12k forks source link

Future Commission Fee #81

Closed dingo9 closed 8 months ago

dingo9 commented 3 years ago

In core/python/kungfu/wingchun/book/position.py

def _apply_close(self, trade):
        if self.volume < trade.volume:
            raise Exception("{} over close position, current volume is {}, {} to close".format(self.uname, self.volume, trade.volume))
        if trade.offset == Offset.CloseToday and self.volume - self.yesterday_volume < trade.volume:
            raise Exception("{} over close today position, current volume is {}, {} to close".format(self.uname, self.volume - self.yesterday_volume, trade.volume))
        margin = self.contract_multiplier * trade.price * trade.volume * self.margin_ratio
        self.margin -= margin
        self.book.avail += margin
        self.volume -= trade.volume
        if self.yesterday_volume > 0 and trade.offset != Offset.CloseToday:
            self.yesterday_volume = 0 if self.yesterday_volume <= trade.volume else \
                self.yesterday_volume - trade.volume
        realized_pnl = (trade.price - self.avg_open_price) * trade.volume * \
                       self.contract_multiplier * (1 if self.direction == Direction.Long else -1)
        self.realized_pnl += realized_pnl
        self.book.realized_pnl += realized_pnl

对于中国期货市场,平今仓的时候应该返还开仓保证金,在Rolling Day的时候应该按照逐日盯市的方式重新计算保证金,平昨仓按照新的保证金返还。