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 Segwin, 05-07-2018, 02:15 PM
      14 responses
      1,789 views
      0 likes
      Last Post aligator  
      Started by Jimmyk, 01-26-2018, 05:19 AM
      6 responses
      837 views
      0 likes
      Last Post emuns
      by emuns
       
      Started by jxs_xrj, 01-12-2020, 09:49 AM
      6 responses
      3,293 views
      1 like
      Last Post jgualdronc  
      Started by Touch-Ups, Today, 10:36 AM
      0 responses
      13 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      11 responses
      63 views
      0 likes
      Last Post halgo_boulder  
      Working...
      X