Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close All open Orders and Positions

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

    Close All open Orders and Positions

    Hi,

    When a strategy is running on 2 different instruments and the cashvalue reaches a defined target, is it possible to close all orders and open positions from code inside the strategy?

    #2
    daxtrading, it is definitely possible to close positions and cancel orders once a value reaches a certain target. To close positions, you can do an ExitLong() and ExitShort() inside each instrument's code block. Then you could also create some sort of flag to tell the strategy to not take anymore trades, like this:
    Code:
    if (cashvalue > target)
    {
    ExitLong();
    ExitShort();
    
    canTrade = false;
    }
    
    // and for your order submission stuff....
    if (tradeConditions == true  && canTrade == true)
    {
    // submit orders
    }
    For canceling the orders, you'll have to just run through each open order and call CancelOrder() for it.

    Please let me know if you have any other questions.
    AustinNinjaTrader Customer Service

    Comment


      #3
      CashValue doesn,t exist.

      Comment


        #4
        daxtrading, I'm not sure in which context you check it, it would be available in a strategy -

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          daxtrading, I'm not sure in which context you check it, it would be available in a strategy -

          http://www.ninjatrader.com/support/h...countvalue.htm
          I did use that but my position closed a secound after the open.

          This is the code I used:

          if (GetAccountValue(AccountItem.CashValue) > 100500)

          ExitLong();
          ExitShort();

          Comment


            #6
            This would mean that the condition evaluated to 'true' then triggering the exit you coded in - you will need to keep in mind that this is the account cash value that does not include the open profit you would have.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Are there any objections to using this to close any open positions?

              if(Position.MarketPosition!=MarketPosition.Flat)
              Position.Close();

              Comment


                #8
                Originally posted by NinjaTrader_Bertrand View Post
                This would mean that the condition evaluated to 'true' then triggering the exit you coded in - you will need to keep in mind that this is the account cash value that does not include the open profit you would have.
                What is the code to close all orders/positions when account cash value + open profit > 125000

                Comment


                  #9
                  Originally posted by nailz420 View Post
                  Are there any objections to using this to close any open positions?

                  if(Position.MarketPosition!=MarketPosition.Flat)
                  Position.Close();
                  You would need to do some testing with in in your scenario, as it would not officially supported NinjaScript.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by daxtrading View Post
                    What is the code to close all orders/positions when account cash value + open profit > 125000
                    Maarten, I would unfortunately not have a code snippet here for you - you would add the open Pnl for example to your account balance (GetProfitLoss()) - however the strategy would only know about it's own positions, it would not have access to any other strategies positions or manually entered ones.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Ok Betrand, If I want it only to happen on the open position in one open strategy, can I do that and what code is needed?

                      Comment


                        #12
                        Yes Maarten, I already gave you code tips how to get started - you would now need to create the code needed.
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          I know how to exit on account balance and on GetProfitLoss seperately but not on a combination of these 2 conditions.

                          Comment


                            #14
                            Hi Maarten,

                            All snippets and info are provided. You now need to custom code it. Unfortunately we can't continue to hand-hold with this. You either need to learn how to program this or get another consultant involved.

                            Vince
                            Vince B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by GussJ, 03-04-2020, 03:11 PM
                            11 responses
                            3,227 views
                            0 likes
                            Last Post xiinteractive  
                            Started by andrewtrades, Today, 04:57 PM
                            1 response
                            13 views
                            0 likes
                            Last Post NinjaTrader_Manfred  
                            Started by chbruno, Today, 04:10 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post chbruno
                            by chbruno
                             
                            Started by josh18955, 03-25-2023, 11:16 AM
                            6 responses
                            440 views
                            0 likes
                            Last Post Delerium  
                            Started by FAQtrader, Today, 03:35 PM
                            0 responses
                            12 views
                            0 likes
                            Last Post FAQtrader  
                            Working...
                            X