• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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.
                        https://drive.google.com/file/d/1Hzp...w?usp=drivesdk

                        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

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by FMtrader, 03-15-2019, 02:30 AM
                            6 responses
                            31 views
                            0 likes
                            Last Post James108  
                            Started by timko, Today, 08:18 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post timko
                            by timko
                             
                            Started by victorsmith, Today, 06:00 AM
                            0 responses
                            4 views
                            0 likes
                            Last Post victorsmith  
                            Started by sohailashraf, Yesterday, 10:18 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post sohailashraf  
                            Started by jjnc2006, Yesterday, 07:36 PM
                            0 responses
                            16 views
                            0 likes
                            Last Post jjnc2006  
                            Working...
                            X