Leg Handler and Order Management
In Buildalgos.com, a Leg represents a fully managed trading position. It is created with addLeg() and then automatically tracks stop-loss, target, re-entry conditions, and lifecycle events.
Each addLeg(...) call spawns an internal LegHandler object and returns a unique legID (UUID).
Why Use addLeg Instead of placeOrder?
| Feature | addLeg | placeOrder |
|---|---|---|
| Automatic SL / Target | ✅ | ❌ |
| Trailing stop-loss | ✅ | ❌ |
| Re-entry after SL / Target | ✅ | ❌ |
| Wait-Trade (delayed entry) | ✅ | ❌ |
| Group square-offs | ✅ | Manual |
| Position tracking | Auto | Manual |
| Best for | Systematic strategies | One-off discretionary trades |
addLeg() Signature
leg_id = self.addLeg(
transactionType='buy', # "buy" or "sell"
optionType='PE', # "CE" | "PE" (ignored for FUT/CASH)
qty=75,
Symbol: Optional[str] = None,
strikeSelection={'strikeBy': 'moneyness','strikeVal': 0,'asof': 'None','roundoff': None},
expiry={'expType': 'weekly','expNo': 0},
target={'isTarget': True,'targetOn': '%','targetValue': 50},
SL={'isSL': True,'SLon': '%','SLvalue': 35},
trailSL={'isTrailSL': False,'trailSLon': 'val','trailSL_X': 1,'trailSL_Y': 1},
reEntryTg={'isReEntry': False,'reEntryOn': 'asap','reEntryVal': 0,'reEntryMaxNo': 20},
reEntrySL={'isReEntry': False,'reEntryOn': 'asap','reEntryVal': None,'reEntryMaxNo': 20},
waitTrade={'isWT': False,'wtOn': 'val-up','wtVal': 1,'triggers': []},
segment='OPT', # "OPT" | "FUT"
entryValidity='Day', # currently only "DAY"
squareoff='this', # "this" | "all" | [legIDs]
legName:Optional[str] = None,
webhook={'is': False,'entryMessage': {},'exitMessage': {}}
)
This structure allows for flexible, reusable, and fully-managed execution flows for any options or futures strategy.
Top Level Parameters
| Parameter | Values | Description |
|---|---|---|
| transactionType | 'buy' or 'sell' | Buy or sell leg |
| symbol | str or None | Optional: direct symbol override; else uses strikeSelection |
| optionType | 'CE' or 'PE' | Option type (for options); otherwise None |
| qty | int | Quantity (usually multiple of lot size) |
| segment | "OPT" | "FUT" | Segment to trade |
| entryValidity | 'Day' | Order validity |
| remark | str | Optional note, e.g. 'Entering Leg' |
| legName | str | Optional unique ID, e.g. 'lg_ac380_1' |
Nested Parameters Dictionaries
strikeSelection :
| Key | Type | Example Values | Description |
|---|---|---|---|
| strikeBy | str |
| Method used to select the strike price |
| strikeVal | int |
| Value used according to strike-selection logic |
| asof | str or None |
| Time reference for strike selection |
| roundoff | int or None |
| Round strike to nearest value (e.g., 100) |
expiry Selection:
| Key | Type | Example Value | Description |
|---|---|---|---|
| expType | str | 'weekly' | Expiry type (weekly/monthly) |
| expNo | int | 0 | 0 = nearest, 1 = next |
Stop-Loss (SL) & Target (target):
| Key | Choices / Type | Effect / Description |
|---|---|---|
isSL / | True / False (bool) | Enable/disable SL or target logic |
SLon / |
| How exit threshold is computed |
SLvalue / | number(float/int) | Magnitude of SL/target |
Trailing SL (trailSL):
Note : Value must be between 0-100% for %
| Key | Type | Example Value | Description |
|---|---|---|---|
| isTrailSL | bool | True | Enable trailing SL |
| trailSLon | str | '%', 'val' | Based on value or % |
| trailSL_X | float | 2 | Distance (₹ or %) that triggers a trailing SL adjustment |
| trailSL_Y | float | 0.98 | Amount (₹ or %) by which SL is tightened each time trailSL_X is hit |
Re-Entry Rules (reEntrySL, reEntryTg):
| Key | Type / Choices | Description |
|---|---|---|
| isReEntry | bool (True/False) | Enables re-entry after SL/target |
| reEntryOn | strChoices:
|
|
| reEntryVal | None or self.act |
|
| reEntryMaxNo | int | Max allowed re-entry attempts (range 1–20) |
Wait‑Trade (waitTrade):
Delays order entry until the instrument's price changes by a defined amount or percentage.
| Key | Type | Allowed Values / Examples | Description |
|---|---|---|---|
| isWT | bool | True, False | Enable/disable wait-trade logic |
| wtOn | str | "val-up" → price ↑ by ₹X"val-down" → price ↓ by ₹X"%val-up" → price ↑ by X%"%val-down" → price ↓ by X% | Price-change condition required to place the leg |
| wtVal | float | 1, 2.5, … | Required move (₹ or %) — depends on wtOn |
| triggers | list | Optional list of other leg definitions | Auto-trigger extra legs when the wait condition is met |
webhook (Custom Webhooks):
| Key | Type | Example Value | Description |
|---|---|---|---|
| is | bool | False | Whether webhook is active |
| entryMessage | dict | {} | Custom webhook payload on entry |
| exitMessage | dict | {} | Custom webhook payload on exit |