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

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!");[INDENT]}[/INDENT]

      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:


            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.

                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 Radano, 06-10-2021, 01:40 AM
                  20 responses
                  616 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by Mizzouman1, Today, 07:35 AM
                  0 responses
                  4 views
                  0 likes
                  Last Post Mizzouman1  
                  Started by i019945nj, 12-14-2023, 06:41 AM
                  6 responses
                  66 views
                  0 likes
                  Last Post i019945nj  
                  Started by aa731, Today, 02:54 AM
                  1 response
                  8 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Started by BarzTrading, Today, 07:25 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post BarzTrading  
                  Working...
                  X