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

Another - Internal Order Handling Rules ERROR situation

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

    Another - Internal Order Handling Rules ERROR situation

    Hi,
    i have seen several of the other posts that concern this issue. But in my case the error is produced after I have closed the position and attempt to open a new order in opposite direction.


    This is the basic approach:

    1. Open limit entry order (with its set of StopLoss/TakeProfit orders)
    2. limit order gets filled and now we have an open order with StopLoss/TakeProfit orders
    3. new signal in opposite direction appears
    4. close open position
    5. open new limit entry order in opposite direction with its own StopLoss/TakeProfit orders (this is where the IGNORE error happens).


    BTW all the trades that failed were tested in the replay connection and in that case the error is not happening. LOL my customer keeps informing me that the opposite order things is not working and when I show them this working every time in the replay connection, they are completely baffled, lol.

    I looked at the Order Handling Rules and this one catches my attention:

    -The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction

    I have no idea what the author meant to communicate with this, at first impression it says to me that even with no open opsitions nor orders that on attempting to place any EnterXXXLimi or StopMarket that the request will be ignored... LOL makes no sense, right?

    So, based on that this is the idea I will try. Instead of just closing the position, I will have the strat cancel the StopLoss/TakeProfit orders before it closes it. This should then leave no trace of any open positions or orders and allow the new limit entry to be executed with its own StopLoss/TakeProfit orders.

    But who knows, according to the rule since the account will be flat a new order in the opposite direction will be ignored, right? LOL... If this does not work and there is no other solution, I guess the next step is the unmanaged route or just have the strat do all of of the StopLoss/TakeProfit itselft instead of using the ninja SET functions. Hmm we'll see....


    Here's a stripped down version of the code is it handles this situation, now . Hopefully the comments wll make it clear as to what is happening.
    Code:
                //signal can contain the values OP_BUY , OP_SELL or -1 (yea I'm a MetaQuotes buff LOL)
                signal=getSignal(); //Get the signal from our super duper classified approach!!
    
                if(signal==-1) return;    //Aww no new signal  return.. do nothing        
    
                //close opened position on any new signal
                if(Position.MarketPosition!=MarketPosition.Flat) {   //Check if we have an open position
    
                    if(Position.MarketPosition==MarketPosition.Long) ExitLong();  //BTW there is only one open position at any given time, so no need to identify which position is being dealt with
                    if(Position.MarketPosition==MarketPosition.Short) ExitShort();
    
                }
    
    
                openLimitTrade(signal);     //Ok lets opn a limit order with a new signal!
    
    
            void openLimitTrade(int op){
    
                bool isStopOperation=false;     //Flag for later use, we make sure that current price is at correct level for limit order
    
                //entryDelta is set by user it can only 0 or greater
                limitprice=(op==OP_BUY?Close[2]+entryDelta*TickSize:Close[2]-entryDelta*TickSize);
                limitprice=Math.Round(limitprice,digits);   //digits is global, properly calculated for the current instrument digits after decimal
    
                //Quick Check of limitprice versus current price
                if(op==OP_BUY && limitprice>Open[0]) isStopOperation=true;    
                if(op==OP_SELL && limitprice<Open[0]) isStopOperation=true;                
    
                //User sets StopLoss and TakeProfit in inputs section
                if(StopLoss>0) SetStopLoss("dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"), CalculationMode.Ticks, StopLoss,false);
                if(TakeProfit>0) SetProfitTarget("dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"), CalculationMode.Ticks, TakeProfit);                                
    
                if(op==OP_BUY)
                        if(!isStopOperation) { EnterLongLimit(0,true,contractSize,limitprice,"dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"));
                        } else  EnterLongStopMarket(0,true,contractSize,limitprice,"dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"));
    
                if(op==OP_SELL)
                        if(!isStopOperation) { EnterShortLimit(0,true,contractSize,limitprice,"dT_" + Time[1].ToString("yyyy.MM.dd HH:mm"));
                        } else EnterShortStopMarket(0,true,contractSize,limitprice,"dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"));
    
                //User sets alert in input section and ah yes,, lets use telegram to get signals sent to our phones :).
                if(alert) telegram("opentrade",op,isStopOperation,token,chanid,message);            
    
            }​

    AND .. This is the new approach we will try this next week when the market opens.

    Code:
                //signal can contain the values OP_BUY or OP_SELL (yea I'm a MetaQuotes buff LOL)
                signal=getSignal(); //Get the signal from our super duper classified approach!!
    
                if(signal==-1) return;    //Aww no new signal  return.. do nothing        
    
                //close opened position on any new signal
                if(Position.MarketPosition!=MarketPosition.Flat) {   //Check if we have an open position
    
                    if(Position.MarketPosition==MarketPosition.Long)  { closeStopTakeOrders(); ExitLong(); }
                    if(Position.MarketPosition==MarketPosition.Short) { closeStopTakeOrders(); ExitShort(); }
    
                }
    
                openLimitTrade(signal);     //Ok lets opn a limit order with a new signal!
    
    
          void closeStopTakeOrders() {
                  //takeprofitOrder and stoplossOrder will of course be assigned when a new imit emtry order is placed
                  CancelOrder(stoplossOrder);
                  CancelOrder(takeprofitOrder);
          }
    
            void openLimitTrade(int op){
                   //NO CHANGE TO CODE
            }​


    Any advice is greatly appreciated,
    ​Jess
    Last edited by xmess7; 11-19-2022, 03:42 PM.

    #2
    Hello Jess,

    Thanks for your post.

    Managed Approach Internal Order Handling Rules: https://ninjatrader.com/support/helpGuides/nt7/managed_approach.htm

    "Methods that generate orders to enter a position will be ignored if:
    - A position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction"


    ​If you are in a position and a stop loss is created with SetStopLoss (or even if there were a working exit limit or stop order with exit methods) you would not be able to place an Enter limit order. You can place market orders and immediately change the position, but this would not be possible with working orders.

    The issue is that since both orders are working, both orders could fill. This would cause the position to be doubled in the opposite direction which would be an unwanted position. Instead, when using the managed approach it is not allowed for two orders to be working if the second order is opposite to the position to prevent the possibility for the unwanted position if both orders were to fill at the same time.

    You would need to change to the unmanaged approach and code that logic manually.

    Below is a link to the help guide on the unmanaged approach.
    https://ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm

    Let me know if I may assist further.​
    Last edited by NinjaTrader_BrandonH; 11-29-2022, 09:53 AM.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      BrandonH:

      As you can see, from the code I posted, when a new NEW SIGNAL is detected the strat will do following:

      1. Determine if the new order is in the OPPOSITE direction
      2. IF the NEW SIGNAL is in the OPPOSITEdirection then CLOSE ANY open position (I assume that closing the position would OCOed the set stop and take profit orders associated to it)
      3. After the position is closed, continue and open the new signal as a limit order and set its corresponding stop and take profit orders.

      So since closing the position seems not to be working, I have implemented the approach I outlined above. I now have the strat CANCEL all ORDERS and then CLOSE the position before any new order is attempted to be opened. Hopefully this will correct the issue and allow the opposite limit order to be placed with its own associated stop and take profit orders.

      The client started testing this today, unfotunately (or fortunately lol) no opposite order situations occurred today. So lets see what happens. If I need to I will use the unmanaged approach, I just hope this does not required an extensive update to the code.... we'll see

      Thnx
      Jess

      Comment


        #4
        Hello ninjas

        I had the same problem with this issue.
        I understand NinjaTrader's concern with its users who make strategies and have little knowledge of ninjascript development and who somehow can send duplicate orders, especially with limit orders, and the result is very different from what was expected.
        However, I think there should be a way to let the developer code limit orders without restrictions.
        The solution I got was NOT use SetProfitTarget and SetStopLoss
        Targets and stops must be sent and managed separately.
        Once you use SetProfitTarget and/or SetProfitTarget, posting limit orders (ExitLongLimit and ExistShortLimit) are not accepted by the system.​

        Comment


          #5
          Hello MarceloF,

          Thanks for your note.

          The Managed approach offers you order methods that are wrapped with an invisible convenience layer that allows you to focus on your system's trading rules leaving the underlying mechanics of order management and the relationships between entry and exit orders and positions to NinjaTrader. The cost for having the convenience layer is that there are order handling rules that must be followed to prevent order errors.

          The Unmanaged approach is reserved for VERY EXPERIENCED programmers. In place of the convenience layer that the Managed approach offered, the Unmanaged approach instead offers ultimate flexibility in terms of order submission and management.​ This means you are not restricted to any order handling rules besides those imposed by the brokerage/exchange. With such flexibility though, you will have to ensure to program your strategy to handle any and all issues that may arise with placing orders.

          See this help guide page for more information about using the Unmanaged approach for developing strategies: https://ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm

          Let me know if I may assist further.
          Last edited by NinjaTrader_BrandonH; 11-29-2022, 09:53 AM.
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Thanks for the reply. Let me see if I understand:
            If I set IsUnmanaged = true, and using SubmitOrderUnmanaged() orders, will the strategy analyzer run the backtests normally? As long as the order management is correct.​

            Comment


              #7
              Hello MarceloF,

              Thanks for your note.

              That is correct. The Unmanaged approach for developing strategies is able to be used for backtests in the Strategy Analyzer.

              Let me know if you have further questions.
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_BrandonH View Post
                Hello Jess,

                Thanks for your post.
                ......

                "Methods that generate orders to enter a position will be ignored if:
                - A position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction"


                ​If you are in a position and a stop loss is created with SetStopLoss (or even if there were a working exit limit or stop order with exit methods) you would not be able to place an Enter limit order. You can place market orders and immediately change the position, but this would not be possible with working orders.

                The issue is that since both orders are working, both orders could fill. This would cause the position to be doubled in the opposite direction which would be an unwanted position. Instead, when using the managed approach it is not allowed for two orders to be working if the second order is opposite to the position to prevent the possibility for the unwanted position if both orders were to fill at the same time.

                ..........

                Let me know if I may assist further.​
                Brandon:
                Thank you for the reponse, and I undertand the rule but please clarify this for me.

                This strategy tester will close all open positions and, if any, cancel all orders. So by the time the new order is submitted there is no open order nor position nor any working orders. So doesn't this condition satisfy the rule .??

                When this strategy is run in the tester it works with no issues. No errors are detected and it all works fine.

                We have also added print statements to monitor the condition on a live account the print statement clearly show that all orders/positions are closed right before the strategy attempts to place a new limit order. In addition the logs show that that there are no working orders right before the time the limit order is to be placed. This is what is baffling.

                Any recommendation on what else we can do to verify that the stragey is clearing all open positions/orders would greatly be apprecated.

                I am to lazy to go into the unamanged order stuff LOL.. But If I must I will...

                Thanks,
                Jess
                Last edited by xmess7; 11-28-2022, 07:12 PM.

                Comment


                  #9
                  Hello xmess7,

                  Thanks for your note.

                  If your strategy is still hitting an internal order handling rule error then that means that the logic in your script somewhere is doing something that is not valid or allowed based on the internal order handling rules.

                  You should make sure that you have prints throughout the strategy to make sure the logic you used is not conflicting in ways that go against the internal order handling rules. If you test a situation and see a rule being hit, you can use your prints to understand which orders applied to that situation and modify your logic.

                  "But in my case the error is produced after I have closed the position and attempt to open a new order in opposite direction."

                  A reversal does not need an exit so you would not need to close the position, this is likely the error you are running into. In this case, you would just call the opposite entry order and the strategy will close the current order and reverse the position.

                  Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position.

                  If you want to exit the order first, you would need to exit the order and then wait for 1 OnBarUpdate() to process before you can re-enter again in the opposite direction so that the positions are updated.

                  Please let me know if I may assist further.​
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_BrandonH View Post
                    Hello xmess7,

                    Thanks for your note.

                    If your strategy is still hitting an internal order handling rule error then that means that the logic in your script somewhere is doing something that is not valid or allowed based on the internal order handling rules.

                    You should make sure that you have prints throughout the strategy to make sure the logic you used is not conflicting in ways that go against the internal order handling rules. If you test a situation and see a rule being hit, you can use your prints to understand which orders applied to that situation and modify your logic.

                    "But in my case the error is produced after I have closed the position and attempt to open a new order in opposite direction."

                    A reversal does not need an exit so you would not need to close the position, this is likely the error you are running into. In this case, you would just call the opposite entry order and the strategy will close the current order and reverse the position.

                    Entry() methods will reverse the position automatically. For example if you are in a 1 contract long position and now call EnterShort() -> you will see 2 executions, one to close the prior long position and the other to get you into the desired 1 contract short position.

                    If you want to exit the order first, you would need to exit the order and then wait for 1 OnBarUpdate() to process before you can re-enter again in the opposite direction so that the positions are updated.

                    Please let me know if I may assist further.​

                    Brandon:
                    Thnx for the post, the info you provide sheds more light on what the rules entail.

                    So here's a couple question I have:

                    1. Is there a time limit between when a postiion is closed (ExitLong()/ExitShort()) and when one can open a new limit orded in the opposite direction?

                    2. Does the ninja platform have a function that can update/refresh account position/order activites??

                    3. You mention "then wait for 1 OnBarUpdate() " , great, but to what resolution? Calculate.OnEachTick, Calculate.OnBarClose, or Calcualte,OnPriceChange ?

                    Look forward to your responses,, thnx
                    Jess


                    Comment


                      #11
                      Hello xmess7,

                      Thanks for your note.

                      There is no specific time limit for when a position will be closed and a new position is opened.

                      NinjaTrader does not have a documented function that forces an update to the account position. You could use the OnPositionUpdate event-driven method to get information about each time the position of a strategy changes state.

                      OnPositionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm

                      If you are running your strategy with Calculate.OnBarClose then the strategy will process logic and place trades at the close of the bar.

                      "then wait for 1 OnBarUpdate() "

                      In the case of running OnBarClose, you would need to wait till the close of the next bar before another order is submitted. This ensures that the previous exit order is closed before a new entry order is placed.

                      Another example would be if you are running OnPriceChange, the strategy will process logic and place trades for each change in price. This means that if an exit order is submitted at one price level, you would need to wait till there is another change in price before submitting a new entry order.

                      Similar rules would apply to using OnEachTick. An exit order and entry order should not be submitted on the same tick.

                      More information about Calculate could be found on this help guide page: https://ninjatrader.com/support/help.../calculate.htm

                      Let me know if I may assist further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_BrandonH View Post
                        Hello xmess7,

                        Thanks for your note.

                        There is no specific time limit for when a position will be closed and a new position is opened.
                        ........................................

                        Let me know if I may assist further.
                        Brandon:
                        The Issue has been solved. The customer reported that they saw an opposite limit order apear as expected on the same candle after a position was closed. How was it solved? Just add enough of time delay and that does the job.

                        FYI:
                        We are using Calcualte.OnEachTick. Initially we tried to open the new opposite limit order on the NEXT tick, that did not work. So then with the use of DateTime.Now and a global variable I had the strategy add a delay in seconds to open a new limit order.

                        I tested it at 1 second delay and that didn;t work, then I extended it to 4 seconds and BAM!! It that worked!! That was however on the startegy replay tester and the real test would be on an active account.

                        So long story short my client just informed me that they saw a successful opposite limit order opened!!

                        YES!!! We don't need to go the unmanaged route LOL!!!

                        The key is to add enough delay so that the positions in the account are updated appropriately so that then new orders can be submitted so that the unmanaged rules are not voilated.

                        Take care and if anyone needs more info on this, send me a message

                        Jess

                        Last edited by xmess7; 12-02-2022, 04:50 PM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by jclose, Today, 09:37 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post jclose
                        by jclose
                         
                        Started by WeyldFalcon, 08-07-2020, 06:13 AM
                        10 responses
                        1,413 views
                        0 likes
                        Last Post Traderontheroad  
                        Started by firefoxforum12, Today, 08:53 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post firefoxforum12  
                        Started by stafe, Today, 08:34 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post stafe
                        by stafe
                         
                        Started by sastrades, 01-31-2024, 10:19 PM
                        11 responses
                        169 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Working...
                        X