Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnExecution(IExecution execution) stop loss

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

    OnExecution(IExecution execution) stop loss

    HI. I use your sample OnOrderUpdate. Strategy place stop loss and stop limit correctly but move to breakeven long or short postion only not boht of them.
    It is depand on whih stop loss is typed first in code and whih one is second.
    If I put stop loss for long positon as first and stop loss for short position as second, only the second one is moved to breakeven. The first one is not moved.
    In the sample below only the stop loss for long position is moved to breakeven.
    Could you help me to find where the mistake is and what should I change??

    Czarek

    Code:
    /// <summary>
            /// Called on each incoming execution
            /// </summary>
            protected override void OnExecution(IExecution execution)
            {
     
       /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
       which ensures your strategy has received the execution which is used for internal signal tracking. */
       if (entryOrder != null && entryOrder.Token == execution.Order.Token)
       {
        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
        { 
         // Stop-Loss order 4 ticks below our entry price
         stopOrder  = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + stop * TickSize, "MyStopShort1", "MyEntryShort1");
     
         // Target order 8 ticks above our entry price
         targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - limit * TickSize, "MyTargetShort1", "MyEntryShort1");
     
     
         // Stop-Loss order 4 ticks below our entry price
         stopOrder  = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - stop * TickSize, "MyStopLong1", "MyEntryLong1");
     
     
         // Target order 8 ticks above our entry price
         targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + limit * TickSize, "MyTargetLong1", "MyEntryLong1");
         {
          entryOrder  = null;
         }
        }
     
       }

    #2
    The problem is you are using the same IOrder object for both of your orders. You want unique IOrder objects.

    stopOrderLong
    stopOrderShort
    instead of just stopOrder for both.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh.
      Thanks for promt it works.
      Now strategy work good for 1 or 2 entries per direction.
      Unfortunately if i want to set 3 entries sometimes strategy enlarge stop loss a lot.
      It happen if one or two of 3 entries hit the limit target and and new position is oppend before the third one is closed. The stop loss in this case is enlarged a lot.
      Probably the reason is because Position.AvgPrice is use.
      I try to scale out each entry price but there is some problem with syntax.
      I tryed to transform the method from ScaleOutSample
      Code:
      SetProfitTarget("Long 1a", CalculationMode.Ticks, 10);
      Probably OnOrderUpdate method need some changes.
      Could you tell if I'm right and show me how to scale out entry price in the below code.
      Czarek

      Code:
      if (Position.MarketPosition == MarketPosition.Short && Close[0] <= Position.AvgPrice - (breakeven * (TickSize/2)))
         {
          // Checks to see if our Stop Order has been submitted already
          if (stopOrderShort != null && stopOrderShort.StopPrice > Position.AvgPrice)
          {
           // Modifies stop-loss to breakeven
           stopOrderShort = ExitShortStop(0, true, stopOrderShort.Quantity, Position.AvgPrice - breakeventicks * TickSize , "MyStopShort1", "MyEntryShort1");
           stopOrderShort = ExitShortStop(0, true, stopOrderShort.Quantity, Position.AvgPrice - breakeventicks * TickSize , "MyStopShort2", "MyEntryShort2");
           stopOrderShort = ExitShortStop(0, true, stopOrderShort.Quantity, Position.AvgPrice - breakeventicks * TickSize , "MyStopShort3", "MyEntryShort3");
          }
         }
         if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (breakeven * (TickSize/2)))
         {
          // Checks to see if our Stop Order has been submitted already
          if (stopOrderLong != null && stopOrderLong.StopPrice < Position.AvgPrice)
          {
           // Modifies stop-loss to breakeven
           stopOrderLong = ExitLongStop(0, true, stopOrderLong.Quantity, Position.AvgPrice + breakeventicks * TickSize , "MyStopLong1", "MyEntryLong1");
           stopOrderLong = ExitLongStop(0, true, stopOrderLong.Quantity, Position.AvgPrice + breakeventicks * TickSize , "MyStopLong2", "MyEntryLong2");
           stopOrderLong = ExitLongStop(0, true, stopOrderLong.Quantity, Position.AvgPrice + breakeventicks * TickSize , "MyStopLong3", "MyEntryLong3");
          }
         }

      Comment


        #4
        Czarek,

        You can't use the same IOrder for each one. You need unique ones for every case. If you want to scale in and scale out you will need 6 IOrders. 3 for stoploss and 3 for profit target.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Is that mean that I need unique IOrder for each order whih are placed?
          For example for 1 entry I need :
          entryOrderLong1
          entryOderShort1
          stopOrderLong1
          stopOrderShort1
          targetOrderLong1
          targetOrderShort1
          For second entry per direction with end- 2 for example etc...

          Comment


            #6
            You need a unique IOrder for every unique type of order you wish to place. You want to scale in 3 times therefore you need a total of 9 IOrders if you wish to be capable of having all of them work at the same time.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I made unique IOrder for each order. For 4 entries per direction there are 24 unique IOrders, for long 12 and short 12 for three king of ordres: enter, target and stop.
              Now I'm having the problem to amend the targetOrder.
              I would like to move second entry's targetOrder to the first entry target order price. I want to close all possition in first entry target price.
              Example:
              first entry 100, targer limit 30, target price 130
              second entry 120, target limit 30, target price 150, move target price to 130.
              To do it I have to denote first entry price in code and add target value- and this is my problem.
              I think I have to replace Position.AvgPrice and put first entry price. But how to denote it.
              Position entry code is:
              Code:
              entryOrderLong1 = EnterLong(positionsize, "MyEntryLong1");
              Here is my code I'm trying to use but it does not work:
              Code:
              //Move target limit for long
                 if (Position.MarketPosition == MarketPosition.Long)
                 {
                  // Checks to see if our Limit Order has been submitted already
                  if (targetOrderLong2 != null && targetOrderLong2.LimitPrice > Position.AvgPrice)
                  { 
                  // Modifies target to targetLimitLong1
                   stopOrderLong2 = ExitLongLimit(0, true, targetOrderLong2.Quantity, Position.AvgPrice + limit * TickSize , "MyStopLong2", "MyEntryLong2");
                  }

              Comment


                #8
                You will have to debug it yourself. If you want to change the price, just use the same method call originally to submit the order but pass in a different price.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Could you show me how the code should looks like. Just sample of syntax.

                  Comment


                    #10
                    Sure, from your code here is a sample of modifying the limit price:

                    ExitLongLimit(0, true, targetOrderLong2.Quantity, newPriceHere , "MyStopLong2", "MyEntryLong2");
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Josh,Thanks a lot.
                      There is only one problem I have. How to denote newPrice???
                      It should be MyEntryLong1 price but I can not find the way how to denote it.
                      Czarek

                      Comment


                        #12
                        I'm not sure what you mean by denote. You can either place a double value in there or place a variable that contains a double value representing your price in there.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Josh.
                          Maybe my question are not too clear. Sorry, but my english is not so good.

                          If I put the duble value or variable represent the duble value, the price will be fixed but price have to be calculated form FIRST ENTRY PRICE to move second entry target limit price so code should contain: FIRST ENTRY PRICE + target limit.
                          Target limit is fixed for example 30ticks.
                          I do not know the code sequence what could represent FIRST ENTRY PRICE.

                          In the other worlds new price should equal FIRST ENTRY PRICE + target.
                          Questions is how to place FIRST ENTRY PRICE in code sentence?? Which code sequence would represent FIRST ENTRY PRICE?

                          Czarek

                          Comment


                            #14
                            Position.AvgPrice + 30 * TickSize
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              So code for second entry targetlimit looks like that:
                              Code:
                              stopOrderLong2 = ExitLongLimit(0, true, targetOrderLong2.Quantity,  Position.AvgPrice +30* TickSize , "MyStopLong2", "MyEntryLong2");
                              But unfortunately if I use above code the sequence "Position.AvgPrice" represent second entry price not FIRST ENTRY PRICE.

                              How to put instead "Position.AvgPrice" sequence represent FIRST ENTRY PRICE????

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              601 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              347 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              559 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              558 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X