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

Short order immediately cancelled

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

    Short order immediately cancelled

    Hi,

    I'm running a custom strategy on NT 6.5.1000.11 using Interactive Brokers (with live trading).

    It's a simple EMA cross strategy, and this morning when the fast crossed below the slow EMA, it entered a short order, but then it was immediately cancelled.

    Thus, I had to enter a sell order manually on the Orders tab (which worked fine), but I'm trying to figure out why this happened. I had no existing positions open at the time, and my strategy does not have any stop loss / profit target setup.

    I should mention that on the orders tab, for the strategy order (the cancelled order), the order type is SellShort. By contrast, for my manual order, the order type is Sell (but the Side column does show it as a short trade on the Positions tab).

    So, I'm wondering what I might have done wrong in my code? I followed the simple SMA cross example that you folks provide. Could someone take a quick look at my OnBarUpdate() and tell me what I might be doing wrong? Thank you.

    Code:
        public class SimpleEMA : Strategy
        {
            private int fast = 1; // Default setting for Fast
            private int slow = 1; // Default setting for Slow
            private int shares = 100; // Default setting for Shares
          
            protected override void Initialize()
            {
                EMA(Fast).Plots[0].Pen.Color = Color.Orange;
                EMA(Slow).Plots[0].Pen.Color = Color.Green;
                
                Add(EMA(Fast));
                Add(EMA(Slow));
    
                CalculateOnBarClose = true;
            }
          
            protected override void OnBarUpdate()
            {
                if (CrossAbove(EMA(Fast), EMA(Slow), 1))
                {
                    ExitShort();
                    EnterLong(this.Shares, "");
                }
                else if (CrossBelow(EMA(Fast), EMA(Slow), 1))
                {
                    ExitLong();
                    EnterShort(this.Shares, "");
                 }
            }                
        
            public int Fast
            {
                get { return fast; }
                set { fast = Math.Max(1, value); }
            }
    
            public int Slow
            {
                get { return slow; }
                set { slow = Math.Max(1, value); }
            }
    
            public int Shares
            {
                get { return shares; }
                set { shares = Math.Max(100, value); }
            }
        }
    }

    #2
    jeffshantz, thanks for the post - you would not need the ExitShort and ExitLong commands, EnterShort and EnterLong would automatically reverse the position as needed for you.

    On which 'On starting a real time strategy' setting do you use this?



    Please also consider to monitor your strategy vs. account positions in order to ensure proper synching - http://www.ninjatrader-support.com/H...tPosition.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      jeffshantz, thanks for the post - you would not need the ExitShort and ExitLong commands, EnterShort and EnterLong would automatically reverse the position as needed for you.
      Okay, I removed the exit commands, as you suggested. Had the same problem today. It went long this morning just fine (as it always does -- the first trade is always fine), but when the lines crossed and it was time to short, it put in a sell order to exit my long position, and that was immediately cancelled. Similarly, it put in a sell order to enter a short position, and that was also immediately cancelled. Therefore, I once again had to do these trades manually on the NT orders tab, which works just fine.

      On which 'On starting a real time strategy' setting do you use this?
      It is set to "Immediately submit live working orders." What should it be set to?

      Could this problem be caused by partial fills? I am trading 1000 shares at a time, and I usually get them filled in blocks of 100 or 200.

      Comment


        #4
        jeffshantz, if you work with in 'immediately submit' mode, do you sync up the strategy properly as you start it up for realtime trading? Most likely for a crossover strategy it would have a historical strategy position coming into the day, so to properly sync this, you would need to place the corresponding order manually at live startup as explained here - http://www.ninjatrader-support.com/H...tPosition.html

        You also want to enable TraceOrders in the Initialize(), as it is a very helpful debugging tool for order behavior - http://www.ninjatrader-support2.com/...ead.php?t=3627
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Bertrand View Post
          jeffshantz, if you work with in 'immediately submit' mode, do you sync up the strategy properly as you start it up for realtime trading? Most likely for a crossover strategy it would have a historical strategy position coming into the day, so to properly sync this, you would need to place the corresponding order manually at live startup as explained here
          No, I didn't sync it. But I don't believe this applies here since, as I mentioned, for the first trade of the day (the first cross over), it went long just fine. And it correctly identified when to exit long and go short when the lines crossed in the middle of the day -- the problem is that the exit long and go short orders get cancelled immediately after submitting them (submitting them manually on the orders tab works fine, though, so I doubt it's a brokerage problem). So synchronization was not a problem, because it did everything right, with the exception of the orders being cancelled.

          You also want to enable TraceOrders in the Initialize(), as it is a very helpful debugging tool for order behavior - http://www.ninjatrader-support2.com/...ead.php?t=3627
          I will try enabling this.

          Comment


            #6
            Ok, thanks for clarifying, then please work with TraceOrders = true in the Initialize() and check why those orders get cancelled immediately.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              Ok, thanks for clarifying, then please work with TraceOrders = true in the Initialize() and check why those orders get cancelled immediately.
              Hi, finally had a trade today and got an error.

              Okay, so today, it went long in the morning just fine. The lines crossed mid-day and it sold and went short also just fine (first time that's ever worked). Then, at the end of the day, when it went to buy to cover (I have ExitOnClose set to true, and ExitOnCloseSeconds set to 60), it cancelled the order once again.

              Here is the relevant portion of the trace file. It gives error 202 - order cancelled.

              Code:
              2009-07-28 15:59:00:649 (IB) Cbi.Connection.ProcessEventArgs.OrderStatusEventArgs: Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' New State=Initialized Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:00:649 (IB) Cbi.Order.Update1: oldid='69e52ac15e8e45fd8f5846568bdacaed' Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' New State=Initialized Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:00:649 (IB) Cbi.Order.Submit: Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' State=Initialized Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Strategy='EMACross' Type=Market Tif=Day Oco='' Filled=0 Fill price=0 Token='f1e3579363fe423ebd07a694a9defc19' Gtd='12/1/2099 12:00:00 AM'
              2009-07-28 15:59:00:649 (IB) IB.Adapter.Submit: Id=4 Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' State=Initialized Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Strategy='EMACross' Type=Market Tif=Day Oco='' Filled=0 Fill price=0 Token='f1e3579363fe423ebd07a694a9defc19' Gtd='12/1/2099 12:00:00 AM'
              2009-07-28 15:59:00:696 (IB) IB.OrderRequest.Send: tmOrderId='69e52ac15e8e45fd8f5846568bdacaed' orderId ='4' instrument='SRS' expiry='' strikePrice=0 right='' exchange='ARCA' action='BUY' limitPrice=0 auxPrice=0 quantity=1700 type='MKT' tif='DAY' oca=''
              2009-07-28 15:59:00:696 (IB) Cbi.Connection.ProcessEventArgs.OrderStatusEventArgs: Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' New State=PendingSubmit Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:00:696 (IB) Cbi.Order.Update1: oldid='69e52ac15e8e45fd8f5846568bdacaed' Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' New State=PendingSubmit Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:00:978 (IB) IB.OrderRequest.ProcessOpenOrders: id='2072968835' orderId=4 quantity=1700 lmtPrice=0 auxprice=0 account='U778252'
              2009-07-28 15:59:00:978 (IB) IB.OrderRequest.Process: id='2072968835' state='Submitted' filled=0 price=0 / Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' State=PendingSubmit Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Strategy='EMACross' Type=Market Tif=Day Oco='' Filled=0 Fill price=0 Token='f1e3579363fe423ebd07a694a9defc19' Gtd='12/1/2099 12:00:00 AM'
              2009-07-28 15:59:01:056 (IB) IB.OrderRequest.ProcessOpenOrders: id='2072968835' orderId=4 quantity=1700 lmtPrice=0 auxprice=0 account='U778252'
              2009-07-28 15:59:01:056 (IB) IB.OrderRequest.Process: id='2072968835' state='PendingCancel' filled=0 price=0 / Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' State=PendingSubmit Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Strategy='EMACross' Type=Market Tif=Day Oco='' Filled=0 Fill price=0 Token='f1e3579363fe423ebd07a694a9defc19' Gtd='12/1/2099 12:00:00 AM'
              2009-07-28 15:59:01:071 (IB) Cbi.OrderStatusEventArgs.Process: Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' New State=Initialized Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:071 WARNING: Order Event Warning:Attribute 'Outside Regular Trading Hours' is ignored based on the order type and destination. PlaceOrder is now being processed. 2109
              2009-07-28 15:59:01:071 (IB) Cbi.OrderStatusEventArgs.Process: Order='69e52ac15e8e45fd8f5846568bdacaed/U778252' Name='Exit on close' New State=PendingSubmit Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:071 (IB) Cbi.Connection.ProcessEventArgs.OrderStatusEventArgs: Order='2072968835/U778252' Name='Exit on close' New State=Accepted Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:071 (IB) Cbi.Order.Update1: oldid='69e52ac15e8e45fd8f5846568bdacaed' Order='2072968835/U778252' Name='Exit on close' New State=Accepted Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:071 (IB) Cbi.Connection.ProcessEventArgs.OrderStatusEventArgs: Order='2072968835/U778252' Name='Exit on close' New State=Working Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:087 (IB) Cbi.Order.Update1: oldid='2072968835' Order='2072968835/U778252' Name='Exit on close' New State=Working Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:149 (IB) Cbi.OrderStatusEventArgs.Process: Order='2072968835/U778252' Name='Exit on close' New State=Accepted Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:149 (IB) Cbi.OrderStatusEventArgs.Process: Order='2072968835/U778252' Name='Exit on close' New State=Working Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              2009-07-28 15:59:01:228 (IB) IB.OrderRequest.Process: id='2072968835' state='Cancelled' filled=0 price=0 / Order='2072968835/U778252' Name='Exit on close' State=Working Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Strategy='EMACross' Type=Market Tif=Day Oco='' Filled=0 Fill price=0 Token='f1e3579363fe423ebd07a694a9defc19' Gtd='12/1/2099 12:00:00 AM'
              2009-07-28 15:59:01:243 (IB) Cbi.Connection.ProcessEventArgs.OrderStatusEventArgs: Order='2072968835/U778252' Name='Exit on close' New State=Cancelled Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error='Order Canceled - reason:Invalid order (202)'
              2009-07-28 15:59:01:243 (IB) Cbi.Order.Update1: oldid='2072968835' Order='2072968835/U778252' Name='Exit on close' New State=Cancelled Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error='Order Canceled - reason:Invalid order (202)'
              2009-07-28 15:59:01:243 (IB) Cbi.OrderStatusEventArgs.Process: Order='2072968835/U778252' Name='Exit on close' New State=Cancelled Instrument='SRS' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1,700 Type=Market Filled=0 Fill price=0 Error=NoError Native error='Order Canceled - reason:Invalid order (202)'
              Please advise. Thank you.

              Comment


                #8
                Please check with IB for what reason it was rejected - I'd guess it was issued too late for MOC processing.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  Please check with IB for what reason it was rejected - I'd guess it was issued too late for MOC processing.
                  I'll check with IB, but this really doesn't seem like a brokerage problem. As I keep reiterating, it works fine when I do a trade manually on the Orders tab, so that suggests it's not a brokerage problem but a problem with the way the strategy is putting the order in. When the end of day buy to cover issued by the strategy got cancelled yesterday, I immediately went to the Orders tab and did the cover myself -- it executed just fine.

                  Anytime I do it manually on the Orders tab, it works fine. Most times when the strategy tries to do it, it gets cancelled. I had asked in a previous post if partial fills might be causing a problem and didn't get a response. I have the EntriesPerDirection setting set to 1. If I get partial fills, do these count as multiple entries?

                  Comment


                    #10
                    jeffshantz, no the partial fills would not count as multiple entry. Please post back what IB says about why the order got rejected, do you have the same TIF setting set for the strategy as well as for the manual order?
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      I don't know if jeffshantz is stil using NT but I'm getting the same trouble. How did you solve it?

                      Comment


                        #12
                        guillembm,

                        Are you having the issues with a live or simulation orders? If live, can you reproduce on a sim account? Is this a custom script you're working with?

                        Have you ran TraceOrders = true to get more information on why the orders are being canceled? If so, what did this output?

                        More information on using TraceOrders can be found below:

                        MatthewNinjaTrader Product Management

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by sidlercom80, 10-28-2023, 08:49 AM
                        168 responses
                        2,262 views
                        0 likes
                        Last Post sidlercom80  
                        Started by Barry Milan, Yesterday, 10:35 PM
                        3 responses
                        10 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by WeyldFalcon, 12-10-2020, 06:48 PM
                        14 responses
                        1,429 views
                        0 likes
                        Last Post Handclap0241  
                        Started by DJ888, 04-16-2024, 06:09 PM
                        2 responses
                        9 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by jeronymite, 04-12-2024, 04:26 PM
                        3 responses
                        41 views
                        0 likes
                        Last Post jeronymite  
                        Working...
                        X