feat(context): order convenience functions (#440)#555
Merged
Conversation
Add five high-level ordering helpers on Context that simplify portfolio management and rebalancing by removing manual amount calculations: - order_value(symbol, value, side, price): place a LIMIT order for a fixed currency amount (amount = value / price). - order_percent(symbol, percent, side, price): place a LIMIT order for a percentage of the portfolio's net size. - order_target(symbol, target_amount, price): adjust the position to exactly target_amount units; submits BUY/SELL for the difference, or no order when already on target. - order_target_value(symbol, target_value, price): adjust the position so its market value at price equals target_value. - order_target_percent(symbol, target_percent, price): adjust the position so its market value equals target_percent of the portfolio's net size (rebalancing). All helpers produce LIMIT orders by default and validate inputs (positive value/percent/price, non-negative targets). Implemented as thin wrappers around create_limit_order; target_* helpers compute the current position size and place a BUY or SELL for the difference. Tests: 14 new tests in test_order_convenience_functions.py covering buy paths, target-down rebalancing, no-op when already on target, and validation errors. Closes #440
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #440.
Summary
Adds five high-level ordering convenience functions on
Contextthat simplify portfolio management and rebalancing. The framework previously required users to manually compute order amounts from target values/percentages — these helpers do it.New API
order_value(symbol, value, side, price)amount = value / priceorder_percent(symbol, percent, side, price)percentof portfolio net sizeorder_target(symbol, target_amount, price)target_amountunits (BUY/SELL diff, no-op if equal)order_target_value(symbol, target_value, price)priceequalstarget_valueorder_target_percent(symbol, target_percent, price)target_percentof portfolio net sizeAll helpers produce LIMIT orders (require
price) and validate inputs (positivevalue/percent/price, non-negative targets). Internally they delegate tocreate_limit_order; thetarget_*family looks up the current position and submits the BUY/SELL difference, or no order when already on target.Example
Tests
14 new tests in
tests/app/algorithm/test_order_convenience_functions.pycovering buy paths, target-down rebalancing, no-op when already on target, and input validation errors.