Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Out-of-range error on SystemPerformance.AllTrades

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

    Out-of-range error on SystemPerformance.AllTrades

    I am getting an out of bounds error as the title says.
    I have been using

    Code:
    Trade lasttrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    for years. Not sure what I could have changed to break it. In the screenshot, I have put debugging logic to count the trades, and show their execution. When I try to access the last trade after it has counted that 2 trades have both been placed, and completed, it throws the out-of-bounds exception.

    Click image for larger version

Name:	2022-06-28 10_37_33-NinjaScript Output.png
Views:	177
Size:	1.2 KB
ID:	1206872

    #2
    Hello kfinkelstein,

    Thank you for your note.

    Are you checking that there are trades in SystemPerformance before trying to access them?

    Code:
    if (SystemPerformance.AllTrades.Count > 0)
    {
    Trade lasttrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    }
    I would also advise turning on Trace Orders to confirm trades are actually being taken.

    Code:
     if (State == State.SetDefaults)
    {
    TraceOrders = true;
    }
    Once you then recompile the strategy, you can open a new NinjaScript Output window under New > NinjaScript Output. This will print a log of any orders submitted by the strategy during while it's running, along with any ignored orders. You can then look through and see what may be occurring.

    Here is a link to our help guide that goes into more detail on tracing orders:

    https://ninjatrader.com/support/help...aceorders2.htm

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      The screenshot shows 2 orders placed, and their stop-loss being triggered before I even try to access the array. I also tried to access the first trade explicitly, knowing there should be two trades in the array, and it still threw an exception

      Comment


        #4
        Code:
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                TraceOrders = true;
            }
        }
        This did not have any perceivable effect. Orders are definitely being placed, but nothing is outputting to the console/debug window other than the logic I already had Printing there.
        Click image for larger version

Name:	2022-06-28 12_48_32-Window.png
Views:	164
Size:	116.5 KB
ID:	1206892

        Comment


          #5
          Hello kfinkelstein,

          Thank you for your reply.

          Please disregard the post I made just a moment ago, as it appears you replied as I was typing a response. I've deleted that now.

          If you print the value of SystemPerformance.AllTrades.Count, what result is seen? Or does attempting to print this cause an error?

          Your screenshot above doesn't result in an error that I can see. Can you provide a code sample or simplified example script that reproduces the error you are seeing?

          Thanks in advance; I look forward to assisting you further.

          Comment


            #6
            Quick update, I updated ninjatrader. The order trace is working now but it still errors out when trying to index trades
            Click image for larger version

Name:	2022-06-28 12_57_03-NinjaScript Output.png
Views:	160
Size:	88.3 KB
ID:	1206896

            Comment


              #7
              That number (1, 2 etc) Is SystemPerformance.AllTrades.Count

              Comment


                #8
                Hello kfinkelstein,

                Thank you for your reply.

                Looks like the error is occurring in OnExecutionUpdate - can you supply your code from that method?

                Thanks in advance; I look forward to assisting you further.

                Comment


                  #9
                  Code:
                   protected override void OnExecutionUpdate (
                  Execution execution, string executionId, double price, int quantity,
                  MarketPosition marketPosition, string orderId, DateTime time
                  )
                  {
                  // This is the code I used to discover the Stop loss order's name
                  //Print("OOU: order: " + execution.Order);
                  
                  // Now that I know it is 'Stop loss', I can use this name here
                  if (execution.Order.Name == "Stop loss" && SystemPerformance.AllTrades.TradesCount >0)
                  {
                  double reward2 = get_reward();
                  reward_and_update(reward2);
                  Print(SystemPerformance.AllTrades.TradesCount);
                  
                  Print("Stop Loss Triggered");
                  }
                  if (execution.Order.Name == "Profit target" && SystemPerformance.AllTrades.TradesCount >0)
                  {
                  double reward2 = get_reward();
                  reward_and_update(reward2);
                  Print(SystemPerformance.AllTrades.TradesCount);
                  Print("Take Profit Triggered");
                  }
                  
                  }
                  The error is being thrown in the get_reward() function. I can't share too much of that code but here is how it is currently working. This snippet is the only way it attempts to index the array


                  Code:
                  public double get_reward()//string trade_state
                  {
                  
                  if (SystemPerformance.AllTrades.TradesCount > 2 )//&& State == State.Realtime
                  {
                  
                  //Trade lasttrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.TradesCount -1];//-1
                  Trade lasttrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
                  //Trade lasttrade = SystemPerformance.AllTrades.Last(); //Print(SystemPerformance.AllTrades.TradesPerformanc e);
                  I didn't mess with the curly braces so this snippet isn't instantly usable but you get the idea hopefully. There isnt much going on wrt accessing the array.

                  Comment


                    #10
                    OK as per


                    I needed to have the strategy attached to a chart. Super annoying and seems like a bug

                    Comment


                      #11
                      Also, thanks for your help! Hopefully this thread assists people in the future.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      53 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      130 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      70 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      44 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      49 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X