Skip to content

Feature: Order Convenience Functions (order_value, order_percent, order_target) #440

Description

@MDUYN

Description

Add high-level ordering convenience functions that simplify portfolio management and rebalancing. The framework currently requires users to calculate order amounts manually.

Desired Capabilities

  • order_value(symbol, value): Place an order for a fixed currency amount (automatically calculates quantity)
  • order_percent(symbol, percent): Order a percentage of current portfolio value
  • order_target(symbol, target_amount): Adjust position to a target number of units (calculates the difference)
  • order_target_value(symbol, target_value): Adjust position to a target currency value
  • order_target_percent(symbol, target_percent): Adjust position to a target percentage of portfolio

Why This Matters

  1. order_target_percent is essential for portfolio rebalancing strategies (e.g., maintain 60/40 allocation)
  2. order_value simplifies dollar-cost averaging and fixed-amount strategies
  3. order_percent makes it easy to allocate a percentage of capital to each asset
  4. These functions reduce boilerplate code and prevent common calculation errors
  5. Target-based functions automatically handle existing positions (buy/sell difference)

Current State

The framework has create_limit_order() with percentage_of_portfolio and percentage_of_position parameters, which covers some of these cases. However, there are no target-based functions that automatically compute the difference between current and desired position, and no order_value for fixed currency amounts.

Suggested API

class MyStrategy(TradingStrategy):
    def run_strategy(self, context, data):
        price = data["btc_ticker"]["price"]

        # Rebalance to target allocation — buys or sells to reach target
        context.order_target_percent(
            target_symbol="BTC",
            target_percent=10.0,    # Ensure BTC is 10% of portfolio
            price=price,
            order_type=OrderType.LIMIT
        )

        # Fixed currency amount order
        context.order_value(
            target_symbol="ETH",
            value=500.0,            # Buy 500 EUR worth of ETH
            order_side=OrderSide.BUY,
            price=price
        )

        # Adjust to target position size
        context.order_target(
            target_symbol="BTC",
            target_amount=1.5,      # End up with exactly 1.5 BTC
            price=price
        )

These methods would be added to the Context class alongside the existing create_limit_order() and create_order() methods. Internally they would query current positions and portfolio value, calculate the required order, and delegate to create_order().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions