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

margin_available logic imcompatable fot stock short #77

Closed
sdmovie opened this issue May 26, 2020 · 1 comment · Fixed by #47
Closed

margin_available logic imcompatable fot stock short #77

sdmovie opened this issue May 26, 2020 · 1 comment · Fixed by #47
Labels
wontfix This will not be worked on

Comments

@sdmovie
Copy link

sdmovie commented May 26, 2020

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:
@sdmovie
Copy link
Author

sdmovie commented May 27, 2020

absolute size of buy/sell seems being correct. It was the defasult percentage (0.95) has the issue.Will try furthur.
Absoulte size is no problem 'cause it's set size , not caculated by margin.
when there is a stock short position, the "cash" should be net equity +"frozen" short position value, thus you can always close short position as long as net equity is positive.
Anyway, absolute size can work~

@kernc kernc closed this as completed in #47 Jul 15, 2020
@kernc kernc added the wontfix This will not be worked on label Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants