Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Managing Multi Strategies w/Independent PnL & Ord Cancel

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Managing Multi Strategies w/Independent PnL & Ord Cancel

    Hi all,

    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:
    1. Track each strategy’s realized and unrealized PnL independently while contributing to the total PnL.
    2. Associate specific orders with each strategy so only the relevant ones are canceled when a goal/loss is hit.
    3. 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?
    Thank you in advance

    #2
    Hello lacerda4269,

    "How can I structure my code to track each strategy’s PnL (realized + unrealized) separately while updating the total PnL?"

    This thread will remain open for any community members that would like to provide you advice on what to code.

    As a suggestion, you could use a List<double> to track multiple pnl values. Or you could use separate double variables.

    "What’s the best way to tag or group orders by strategy for selective cancellation?"

    You could add the orders to a List<order> object or other collection type, or you could add specific characters to the signal name string when submitting the order.

    "Any tips for dynamically adjusting contract sizes as strategies deactivate?"

    You can use a variable for the quantity supplied to order methods. If you want to check the account pnl this can be accessed with PositionAccount.GetUnrealizedProfitLoss() / Account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar)​ and Account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar)
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    39 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    64 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X