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

Order reference

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

    Order reference

    Hello guys and thanks for your support, I have 3 questions.

    Question #1
    I want to clarify about order reference when I'm calling EnterLongStopMarket

    Can I just use it like that(code below) and later on use it for cancellation without assignments on OnOrderUpdate?
    If yes, will this work same way on Backtest/Live? Because I see most of your examples trying to assign order reference in OnOrder### callback, but actually calling EnterLongStopMarket already returns Order object (I understand that it is read-only), I only need to know if reference remain the same during order life-time and I can cancel it with that reference object.

    In my example I'm trying to simulate my question in code. Calling EnterLongStopMarket on bar 300 and assuming price did not move cancelling it on bar 305. This is just simplification to explain what I'm asking. Important point if this code is fine, I want to know if it should behave the same in Realtime also. I understand that I need to call GetRealtimeOrder for Realtime, and want to know if anything else needed?


    PHP Code:

    public class OrdersTestStrategy Strategy
    {
        protected 
    override void OnStateChange()
        {
            if (
    State == State.SetDefaults)
            {
                
    Name "OrdersTestStrategy";
                
    Calculate Calculate.OnBarClose;
                
    TimeInForce TimeInForce.Gtc;
            }
        }

        private 
    Order order;

        protected 
    override void OnBarUpdate()
        {
            var 
    entryPrice Close[0] + (TickSize 100);

            if(
    CurrentBar == 300)
                
    order EnterLongStopMarket(0true1entryPrice"Long123");
            else if(
    CurrentBar == 305)
                
    CancelOrder(order);

        }



    Question #2
    Sometimes reply on forum conflicting with documentation, or maybe there is few ways of doing same thing, but only one is documented. So I'm confused what could be used as source of truth.

    So my question from which state GetRealtimeOrder can/should be called? From Transition or Realtime?


    Question #3

    Which from following approaches I can use in Realtime (in backtest they simply do nothing)? I have some logic where at some point I want to cancel all pending orders from Strategy, and make sure that there is nothing hidden stays which will be executed later.

    PHP Code:

    protected override void OnBarUpdate()
    {

           
    //approach 1
           
    foreach(var order in this.Orders)
           {
                 
    CancelOrder(order);
           }

           
    //approach 2
           
    this.Account.Cancel(this.Orders);

           
    //approach 3
           
    this.Account.CancelAllOrders(this.Instrument);


    Last edited by login_dejavu; 01-16-2020, 09:27 AM.

    #2
    Hi login_dejavu, thanks for your post.

    Question 1: Orders are assigned in OnOrderUpdate because it's more reliable than referencing them in OnBarUpdate. The order reference will be the same, but it's possible that the Order object can still be null in the OnBarUpdate call, so it's best practice to assign the order object in OnOrderUpdate. See here for more documentation on advanced order handling:



    Question 2: All documentation demonstrates calling GetRealtimeOrder in State.RealTime, but Transition happens right before this so you can use either one.

    Question 3: I do not fully understand the question. Are you looking for something that will trach account values in historical mode? If so this would need to be simulated with your own code, the Account object is not available in the historical mode.

    I look forward to hearing from you.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris, thank for your response,

      I will show in code what I mean. Basically I want to clear all not filled orders before setting new one (seeking alternative approach to cancel them without collecting references and so on, because I only want simple cancel, do not plan to change them or track them somehow)


      PHP Code:

      protected override void OnBarUpdate()
      {
           
      //clear all NOT filled previous EnterLongStopMarket orders
           
      Account.CancelAllOrders(this.Instrument);

           
      //set new EnterLongStopMarket
           
      EnterLongStopMarket(0trueDefaultQuantityClose[0] + (TickSize 100), "Long123");

      Last edited by login_dejavu; 01-16-2020, 10:51 AM.

      Comment


        #4
        Hi login_dejavu, thanks for your reply.

        The Account object is needed to use CancelAllOrders so it can not be used in Historical mode. The recommended approach is to always get references to your order objects so they can be canceled using CancelOrder. We have an example of doing this here:



        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Ok, got it, could you please review the code of the strategy, where I implemented your suggestion and covered my original request (cancel all not filled orders). If it looks good to you. I cutted all not needed information. The only maybe missing part is to lock the Dictionary for cases if few OnOrderUpdate happens at same time, do not put it here to not overload the code.
          * Of course I will not be trying to cancel on each bar(it will be some condition)

          PHP Code:

          public class OrdersTestStrategy Strategy
          {
              private 
          Dictionary<stringOrderorders;

              protected 
          override void OnStateChange()
              {
                  if (
          State == State.SetDefaults)
                  {
                      
          Name  "OrdersTestStrategy";
                      
          Calculate Calculate.OnBarClose;
                      
          TimeInForce TimeInForce.Gtc;
                  }
                  else if(
          State == State.Configure)
                  {
                      
          orders = new Dictionary<stringOrder>();
                  }
                  else if(
          State == State.Realtime)
                  {
                      foreach(var 
          okv in orders.ToList())
                      {
                          var 
          orderName okv.Key;
                          var 
          order okv.Value;
                          
          orders[orderName] = GetRealtimeOrder(order);
                      }
                  }
              }

              protected 
          override void OnBarUpdate()
              {
                  
          CancelAllNotFilledOrders();
                  
          EnterLongStopMarket(0trueDefaultQuantityClose[0] + (TickSize 100), "Long" CurrentBar);
              }

              protected 
          override void OnOrderUpdate(Order order,
                             
          double limitPrice,
                             
          double stopPrice,
                             
          int quantity,
                             
          int filled,
                             
          double averageFillPrice,
                             
          OrderState orderState,
                             
          DateTime time,
                             
          ErrorCode error,
                             
          string nativeError)
              {
                  
          orders[order.Name] = order;
              }

              private 
          void CancelAllNotFilledOrders()
              {
                  foreach(var 
          okv in orders.ToList())
                  {
                      var 
          orderName okv.Key;
                      var 
          order okv.Value;

                      if(
          order.OrderState != OrderState.Filled)
                      {
                          
          CancelOrder(order);
                          
          orders.Remove(orderName);
                      }
                  }
              }


          Last edited by login_dejavu; 01-16-2020, 12:28 PM.

          Comment


            #6
            Hi login_dejavu , thanks for your reply.

            From a glance, this looks good to me. It should be well tested on the Sim101 account to confirm this works the way you need it to.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Sure, just wanted you to quickly look if you'll not spot anything strange. Thanks again and have a nice day!
              Last edited by login_dejavu; 01-16-2020, 02:53 PM.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by AveryFlynn, Today, 04:57 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by Max238, Today, 01:28 AM
              4 responses
              37 views
              0 likes
              Last Post Max238
              by Max238
               
              Started by r68cervera, Today, 05:29 AM
              1 response
              8 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by geddyisodin, Today, 05:20 AM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by timko, Today, 06:45 AM
              2 responses
              14 views
              0 likes
              Last Post NinjaTrader_ChristopherJ  
              Working...
              X