• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse Position / Close position Stop loss conflicts

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

    Reverse Position / Close position Stop loss conflicts

    I have an issue where, under conditions where a position reversal occurs and the Close Position order is used to close an open position, extant stop loss orders remain active and immediately trigger upon order placement of the new, reversed, position's order.

    I have looked at the CancelOrder() and SetStopLoss sections of the manual. I also have looked through the Order, OnOrderUpdate, and OnExecutionUpdate parts of the manual. I am confused as to how to proceed, because the code does not SEEM to be working on historical data.

    The relevant code I have so far is below. I am unsure how to proceed from here. For reference what happens is I will be short, and a bar pattern will occur triggering a long position. The short position will close with an execution called Close position, submit the long, fill, and then the old short position stop loss will activate. I am trying to cancel it to reset upon submittal...there has to be something I am doing wrong.

    Code:
    private Order stoplossorder
    
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
    if ((order.FromEntrySignal == "Stop loss")
    && order.OrderState == OrderState.Initialized)
    {
    stoplossorder = order;
    }
    }
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    if (execution.Name == "Stop loss" || execution.Name == "Close position")
    {
    CancelOrder(stoplossorder);
    }

    #2
    Hello liquid150,

    Thank you for the post.

    You should make sure the stop loss for the previous order is canceled before you submit a reversal. Please see the reference sample linked below. It is an exemplary approach to using CancelOrder in your script.

    https://ninjatrader.com/support/help...thod_to_ca.htm

    Also, note that SetStopLoss and SetProfitTarget will handle the OCO logic for you and can be called dynamically in OnBarUpdate.

    Please let me know if I can assist further.

    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hello liquid150,

      Thank you for the post.

      You should make sure the stop loss for the previous order is canceled before you submit a reversal. Please see the reference sample linked below. It is an exemplary approach to using CancelOrder in your script.

      https://ninjatrader.com/support/help...thod_to_ca.htm

      Also, note that SetStopLoss and SetProfitTarget will handle the OCO logic for you and can be called dynamically in OnBarUpdate.

      Please let me know if I can assist further.
      I am already using SetStopLoss in OnBarUpdate code, thinking its standard operating code would work for flipped positions, but it appears the order of operations which occurs in this specific scenario does not play well with this command under these very specific, admittedly uncommon, conditions.

      It is good to know I need to ensure the stop loss for previous orders are canceled before submission of reversal. I will see what I can do elsewhere in the code to try to accomplish this cancellation earlier. Based on your post, it should be sufficient to create a second set of conditions for each potential reversal (long and short) order. I will try adding position condition calls to one of each set, and try performing the order cancellation the line before the new order placement. I will let you know my results.

      If this more simple way of solving the problem doesn't work, I will try implementing the schema shown in the linked download.

      Comment


        #4
        Well it does not seem the simple solution worked, either, in backtest. I will see about using the file setup.

        Comment


          #5
          Commentary on progress, or lack thereof. Realized I will have to use a list of orders and probably multiple stop order objects to accomplish this due to the nature of my strategy.

          I can't seem to get Stop loss orders passed to variable order objects, no matter what I do. Is there a conflict using SetStopLoss and this methodology at the same time? I can't even pass the stop loss orders to a list using this code in OnOrderUpdate with appropriate list variable callout and list creation during configuration:

          Code:
          if (order.OrderState == OrderState.Initialized && order.Name == "Stop loss"
                          )
                      {
                          stopLossOrders.Add(order);
                          Print("Stop Loss Orders are " + stopLossOrders);
                      }
          The print never appears in Output. I am using Simulated Stops, so I have to stick with Initialized orders...but changing them from Simulated to not has no effect, anyway.

          I am calling "Stop loss" based on the documentation for SetStopLoss. I would like to figure out how to get past this first assignment step, because everything depends on it.

          Comment


            #6
            Hello liquid150,

            Thanks for the reply.

            Orders made with SetStopLoss should not be modified directly. If you want to change the stop loss value, simply call SetStopLoss again before you make the next order. See the sample linked below for a demonstration of that.

            https://ninjatrader.com/support/help...of_stop_lo.htm

            You can do something like the following to change your stop easily, this is a modification of the SampleMACrossover strategy:

            Code:
            protected override void OnBarUpdate()
                    {
                        if (CurrentBar < BarsRequiredToTrade)
                            return;
            
                        if (CrossAbove(smaFast, smaSlow, 1))
                        {
                            //Do stop price calculation logic above this line
                            SetStopLoss(CalculationMode.Ticks, 10);
                            EnterLong();
                        }
                        else if (CrossBelow(smaFast, smaSlow, 1))
                        {
                            //Do stop price calculation logic above this line
                            SetStopLoss(CalculationMode.Ticks, 5);
                            EnterShort();
                        }
                    }
            If you need direct control of the stop-loss order, use ExitLongStopMarket() or ExitLongStopLimit() (for long orders). With these methods, you can assign order object as the "Using CancelOrder" example demonstrates. Please also see this example that shows how to properly set up protective orders. It shows how to set orders to a variable, and how to cancel those orders properly:

            https://ninjatrader.com/support/help...and_onexec.htm

            Please let me know if I can assist further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thanks Chris,

              I'll see about substituting ExitLongStopMarket and the equal command for shorts for my SetStopLoss commands. I already have functional code moving the stop loss when I want, but thanks.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by TikDaddy, Today, 02:08 PM
              2 responses
              8 views
              0 likes
              Last Post NinjaTrader_JoshG  
              Started by Brillo, Today, 01:52 PM
              1 response
              13 views
              0 likes
              Last Post NinjaTrader_Jim  
              Started by TazoTodua, 02-09-2018, 08:40 AM
              10 responses
              881 views
              0 likes
              Last Post NinjaTrader_JoshG  
              Started by TazoTodua, Yesterday, 04:35 AM
              6 responses
              18 views
              0 likes
              Last Post eDanny
              by eDanny
               
              Started by MattP, Today, 12:25 PM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_PatrickG  
              Working...
              X