A Threshold-Based Strategy for Rebalancing Position Value
Summary
The strategy compares the current marked value of its position with a stored balance amount on each new bar. When the relative difference reaches a configurable threshold, it trades toward balance: it buys when the stored amount exceeds position value and sells when position value is larger. The proposed order size is based on half the absolute gap, converted into units at the bar’s closing price. Before evaluating the condition, it cancels outstanding orders, and it avoids placing orders when the stored balance is nonpositive.
Trade callbacks update the stored balance by subtracting the value of opening trades and adding the value of closing trades. The code also includes lifecycle and account-event callbacks, but the account callback only prints received data. This is an illustrative implementation, not a validated strategy: it provides no backtest, performance results, transaction cost model, or risk controls. Its balance bookkeeping and assumptions about position value and trade offsets would need review before use, especially for short positions or derivatives.
Key ideas
- The strategy checks position value against a stored balance whenever a new bar arrives.
- It trades only when the relative gap reaches a configurable threshold.
- The order direction is chosen to reduce the gap between balance and marked position value.
- The proposed trade size is half the absolute value difference divided by the bar close.
- Trade callbacks adjust the stored balance according to whether a trade opens or closes a position.
- The implementation supplies no backtest or broader risk controls.
Tags
From a private course collection; the original is not published.