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

Backtest MIT order executing too soon?

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

    Backtest MIT order executing too soon?

    My goal with this is to generate an MIT order and move it with my price as a trailing exit, but using increments. I can do this with a price and then create an exit as needed but I want to try using a stop loss type exit. I would do a stoploss, but they don't have both entry and exit signals, which my strategy relies on. is MIT the wrong order type? Can it be below the current price?

    The conditions for creating an MIT order look like this, where I basically wait for the right time and submit a request to another function to submit the order.

    Code:
    if (orderRecordMaster.OrderStatsProfit > profitHardFloorP
                                        && orderRecordMaster.OrderStatsProfit > orderRecordMaster.OrderStatsTrailingStopProfit
                                        && orderRecordMaster.OrderStatsProfit < targetTakeProfit)
                                    {
                                        if (orderRecordMaster.ExitOrder == null)
                                        {
                                            orderRecordMaster.OrderStatsTrailingStop = GetCurrentAsk(0) - (profitHardFloorP * TickSize);
    
                                            double stop = (double)orderRecordMaster.OrderStatsTrailingStop;
                                            double limit = stop - stopLimitSpread;
    
                                            orderRecordMaster.OrderStatsTrailingStopProfit = orderRecordMaster.OrderStatsProfit;
                                        
                                            // Add MIT exit order
                                            ExitOrderFunction(orderRecordMaster.EntryOrder.Quantity, orderRecordMaster, "Add EXIT $", false, (orderRecordMaster.EntryOrder.OrderAction == OrderAction.SellShort ? ExitOrderType.ExitShortStopProfitable : ExitOrderType.ExitLongStopProfitable), OrderType.MIT, stop, limit);
                                        }​
    in ExitOrderFunction, the order submission looks like this

    Code:
        if(orderType == OrderType.MIT)
                                        {
                                          ExitLongMIT(0,true,QuantityVal,stopPrice,thisOrderRecordMaster.ExitOrderUUID, thisOrderRecordMaster.EntryOrderUUID);
                                         }​
    Then in OnOrderUpdate
    Code:
    if(order.OrderAction == OrderAction.Sell)
                    {
                        if(order.OrderType == OrderType.MIT && order.OrderState == OrderState.Working)
                        {
                            Draw.Dot(this, "dbffsdgwgf"+CurrentBar, true, 0,order.StopPrice, Brushes.Orange);
                        }​
    In the attached image, what you see is:
    The red dot is the MIT order being created, from OnOrderUpdate()
    The orange dot is the MIT order being filled, from OnOrderUpdate()

    I'm not modifying the order, so why isn't it waiting until price drops below the orange dot to fill the order? I've tried all time in force, even though it shouldn't matter, and the order should be open perpetually.
    Attached Files
    Last edited by Skifree; 03-12-2024, 12:15 PM.

    #2
    Hello Skifree,

    A sell market if touched order would be placed above the current market price or it would fill immediately.

    May I confirm this order is being submitted above the current bid price?

    If you print stopPrice and GetCurrentBid() one line above where the order is submitted, does the output show the stopPrice is greater than the current bid?

    Where you intending to submit a sell stop market order below the market price instead?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      A sell market if touched order would be placed above the current market price or it would fill immediately.
      This is the problem then. Is StopLimit the best way to manage the stoploss behaviour, without using Set()?

      Comment


        #4
        Hello Skifree,

        You could use a sell stop market order below the market price with ExitLongStopMarket() or a stop limit order below the market with ExitLongStopLimit().

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Can either of those be changed? I'm having trouble with ChangeOrder in that the states for Change are not reflecting the request to do so in OnOrderUpdate OrderState ChangePending or ChangeSubmitted?

          Code:
           if (SMA1[0] > SMA1[1])// && orderRecordMaster.OrderStatsTrailingStop < GetCurrentAsk(0))
                                                      {
                                                          // Update the MIT exit order
                                                          
                                                          double newStop = (double)orderRecordMaster.OrderStatsTrailingStop+0.005;//Math.Min((double)orderRecordMaster.OrderStatsTrailingStop + (Math.Abs(SMA1[0] - SMA1[1])/2) * orderRecordMaster.ExitOrder.Quantity * Bars.Instrument.MasterInstrument.PointValue, GetCurrentAsk(0));
                                                          double limit = newStop - stopLimitSpread;
                                                          orderRecordMaster.OrderStatsTrailingStop = (double?)newStop;
                                                      
                                                          if(newStop < GetCurrentAsk(0))
                                                          {
                                                          //Draw.TriangleUp(this, "fghdsdgf"+CurrentBar, true, 0,newStop, Brushes.Green);
                                                          ChangeOrder(orderRecordMaster.ExitOrder, orderRecordMaster.EntryOrder.Quantity, limit, newStop);
                                                          }
                                                      }​

          Comment


            #6
            Hello Skifree,

            Yes, they can have the prices updated. You can call the method again using the same signal name with a new price and this will adjust the order.

            Or you can assign the order to a variable in OnOrderUpdate(), and then supply the order variable to ChangeOrder() (in any method) with the new price.

            The ProfitChaseStopTrailExitOrdersExample and ProfitChaseStopTrailUnmanagedExample examples demonstrate.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rhyminkevin, Today, 04:58 PM
            1 response
            42 views
            0 likes
            Last Post Anfedport  
            Started by iceman2018, Today, 05:07 PM
            0 responses
            5 views
            0 likes
            Last Post iceman2018  
            Started by lightsun47, Today, 03:51 PM
            0 responses
            7 views
            0 likes
            Last Post lightsun47  
            Started by 00nevest, Today, 02:27 PM
            1 response
            14 views
            0 likes
            Last Post 00nevest  
            Started by futtrader, 04-21-2024, 01:50 AM
            4 responses
            49 views
            0 likes
            Last Post futtrader  
            Working...
            X