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

Stop loss not working

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

    #16
    You could set for example a bool / flag variables if you see those having OrderState.Cancelled in your OnOrderUpdate() and only then do the reverse orders with limits.

    Another, easier approach is using market orders to reverse here - they would not be impacted by the order handling rules in the managed approach.
    BertrandNinjaTrader Customer Service

    Comment


      #17
      Originally posted by NinjaTrader_Bertrand View Post
      Another, easier approach is using market orders to reverse here - they would not be impacted by the order handling rules in the managed approach.
      I don't see how this applies. I'm not having a problem entering in the opposition direction: when my stop fires off exiting me from a long, my position does reverse and I go short. What doesn't happen is my stop loss exit for the short position isn't entered.

      Comment


        #18
        Originally posted by NinjaTrader_Bertrand View Post
        You could set for example a bool / flag variables if you see those having OrderState.Cancelled in your OnOrderUpdate() and only then do the reverse orders with limits.
        I attempted this with the following code:

        //use this method to listen for stop order execution and execute reversal orders as appropriate
        protected override void OnExecution(IExecution execution)
        {
        if(longStopOrder != null && longStopOrder == execution.Order && shortOnReversal)
        {
        CancelOrder(longTargetOrder);
        }

        if(shortStopOrder != null && shortStopOrder == execution.Order && longOnReversal)
        {
        CancelOrder(shortTargetOrder);
        }

        }

        /// <summary>
        /// Called on each incoming order event
        /// </summary>
        protected override void OnOrderUpdate(IOrder order)
        {
        // When a position is stopped out, the target order is canceled in OnExecution. Check for that condition, and then place entry orders
        if (longTargetOrder != null && longTargetOrder == order && shortOnReversal
        && order.OrderState == OrderState.Cancelled && order.Filled == 0)
        {
        EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
        shortStopOrder = ExitShortStop(shortStop, "short_entry");
        shortTargetOrder = ExitShortLimit(shortPT, "short_entry");

        }

        if (shortTargetOrder != null && shortTargetOrder == order && longOnReversal
        && order.OrderState == OrderState.Cancelled && order.Filled == 0)
        {
        EnterLongStopLimit(longEntry, longEntry, "long_entry");
        longStopOrder = ExitLongStop(longStop, "long_entry");
        longTargetOrder = ExitLongLimit(longPT, "long_entry");
        }
        }
        I'm still seeing the same behavior. My entry order goes through, but my ExitShortStop does not.

        I've attached the strategy. Please advise.
        Attached Files

        Comment


          #19
          I thought I'd try using the Set's for exits, and not issuing those commands until after the entry orders fired via OnExecution:

          //use this method to listen for stop order execution and execute reversal orders as appropriate
          protected override void OnExecution(IExecution execution)
          {

          if (execution.Name == "Stop loss")
          {
          if (shortOnReversal)
          shortEntryOrder = EnterShortStopLimit(shortEntry, shortEntry, "short_entry");
          else if (longOnReversal)
          longEntryOrder = EnterLongStopLimit(longEntry, longEntry, "long_entry");
          }
          else if (execution.Name == "short_entry")
          {
          SetStopLoss("short_entry", CalculationMode.Price, shortStop, false);
          SetProfitTarget("short_entry", CalculationMode.Price, shortPT);
          }
          else if (execution.Name == "long_entry")
          {
          SetStopLoss("long_entry", CalculationMode.Price, longStop, false);
          SetProfitTarget("long_entry", CalculationMode.Price, longPT);
          }

          }
          For some reason when I run this code in a strategy analyzer, NT freezes up and never returns, even if I let it run for hours, and performance on my laptop comes to a stand still. I have to kill NT via task manager to go out of it. Not sure why. Any ideas?
          Attached Files

          Comment


            #20
            Originally posted by Style View Post
            I'm trying to make a go of this via the managed approach. I've went through the code, making sure that I don't have orders that will be ignored because of the order management rules detailed by Adam in the 6th post of this thread. However, I'm still seeing some cases where my stop is being ignored.

            So far I've been using the functions ExitLongStop and ExitShortStop for stops. I listen for their execution via OnExecution, and when they are executed I submit my stop and reverse orders, both entry and exit:



            But the stop is some times ignored.

            I could try using SetStopLoss and SetProfitTarget, but I'm not sure how to listen for their execution via OnExecution, since the set commands do nor return an iorder object.

            Any ideas what I could try next? Thanks!
            You could set a bool flag when you cancel the order, then use OnMarketData() to check the flag, order state and MarketPosition, and then place the reverse order. Remember to reset the flag after you place the order.

            Comment


              #21
              Originally posted by koganam View Post
              You could set a bool flag when you cancel the order, then use OnMarketData() to check the flag, order state and MarketPosition, and then place the reverse order. Remember to reset the flag after you place the order.
              Couldn't you just use OnOrderUpdate to check for when the order was canceled rather than OnMarketData?

              In the latest version of the code I'm using SetProfitTarget. You don't even need to cancel that, right? Is there something wrong with what I do below in post 19, checking via OnExecution for the order "Stop Loss" and then issuing the entry order?

              Comment


                #22
                Style, looking into the code I'm not exactly clear what you wish to do - why do you filter for BarsInProgress if only one time frame is used in the strategy?

                If you have a resting set order protecting an open position, it would be preventing you from reversing with anything other than market orders.

                I didn't see TraceOrders added in the Initialize() of your code, this would be a very helpful tool to debug your order behavior seen.

                BertrandNinjaTrader Customer Service

                Comment


                  #23
                  Originally posted by NinjaTrader_Bertrand View Post
                  Style, looking into the code I'm not exactly clear what you wish to do - why do you filter for BarsInProgress if only one time frame is used in the strategy?
                  It will eventually be a multi-time frame strategy. I’ve not yet added that additional logic because I can’t get passed this stop loss issue. It doesn’t seem wise to add more complexity to a strategy that isn’t working in a simpler form. When I get the simpler strategy working I will add more layers.

                  Originally posted by NinjaTrader_Bertrand View Post
                  If you have a resting set order protecting an open position, it would be preventing you from reversing with anything other than market orders.
                  Even if the stop loss has fired off? I use the EnterShortStopLimit/EnterLongStopLimit for reversals in the OnExecution function when execution.Name == "Stop loss”. At the point that I’m entering the limit entry order, I should be flat, since the stop has already fired. Is that not correct?

                  By the way, I have good reason for not using EnterLong/EnterShort to reverse, instead using the stop limit. The reason is sometimes the level to go long and the level to exit a short do not align. For example, sometimes the level to stop out my short is 1.3000, but the level to enter long may be 1.3010. When the stop fires for the short I do NOT want to immediately go long! I just want to submit a stop limit entry order. Make sense?

                  Originally posted by NinjaTrader_Bertrand View Post
                  I didn't see TraceOrders added in the Initialize() of your code, this would be a very helpful tool to debug your order behavior seen.
                  I will investigate.

                  Thanks!
                  Last edited by Style; 03-20-2012, 05:32 PM.

                  Comment


                    #24
                    I see, definitely agreed on the 'not adding complexity until the basics are working' part.

                    Correct, if the Set() method is not working anymore and the position is closed, it should not lead to ignored orders. Is your reverse happening on the same bar?
                    BertrandNinjaTrader Customer Service

                    Comment


                      #25
                      Originally posted by NinjaTrader_Bertrand View Post
                      Correct, if the Set() method is not working anymore and the position is closed, it should not lead to ignored orders. Is your reverse happening on the same bar?
                      The reverse was working in the same bar back when I was still using ExitLongStop/ExitShortStop, and listening for its execution via OnExecution. Since switching to the Set() method, NT freezes up when I run a backtest. I no longer get a successful completion of the backtest.

                      Comment


                        #26
                        Originally posted by Style View Post
                        Couldn't you just use OnOrderUpdate to check for when the order was canceled rather than OnMarketData?

                        In the latest version of the code I'm using SetProfitTarget. You don't even need to cancel that, right? Is there something wrong with what I do below in post 19, checking via OnExecution for the order "Stop Loss" and then issuing the entry order?
                        You probably could do it that way, but remember that because of the asynchronous nature of NT, you cannot tell the order in which events happen. Except that as market data is what drives everything else, the only thing you can be certain of is that an OnMarketData() event has caused a change in the market, so any test there is testing at the first point of a market event.

                        Comment


                          #27
                          Originally posted by NinjaTrader_Bertrand View Post
                          I didn't see TraceOrders added in the Initialize() of your code, this would be a very helpful tool to debug your order behavior seen.
                          I added TraceOrders to the latest version (which is attached). It's had no effect. The strategy analyzer just freezes up, and eventually I have to close the window.

                          I do get the following error when I click the red X to close the window:

                          3/20/2012 7:51:59 PM|0|4|Error on running backtest: Object reference not set to an instance of an object.
                          What could be happening here? I could use TraceOrders and see what is going on with the ExitLongStop/ExitShortStop version of the strategy, which does not freeze up. Should I give up on the Set() Method? I got the impression that using the Set() method was the preferred way.

                          Please advise. Thanks!
                          Attached Files

                          Comment


                            #28
                            OK, just for grins I tried running one of my older versions of the code that used ExitLongStop/ExitShortStop instead of Set(). It also froze up and gave the "Object reference not set to an instance of an object" error. This was working before, and I made no changes to the code. It's also running on the same data. How is this possible? Did my installation of NT some how get hosed? What should I try now?

                            Comment


                              #29
                              OK, I uninstalled NT, reinstalled it, yet the problem of the back test hanging still persists. I tried running some of the sample strategies that come with NT on the same data set, and they worked with out issue. So my install appears to be ok. It is a code issue.

                              I even went back several versions of this same strategy, and it works. I will go back to the latest working version and start over from there. The latest 4 versions of the code do not work, but the 5th newest does.

                              It is so weird that this issue of hanging did not surface until the two latest versions of the code, but now none of the 4 latest versions work. Why did these issues not surface earlier? I can't explain it.

                              Comment


                                #30
                                Hi Style, if you would like - I can offer to run your latest non working version here on my end to see if I can duplicate the 'freezing' issue you saw on your end - for this please contact me directly here at support at ninjatrader dot com via email.

                                Thanks,
                                BertrandNinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by burtoninlondon, Today, 12:38 AM
                                0 responses
                                4 views
                                0 likes
                                Last Post burtoninlondon  
                                Started by AaronKoRn, Yesterday, 09:49 PM
                                0 responses
                                12 views
                                0 likes
                                Last Post AaronKoRn  
                                Started by carnitron, Yesterday, 08:42 PM
                                0 responses
                                11 views
                                0 likes
                                Last Post carnitron  
                                Started by strategist007, Yesterday, 07:51 PM
                                0 responses
                                13 views
                                0 likes
                                Last Post strategist007  
                                Started by StockTrader88, 03-06-2021, 08:58 AM
                                44 responses
                                3,982 views
                                3 likes
                                Last Post jhudas88  
                                Working...
                                X