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

ExitLong() and ExitShort() doesnt close StopLoss and ProfitTarget() orders

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

    ExitLong() and ExitShort() doesnt close StopLoss and ProfitTarget() orders

    I use ExitLong() and ExitShort() to Close positions on end of week. The Close is working fine for the position but it doesn't close out the StopLoss and TakeProfit orders. How to make the ExitLong() and ExitShort() to close out all orders for the position.

    #2
    Originally posted by ajsdnyyrr1234DDf View Post
    I use ExitLong() and ExitShort() to Close positions on end of week. The Close is working fine for the position but it doesn't close out the StopLoss and TakeProfit orders. How to make the ExitLong() and ExitShort() to close out all orders for the position.
    You need to cancel them just before using exit command.
    RJay
    NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

    Comment


      #3
      Could you point me to a CancelOrder() sample script?
      Do I have to call two separate Cancel calls, one for the StopLoss and one for TakeProfit?
      It looks like these are two separate OrderIDs and linked via a single OCO ID.

      How about the CloseStrategy() call instead of ExitLong()/ExitShort()/SetState()
      Will CloseStartegy close positions, cancel orders, and strategy properly?

      Following is what I have currently:

      //Friday Code

      if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
      {
      if (Position.MarketPosition == MarketPosition.Long)
      ExitLong(@"LongX");
      else if (Position.MarketPosition == MarketPosition.Short)
      ExitShort(@"ShortX");
      if (State == State.Realtime)
      {
      SetState(State.Terminated);
      Print("Friday Condition - Disabling Strategy" + Time[0].ToString());
      return;
      }
      }

      Comment


        #4
        Originally posted by ajsdnyyrr1234DDf View Post
        Could you point me to a CancelOrder() sample script?
        Do I have to call two separate Cancel calls, one for the StopLoss and one for TakeProfit?
        It looks like these are two separate OrderIDs and linked via a single OCO ID.

        How about the CloseStrategy() call instead of ExitLong()/ExitShort()/SetState()
        Will CloseStartegy close positions, cancel orders, and strategy properly?

        Following is what I have currently:

        //Friday Code

        if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
        {
        if (Position.MarketPosition == MarketPosition.Long)
        ExitLong(@"LongX");
        else if (Position.MarketPosition == MarketPosition.Short)
        ExitShort(@"ShortX");
        if (State == State.Realtime)
        {
        SetState(State.Terminated);
        Print("Friday Condition - Disabling Strategy" + Time[0].ToString());
        return;
        }
        }
        NT7 - CancelAllOrders(true, true);
        Last edited by RJay; 03-04-2018, 02:25 PM.
        RJay
        NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

        Comment


          #5
          I am getting the following error:
          The name CancelAllOrders does not exist in the current context
          CS0103

          Comment


            #6
            Originally posted by ajsdnyyrr1234DDf View Post
            I am getting the following error:
            The name CancelAllOrders does not exist in the current context
            CS0103
            NT7 or NT8?
            RJay
            NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

            Comment


              #7
              I am using NT8

              Comment


                #8
                Originally posted by ajsdnyyrr1234DDf View Post
                I am using NT8
                Have you tried adapting this sample from the "NT8 Help"

                privateAccountmyAccount;

                protectedoverridevoidOnStateChange()
                {
                if(State==State.SetDefaults)
                {
                // Initialize myAccount
                }
                }

                privatevoidOnExecutionUpdate(objectsender,ExecutionEventArgse)
                {
                // Cancel all orders if an execution is triggered after 9pm
                if(e.Time>newDateTime(now.Year,now.Month,now.Day,21,0,0))
                myAccount.CancelAllOrders(e.Execution.Instrument);
                }
                RJay
                NinjaTrader Ecosystem Vendor - Innovative Trading Solutions

                Comment


                  #9
                  Here is what I have come up with and seems to be working:


                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {
                  lock (Account.All)
                  myAccount = Account.All.FirstOrDefault(a => a.Name == "Realmoney");
                  } }



                  //Friday Code
                  if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
                  {
                  if (Position.MarketPosition == MarketPosition.Long)
                  {
                  myAccount.CancelAllOrders(BarsArray[0].Instrument);
                  ExitLong(@"LongX");
                  }
                  else if (Position.MarketPosition == MarketPosition.Short)
                  {
                  myAccount.CancelAllOrders(BarsArray[0].Instrument);
                  ExitShort(@"ShortX");
                  }
                  if (State == State.Realtime)
                  {
                  SetState(State.Terminated);
                  Print("Condition not met. Disabling Strategy" + Time[0].ToString());
                  return;
                  }
                  }

                  Comment


                    #10
                    Hello ajsdnyyrr1234DDf,

                    To confirm, you are not disabling the script while a position is open, is this correct?

                    (If so, the 'Cancel exit orders when a strategy is disabled' option will cancel any working orders when the script is disabled)

                    Instead, the strategy is staying running, a position is open, then an ExitLong() or ExitShort() call is used to exit the position, the position becomes closed, however, a stop loss and profit target that were generated from the script remain working?

                    Is this all correct?

                    If so, I would like to test script to see if I am able to reproduce this behavior on my end.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Yes correct. Following is the order I want to accomplish
                      1) Exit position
                      2) Close all orders
                      3) Disable strategy for the weekend.

                      Yes the behavior you have described is correct.
                      Using ExitLong() and ExitShort() only exit the positions and not any Stop or Limit orders associated with the position for StopLoss or TakeProfit.

                      I don't see the issue with Historical backtesting. I see the issue with realtime.

                      Comment


                        #12
                        Hello ajsdnyyrr1234DDf,

                        I am not able to reproduce this behavior.

                        Below is a link to a video.


                        Attached is the tested script.

                        The video demonstrates when the position is exited, the stop loss and profit target are automatically cancelled.

                        Are you using SetProfitTarget and SetStopLoss?
                        Attached Files
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Yes. I am using SetStopLoss() and SetProfitTarget(). I see what I am different that your example, and that is probably why the problem is not there for the historical back test.

                          Right after the Ext() call I am terminating the Strategy using SetState(State.Terminated) and I am doing this only for Real-time. I am not doing this for historical.

                          It appears, the limit orders closes out after some time when the position is exited. Since I am not waiting and immediately setting the state to terminated, that may be the reason why the limit orders stay open.

                          I read a similar behavior in some forum, where the limit orders associated with a position cancels out after a certain timeout but not immediately.

                          I modified my code as below:

                          ====

                          if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
                          {
                          if (Position.MarketPosition == MarketPosition.Long)
                          {
                          myAccount.CancelAllOrders(BarsArray[0].Instrument);
                          ExitLong(@"LongX");
                          }
                          else if (Position.MarketPosition == MarketPosition.Short)
                          {
                          myAccount.CancelAllOrders(BarsArray[0].Instrument);
                          ExitShort(@"ShortX");
                          }
                          if (State == State.Realtime)
                          {
                          SetState(State.Terminated);
                          Print("Condition not met. Disabling Strategy" + Time[0].ToString());
                          return;
                          }
                          }

                          ====

                          I see no difference in historical back tests but will see how it behaves this weekend.

                          Comment


                            #14
                            Hello ajsdnyyrr1234DDf,

                            If you are forcing the strategy to be terminated and you want orders to be automatically exited, you need to wait until the position is flat and all orders are cancelled.

                            OnPositionUpdate and OnOrderUpdate checking that all positions and orders are cancelled or filled will allow the stop losses and profit targets to be cancelled before the strategy stops processing and can no longer make actions with orders.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              I also have the problem that the following code exits the position but leaves the stoploss and profit targets associated with that position open. How do i identify and cancel the profit target and stoploss orders which are set via setstoploss and setprofittarget when the original position is created? Why are these OCO orders not cancelled when the Exitlong/short is called?

                              Regards



                              if (BarsInProgress == 23 && Positions[23].MarketPosition != MarketPosition.Flat && Bars.PercentComplete > 0.999
                              ||BarsInProgress == 11 && Positions[11].MarketPosition != MarketPosition.Flat && Bars.PercentComplete > 0.999)

                              {
                              ExitLong(23,tradeSizemnq,"ON NQ Long end of session close","NQ ON LONG LIMIT");
                              ExitShort(23,tradeSizemnq,"ON NQ Short end of session close","NQ ON SHORT LIMIT");
                              ExitShort(11,tradeSizemnq,"ON NQ Short end of session close","NQ ON SHORT LIMIT MICRO");
                              ExitLong(11,tradeSizemnq,"ON NQ Long end of session close","NQ ON LONG LIMIT MICRO");
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by RookieTrader, Today, 07:41 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by kujista, Today, 05:44 AM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by elderan, Yesterday, 08:03 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by samish18, Yesterday, 08:57 AM
                              8 responses
                              25 views
                              0 likes
                              Last Post samish18  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              3 responses
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X