Skip to content

margin_available logic imcompatable fot stock short #77

Closed
@sdmovie

Description

@sdmovie

Expected Behavior

whenI short a stock and hold the negative position, my margin_available should be able to buy_back all the short position.

Actual Behavior

in process_order, and prviously all shorted stock, strategy instructs buyback:

size = order.size
if -1 < size < 1:
    size = np.sign(size) * int((self.margin_available * self._leverage * abs(size))
                                           // adjusted_price)

can buyback size is calculated as above, however the margin_available is calculated as :

margin_used = sum(trade.value / self._leverage for trade in self.trades)
return self.equity - margin_used   

in my case , initial captical 10000, shorted -193 CSCO @46$ , new price is 48$ then equity is 10000+380 profit =10380, this is correct. But the margin_used is 193*46 ~9000, the calculation results in margin available = equity(10380)-margin_used(~9000) =~1300$, then the buyback can only execute 26shares

Steps to Reproduce

Additional info

Branch Order

  • Backtesting version:

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions