Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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?​
    Handlar
    NinjaTrader Ecosystem Vendor - Handlar

    #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.

      Chris L.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by StockTrader88, 03-06-2021, 08:58 AM
      45 responses
      3,991 views
      3 likes
      Last Post johntraderuser2  
      Started by TAJTrades, Today, 09:46 AM
      0 responses
      7 views
      0 likes
      Last Post TAJTrades  
      Started by rhyminkevin, Yesterday, 04:58 PM
      5 responses
      62 views
      0 likes
      Last Post dp8282
      by dp8282
       
      Started by realblubb, Today, 09:28 AM
      0 responses
      8 views
      0 likes
      Last Post realblubb  
      Started by AaronKoRn, Yesterday, 09:49 PM
      1 response
      19 views
      0 likes
      Last Post Rikazkhan007  
      Working...
      X