I’m working on a fully automated trading strategy that I’ve split into four independent sub-strategies (Strat 1–4), each with its own contract size, daily profit goal, and daily max loss. The strategies use the same entry/exit logic and track both realized and unrealized PnL. My goal is to shut down each strategy individually when it hits its profit goal or max loss, canceling only its associated orders (open positions, stop-loss, take-profit, pending) while the others continue trading. All strategies contribute to a shared total PnL.
Here’s the setup:
- Strat 1: 1 contract, $500 profit goal, $500 max loss
- Strat 2: 1 contract, $600 profit goal, $500 max loss
- Strat 3: 2 contracts, $800 profit goal, $500 max loss
- Strat 4: 2 contracts, $1000 profit goal, $500 max loss
How it should work:
- On the first trade, all strategies enter simultaneously (total 6 contracts).
- If Strat 1 hits its $500 profit goal, it closes its position, cancels its orders, and deactivates for the day. The other strategies continue.
- Subsequent trades use only active strategies’ contracts (e.g., 5 contracts if Strat 1 is off).
- This repeats as each strategy hits its profit goal or max loss, locking in gains (e.g., $500 + $600 if Strat 1 and 2 deactivate).
- If Strat 4, for example, swings from $900 unrealized profit to -$500, it shuts down, canceling its orders.
My challenge: I’m struggling to:
- Track each strategy’s realized and unrealized PnL independently while contributing to the total PnL.
- Associate specific orders with each strategy so only the relevant ones are canceled when a goal/loss is hit.
- Ensure new trades reflect only active strategies’ contract sizes.
Example:
- Trade 1 (6 contracts): Strat 1 hits $500 profit, deactivates, cancels its 1-contract orders. Total PnL = $500, 5 contracts for next trade.
- Trade 2 (5 contracts): Strat 2 hits $600 profit, deactivates. Total PnL = $1100, 4 contracts next.
- Trade 3 (4 contracts): Strat 3 hits $800 profit, deactivates. Total PnL = $1900, 2 contracts next.
- Trade 4 (2 contracts): Strat 4 swings to -$500 loss, shuts down. Final PnL = $1400.
Questions:
- How can I structure my code to track each strategy’s PnL (realized + unrealized) separately while updating the total PnL?
- What’s the best way to tag or group orders by strategy for selective cancellation?
- Any tips for dynamically adjusting contract sizes as strategies deactivate?

Comment