Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to close all positions for active account without disabling the strategy?

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

    How to close all positions for active account without disabling the strategy?

    Hello, I have a question and maybe someone has an answer.
    I have a strategy for example named "Strategy01" and I open 3 charts: CL M5, GC M5 and ES M5. I run the strategy on each chart, and as a result I have 3 working instances of one strategy. All instances of strategy work on the same account Sim101.

    There is some logic for opening a positions and let's imagin, each instance of strategy of three open a position. And as a result we have 3 opened positions.

    Then I have a code where I control unrealized PnL for whole Account and if the value of unrealized PnL will hit some value, for example 40$, I want to close all opened positions but I want to leave also all strategies working. Here below is a snippet of my code:​

    Code:
    double unrealizedPNL = Position.MarketPosition == MarketPosition.Flat ?
        0 :
        Math.Round(Account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar), 2)  - Math.Round(Account.Get(AccountItem.Commission, Currency.UsDollar), 2);
    
    if (unrealizedPNL >= 40) {
        foreach (var position in Account.Positions) {
            if (position.MarketPosition != MarketPosition.Flat) {
                try {
                    position.Close();
                } catch (Exception ex) {
                    Print("There is an error during closing the position : " + ex.ToString());
                }
            }
        }
    }
    ​
    And the logic works fine: when the total unrealized PnL is more, than 40$, then all opened positions are closed, BUT the strategies are immediately disabled (((

    How can I close all opened positions without disabling the strategies?​

    #2
    One possible way:

    Add a timer to each strategy so it periodically checks, let's say once a second.

    In an AddOn namespace that is accessible by each strategy, put a static dictionary so that each instance can update it's own PL in the dictionary, and a flag for whether or not to go flat across all of the instances, and a method that sums up the PL and if appropriate, sets the flag (too much win, too much loss).

    In each of the strategy instances, on the timer, update your own instance's P/L and invoke the static common method to update the sum and the flag. Then, if the flag is set, exit any position in this instance.

    In that way, any of the instances can push it over the threshold and cause the flag to be set, and when that happens, all of the instances will issue their own orders to go flat within 1 second without being disabled.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Hi handlar, thanks for your post. Bruces suggestion is one way to do this. You can also make a multi-instrument script and operate all positions from one instance of a strategy, this will not disable the strategy because it is managing all open positions.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      135 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X