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

Cancelling a trailingStop?

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

    Cancelling a trailingStop?

    Is it possible to cancel a setTrailingStop() on an order? I've tried about everything I could come up with using the documentation to do this. Basically what I'm trying to do it run a backtest using a strategy that uses trailing stops. Everytime a position exits, I want to cancel the trailing stop so that that trailing stop is not used for the next position.

    While debugging in VS, I realized that once the trailingStop is set, every time a new position is opened, it closes out immediately with a trail stop exit. So only the first entry works as expected and every other order gets stopped out immediately (at the same price as the entry fill price). The following code shows how I enter the trailing stop orders and how I try to cancel them. (only showing select relevant code here)


    Code:
                        if (quickScalpOrder != null && scalpTrailingStopActivated == false &&
                            (Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) - calculatedRTSlippage >= 70.00))
                        {
                            Print("Quick Scalp Trail Stop Set" + quickScalpOrder.ToString());
                            double stopLossVal = Instrument.MasterInstrument.Round2TickSize((Close[0] - Position.AvgPrice) / 2);
                            SetTrailStop("quickScalp", CalculationMode.Ticks, stopLossVal, false);
                            scalpTrailingStopActivated = true;                        
                        }
    
    
    
            protected override void OnExecution(IExecution execution)
            {
                if (execution.Name == "Trail stop" && execution.Order.FromEntrySignal == "quickScalp") 
                {
                    Print("Quick Scalp Trailing Stop Hit **** " + execution.ToString());
                    Print("\r\n");
                    CancelOrder(execution.Order);
                    quickScalpOrder = null;
                }
    
                else if (execution.Name == "Trail stop" && execution.Order.FromEntrySignal == "wideScalp")
                {
                    Print("Wide Scalp Trailing Stop Hit **** " + execution.ToString());
                    Print("\r\n");
                    CancelOrder(execution.Order);
                    wideScalpOrder = null;
                }
    
                if (quickScalpOrder == null && wideScalpOrder == null)
                {
                    CancelAllOrders();
                    breakEvenStopActivated = false;
                    scalpTrailingStopActivated = false;
                    wideTrailingStopActivated = false;
                    breakEvenStopLossVal = 0.0;
                }
            }

    #2
    Nitro, to cancel a trailing stop, it is necessary to submit the trailing stop as an IOrder and then you can cancel it directly:
    Code:
    IOrder longStopOrder;
    
    OnBarUpdate()
    {
        // to submit:
        longStopOrder = ExitLongStop(...);
    
        // to cancel:
        CancelOrder(longStopOrder);
    }
    AustinNinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply Austin.

      The setTrailStop() method doesn't return an IOrder object so I don't have an IOrder object to call CancelOrder() on. Basically, I want to be able to cancel the trailStop order that I set using setTrailStop() after a single occurrence of setTrailStop() executes. How can I go about doing that?

      Comment


        #4
        nitro, you will need to stop using SetTrailStop() and start using IOrders if you want to be able to cancel the order. Here is a reference sample that describes how to cancel an order.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Error when cancelling IOrder SetTrailStop

          Hi, I'm helping someone program a strategy and I'm trying to cancel a trailing stop order by attempting to follow the instructions provided, but I get the error code "Cannot Implicitly convert type 'void' to 'Ninjatrader.CBI.IOrder' when I try to create an IOrder using SetStopLoss() or SetTrailStop(). Creating an IOrder using EnterLongLimit() as provided in the sample does not return the error. I am not sure what I am doing wrong. Thanks for any help.
          Attached Files
          Last edited by vegasfoster; 08-22-2011, 05:20 PM.

          Comment


            #6
            vegasfoster, the Set()'s would not provide IOrder returns, thus they could not be cancelled or un-Set(). To track them you can capture their objects though in the OnOrderUpdate():

            BertrandNinjaTrader Customer Service

            Comment


              #7
              Ok, I think I need to back up. The strategy worked correctly until I added the breakeven and trailing stop code using the Sample Price Modification template http://screencast.com/t/pzm53k9mP6 and then we started getting this error http://screencast.com/t/1k8WsXP4. I was assuming that the breakeven and/or trail stop orders are not correctly cancelling on reversal. If that's not the case or I can't manually cancel then I would appreciate any suggestions on how to proceed cuz that's my only idea. Thank you again.

              Comment


                #8
                vegasfoster, those overfills can happen if work with too many competing too close to the actual inside market, thus you need to debug the strategy and event sequence seen to understand how to minimize the potential for impact here. The IOrder object would give you more control here as the Set()'s.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for the response, but I'm not trying to minimize errors. I'm intentionally placing market orders at entry and exit so the orders fill at any price to eliminate errors. Just to understand the problem, is NT getting confused because of the order in which the entries and exits are being filled, e.g. it's trying to buy on the entry before buying to cover the current short position? Thanks again.

                  Comment


                    #10
                    No, it's just like an inflight execution you could have in manual trading as well :

                    1. Place order
                    2. Cancel it
                    3. Order placed in 1 comes back filled, before you received any cancel confirmation
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Ok, that clarifies it. I still have no clue how to fix it, but it's perfectly clear.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
                      13 responses
                      48 views
                      0 likes
                      Last Post cmtjoancolmenero  
                      Started by sgordet, Today, 10:40 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post sgordet
                      by sgordet
                       
                      Started by RaygunWizzle, Today, 09:30 AM
                      2 responses
                      15 views
                      0 likes
                      Last Post bltdavid  
                      Started by Ashkam, Today, 09:28 AM
                      0 responses
                      13 views
                      0 likes
                      Last Post Ashkam
                      by Ashkam
                       
                      Started by gbourque, Today, 08:20 AM
                      1 response
                      112 views
                      0 likes
                      Last Post gbourque  
                      Working...
                      X