You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docusaurus/docs/Getting Started/orders.md
+63Lines changed: 63 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -166,6 +166,69 @@ In an event-driven backtest, the trigger condition is evaluated against each can
166
166
- A **STOP_LIMIT** that triggers becomes a resting limit order at `price` from the triggering candle onwards, and fills under the normal limit-fill rules (`Low <= price` for BUY, `High >= price` for SELL).
167
167
- The triggering timestamp is stored on the order as `triggered_at` for auditability.
168
168
169
+
## Convenience Helpers
170
+
171
+
For common sizing patterns the framework exposes five high-level helpers on `Context` (also available as `self.*` from a `TradingStrategy`). They all produce **LIMIT** orders and require an explicit `price`, delegating internally to `create_limit_order`. The `order_target*` family looks up the current position and submits a BUY or SELL for the difference (no order is placed when the position already matches the target).
from investing_algorithm_framework import OrderSide, TradingStrategy
183
+
184
+
classRebalanceStrategy(TradingStrategy):
185
+
186
+
defapply_strategy(self, context, data):
187
+
price = data["BTC/EUR"]["close"].iloc[-1]
188
+
189
+
# Spend exactly 500 EUR on BTC
190
+
context.order_value(
191
+
target_symbol="BTC",
192
+
value=500,
193
+
order_side=OrderSide.BUY,
194
+
price=price,
195
+
)
196
+
197
+
# Allocate 25% of portfolio to ETH
198
+
context.order_percent(
199
+
target_symbol="ETH",
200
+
percent=25,
201
+
order_side=OrderSide.BUY,
202
+
price=data["ETH/EUR"]["close"].iloc[-1],
203
+
)
204
+
205
+
# End up holding exactly 1.5 BTC (buys or sells the difference)
206
+
context.order_target(
207
+
target_symbol="BTC",
208
+
target_amount=1.5,
209
+
price=price,
210
+
)
211
+
212
+
# Rebalance BTC to 10% of portfolio
213
+
context.order_target_percent(
214
+
target_symbol="BTC",
215
+
target_percent=10,
216
+
price=price,
217
+
)
218
+
```
219
+
220
+
**Validation**
221
+
222
+
-`value`, `percent`, `price` must be positive.
223
+
-`target_amount`, `target_value`, `target_percent` must be non-negative.
224
+
-`order_target*` returns `None` (no order placed) when the current position already matches the target.
225
+
226
+
All helpers accept the same optional `market`, `precision` and `metadata` parameters as `create_limit_order`.
227
+
228
+
:::tip MARKET variant & auto-price
229
+
The helpers currently require an explicit `price` and always produce LIMIT orders. A future enhancement (tracked as a follow-up to #440) will add `order_type=OrderType.MARKET` support and fall back to `context.get_latest_price()` when `price` is omitted.
0 commit comments