Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop order state?

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

    #16
    Josh,

    I don't know how many ways I can tell you that I understand what you are saying. Again, I'm coming from Wealth Lab Pro, which had exactly the same behavior. I understand I'm responsible for synchronizing between hypothetical portfolio positions and actual portfolio positions. That's perfectly fine.

    What I want is a clear answer is what happens on a restart with *stop* orders that (hypothetically) would've been placed previously using SetStopLoss(), because the strategy sees a hypothetical position.

    Comment


      #17
      Originally posted by NinjaTrader_Josh
      heech,

      I've been answering that in as straight as possible the whole time.

      When you start up a strategy it calculates out a strategy position. Let us say it starts up and it decides it is long. If the strategy would have been long with some stop/limit order active at this point in time those orders will be submitted live. If there are none the strategy will just wait for your closing logic and then just close the position.
      Josh,

      Well, that's exactly the behavior I want to see, but I'm not seeing it. I've written together a dummy strategy I called dummy. Here's the code:

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class Dummy : Strategy
      {
      #region Variables
      // User defined variables (add any user defined variables below)
      #endregion
      bool foo = false;

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      TraceOrders = true;
      CalculateOnBarClose = true;
      ExitOnClose = false;
      foo = false;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (!foo)
      {
      foo = true;
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss(CalculationMode.Price, Close[0] * 0.9);
      EnterLong(10, "");
      }
      }
      SetStopLoss(CalculationMode.Price, 19);
      }

      #region Properties
      #endregion
      }
      }
      Here's the sequence of events:

      1) I externally bought 10 shares of MSFT, so that actual and hypothetical position will match.
      2) I start up a chart of MSFT starting from *yesterday*; the code hypothetically would have bought 10 shares of MSFT at the open yesterday, so the two will match.
      3) Note that I have time of life set to Day in the strategy, not GTC.

      Every time through the bar, I call SetStopLoss(). Since there is a hypothetical long position, I would expect a stop order to be placed at startup. There isn't. Here's what I see:

      Output window:
      11/25/2008 7:55:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=19 Currency=0 Simulated=False
      11/25/2008 8:00:16 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=19 Currency=0 Simulated=False
      However, no order is being placed. Nothing in the order menu, nothing in the trace logs. This is actually a different scenario from yesterday, but more or less relevant. Why isn't SetStopTarget() causing a stop order to be placed?

      Comment


        #18
        Josh,

        I'm coming to the conclusion SetStopLoss() really doesn't do what I want... but the good nwes is, it looks like ExitLongStop() will. The behavior of the two are clearly not the same, but at least with Dummy updated to use ExitLongStop().. .I get a live sell stop.

        I will try in my code.

        Comment


          #19
          heech,

          I am going to rewind all the way back to the start.

          1. Connect to brokerage
          2. Trading day has already started for an hour
          3. Immediately submit live working historical orders = true
          4. Start strategy
          5. Strategy decides it is currently in a Long strategy position
          6. There is an associated SetStopLoss order associated with this Long position
          7. This SetStopLoss order should be submitted to become a live working order
          8. The order is not submitted.

          Step 8 is what you are having problems with correct?
          Josh P.NinjaTrader Customer Service

          Comment


            #20
            Originally posted by NinjaTrader_Josh View Post
            heech,

            I am going to rewind all the way back to the start.

            1. Connect to brokerage
            2. Trading day has already started for an hour
            3. Immediately submit live working historical orders = true
            4. Start strategy
            5. Strategy decides it is currently in a Long strategy position
            6. There is an associated SetStopLoss order associated with this Long position
            7. This SetStopLoss order should be submitted to become a live working order
            8. The order is not submitted.

            Step 8 is what you are having problems with correct?
            Josh,

            Exactly right. I'm reasonably sure the issue is due to TimeInForce being set to Day. NOTE: This is the problem I'm having *today*.. the problem I was having yesterday is with stop orders being stuck in PendingSubmit.

            Comment


              #21
              That is exactly the issue. If your orders are TIF=Day and your stop is not hit by the end of the day it doesn't track your open position anymore because it is no longer "in force". You need to use Gtc.
              Josh P.NinjaTrader Customer Service

              Comment


                #22
                Originally posted by NinjaTrader_Josh View Post
                That is exactly the issue. If your orders are TIF=Day and your stop is not hit by the end of the day it doesn't track your open position anymore because it is no longer "in force". You need to use Gtc.
                I purposely didn't want to use gtc, because I don't want the stop orders active at the open on the next day. I don't know what "doesn't track open position anymore" means, since the strategy is certainly aware of the open position... I assumed SetStopLoss() would re-establish itself at the correct price whenever I called it again on the next day. Bad assumption, I guess.

                I've reworked my code with ExitLongStop(), and am running it live now... have to wait until the 30 minute bar to see whether it does what I want in my strategy, and whether that PendingSubmit problem goes away. That will give me more control over all of this, and it's probably the better choice for anyone out there looking at inter-day strategies.

                Comment


                  #23
                  Good luck. SetStopLoss() is for convenience. ExitLongStop() and using IOrders is for ultimate control.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #24
                    Originally posted by NinjaTrader_Josh View Post
                    Good luck. SetStopLoss() is for convenience. ExitLongStop() and using IOrders is for ultimate control.
                    I need more luck, apparently. Everything works great in backtesting... but when I launched live, nothing happened. No stop order was placed (it would have been hit, too), and nothing at all in the log.

                    Now, I launched live in between 30 minute bars... so I thought perhaps there wouldn't be a "live" run until the next 30 minute bar completed (at 12:30 EST). I waited, but still no luck.

                    I run the strategy in the strategy analyzer, and the results are perfect. I close the chart, re-open the chart, load the strategy... and again, perfect, it actually shows the instrument hitting the stop order that *should* have been hit at 12:30 EST. But nothing else.

                    Whenever it's running live, it's just not giving me that stop order. Frustrating.

                    Comment


                      #25
                      heech,

                      Rewind and explain step by step exactly what you are trying to do now. If your strategy placed a stop historically and this stop is still a working stop the moment you go live it will become a real working order with your "Immediately submit live" setting. I do not know what issue you are having.

                      If you submit the order it will go through. If you don't it won't. Use Print() to isolate out your issue and figure out which blocks of code are evaluating.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #26
                        Originally posted by NinjaTrader_Josh View Post
                        If your strategy placed a stop historically and this stop is still a working stop the moment you go live it will become a real working order with your "Immediately submit live" setting.
                        Great input. Now I know I shouldn't sit here like a fool waiting for 30 minute bars to complete. I'll get to work debugging what's going on.

                        Comment


                          #27
                          Wait a second, I think I see what's going on.

                          I'm running multi-timeframe. (30 minute is primary, and 5 min/10 min secondary.)

                          I'm putting in an ExitLongStop() order in the 30 minute time frame, and thinking that it will stay live until the next 30 minute bar. It doesn't look that way. It looks like it's being canceled immediately within the *5* minute bar. I'm guessing that's also how it was designed, but was just unclear to me.

                          I really dont' want to get into canceling stop orders... I like the idea of allowing to expire or not depending on situation. Is there a simple/clean fix? Alternatively, I'll just put a bool in my code... and the 5 minute bar can check the bool to see whether it should replace the ExitLongStop() call. And does this mean the 10 minute bar has to replace the same call?

                          I really have no idea how/why this code worked in backtesting/historical testing, but it does. Is it known that multi-time-frame code behaves differently "live" versus backtest?

                          Comment


                            #28
                            You need to resubmit on all OnBarUpdate() methods related to that instrument. It should have expired in backtesting too. I do not know the exact scenario, but there should be no difference in processing logic just because it is backtesting/realtime.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #29
                              I'm careful now to make sure I insert a stop loss order (using ExitLongStop()) on *every* call into OnBarUpdate, regardless of BarsInProgress. Here's what I get from the output:

                              Bar: 1 Time: 11/25/2008 10:20:00 AM
                              11/25/2008 10:20:00 AM Entered internal PlaceOrder() method at 11/25/2008 10:20:00 AM: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName='' FromEntrySignal=''
                              11/25/2008 10:20:00 AM Ignore order amendment: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName=Sell' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
                              Bar: 2 Time: 11/25/2008 10:20:00 AM
                              11/25/2008 10:20:00 AM Entered internal PlaceOrder() method at 11/25/2008 10:20:00 AM: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName='' FromEntrySignal=''
                              11/25/2008 10:20:00 AM Ignore order amendment: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName=Sell' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
                              Bar: 1 Time: 11/25/2008 10:25:00 AM
                              11/25/2008 10:25:00 AM Entered internal PlaceOrder() method at 11/25/2008 10:25:00 AM: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName='' FromEntrySignal=''
                              11/25/2008 10:25:00 AM Ignore order amendment: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName=Sell' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
                              Bar: 0 Time: 11/25/2008 10:30:00 AM
                              11/25/2008 10:30:00 AM Entered internal PlaceOrder() method at 11/25/2008 10:30:00 AM: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName='' FromEntrySignal=''
                              11/25/2008 10:30:00 AM Ignore order amendment: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName=Sell' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
                              11/25/2008 10:30:00 AM Entered internal PlaceOrder() method at 11/25/2008 10:30:00 AM: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName='' FromEntrySignal=''
                              11/25/2008 10:30:00 AM Ignore order amendment: Action=Sell OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=13.13 SignalName=Sell' FromEntrySignal='' Reason='Order already has this stop price/limit price/quantity'
                              11/25/2008 10:30:00 AM Cancelled expired order: BarsInProgress=1: Order='NT-00002/707028387' Name='Sell' State=Working Instrument='HOG' Action=Sell Limit price=0 Stop price=13.125 Quantity=0 Strategy='ProtectDown' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='9c59595a8e8640348e0e89c7568ba5a3' Gtd='12/1/2099 12:00:00 AM'
                              Received cancelled order: Order='NT-00002/707028387' Name='Sell' State=Cancelled Instrument='HOG' Action=Sell Limit price=0 Stop price=13.125 Quantity=0 Strategy='ProtectDown' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='9c59595a8e8640348e0e89c7568ba5a3' Gtd='12/1/2099 12:00:00 AM'
                              So... you can see attempts to place stop orders on every 5/10 minute bar. (The warning message suggests I don't need to repeat the order actually...) I also have a debugging message at the top of OnBarUpdate showing time + bar.

                              But at 10:30 AM, there's still a canceled order, immediately following an attempt to place an order! I'm completely baffled. It shows BarsInProgress=1 in the canceled order, but I'm not at that point yet, at least OnBarUpdate() hasn't been called with BarsInProgress = 1 yet. (Perhaps that's the BarsInProgress when the original stop order was placed?)

                              Comment


                                #30
                                I think I see the problem, I just have no idea how to fix it.

                                It looks like OnBarUpdate() isn't being called "live" for the 5/10 minute bars, from my chart. I have the chart itself set to 30 minutes, because that's the primary period I'm working with. And looking at the logs, it's obvious to me now the chart in real time is only calling the strategy (with BarsInProgress = 0) every 30 minutes.

                                But when I run things in historical mode (meaning if I reload in the chart with F5) later, it *thinks* it also called the strategy with BarsInProgress = 1, 2 at the 30 minute mark.

                                So, that's what's broken right now. The chart is only calling my strategy every 30 minutes. Have you heard of this before? Do I need to rework my code again, so that the primary is 5 minutes... and add 10/30 minute bars beyond that? But will it actually call OnBarUpdate with BarsInProgress = 1, = 2 in that case? I suspect not...

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                88 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                48 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                31 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                34 views
                                0 likes
                                Last Post TheRealMorford  
                                Started by Mindset, 02-28-2026, 06:16 AM
                                0 responses
                                68 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Working...
                                X