Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy tried to incorrectly execute 10.1k shares

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

    Strategy tried to incorrectly execute 10.1k shares

    I am running NinjaTrader 8.0.0.10 64 bit

    This morning, after successfully executing a trade for 100 shares (live trading), NT attempted to modify my profit target for 10.1k shares. This was rejected by my broker due to the size and the strategy became disabled.

    I ran the following sequence:
    if(triggerLong)
    {
    EnterLong(100); this.SetProfitTarget(CalculationMode.Price,Close[0]+ATR(14)[0]*3);
    }

    4/18/16 8:31 Order
    At this point I was long 100 shares as I should be.
    Order='739564834/Uxxxxx' Name='Buy' New state='Filled' Instrument='AAPL' Action='Buy' Limit price=0 Stop price=0 Quantity=100 Type='Market' Time in force=GTC Oco='' Filled=100 Fill price=108.7992 Error='No error' Native error=''

    Next it shows the order for setting the profit target, as it should be.

    4/18/16 8:32 Order Order='739564836/Uxxxx Name='Profit target' New state='Working' Instrument='AAPL' Action='Sell' Limit price=109.19 Stop price=0 Quantity=100 Type='Limit' Time in force=GTC Oco='AAPL/0000d73b.5714db4a.01.01' Filled=0 Fill price=0 Error='No error' Native error=''

    4/18/16 8:32 Order Order='739564836/Uxxxx' Name='Profit target' New state='Accepted' Instrument='AAPL' Action='Sell' Limit price=109.19 Stop price=0 Quantity=100 Type='Limit' Time in force=GTC Oco='AAPL/0000d73b.5714db4a.01.01' Filled=0 Fill price=0 Error='No error' Native error=''

    4/18/16 8:32 Order Order='0e8bb48280a942f39ac747abca1eff2c/Uxxxx' Name='Profit target' New state='Submitted' Instrument='AAPL' Action='Sell' Limit price=109.19 Stop price=0 Quantity=100 Type='Limit' Time in force=GTC Oco='AAPL/0000d73b.5714db4a.01.01' Filled=0 Fill price=0 Error='No error' Native error=''


    At 9:02, the strategy hit this logic:
    If(shouldExit)
    {
    ExitLong(100);
    }

    This worked as expected, however as it was executing to exit, NT started modifying the profit target, and at one point attempted to submit an order with 10.1k as quantity:

    4/18/16 9:02 Order Order='c11c51ddccbf4fc8856d20a8cb17e347/Uxxxxx' Name='Sell' New state='Submitted' Instrument='AAPL' Action='Sell' Limit price=0 Stop price=0 Quantity=100 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''

    4/18/16 9:02 Execution Execution='AAPL/00014229.57149676.01.01' Instrument='AAPL' Account='Uxxx' Exchange=Default Price=108.29 Quantity=100 Market position=Short Operation=Operation_Add Order='739564838' Time='4/18/2016 9:03 AM'

    4/18/16 9:02 Order Order='739564838/Uxxx' Name='Sell' New state='Filled' Instrument='AAPL' Action='Sell' Limit price=0 Stop price=0 Quantity=100 Type='Market' Time in force=GTC Oco='' Filled=100 Fill price=108.29 Error='No error' Native error=''

    4/18/16 9:02 Order Order='739564836/Uxx' Name='Profit target' New state='Change submitted' Instrument='AAPL' Action='Sell' Limit price=109.19 Stop price=0 Quantity=10.1K Type='Limit' Time in force=GTC Oco='AAPL/0000d73b.5714db4a.01.01' Filled=0 Fill price=0 Error='No error' Native error=''

    That last execution above is when it attempted to do a quantity of 10.1K. At this point I end up with rejected messages from broker, and strategy goes to disabled.

    Any idea what is wrong in my logic that caused this? Should I have sent a cancel order myself before exiting the position?

    The fact that my strategy became disabled due to an order too large is a bit scary since this is running on real money....

    #2
    Hello frostEngine,

    I believe an immediate fix would be to or "! shouldExit" into your "triggerLong" boolean. This would prevent the code that sets profit targets from executing when you exit your long position and thus prevent this from happening again.

    To better understand what actually occurred, I would like to ask if you could sent any relevant code samples you are comfortable sharing along with your log and trace files from the day this occurred to [email protected], referencing this unique ticket ID : 1501550 .

    You will find the log file on your PC in the (My) Documents > NinjaTrader 8 > Log folder.
    • The log file will be named "log.YYYYMMDD.txt"

    You will find the trace file on your PC in the (My) Documents > NinjaTrader 8 > Trace folder.
    • The trace file will be named "trace.YYYYMMDD.txt"
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      You believe this happened because I attempted to trigger another long (which sets the profit target) right when the trade was exited?

      This would not be possible. The logic that generates exitLong and triggeredLong are inverse.

      I believe this occurred as NinjaTrader was attempting to cancel the profit target.

      Comment


        #4
        Hello FrostEngine,

        That isn't quite what I believe. I am unsure why this happened and can not be sure without reviewing your logic, or at least your log output, and reproducing this on my end. I suspect you may be correct as to why this happened.

        However, I do believe that isolating the only part of your code which can modify profit targets, so that it can not continue running while you are cancelling orders, will definitely prevent this from happening in the future. I provided that advice merely so that you could continue using NinjaTrader without this occurring again, without waiting for us to diagnose what occurred. This is a work-around, not a solution.

        If you do add code to every instance of SetProfitTarget so that it can not run while canceling orders, and this occurs again, please let us know, as we will then definitely have enough information to reproduce this on our end.
        Last edited by NinjaTrader_JessicaP; 04-18-2016, 12:29 PM.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          The logic is already set up in such a way that setProfitTarget can not be called while canceling orders. Below is a snippet from the logic. However, what you will see is that for setProfitTarget and EnterLong to be called, market position has to be = flat, and a key oscillator has to be below 50

          For it to exit the long position, market position has to be long, and that SAME oscillator has to be > 50

          Therefore, the logic is already set up as you suggested such that they can not both be called on the same onBarUpdate.

          The strategy only runs on bar close.

          if(this.Position.MarketPosition==MarketPosition.Fl at)
          {
          triggerLong=0;
          if(oscillator<50&&some other conditions)
          {
          triggerLong=1;
          }

          if(triggerLong)
          {
          EnterLong(100);
          this.SetProfitTarget(CalculationMode.Price,Close[0]+ATR(14)[0]*3);
          }

          }

          if(this.Position.MarketPosition==MarketPosition.Lo ng)
          {
          shoudlExit=0;
          if(oscillator>50&&some other conditions)
          {
          shouldExit=1;
          }

          If(shouldExit)
          {
          ExitLong(100);
          }

          }

          Comment


            #6
            Also, here is some additional insight into the issue. The strategy just had the same scenario, where it was long, then triggered the oscillator based exit instead of the profit target based exit. This time when it happened, everything executed properly and did not generate any errors.

            No changes to the strategy took place between the two events. However, one change I did make was I changed the settings on the strategy from GTC to Day. Doubt this would have changed the outcome. Seems like a race condition of some sort.

            Comment


              #7
              I just had the same issue appear. It attempted to change the order quantity to 10.1k shares. This caused the order to be rejected just like yesterday. This results in the profit target limit order not being cancelled and the strategy becoming disabled.

              Any other ideas to prevent this from happening?

              Comment


                #8
                Hello frostengine,

                I am currently using the information you provided in order to attempt to reproduce on my end. If you could also send your log and trace files to platformsupport[at]ninjatrader[dot]com, referencing the unique ticket ID 1501550, it would be appreciated and will make it easier to determine what is happening.

                In the meantime, I would like to point out this excerpt from the documentation on SetProfitTarget,

                Originally posted by http://ninjatrader.com/support/helpGuides/nt7/setprofittarget.htm
                Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position
                Again while I can not be certain until I am done testing, I believe this is related to what you have observed.

                I will follow up in this thread when I have more information.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  I just sent the log files. It contains the event yesterday as well as today. Look for trades involving AAPL.

                  Comment


                    #10
                    Hello frostengine,

                    Based on my testing and your results, I have the following code recommendation you can adopt. Please let me know if you have any questions.

                    Code:
                    [FONT=Courier New]
                            /// <summary>
                            /// This method is used to configure the strategy and is called once
                            /// before any strategy method is called.
                            /// </summary>
                            protected override void Initialize()
                            {
                                // Please see the help guide section to understand why this should
                                // be set here
                                SetProfitTarget(CalculationMode.Ticks, 0);
                                CalculateOnBarClose = true;
                            }
                    
                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            private int triggerLong = 0; // test variable
                            private int oscillator = 10; // test variable
                            protected override void OnBarUpdate()
                            {
                                bool some_other_conditions = true; // test variable
                                int shouldExit = 0; // test variable
                                triggerLong = triggerLong == 0 ? 1 : 0; // test square wave
                    
                                if(this.Position.MarketPosition == MarketPosition.Flat)
                                {
                                    triggerLong=0;
                                    if(oscillator < 50 && some_other_conditions)
                                    {
                                        triggerLong = 1;
                                    }
                    
                                    if(triggerLong != 0)
                                    {
                                        EnterLong(100);
                                    }[/FONT]
                    [FONT=Courier New][FONT=Courier New][FONT=Courier New]
                    [/FONT] 
                                    // reset when flat so that, the next time you update your
                                    // profit target, it will use the currently traded price as a
                                    // base
                                    this.SetProfitTarget(CalculationMode.Ticks, 0);
                    [/FONT]             }
                    
                                if(this.Position.MarketPosition == MarketPosition.Long)
                                {
                                    shouldExit = 0;
                                    if(oscillator > 50 && some_other_conditions)
                                    {
                                        shouldExit = 1;
                                    }
                    
                                    if(shouldExit != 0)
                                    {
                                        ExitLong(100);
                                    }
                    
                                    else
                                    {[/FONT][FONT=Courier New][FONT=Courier New]
                                        // Please note that this updates profit targets,
                                        // it does not place profit targets.
                    
                                        this.SetProfitTarget (
                                                CalculationMode.Price, Close[0] + ATR(14)[0] * 3
                                                );[/FONT]
                                    }
                                }
                            }[/FONT]
                    Last edited by NinjaTrader_JessicaP; 04-19-2016, 11:20 AM.
                    Jessica P.NinjaTrader Customer Service

                    Comment


                      #11
                      Your comment states to reset when flat, but you put it in the part of the strategy that is hit when Long. Did you mean to put the reset in the ==Flat if statement instead?

                      Comment


                        #12
                        Thank you, that was a mistake on my part and a good catch. Please swap the two calls to SetProfitTarget, so that the one that updates happens whenever you are in a long position, and the one that resets happens whenever you are flat.

                        I am editing the provided sample so it is correct.
                        Jessica P.NinjaTrader Customer Service

                        Comment


                          #13
                          With the modified code as you suggested, the strategy did the same thing again today. This time it was on SPY, but essentially tried to modify the profit target to trade 10.1k shares.

                          It appears its part of when the strategy exits the position with an ExitLong(), NT tries to clear out the waiting profit target and in the process attempts to modify it to be 10.1k shares.

                          Comment


                            #14
                            Enter / Exit managed order methods are designed to work separately from SetProfitTarget / SetTrailStop / SetStopLoss . The latter three are designed to work together and to be OCO with each other. If ExitLong or ExitShort are affecting the latter three methods, we will need some more information from your system.

                            I would like to ask that you add the following bolded trace code to your strategy. The next time this occurs, please manually attach your most current log and trace files to your response, so I may investigate this matter further.

                            You will find the log file on your PC in the (My) Documents > NinjaTrader 8 > Log folder.
                            • The log file will be named "log.YYYYMMDD.txt"

                            You will find the trace file on your PC in the (My) Documents > NinjaTrader 8 > Trace folder.
                            • The trace file will be named "trace.YYYYMMDD.txt"

                            Please reference this unique ticket ID in the body of your e-mail : 1501550



                            Code:
                            [FONT=Courier New]/// <summary>
                            /// This method is used to configure the strategy and is called once
                            /// before any strategy method is called.
                            /// </summary>
                            protected override void Initialize()
                            {
                            [B]    Log("Resetting profit target on initialize", NinjaTrader.Cbi.LogLevel.Information);[/B]
                                SetProfitTarget(CalculationMode.Ticks, 0);
                            [B]    Log("Reset profit target complete");[/B]
                                CalculateOnBarClose = true;
                            }
                            
                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            private int triggerLong = 0; // test variable
                            private int oscillator = 10; // test variable
                            protected override void OnBarUpdate()
                            {
                                bool some_other_conditions = true; // test variable
                                int shouldExit = 0; // test variable
                                triggerLong = triggerLong == 0 ? 1 : 0; // test square wave
                            
                                if(this.Position.MarketPosition == MarketPosition.Flat)
                                {
                                    triggerLong=0;
                                    if(oscillator < 50 && some_other_conditions)
                                    {
                                        triggerLong = 1;
                                    }
                            
                                    if(triggerLong != 0)
                                    {
                            [B]            Log("Entering long 100 deep at " + Close[0], NinjaTrader.Cbi.LogLevel.Information);[/B]
                                        EnterLong(100);
                            [B]            Log("Enter long 100 complete");[/B]
                                    }
                            
                            [B]        Log("Resetting profit target at " + Close[0], NinjaTrader.Cbi.LogLevel.Information);[/B]
                                    this.SetProfitTarget(CalculationMode.Ticks, 0);
                            [B]        Log("Live reset complete");[/B]
                                 }
                            
                                if(this.Position.MarketPosition == MarketPosition.Long)
                                {
                                    shouldExit = 0;
                                    if(oscillator > 50 && some_other_conditions)
                                    {
                                        shouldExit = 1;
                                    }
                            
                                    if(shouldExit != 0)
                                    {
                            [B]            Log("Exiting long at " + Close[0], NinjaTrader.Cbi.LogLevel.Information);[/B]
                                        ExitLong(100);
                            [B]            Log("Exit long complete");[/B]
                                    }
                            
                                    else
                                    {
                            [B]            Log("Updating profit target at " + Close[0], NinjaTrader.Cbi.LogLevel.Information);[/B]
                                        this.SetProfitTarget(CalculationMode.Price, Close[0] + ATR(14)[0] * 3);
                            [B]            Log("Live update complete");[/B]
                                    }
                                }
                            }[/FONT]
                            Jessica P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by AttiM, 02-14-2024, 05:20 PM
                            12 responses
                            211 views
                            0 likes
                            Last Post DrakeiJosh  
                            Started by cre8able, 02-11-2023, 05:43 PM
                            3 responses
                            236 views
                            0 likes
                            Last Post rhubear
                            by rhubear
                             
                            Started by frslvr, 04-11-2024, 07:26 AM
                            8 responses
                            114 views
                            1 like
                            Last Post NinjaTrader_BrandonH  
                            Started by stafe, 04-15-2024, 08:34 PM
                            10 responses
                            47 views
                            0 likes
                            Last Post stafe
                            by stafe
                             
                            Started by rocketman7, Today, 09:41 AM
                            3 responses
                            11 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Working...
                            X