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

Added support for shorting in the drift rebalancer; fixed a bug in limit price calculation. #611

Merged
merged 8 commits into from
Nov 8, 2024

Conversation

brettelliot
Copy link
Collaborator

@brettelliot brettelliot commented Nov 5, 2024

Description by Korbit AI

What change is being made?

Add support for shorting in the drift rebalancer and fix a bug in limit price calculation.

Why are these changes being made?

Shorting capability has been implemented to extend the flexibility of the strategy, enabling both long and short positions to be taken based on market opportunities. The fix in limit price calculation resolves a bug in slippage handling to ensure more accurate trade execution, adhering to the target slippage percentage. Adjustments include replacing 'absolute_drift' with 'drift', modifications to parameter handling, and updates to the strategy initialization and execution logic.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

Copy link
Contributor

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary by Korbit AI

Code Execution Comments

  • Update rebalance to prevent unintended shorting and incorporate mechanisms for borrowing, tracking, and risk management in short selling.
Files scanned
File Path Reviewed
lumibot/example_strategies/classic_60_40.py
lumibot/strategies/drift_rebalancer.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Naming
Database Operations
Documentation
Logging
Error Handling
Systems and Environment
Objects and Data Structures
Readability and Maintainability
Asynchronous Processing
Design Patterns
Third-Party Libraries
Performance
Security
Functionality

Feedback and Support

# Sell everything
symbol = row["symbol"]
quantity = row["current_quantity"]
last_price = Decimal(self.strategy.get_last_price(symbol))
limit_price = self.calculate_limit_price(last_price=last_price, side="sell")
self.place_limit_order(symbol=symbol, quantity=quantity, limit_price=limit_price, side="sell")
elif row["absolute_drift"] < 0:
if quantity > 0 or (quantity == 0 and self.shorting):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Functionality severity potentially major

Review condition for placing a sell order in the rebalance method.

Tell me more

In the rebalance method of LimitOrderRebalanceLogic, the condition for placing a sell order when row['drift'] == -1 allows shorting even when the current quantity is zero. This may lead to unintended shorting. Consider modifying the condition to only allow shorting when self.shorting is True and the current quantity is greater than zero, or when explicitly intending to open a new short position.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

self.fill_sleeptime = self.parameters.get("fill_sleeptime", 15)
self.target_weights = {k: Decimal(v) for k, v in self.parameters["target_weights"].items()}
self.shorting = self.parameters.get("shorting", False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Security

Shorting functionality lacks risk management and tracking mechanisms.

Tell me more

The code allows shorting if the 'shorting' parameter is set to True, but it does not properly handle the risks and complexities associated with short selling. Short selling involves borrowing shares and selling them, with the expectation of buying them back at a lower price later. However, the code does not include any mechanisms to borrow shares, track borrowed positions, or handle the potential for unlimited losses if the shorted asset's price increases. To properly implement shorting, additional logic should be added to handle borrowing shares, tracking borrowed positions, and managing the risks associated with short selling. This may involve integrating with a broker's API to borrow shares, implementing risk management measures such as stop-loss orders, and properly accounting for the borrowed shares in the portfolio calculations. Without these considerations, allowing shorting in the current implementation could lead to incorrect portfolio tracking and potential financial losses.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.

@grzesir grzesir merged commit 9dbe81c into dev Nov 8, 2024
1 check passed
@brettelliot brettelliot deleted the be-drift-rebalancer-3 branch November 9, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants