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

Script thinks order filled before it really filled

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

    Script thinks order filled before it really filled

    Hello. In my script
    Code:
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    				{
    					DrawText("tag2", "Filled ", 0, Low[1] - 100*TickSize, Color.Blue);
    ...
    As you can see on print-screen, order appeared, but it was filled on next day (price has reached it). BUT script think that it was filled on day when order was placed.
    Click image for larger version

Name:	filled order.PNG
Views:	1
Size:	30.6 KB
ID:	900587
    I need stop order to be placed ONLY when entry order filled.

    #2
    Hi,

    When backtesting a Stop Order type, the fill price will be the next bar high/low/open.

    This means the fill would have occurred on the bar it's drawing on, but not until that bar closes, as a result the execution is displayed on the next bar.

    For your custom draw method, you can account for this by drawing it on bar -1

    DrawText("tag2", "Filled ", -1, Low[1] - 100*TickSize, Color.Blue);
    MatthewNinjaTrader Product Management

    Comment


      #3
      Originally posted by NinjaTrader_Matthew View Post
      Hi,

      When backtesting a Stop Order type, the fill price will be the next bar high/low/open.

      This means the fill would have occurred on the bar it's drawing on, but not until that bar closes, as a result the execution is displayed on the next bar.

      For your custom draw method, you can account for this by drawing it on bar -1

      DrawText("tag2", "Filled ", -1, Low[1] - 100*TickSize, Color.Blue);
      Mathew, Could you elaborate on your answer to alexstox regarding the following. If the order is filled on the bar as stated but not drawn until the next bar: if the next bar gaps way up or down so it does not overlap the fill bar will it show the fill price hanging in mid air ? I don't mean to be rude by impinging on alexstox's thread but I thought it was related. Alextox if your offended I apologize.

      Jerry

      Comment


        #4
        Not that!
        1. As you see stop order (red dot) was placed before order was filled! I need to place Stop Order AFTER order filled (not earlier)
        2. How to reduce dot size?
        Last edited by alexstox; 10-24-2013, 01:59 PM.

        Comment


          #5
          JerryWar,

          The stop order would have been triggered on the previous bar, but the fill/execution occurs on the next. This could result in slippage if there was a gap on the bar. You're free to change the fill type behavior should you want to to work differently.

          If you navigate to (My) Documents\NinjaTrader 7\bin\Custom\Type\ and open the @DefaultFillType youw ill see the logic used for all order types. Specific to a Stop order type:

          Code:
          	else if (order.OrderType == OrderType.Stop)
          			{
          				// Stop orders are triggered when traded at the stop price
          				double nextLow	= NextLow;
          				double nextHigh = NextHigh;
          				double nextOpen	= NextOpen;
          				if ((order.OrderAction == Cbi.OrderAction.Buy					&& order.StopPrice <= nextHigh + epsilon)
          						|| (order.OrderAction == Cbi.OrderAction.BuyToCover	&& order.StopPrice <= nextHigh + epsilon)
          						|| (order.OrderAction == Cbi.OrderAction.Sell			&& order.StopPrice >= nextLow - epsilon)
          						|| (order.OrderAction == Cbi.OrderAction.SellShort	&& order.StopPrice >= nextLow - epsilon))
          				{
          					if (order.OrderAction == Cbi.OrderAction.Buy || order.OrderAction == Cbi.OrderAction.BuyToCover)
          						FillPrice = order.StopPrice < nextOpen - epsilon ? Math.Min(nextHigh, nextOpen + SlippagePoints) : Math.Min(nextHigh, order.StopPrice + SlippagePoints);
          					else
          						FillPrice = order.StopPrice > nextOpen + epsilon ? Math.Max(nextLow, nextOpen - SlippagePoints) : Math.Max(nextLow, order.StopPrice - SlippagePoints);
          				}
          			}
          MatthewNinjaTrader Product Management

          Comment


            #6
            Originally posted by NinjaTrader_Matthew View Post
            Hi,
            When backtesting a Stop Order type, the fill price will be the next bar high/low/open.
            3. Of course, so as to my print-screen stop order should be placed on bar when Entry Order was filled, and not 1 bar earlier as script did.

            Comment


              #7
              To clarify, your inquiry has nothing to do with the blue fill text?
              MatthewNinjaTrader Product Management

              Comment


                #8
                Originally posted by NinjaTrader_Matthew View Post
                To clarify, your inquiry has nothing to do with the blue fill text?
                NO! It's only for debugging. I need stop order to be placed ONLY when entry order filled.

                Oh my God! The further I go into writing the script, the more dense and untrodden forest becomes ...

                Comment


                  #9
                  Thanks for the clarification.

                  In that case, you should wait until the actual position has been updated and put your stop loss in the OnBarUpdate section to account for when the order fills on that day bar

                  Code:
                  if(Position.MarketPosition == MarketPosition.Long)
                  	//set stop
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    Originally posted by NinjaTrader_Matthew View Post
                    Thanks for the clarification.

                    In that case, you should wait until the actual position has been updated and put your stop loss in the OnBarUpdate section to account for when the order fills on that day bar

                    Code:
                    if(Position.MarketPosition == MarketPosition.Long)
                    	//set stop
                    This will work only in OnBarUpdate? And won't work in OnExecution?

                    Comment


                      #11
                      No because the execution event happens on the previous bar which is putting the stop in before the position is taken on the next bar. You'd have to wait until the next bar event.
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        So, in NT execution of order mean that order is executed if it placed. But there is no difference between filled and not filled. Because after placing stop order for enter, it can be not filled for some period, but OnExecution() fill it though in bar when entry order was placed.

                        Comment


                          #13
                          I'm not sure I understand that question - but the reason for this behavior is that the orders/executions are processes at the close of the bar. As you're using a single daily data point, the order price is filled on the next bar.

                          One thing you may consider is programming in a smaller time frame and executing orders on that time frame, while taking signals from your primary data series as you have been doing:

                          You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            Let me clarify what I mean. Please look at print-screen in 1st post. You can see there aqua color line. When it started - order is placed. On next bar script place Stop Loss order for exit!!! BUT in condition script might do this AFTER entry order is filled! Entry order filled only on next bar (blue triangle and blue arrow as default in NT). So, why script placed stop-loss order if entry order was not filled yet? Is it clear? or my English is so bad

                            Comment


                              #15
                              Hello,

                              If I'm understanding correct your using OnExecution to place your stop order.

                              Think of OnExecution as being triggered for your order intra bar. The daily candle that you see your order being filled on has not yet closed. Because of this you stop level is being based off of the most recent closed bar. This is where you see the read dot.

                              In real time if CalculateOnBarClose = true you would see the same result.

                              If you wanted OnExecution() to place your stop based off of the current real time low you would need COBC = false or use a multi time frame script to backtest properly.

                              Alternatively, after setting your stop in OnExeuction you could then update your stop to the new value in OnBarUpdate by adjusting the stop level.

                              For an example of setting the initial stop in OnExecution and then adjusting it in OnBarUpdate: http://www.ninjatrader.com/support/f...ead.php?t=7499

                              Let me know if I can further assist.
                              LanceNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,406 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              98 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              160 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X