Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with change order

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

    Need help with change order

    Hi,

    I need help with a strategy to change order:
    I followed the guide NinjaTrader 8

    I am able to get the order ID from the OnOrderUpdate.

    if (order.Name == "Stop loss")
    StoplossOrder = order;



    Later I use this Order in OnBarUpdate to change the stop loss.
    I am able to print the current order ID and stop loss.
    After executing the ChangeOrder, the stop is not getting updated.
    Would you please help me how to update the order ?



    protected override void OnBarUpdate()
    {
    // Raise stop loss to breakeven when you are at least 4 ticks in profit
    if (stopOrder != null && stopOrder.StopPrice < Position.AveragePrice && Close[0] >= Position.AveragePrice + 4 * TickSize)
    ChangeOrder(stopOrder, stopOrder.Quantity, 0, Position.AveragePrice);
    }




    #2
    Hello pvincent,

    If you used SetStopLoss then you won't use ChangeOrder, you just call SetStopLoss again with the updated values. ChangeOrder is only relevant for Managed orders with IsLiveUntilCancelled set to true and Unmanaged orders.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello pvincent,

      If you used SetStopLoss then you won't use ChangeOrder, you just call SetStopLoss again with the updated values. ChangeOrder is only relevant for Managed orders with IsLiveUntilCancelled set to true and Unmanaged orders.
      Thank you very much.

      Also, I am able to get the GetUnrealizedProfitLoss.
      Is there a way to get the realizedProfitLoss for that strategy that is running?

      Comment


        #4
        Hello pvincent,

        You can find a sample of using the realized profit in the following link. The realized profit needs some specific logic so that you can reset it for each session.


        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello pvincent,

          You can find a sample of using the realized profit in the following link. The realized profit needs some specific logic so that you can reset it for each session.

          Thank you very much.

          Comment


            #6
            I am trying to use the settrailstop. I am not getting any error, The logs looks fine and when I use Print, it looks like the code gets executed.
            However, the trail does not work.

            if (State == State.DataLoaded)
            {

            SetStopLoss(@"long", CalculationMode.Ticks, SL, false);
            SetProfitTarget(@"long", CalculationMode.Ticks, TP);
            SetStopLoss(@"short", CalculationMode.Ticks, SL, false);
            SetProfitTarget(@"short", CalculationMode.Ticks, TP);

            }


            And in OnBarUpdate():


            // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
            if (Close[0] > Position.AveragePrice + 50 * TickSize)
            {
            SetTrailStop(@"long",CalculationMode.Ticks, 10,false);
            }



            Please help.

            Comment


              #7
              Hello pvincent,

              SetStopLoss and SetTrailStop cannot be used at the same time to target the same entry. If you wanted to switch to using a trail stop you would have to cancel the stoploss first and then submit the trail stop.

              Comment


                #8
                Hi Jesse,


                I tried the below.
                The stops are set during the entry and I am trying to set the trail after some profit.
                I got the stoploss order and using the cancelorder.
                The print is working, so the code is triggerred, but still the trail is not set.
                Please help.


                if ( Position.MarketPosition == MarketPosition.Long
                && StoplossOrder != null )
                {
                if (Close[0] > Position.AveragePrice + StartTrail * TickSize)
                {
                CancelOrder(StoplossOrder);
                Print( "*SetTrailStop*");
                SetTrailStop(@"long",CalculationMode.Ticks, TrailStop,false);
                }
                }


                OnOrderUpdate:

                protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                {
                // check if the current order matches the orderName passed in "EnterLong"()
                // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
                // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
                if (order.Name == "Stop loss")
                StoplossOrder = order;

                // if stop loss order exists
                if (StoplossOrder != null && StoplossOrder == order)
                {
                Print(order.ToString());
                if (order.OrderState == OrderState.Cancelled)
                {
                // Do something here
                StoplossOrder = null;
                }
                }
                }


                Comment


                  #9
                  Hello pvincent,

                  The problem is that you need to wait for the cancellation to happen. The following code executes all at once meaning it submits a cancel, does a print and calls SetTrailStop within a very tiny amount of time.

                  Code:
                  CancelOrder(StoplossOrder);
                  Print( "*SetTrailStop*");
                  SetTrailStop(@"long",CalculationMode.Ticks, TrailStop,false);
                  The order that was cancelled hasn't event started the process of cancelling by the time SetTrailStop is called. You would have to move the SetTrailStop into OnOrderUpdate under the condition of OrderState.Cancelled at the very earliest. Alternatively if you wanted to set a variable like StoplossOrder to null you could check for that in OnBarUpdate and then do SetTrailStop based on a condition.

                  Comment


                    #10
                    Awesome. Makes sense.

                    Thank you for your help!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    72 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    143 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    76 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    47 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    51 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X