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

Strategy Disabling and ignoring Order

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

    Strategy Disabling and ignoring Order

    Hello,

    I have been having problems because the strategy keeps disconnecting.

    I made a video of the problem: https://1drv.ms/v/s!AqPxJQ-JMIkyibUZ...8bF9g?e=8eaCpM

    I also have screenshots of the messages and the log file: https://1drv.ms/x/s!AqPxJQ-JMIkyibUU...lIfXQ?e=bvOGnJ

    Click image for larger version

Name:	Errors.png
Views:	146
Size:	16.3 KB
ID:	1111060

    Would you please help me? If you need the code, I will send it to you via email


    #2
    Hello Jorge.andres.o,

    Thanks for your post.

    Can you clarify what the Calculate setting is of the strategy? (Calculate.OnBarClose or Calculate.OnPriceChange or Calculate.OnEachTick)

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      This is the code that I have to handle the orders:

      private void PlaceShortEntries()
      {
      //store the entry bar number, used for entry timout
      barNumberOfShortEntries = CurrentBar;

      switch(OrdersToPlace)
      {
      case 01:
      EnterShortLimit(0, true, Convert.ToInt32(Order01LotSize), Close[0] + (Convert.ToInt32(Order01Offset) * TickSize), "SE01");
      break;
      }

      } //Place short entries end



      if (execution.MarketPosition == MarketPosition.Short)
      {
      switch (orderNumber)
      {

      case 1:
      stopOrder01 = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Price + Order01SL * TickSize, "SE01 Stop", "SE01");

      profitOrder01 = ExitShortLimit(0, true, execution.Order.Filled, Position.AveragePrice - Order01PT * TickSize, "SE01 Target", "SE01");
      break;
      }
      }
      } //Place bracket exits end

      private void CancelOpenOrders()
      {
      CancelOpenEntries();

      //reset entry bar to zero
      barNumberOfLongEntries = 0;
      barNumberOfShortEntries = 0;

      } //Cancel open orders end


      private void ShortReverse()
      {
      CancelOpenEntries();
      ExitShort();
      CancelBrackets();

      //reset short order bar to zero
      barNumberOfShortEntries = 0;

      } //Short Reverse end

      private void CancelOpenEntries()
      {
      CancelOrder(entryOrder01);
      CancelOrder(entryOrder02);
      CancelOrder(entryOrder03);
      CancelOrder(entryOrder04);
      CancelOrder(entryOrder05);
      CancelOrder(entryOrder06);
      CancelOrder(entryOrder07);
      CancelOrder(entryOrder08);
      CancelOrder(entryOrder09);
      CancelOrder(entryOrder10);

      } //Cancel Open Entries End


      private void CancelBrackets()
      {
      //Cancel any open brackets - there should be none
      CancelOrder(stopOrder01);
      CancelOrder(profitOrder01);

      ;

      //Reset Bracket Order Objects to null
      stopOrder01 = null;
      profitOrder01 = null;

      } //Cancel Brackets End


      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {

      if ( order.Name == "SE01 Stop" || order.Name == "SE01 Target")
      {
      //Reserved
      }

      // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.

      if (order.Name == "SE01")
      {
      entryOrder01 = order;

      // Reset the entryOrder object to null if order was cancelled without any fill
      if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
      {
      entryOrder01 = null;
      sumFilled01 = 0;
      }
      }

      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
      //Entry order 01
      if (entryOrder01 != null && entryOrder01 == execution.Order)
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
      {
      // We sum the quantities of each execution making up the entry order
      sumFilled01 += execution.Quantity;

      // Submit exit orders for partial fills
      if (execution.Order.OrderState == OrderState.PartFilled)
      {
      PlaceBracketExits(01, execution, marketPosition);
      }
      // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
      else if (execution.Order.OrderState == OrderState.Filled && sumFilled01 == execution.Order.Filled)
      {
      PlaceBracketExits(01, execution, marketPosition);
      }

      // Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled
      if (execution.Order.OrderState != OrderState.PartFilled && sumFilled01 == execution.Order.Filled)
      {
      entryOrder01 = null;
      sumFilled01 = 0;
      }
      }
      }


      Comment


        #4
        This is the calculate setting:

        Calculate = Calculate.OnBarClose;

        Comment


          #5
          Hello Jorge.andres.o ,

          Thanks for your replies.

          Can you clarify if your strategy is also using the SetTrailStop() method?
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Paul,

            You are ahead of the game. Currently, I am not using it. However, I am planning on insetting this part:

            protected override void OnBarUpdate()
            {
            // Resets the stop loss to the original value when all positions are closed
            if (Position.MarketPosition == MarketPosition.Flat)
            {
            //SetStopLoss(CalculationMode.Ticks, 200);
            stopOrder01 = ExitShortStopMarket(0, true, Convert.ToInt32(Order01LotSize), Position.AveragePrice + Order04SL * TickSize, "SE01 Stop", "SE01");
            }

            // If a long position is open, allow for stop loss modification to breakeven
            if (Position.MarketPosition == MarketPosition.Short)
            {
            // Once the price is greater than entry price-20 ticks, set stop loss to breakeven
            if (Close[1] < Position.AveragePrice - 20 * TickSize)
            {

            SetTrailStop("SE01", CalculationMode.Ticks, (Atr[1] * StopATR), false);
            //or this order: stopOrder01 = ExitShortStopMarket(0, true, Convert.ToInt32(Order01LotSize), trendEMA2[0], "SE01 Stop", "SE01");

            }
            }

            Comment


              #7
              Hello Jorge.andres.o,

              Thanks for your reply.

              The Log is showing these stop prices being submitted in this sequence

              10702.50
              10702.25
              10702.00
              10701.75
              10670.50
              10701.75

              So I suspect the sudden change from 10701.75 to 10670 was what ultimately caused the issue of "price can't be changed below the market".

              You would need to ensure that your logic is checking the current price against what you are calculating for the stop price.

              Concerning the SetTrailStop(), please make sure you review all of the notes with this method: https://ninjatrader.com/support/help...ttrailstop.htm, In particular, is this note: "Trail stop orders are modified based on the strategies 'Calculate' settings. In the case of 'Calculate' on bar close, when the bar closes the trail stop order modification will occur using the closing price of the bar as the reference price to apply the trail offset. Subsequently if the open price of the next bar is significantly higher or lower then the current close price then there is a possibility that the calculated trail stop price is now an invalid stop price. This is a risk with modifying any stop order closer to the current market price since any modification above/below the current price would be rejected."

              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Paul,

                Would you explain to me, how I "to ensure that your logic is checking the current price against what you are calculating for the stop price." or where can I find this information?

                Comment


                  #9
                  Hello Jorge.andres.o,

                  Thanks for your reply.

                  You would need your strategy to run with Calculate.OnpriceChange or Calculate.OnEachTick as in either of those modes Close[0] will represent the live price of the currently forming bar. So you would check the value of Close[0] against what you intend to move your order to. Keep in mind this note from the SetTrailStop() as it applies to all stop orders that are moved, "This is a risk with modifying any stop order closer to the current market price since any modification above/below the current price would be rejected."

                  If you need some parts of your code to only run once per bar and other parts on each tick (or on price change), here is an example that shows this: https://ninjatrader.com/support/help...either_cal.htm
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Paul,

                    I apologize for keeping asking basic questions. I checked the information you sent me, and still, I am not able to figure out the positioning and code for: check the value of Close[0] against what you intend to move your order to.

                    Will I need to create a code before each command to submit/change an order?

                    Would you please elaborate it more or guide me in more detail?

                    Comment


                      #11
                      Hello Jorge.andres.o,

                      Thanks for your reply.

                      Here is a simple example:

                      If in a short position and you want to move your stop (which is above price) closer

                      if (Close[0] < your intended new stop position)
                      {
                      //modify stop to your intended new stop position
                      }

                      You may want to give the stop some breathing room to avoid sudden price changes so you could add an offset, say 5 ticks, like this

                      if (Close[0]+5 * TickSize < your intended new stop position)
                      {
                      //modify stop to your intended new stop position
                      }
                      Paul H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by DJ888, 04-16-2024, 06:09 PM
                      4 responses
                      12 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by terofs, Today, 04:18 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post terofs
                      by terofs
                       
                      Started by nandhumca, Today, 03:41 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post nandhumca  
                      Started by The_Sec, Today, 03:37 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post The_Sec
                      by The_Sec
                       
                      Started by GwFutures1988, Today, 02:48 PM
                      1 response
                      9 views
                      0 likes
                      Last Post NinjaTrader_Clayton  
                      Working...
                      X