• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy analyzer multiple iteration error

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

    Strategy analyzer multiple iteration error

    I'm using the code below to generate some trade statistics in the strategy analyzer. This works in a single iteration without error. When I run it with 50 iterations it generates errors. The error generated is that the "sequence contains no elements" on a different bar number for each iteration. The code is placed in OnExecutionUpdate section. The aim is to generate these stats when a trade is closed. If this code is removed, no errors are generated. Is it possible to run this with multiple iterations?

    Code:
    if ((execution.Order.OrderAction == OrderAction.BuyToCover ||  execution.Order.OrderAction == OrderAction.Sell) && execution.Order.OrderState == OrderState.Filled)
            {
                Trade last = SystemPerformance.AllTrades.Last();
                 var a = new []{
                    last.TradeNumber.ToString(),
                    execution.Instrument.FullName,
                    last.Quantity.ToString(),
                    last.Entry.Price.ToString("F"),
                    last.Exit.Price.ToString("F"),
                    last.Entry.Time.ToShortDateString(),
                    last.Exit.Time.ToShortDateString(),
                    last.Entry.Name,
                    last.Exit.Name,
                    last.ProfitCurrency.ToString("F"),
                    SystemPerformance.AllTrades.TradesPerformance.Currency.CumProfit.ToString("F"),
                    last.MaeCurrency.ToString("F"),
                    last.MfeCurrency.ToString("F")
                };
            }

    #2
    Hello Matts,

    Thank you for the post.

    It looks like this is likely the AllTrades.Last method causing this error however I don't see you have any error checking here either. If this is currently being run when there are no trades in the collection, that is likely the problem as the Last has nothing to return. To avoid this error you can surround this statement with a check if there is a count:

    Code:
    if (SystemPerformance.AllTrades.Count > 0)
    {
    
    }
    The reason that this is likely only showing up in the optimization is the change of parameters causing a different sequence of trades to happen. You could additionally see this in a standard test if this was called when there were 0 trades in the collection, for example on bar 0 of OnBarUpdate. I would suspect this works in the standard test because of the sequence the orders are being placed in combination with the used if statement which is targeting specific order types.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for reply Jesse. I tested with the if statement as shown below. I also removed the order condition statement. This prevented the error from occurring, however at no time was the count greater than zero. The strategy analyzer was able to complete, but I'm unable to access the trade stats.
      Code:
      if (SystemPerformance.AllTrades.Count > 0) {
        Print("Count is greater than zero");
        else {
        Print("Order execution but there are no trades!");
      }

      Comment


        #4
        Hello Matts,

        Thank you for your reply.

        I tried this on my end and see a count, are you certain the script is placing paired trades in that test? I would suggest doing a more simple test by using the SampleMACrossOver (make a duplicate) then add a Print and OnExecution to make sure that's working. For example:

        Code:
        protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
        {
            Print(SystemPerformance.AllTrades.Count);
        }
        I have attached the test script I used which I can see the trade count increment.

        This will help to confirm the collection is working in the use case. If that is working, the problem is very likely due to something in the logic of the script when used in the optimization. If that is true, I would likely need a more specific test script which demonstrates the problem to proceed with troubleshooting.

        I look forward to being of further assistance.
        Attached Files
        JesseNinjaTrader Customer Service

        Comment


          #5
          I tested with your script on 2 different installations of Ninjatrader. It looks like it only occurs in the 32-bit version, and not 64-bit. To reproduce, I used 3 years of daily data, on a group of instruments using the <Select All> option.

          Comment


            #6
            Hello Matts,

            Thank you for your reply.

            That explains a lot if you are using the 32-bit process, you can find details about this here:
            https://ninjatrader.com/support/help...inbacktest.htm

            During Strategy Analyzer Optimization in a 32-bit process, the IncludeTradeHistoryInBacktest property is forced to false due to the limited resources available in a 32-bit environment. You must use a 64-bit process if trade history is needed during optimization.
            IncludeTradeHistoryInBacktest is needed here however when using an Optimization specifically, this is false for 32 bit processes. You would need to use the 64 bit to bypass this.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks Jesse, I wasn't aware of this limitation. Also I thought it was a standard test, but really it qualifies as an optimization because it uses multiple instruments. Is there any way to forward test a strategy on daily data? I tried using the Sim account, but if the connection is disconnected, the strategy is disabled.

              Comment


                #8
                Hello Matts,

                Thank you for your response.

                If you need realtime data for your forward testing than unfortunately any disconnect would force the strategy to disable.

                Have you looked into Walk Forward Optimizations? These allow for optimizing over a period and then forward testing over the next period. You can find more details at the following link: https://ninjatrader.com/support/help...e_a_strate.htm

                Please let me know if you have any questions.
                Patrick H.NinjaTrader Customer Service

                Comment


                  #9
                  I've done some walk forward optimizations before which I found useful, but I've found that the only way I can properly test a strategy is to paper trade it with current market prices before I would consider using it with real money. For a daily strategy, I also want to create some conditions that will make for best entry and exit price, but I don't have historical tick or minute data to do this.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by tvaughan4, 12-13-2019, 10:46 PM
                  2 responses
                  14 views
                  0 likes
                  Last Post tvaughan4  
                  Started by digibob, 12-13-2019, 10:01 PM
                  3 responses
                  12 views
                  0 likes
                  Last Post digibob
                  by digibob
                   
                  Started by ldissinger, Yesterday, 12:55 PM
                  1 response
                  15 views
                  0 likes
                  Last Post ldissinger  
                  Started by pstrusi, Yesterday, 09:06 AM
                  1 response
                  27 views
                  0 likes
                  Last Post marcus2300  
                  Started by vpzdcv, Yesterday, 02:31 AM
                  0 responses
                  13 views
                  0 likes
                  Last Post vpzdcv
                  by vpzdcv
                   
                  Working...
                  X