Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Managing Orders

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

    Managing Orders

    Hi guys. I need some help with my orders management. I'm trying to create my order 1 and order 2 after order 0 has been filled. I'm posting parts of my code, first the original that is submitting the orders all together and the second part with the code that i was hoping its gonna work...

    ORIGINAL:
    Code:
                            // Buy	1			
                            entryBuyOrder = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt1, offsetBase(1) - orderTypeSign * this.buy_OFFSET1 * TickSize, 0, "Long Limit1_" + Instrument.Id, "Long Limit1");
    
                            // Buy	2			
                            entryBuyOrder1 = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt2, offsetBase(1) - orderTypeSign * this.buy_OFFSET2 * TickSize, 0, "Long Limit2_" + Instrument.Id, "Long Limit2");
    
                            // Buy	3			
                            entryBuyOrder2 = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt3, offsetBase(1) - orderTypeSign * this.buy_OFFSET3 * TickSize, 0, "Long Limit3_" + Instrument.Id, "Long Limit3");
    
                            // Sell 1
                            entrySellOrder = CreateOrder(0, OrderAction.SellShort, OrderType.Limit, sell_qnt1, offsetBase(-1) + orderTypeSign * this.sell_OFFSET1 * TickSize, 0, "Short Limit1_" + Instrument.Id, "Short Limit1");
    
                            // Sell 2
                            entrySellOrder1 = CreateOrder(0, OrderAction.SellShort, OrderType.Limit, sell_qnt2, offsetBase(-1) + orderTypeSign * this.sell_OFFSET2 * TickSize, 0, "Short Limit2_" + Instrument.Id, "Short Limit2");
    
                            // Sell 3
                            entrySellOrder2 = CreateOrder(0, OrderAction.SellShort, OrderType.Limit, sell_qnt3, offsetBase(-1) + orderTypeSign * this.sell_OFFSET3 * TickSize, 0, "Short Limit3_" + Instrument.Id, "Short Limit3");
    and the modified part:

    Code:
    						// Buy	1			
                    		entryBuyOrder = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt1, offsetBase(1) - orderTypeSign * this.buy_OFFSET1 * TickSize, 0, "Long Limit1_" + Instrument.Id, "Long Limit1");
    					
    						// Sell 1
    						entrySellOrder = CreateOrder(0, OrderAction.SellShort, OrderType.Limit, sell_qnt1, offsetBase(-1) + orderTypeSign * this.sell_OFFSET1 * TickSize, 0, "Short Limit1_" + Instrument.Id, "Short Limit1");
    						
    						if (entryBuyOrder.OrderState == OrderState.Filled)
    						// Buy	2			
                            entryBuyOrder1 = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt2, offsetBase(1) - orderTypeSign * this.buy_OFFSET2 * TickSize, 0, "Long Limit2_" + Instrument.Id, "Long Limit2");
    
    						if (entryBuyOrder.OrderState == OrderState.Filled)
    						// Buy	3
    		                entryBuyOrder2 = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt3, offsetBase(1) - orderTypeSign * this.buy_OFFSET3 * TickSize, 0, "Long Limit3_" + Instrument.Id, "Long Limit3");
    
    						if (entrySellOrder.OrderState == OrderState.Filled)
    						// Sell 2
                            entrySellOrder1 = CreateOrder(0, OrderAction.SellShort, OrderType.Limit, sell_qnt2, offsetBase(-1) + orderTypeSign * this.sell_OFFSET2 * TickSize, 0, "Short Limit2_" + Instrument.Id, "Short Limit2");
    
    						if (entrySellOrder.OrderState == OrderState.Filled)
    						// Sell	3			
                            entrySellOrder2 = CreateOrder(0, OrderAction.SellShort, OrderType.Limit, sell_qnt3, offsetBase(-1) + orderTypeSign * this.sell_OFFSET3 * TickSize, 0, "Short Limit3_" + Instrument.Id, "Short Limit3");
    Thanks a lot!

    #2
    Hello dawidtokyo,

    Thank you for your note.

    To do this kind of order management I recommend you use the OnExecution. This cause your other order entries to wait until an order action has been completed.

    For example:
    Keep the initial order in the OnBarUpdate -
    protected override void OnBarUpdate()
    {
    entryBuyOrder = EnterLongLimit(0, true, buy_qnt1, offsetBase(1) - orderTypeSign * this.buy_OFFSET1 * TickSize, "Long Limit1_" + BarsPeriod.Id);
    }

    Then add the other buy order entries to the OnExecution after a check they exist-
    protected override void OnExecution(IExecution execution)
    {
    if (entryBuyOrder == null || entryBuyOrder != execution)
    return;

    if (execution.OrderState == OrderState.Filled)
    // Buy 2
    entryBuyOrder1 = EnterLongLimit(0, true, buy_qnt2, offsetBase(1) - orderTypeSign * this.buy_OFFSET2 * TickSize, "Long Limit2_" + BarsPeriod.Id);

    if (execution.OrderState == OrderState.Filled)
    // Buy 3
    entryBuyOrder2 = EnterLongLimit(0, true, buy_qnt3, offsetBase(1) - orderTypeSign * this.buy_OFFSET3 * TickSize, "Long Limit3_" + BarsPeriod.Id);
    }

    Below is a link to the help guide on IExecution. In here is an example of using OnExecution you may find beneficial.
    http://www.ninjatrader.com/support/h...nexecution.htm


    Please let me know if I can be of further assistance.
    Last edited by NinjaTrader_ChelseaB; 02-26-2013, 12:11 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      no luck

      All my parameters and rules for my orders are in OnBarUpdate... If i move the orders to OnExecution i'm losing all control over them. I'm not a good programmer as you can see here... Is there any way i could make the orders to wait for first order to fill in OnBarUpdate?

      I will really appreciate any help i could get here.

      I tried few things already and i know its working with:
      Code:
      if (Performance.RealtimeTrades.Count > 1)
      entryBuyOrder2 = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt3, offsetBase(1) - orderTypeSign * this.buy_OFFSET3 * TickSize, 0, "Long Limit3_" + Instrument.Id, "Long Limit3");
      But that is not what i need

      Comment


        #4
        Hello dawidtokyo,

        The best method to check if an order has been filled is with the OnExecution. I recommend you move your orders and parameters to this.

        Using OnExecution will guarantee the desired actions happen when an order has been filled.

        As a tip, CreateOrder() is not supported. You may want to use supported methods if you do not have extensive experience with NinjaScript.

        For example:
        entryBuyOrder = EnterLongLimit(0, true, buy_qnt3, offsetBase(1) - orderTypeSign * this.buy_OFFSET3 * TickSize, "Long Limit3_" + BarsPeriod.Id);
        EnterLongLimit( int BarsInProgress, bool LiveUntilCancelled, int quantity, double limitPrice, string signalName );

        Below is a link to the help guide on using Managed Approach to order entry. In here are the various calls you can make to place orders.
        http://www.ninjatrader.com/support/h...d_approach.htm

        Also, here is a link to a reference sample using the managed approach.
        http://www.ninjatrader.com/support/f...ead.php?t=7499


        Please let me know if I can still of assistance.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by dawidtokyo View Post
          All my parameters and rules for my orders are in OnBarUpdate... If i move the orders to OnExecution i'm losing all control over them. I'm not a good programmer as you can see here... Is there any way i could make the orders to wait for first order to fill in OnBarUpdate?

          I will really appreciate any help i could get here.

          I tried few things already and i know its working with:
          Code:
          if (Performance.RealtimeTrades.Count > 1)
          entryBuyOrder2 = CreateOrder(0, OrderAction.Buy, OrderType.Limit, buy_qnt3, offsetBase(1) - orderTypeSign * this.buy_OFFSET3 * TickSize, 0, "Long Limit3_" + Instrument.Id, "Long Limit3");
          But that is not what i need
          The best place for what you are describing is in OnExecution(). If you insist on doing things in OnBarUpdate(), then you need to check either your IOrders to see if they have been filled, or else your position's quantity to see if it has changed. Either of those events would imply that an order has been filled.

          Comment


            #6
            Could u please explain more or post some example?

            Thank you very much for your input!

            Comment


              #7
              Originally posted by dawidtokyo View Post
              Could u please explain more or post some example?

              Thank you very much for your input!
              Checking for change in position quantity:

              Code:
                      #region Variables
                      private int LastPositionQuantity = 0;
                      #endregion
              Code:
                 protected override void OnBarUpdate()
                 {
                 if (Position.Quantity > this.LastPositionQuantity)
                 {
                  //We have more than we did before, so an order must have been filled
                  //Process whatever you want to do now that you know there has been a fill
               
                  this.LastPositionQuantity = Position.Quantity; //then update the quantity
                 }
               
                 if (Position.MarketPosition == MarketPosition.Flat) 
                  this.LastPositionQuantity = 0; //reset when flat
               
                }
              As to checking the IOrder, look for IOrder in the NT Help.

              Comment


                #8
                Thank you very much. Will try it and post the results!

                Again thank you for help

                Comment


                  #9
                  Got it

                  I managed to get the orders working using on OnOrderUpdate. Thank you guys for all the input!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by judysamnt7, 03-13-2023, 09:11 AM
                  4 responses
                  57 views
                  0 likes
                  Last Post DynamicTest  
                  Started by ScottWalsh, Today, 06:52 PM
                  4 responses
                  36 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by olisav57, Today, 07:39 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post olisav57  
                  Started by trilliantrader, Today, 03:01 PM
                  2 responses
                  19 views
                  0 likes
                  Last Post helpwanted  
                  Started by cre8able, Today, 07:24 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X