Closed
Description
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: