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

Order handling

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

    Order handling

    Working with a strategy that may enter multiple orders, and manages e.g. moving of stops for each entry individually, i.e. cannot use e.g. Position.AveragePrice because that is for all entries combined.

    Starting with a simple question: Why in the various examples is the approach of tracking orders never (as far as I have seen) done as follows:

    private Order entryOrder = null;
    protected override void OnBarUpdate()
    {
    entryOrder = EnterLong(1, "MyEntry");
    }

    ... but the assignment of entryOrder is only done typically in OnOrderUpdate, by checking that the order calling the method is e.g. "MyEntry", as in this case?

    In other words, why is it impractical to link entryOrder to the order already at EnterLong?
    Last edited by Persona; 04-27-2018, 11:42 AM.

    #2
    SetStopLoss() vs. ExitLongStopMarket()

    Another point related to the same strategy...

    In the documentation it says that when using SetStopLoss and SetProfitTarget, the orders are submitted and managed automatically as OCO orders, which is very convenient.

    Question: Does this apply similarly to e.g. ExitLongStopMarket() and other similar methods, as long as the 'fromEntrySignal' is provided, or does one have to manage the cancellation etc. of these profit target and stop loss orders more manually in these cases (compared to using SetStopLoss and SetProfitTarget)?

    I am asking, as I want to do fractional profit-taking, and one approach is to create multiple entries with different signal names, e.g. "ProfitTarget1", "ProfitTarget2", etc. because with SetProfitTarget one cannot specify the quantity (e.g. a certain fraction of the full entry). This is possible with ExitLongStopMarket() and similar methods, but I'm wondering if I'll be making my life significantly more difficult by going down that route, as opposed to trying to work with just SetStopLoss and SetProfitTarget.

    Many thanks!

    Comment


      #3
      Hello Persona,

      Thanks for opening the thread.

      Although this may work in many instances, it should be noted that assigning Order objects outside of OnOrderUpdate() is not guaranteed to be completed if you were to immediately reference the Order object after assigning it.

      The OnOrderUpdate() section of the help guide offers this note:
      Code:
      protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
      {
        // check if the current order matches the orderName passed in "EnterLong"()
        // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
        // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
        if (order.Name == "entryOrder")
            entryOrder = order;
       
        // if entry order exists
        if (entryOrder != null && entryOrder == order)
        {
            Print(order.ToString());
            if (order.OrderState == OrderState.Cancelled)
            {
                // Do something here
                entryOrder = null;
            }
        }
      }
      Publicly available OnOrderUpdate() documentation can be found here: https://ninjatrader.com/support/help...rderupdate.htm

      As for OCO ID's with the Managed Approach, the Set methods for profit target and stop loss will use OCO while the other order methods will not. If you wish to use OCO with your own orders, the Unmanaged Approach can be used.

      Unmanaged Approach - https://ninjatrader.com/support/help...d_approach.htm

      Please let us know if we can be of further assistance.
      Last edited by NinjaTrader_Jim; 04-27-2018, 12:06 PM.
      JimNinjaTrader Customer Service

      Comment


        #4
        "Error on calling 'OnBar' method"

        Thank you, NinjaTrader_Jim! That makes sense.

        I've been working on the strategy quite a bit, and recently have sometimes ran into the following issue when running longer optimizations, e.g. 10 generations on the Genetic algorithm on 4-5 months of 1-minute bars. The algorithm itself is also fairly elaborate.

        I now got e.g. the following exceptions:

        17.5.2018 19:17:49
        Fill type 'Default fill type': Error on calling 'OnBar' method: System.ArgumentNullException: Value cannot be null. Parameter name: key
        at System.ThrowHelper.ThrowArgumentNullException(Exce ptionArgument argument) at System.Collections.Generic.Dictionary`2.FindEntry( TKey key)
        at System.Collections.Generic.Dictionary`2.TryGetValu e(TKey key, TValue& value)
        at NinjaTrader.Cbi.Position.HandleExitExecution(Execu tion exitExecution, SystemPerformance performance)
        at NinjaTrader.Cbi.Position.AddExecution(Execution execution, SystemPerformance performance)
        at NinjaTrader.NinjaScript.StrategyBase.AddExecution( Position position, Execution execution, Order order)
        at NinjaTrader.NinjaScript.StrategyBase.FillOrder(Ord er order, Double fillPrice, Double slippage)
        at NinjaTrader.NinjaScript.DefaultFillType.OnBar()
        at NinjaTrader.NinjaScript.StrategyBase.ProcessBackte stOrders()

        17.5.2018 19:17:52
        Fill type 'Default fill type': Error on calling 'OnBar' method: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
        at System.ThrowHelper.ThrowArgumentOutOfRangeExceptio n(ExceptionArgument argument, ExceptionResource resource)
        at NinjaTrader.Data.BarsSeries.GetTime(Int32 index)
        at NinjaTrader.Data.Bars.GetTime(Int32 index)
        at NinjaTrader.NinjaScript.StrategyBase.SubmitOrderNo w(Int32 selectedBarsInProgress, Boolean isLiveUntilCancelled, OrderAction orderAction, OrderType orderType, Int32 quantity, Double limitPrice, Double stopPrice, String signalName, String fromEntrySignal, String oco, Boolean isSimulatedStop)
        at NinjaTrader.NinjaScript.StrategyBase.SubmitStopTar getNow(Int32 selectedBarsInProgress, OrderAction action, OrderType orderType, Int32 quantity, Double limitPrice, Double stopPrice, String signalName, String fromEntrySignal, String oco, Boolean isSimulatedStop)
        at NinjaTrader.NinjaScript.StrategyBase.SubmitStopTar get(Position position, Order order, StopTarget stopTarget, Execution entryExecution, Boolean changeQuantity)
        at NinjaTrader.NinjaScript.StrategyBase.AddExecution( Position position, Execution execution, Order order)
        at NinjaTrader.NinjaScript.StrategyBase.FillOrder(Ord er order, Double fillPrice, Double slippage)
        at NinjaTrader.NinjaScript.DefaultFillType.OnBar()
        at NinjaTrader.NinjaScript.StrategyBase.ProcessBackte stOrders()

        ... and the optimizer stopped. What should I make of this? Is this common/known to happen with lengthy optimizations? I can usually run the same optimization with e.g. 5 generations with no issues. Or is here something that points to some issue that I should try to improve in my code?

        Any pointers/advice would be much appreciated!
        Last edited by Persona; 05-20-2018, 03:23 PM.

        Comment


          #5
          Hello Persona,

          I generally would not expect to run into exceptions when using the optimizer but without the context of the code, I could not offer much further input.

          Would you be able to create a barebones example strategy and some steps we can take to hit the exceptions so we can look at together?

          I'm happy to be of any further assistance.
          JimNinjaTrader Customer Service

          Comment


            #6
            Hi Jim,

            The exceptions don't happen all the time and not consistently (but still rather frequently), so it's hard to specify exact conditions to reproduce the issue.

            And in general, I'm not asking you to figure out what's wrong with my code -- that I believe is beyond the scope of support you can provide for free -- however, what I would hope you could help with is explaining what these exceptions are telling us. The exception output is generated by the NinjaTrader product, not my code, and I would like to understand what NT is trying to tell me with these. E.g., what would need to happen or what are the kinds of events that could trigger exceptions like this. Could you shed some light on that?

            Many thanks!

            Comment


              #7
              Pending orders

              Another question, potentially related or then not: How to clear the pending orders from the Order tab? These seem to be remnants from the failed optimization runs.
              Attached Files

              Comment


                #8
                Originally posted by Persona View Post
                Another question, potentially related or then not: How to clear the pending orders from the Order tab? These seem to be remnants from the failed optimization runs.
                Resetting the Sim101 account should clear these orders.

                Comment


                  #9
                  Hello Persona,

                  Looking at the stack trace for the first exception, it looks like an issue where an exit is trying to be matched with an entry, but something has gone wrong in the process.

                  For the second exception, it looks like there is an issue with the submitting of Profit Targets/Stop Losses.

                  While these exceptions can give a hint to where an issue is coming from, it would still be most beneficial to know what the strategy/backtest is doing to encounter these exceptions. Just because the exception is seen does not necessarily mean the problem is coming from where the exception is thrown. The issue may be coming further upstream. Looking at your exit handling may lead you down the right track.

                  We cannot offer debugging services in the support department, but we can certainly look at a reduced example of code that throws an issue to give further input. Also, if we can see that these issues are coming from regular use of NinjaTrader's order handling and backtesting, we would very much want to look into it further.

                  Some questions that may be worthwhile:
                  1. Does this only occur with the Genetic Optimizer? -Can it be reproduced with a traditional optimization backtest?
                  2. Can it be reproduced after removing the logic that creates your entry/exit signals? (Essentially only entering when flat and exiting when in position.)
                  3. Do you see this behavior with other example Managed/Unmanaged strategies? If you don't see the issue with these examples, I may suggest replicating that order handling logic in your strategy and testing again.
                  4. When you enable TraceOrders and add debugging prints, do you catch some last orders before the exception is received or some last bit of code before the exception is thrown?


                  If there is anything else I can do to help or provide further direction, please let me know.
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Jim,

                    Thanks again!

                    Regarding what the strategy is doing, in summary:
                    • We take the trade signal using a more granular timeframe for checking the condition, e.g. check for indicator thresholds on a 15M chart every minute (by adding the other data series and using BarsInProgress)
                    • We have multiple profit targets, cumulatively on top of each other, and a stop loss level. As I want to use managed orders, this is done by multiple SetProfitTarget orders and one SetStopLoss order per signal entry. And to get the levels precise, these are submitted at OnExecutionUpdate using the AverageFillPrice as the basis.
                    • I maintain bookkeeping of the multiple entries -- there can be several of these types of entries in parallel, up to ten or more of such entries with multiple SetProfitTarget orders as above each -- in a List of MyEntry (a simple class with entry price, target and stop levels, booleans on whether the each of the targets has been hit, etc.) that I check on OnBarUpdate to see if any of the profit or stop levels have been hit, and then update the stop levels accordingly, or remove the MyEntry from the List, if it has been fully closed.
                    • So, I'm basically doing bookkeeping of the entries without using references to the actual Order objects themselves, because I don't need to, as I use the unique names of the entries, e.g. "L_ProfitTarget2_645" for the second profit target of a long entry entered on bar 645. Using managed orders, this is enough, and I can still benefit from the automatic OCO management etc. I suppose the same could be done by using Order references, but then I found I would have had more open questions to figure out -- or would have needed to go down the Unmanaged path instead all the way.


                    One of the issues I ran into earlier just adding the strategy to a chart with historical data was that sometimes, if the price moved up and down very fast, we might hit one or several of the profit targets, but by the time even the more granular timeframe bar closed, the price had already dropped/risen past where we would have moved the stop to -- and then there was understandably an error saying that e.g. a buy stop cannot be placed below the market price. I don't know if this could throw an exception similar to the copy-pasted ones?

                    Anyway, I haven't been able to reproduce the exceptions for a couple of days now. But every now and then the optimizer does still get stuck -- I just have no visibility into what is causing it. And like you were asking, this happens only on the Genetic optimizer, and typically on the first iteration of the generation, e.g. iteration 26, 76, 226, or similar. The CPU is running at approx. 14-15%, memory stays allocated, but the iteration simply never ends. Is this known to happen with the Genetic algorithm sometimes, or if it does, is it a clear indication that there is still some issue with the strategy code? The optimizer has never gotten stuck on the Default optimizer, I believe.

                    Where are the example Managed/Unmanaged strategy examples you referenced? My strategy is likely to be quite a bit more complex with the several targets per entry, several entries in parallel, moving of stops etc., but indeed would be happy to see if there's something in the examples I should leverage.

                    Regarding using order tracing and debugging prints, the issue is that they slow down the execution of the strategy up to a point that I basically never practically get to the exception or hanging point, as the strategy could do let's say 20000 trades within the time period, and the optimizer run without the debugging prints could take an hour or more -- and then with debugging it would take forever. And if I limit the data set so that debugging is practical, I basically never get the exception or get stuck to begin with.

                    Thanks for all the pointers and suggestions!
                    Last edited by Persona; 05-23-2018, 07:28 AM.

                    Comment


                      #11
                      Hello Persona,

                      One of the issues I ran into earlier just adding the strategy to a chart with historical data was that sometimes, if the price moved up and down very fast, we might hit one or several of the profit targets, but by the time even the more granular timeframe bar closed, the price had already dropped/risen past where we would have moved the stop to -- and then there was understandably an error saying that e.g. a buy stop cannot be placed below the market price. I don't know if this could throw an exception similar to the copy-pasted ones?
                      Historical backfill logic will fill stop market orders that are moved to the other side of the market at the market price instead of throwing a rejection like we would see in real time. The errors that we see in realtime from the rejection would not be seen in backtesting.

                      Anyway, I haven't been able to reproduce the exceptions for a couple of days now. But every now and then the optimizer does still get stuck -- I just have no visibility into what is causing it. And like you were asking, this happens only on the Genetic optimizer, and typically on the first iteration of the generation, e.g. iteration 26, 76, 226, or similar. The CPU is running at approx. 14-15%, memory stays allocated, but the iteration simply never ends. Is this known to happen with the Genetic algorithm sometimes, or if it does, is it a clear indication that there is still some issue with the strategy code? The optimizer has never gotten stuck on the Default optimizer, I believe.
                      I have not heard of this happening with the Genetic Optimizer. I have discussed this matter with our QA team and they would like to review the test involving the strategy and the settings used for the backtest in the Strategy Analyzer. Could you write in to platformsupport[at]ninjatrader[dot]com with the text "Attention Jim," an export of the strategy's source code and a list/screenshot(s) of the settings used for the backtest that threw the exceptions? (Even if those settings were throwing exceptions inconsistently.) If you could also include a URL to this thread, it will also be helpful to quickly link the two tickets together.

                      Where are the example Managed/Unmanaged strategy examples you referenced? My strategy is likely to be quite a bit more complex with the several targets per entry, several entries in parallel, moving of stops etc., but indeed would be happy to see if there's something in the examples I should leverage.
                      I think we should take the route of having QA look into the matter further on our end, but the strategies I was referencing were the SampleMACrossover strategy, SampleOnOrderUpdate strategy, and the UnmanagedTemplate strategy. SampleMACrossover is our go-to testing strategy that is bundled with the platform, while SampleOnOrderUpdate demonstrates using OnOrderUpdate() OnExecutionUpdate() and Order objects. SampleOnOrderUpdate would need to have parameters added to be optimized however.

                      SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

                      UnamangedTemplate is attached.

                      I look forward to working with you further on this matter via email.
                      Attached Files
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Hi Jim,

                        Thank you for the response and offer for further support. The algorithm we have is unique or at least not publicly shared anywhere else, and is performing quite well, so we would rather not share it even via e-mail, and understand you are then not able to support in more detail. The offer for support is much appreciated.

                        As the issues have only happened with the Genetic optimizer and the Default optimizer is working fine, we have switched over to that. We just have to plan the optimization runs and how to divide up the universe smartly, and we'll also get more accurate settings as a result.

                        Many thanks for the support!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by gm7_forum, Today, 05:51 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post gm7_forum  
                        Started by cre8able, Today, 03:20 PM
                        1 response
                        9 views
                        0 likes
                        Last Post cre8able  
                        Started by fiddich, Today, 05:25 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post fiddich
                        by fiddich
                         
                        Started by gemify, 11-11-2022, 11:52 AM
                        6 responses
                        804 views
                        2 likes
                        Last Post ultls
                        by ultls
                         
                        Started by ScottWalsh, Today, 04:52 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post ScottWalsh  
                        Working...
                        X