Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trail Stop Loss in UnManaged Order

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

    Trail Stop Loss in UnManaged Order

    My Stop Loss is created and then should be moved. Basicall it doesn't appear to move and I get taken out at a loss.

    See the code below please


    ************ Stop Loss Created 2 Ticks Below Closed Price *******************
    protected override void OnExecution(IExecution execution)
    {
    if // we need a Stop loss to protect the long Position
    (longOrder != null && longOrder == execution.Order && execution.Order.OrderState == OrderState.Filled)

    {
    // (2)
    stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, tickPrice +2 * TickSize, tickPrice +2 * TickSize, "Oil", "Short limit entry");



    ******************* This should move the Stop Loss *************************

    protected override void OnBarUpdate()
    {

    if(Position.MarketPosition == MarketPosition.Long)
    {
    ChangeOrder(stopOrder, 1, stopOrder.LimitPrice, tickPrice +1 * TickSize);

    #2
    Hi cocopod,

    Thanks for the post.

    Regardless of where the stop is placed, the order is going to fill at the price of the limit or better. You are not changing the limit price here.

    The stop places a limit after touched and the order will fill at the limit price or better.


    If the limit price is the correct price and you want to ensure that the stop price is being moved, add TraceOrders = true; to the Initialize() part of the script so we can see if things are being ignored. Then I'd like to ensure that the ChangeOrder() is being called when you expect it.

    Is this ChangeOrder() being called in historical data or in real time in your testing?

    Please add the following print one line above where ChangeOrder() is called.

    if you are testing this with historical data (meaning calculate on bar close is true) add:
    Print(Time[0]+" - change order called set limit to: "+stopOrder.LimitPrice+", set stop price to: "+(tickPrice +1 * TickSize));

    if you are testing this in real time with CalculateOnBarClose set to false add:
    Print(DateTime.Now+" - change order called set limit to: "+stopOrder.LimitPrice+", set stop price to: "+(tickPrice +1 * TickSize));
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Got this

      Hi Chelsea,

      I pasted part of the Output file below. Can send the entire thing if you wish.

      The Bracket Pending order was created
      The Position was filled
      The Stop Loss was created and eventually filled at what appears to be the creation level. It did not appear to move
      Another Bracket trade was created but its action was not as what I would have expected





      Changing unmanaged order at 14/11/2014 16:17:28: Order='3567b28b099f4767b696856b35a62f3f/Sim101' Name='Short limit entry' State=PendingCancel Instrument='CL 12-14' Action=SellShort Limit price=74.87 Stop price=74.84 Quantity=1 Strategy='TESTprog' Type=StopLimit Tif=Gtc Oco='Oil' Filled=0 Fill price=0 Token='3567b28b099f4767b696856b35a62f3f' Gtd='01/12/2099 00:00:00'
      14/11/2014 16:17:28 Entered internal SubmitOrder() method at 14/11/2014 16:17:28: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=74.89 StopPrice=74.89 OcoId='Oil' Name='Short limit entry'
      **NT** Strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
      14/11/2014 16:17:28 CancelAllOrders: BarsInProgress=0
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87
      14/11/2014 16:17:28 - change order called set limit to: 74.89, set stop price to: 74.87

      Comment


        #4
        Hi cocopod,

        What price is the order filling at?

        Is this ChangeOrder() being called in historical data or in real time in your testing?

        Add the following print so that we can see the fills as well.

        protected override void OnExecution(IExecution execution)
        {
        Print(Time[0]+" - "+execution.ToString());
        }


        Also, it looks like you have a rejected order, is this coming from the same script?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi Again,

          The data is real Time and the rejected order is coming from the same script.. I have additionally included ****** where you requested the line
          Print(Time[0]+" - "+execution.ToString()); To make for easier reading

          **NT** Enabling NinjaScript strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=True EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
          14/11/2014 17:57:27 Entered internal SubmitOrder() method at 14/11/2014 17:57:27: Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=75.42 StopPrice=75.42 OcoId='Oil' Name='long limit entry'
          14/11/2014 17:57:27 Entered internal SubmitOrder() method at 14/11/2014 17:57:27: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=74.96 StopPrice=74.96 OcoId='Oil' Name='Short limit entry'
          14/11/2014 17:58:48 CancelAllOrders: BarsInProgress=0
          **NT** Disabling NinjaScript strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d'
          **NT** Enabling NinjaScript strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=True EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=False MaxRestarts=4 in 5 minutes
          14/11/2014 17:58:51 Entered internal SubmitOrder() method at 14/11/2014 17:58:51: Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=75.47 StopPrice=75.27 OcoId='Oil' Name='long limit entry'
          14/11/2014 17:58:51 Entered internal SubmitOrder() method at 14/11/2014 17:58:51: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=75.01 StopPrice=75.21 OcoId='Oil' Name='Short limit entry'
          Execution Time:*******14/11/2014 18:00:00 - Execution='ac00995520b14adab057104daabee1ef' Instrument='CL 12-14' Account='Sim101' Name='long limit entry' Exchange=Default Price=75.27 Quantity=1 Market position=Long Commission=0 Order='d8a9303091de4762b4708d7a253f7e98' Time='14/11/2014 17:59:11'
          14/11/2014 17:59:11 Entered internal SubmitOrder() method at 14/11/2014 17:59:11: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=75.29 StopPrice=75.29 OcoId='Oil' Name='Short limit entry'
          **NT** Strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests, attempted to close the position and terminated itself.
          14/11/2014 17:59:11 CancelAllOrders: BarsInProgress=0
          14/11/2014 17:59:11 Entered internal PlaceOrder() method at 14/11/2014 17:59:11: BarsInProgress=0 Action=Sell OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Close' FromEntrySignal=''
          Execution Time:*******14/11/2014 18:00:00 - Execution='784f051a76854c29a6458ef51dc05af0' Instrument='CL 12-14' Account='Sim101' Name='Close' Exchange=Default Price=75.26 Quantity=1 Market position=Short Commission=0 Order='8dc76b7e8d7448a5858a7f86c3ab786d' Time='14/11/2014 17:59:12'
          14/11/2014 17:59:12 Entered internal SubmitOrder() method at 14/11/2014 17:59:12: Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=75.49 StopPrice=75.49 OcoId='Oil' Name='long limit entry'
          14/11/2014 17:59:12 Entered internal SubmitOrder() method at 14/11/2014 17:59:12: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=75.03 StopPrice=75.03 OcoId='Oil' Name='Short limit entry'
          **NT** Strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d' submitted a cancellation request for Order ID '1e9a8da3ca7f46db87c2e2bede803752' has not been confirmed cancelled. Please check your account orders and positions.
          14/11/2014 17:59:19 Disable() called: strategy disabled
          14/11/2014 17:59:19 CancelAllOrders: BarsInProgress=0
          **NT** Disabling NinjaScript strategy 'OilSpike/a113e0c6bd9b4532a9145b5405721c0d'

          Comment


            #6
            Hi cocopod,

            The rejection is likely caused by re-using an OcoID string after an order has already filled using that OcoID.

            4/11/2014 17:58:51 Entered internal SubmitOrder() method at 14/11/2014 17:58:51: Action=Buy OrderType=StopLimit Quantity=1 LimitPrice=75.47 StopPrice=75.27 OcoId='Oil' Name='long limit entry'

            14/11/2014 17:58:51 Entered internal SubmitOrder() method at 14/11/2014 17:58:51: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=75.01 StopPrice=75.21 OcoId='Oil' Name='Short limit entry'

            Execution Time:*******14/11/2014 18:00:00 - Execution='ac00995520b14adab057104daabee1ef' Instrument='CL 12-14' Account='Sim101' Name='long limit entry' Exchange=Default Price=75.27 Quantity=1 Market position=Long Commission=0 Order='d8a9303091de4762b4708d7a253f7e98' Time='14/11/2014 17:59:11'

            After this execution the OcoID is used again and that causes the rejection:
            14/11/2014 17:59:11 Entered internal SubmitOrder() method at 14/11/2014 17:59:11: Action=SellShort OrderType=StopLimit Quantity=1 LimitPrice=75.29 StopPrice=75.29 OcoId='Oil' Name='Short limit entry'

            That said, no where in this output do I see 'change order called set limit to:'. This means that the ChangeOrder() was not called a single time in this run.

            The price isn't going to change unless ChangeOrder() is called and a new price is supplied.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Actually When I created teh Stop Limit order I purposely used the same ocoid as I did with the Long / Short order.

              could this be it?

              Comment


                #8
                Hi cocopod,

                The issue is not that you are using an OcoID for two orders, the issue is that you are re-using an OcoID after an order has filled that is using that OcoID.

                Each set of oco orders has to have a unique string. So every time you create a new entry and new set of stops, you need a new OcoID string.

                If you use an OcoID that was already used on an order that has filled, this will be rejected.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Oh nooo...

                  when the first order is filled and finally stopped out I create another one. The number of times this process can be repeated is actually indeterminate.
                  Do I therefore have to add some sort of counter to the ocoid?? How do I do this?

                  Looks like we're both working late today

                  Comment


                    #10
                    Hi cocopod,

                    Absolutely yes, you need a counter. You need to keep each pairing of Oco separate by using different OcoID strings.

                    Are you familiar with how oco works?

                    Whatever uses the same oco string is grouped with the oco. If you continue adding orders using the same oco then everything is in one big oco.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      That is very helpful... Actually Chelsea I'v learnt more from you today from everyone else put together... and the night is still young...

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      627 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      359 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      105 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      562 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      567 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X