-
Notifications
You must be signed in to change notification settings - Fork 220
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
Short positions lead to negative number of shares to buy back #45
Comments
So when I take only long positions the numbers of the backtest
At some point the pybroker starts buying assets and the long positions are added to the uninvested cash, we take no margin and I can life with the results. You can see the whole log file, the portfolio, the positions, the orders and the trades in the csv files attached to this comment. |
When I take short positions only or short and long positions the numbers of the backtest
At some point the pybroker doubles the cash and it is growing exponentially very fast (And I am pretty sure that my strategy is so good :-P) I think |
Hi @Pirat83, Thanks for providing your code and insights, it has been very helpful. The problem is with this line:
If I print ctx.sell_shares, I can see it growing very rapidly:
Eventually, the value becomes too large and causes integer overflow and becomes negative, which then triggers the error you see. If I remove that section of code then the strategy runs without throwing the error. The error is thrown because of the below:
Yes, you are correct. The margin in PyBroker is unlimited, and only "paid back" when the shares are covered (bought back) by the strategy. The portfolio is immediately credited with cash when shorting shares. I believe the reason you see the cash grow exponentially is because the number of shares being shorted increases exponentially. I can add an extra configuration that would allow you to set a margin limit, if that would also be helpful. |
Yes this option to limit the margin was also my preferred solution, while only guessing the root cause. This limit could also apply for long positions, so people could simulate margin in their portfolios. Thank you very much. I appreciate your work. |
Note, that you can check the number of short shares outstanding and use that to bound the value set on |
After giving this more thought, I believe the main problem is with the way ctx.calc_target_shares is being used, which is different from its intended use. From the previous code, it looks like you want to continue to short shares without covering them. Meanwhile, calc_target_shares by default uses the total portfolio equity to calculate the number of shares given a target ratio. So, when What you want to do instead is to be explicit and use the
Where I have also added a check to guarantee that I will consider adding margin limits as a feature in the future. However, I have been hesitant to do so because PyBroker will not simulate maintenance margin or margin calls, and the main goal of the framework is to identify effective trading signals, which are not affected by the use of leverage. Let me know if you need anything else! |
A few updates to share:
|
Hi @edtechre. Thank you for your work and your time. I appretierte your work very much. Just one last question: |
No, since margin is only used for short positions. You can increase the |
Hello I am trying to experiment with pybroker.
It looks very promissing. However I am facing some problems and require help.
My strategy gets into serious trouble when I add shorting to it. Taking only long positions works quite well. Taking only short positions also crashes the numbers. I have published the code here: https://github.com/Pirat83/pybroker-experiments/tree/master and will explain the issues later.
The text was updated successfully, but these errors were encountered: