Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IndexOutOfRangeException error in own code

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

    IndexOutOfRangeException error in own code

    Code:
    private int GetTickValuePosAvgToTP(Order order)
    {
    int tickValue = 0;
    
    if (order == null || Position == null)
    return 0;
    
    try
    {
    tickValue = Position.MarketPosition == MarketPosition.Long ? (int)((order.LimitPrice - Position.AveragePrice) / TickSize)
    : Position.MarketPosition == MarketPosition.Short ? (int)((Position.AveragePrice - order.LimitPrice) / TickSize)
    : 0;
    }
    catch (Exception ex)
    {
    string errorMsg = string.Format("[" + this.GetType().Name + "][" + MethodBase.GetCurrentMethod().Name + "]-[ERROR]-{0}", ex.ToString());
    Debug.Print(errorMsg);
    Print(errorMsg);
    Log(errorMsg, LogLevel.Error);
    }
    return (int)Instrument.MasterInstrument.RoundToTickSize(tickValue);
    }
    i run this method and get an error message every now and then

    02.03.2021 18:16:45:000 > [SidiSystemBase][OnOrderUpdate]-[ERROR]-System.IndexOutOfRangeException: Der Index war außerhalb des Arraybereichs.
    bei NinjaTrader.NinjaScript.StrategyBase.get_Position( )
    bei NinjaTrader.NinjaScript.Strategies.SidiSystemBase. GetTickValuePosAvgToTP(Order order)

    can someone tell me where to look for the error ;-) i am currently stuck looking for IndexOutOfRangeException. where in the code could the error even occur?

    Click image for larger version

Name:	Screenshot_1.jpg
Views:	322
Size:	51.2 KB
ID:	1144448
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thank you for your reply.

    I suspect it's from checking that Position isn't null instead of checking that it isn't flat before returning 0 - Position isn't ever null. Position.MarketPosition can either be MarketPosition.Flat, MarketPosition.Long, or MarketPosition.Short.

    If you switch this:


    if (order == null || Position == null)
    return 0;

    to this:


    if (order == null || Position.MarketPosition == MarketPosition.Flat)
    return 0;

    Do you still get the out of range error?

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

    Comment


      #3
      Hi _Kate, the method is called by OnOrderUpdate(), so it should be rather less flat, shouldn't it? but I will try ;-)
      sidlercom80
      NinjaTrader Ecosystem Vendor - Sidi Trading

      Comment


        #4
        so far all is well, it seems that the problem has been solved, thank you very much! if it should occur again, i will get back to you
        sidlercom80
        NinjaTrader Ecosystem Vendor - Sidi Trading

        Comment


          #5
          Hi _Kate the error has just occurred again, so it was not due to the missing "Position.MarketPosition == MarketPosition.Flat"

          Code:
          if (order == null || Position.MarketPosition == MarketPosition.Flat)
          return 0;

          the code currently in use:
          Code:
          private int GetTickValuePosAvgToTP(Order order)
          {
          int tickValue = 0;
          
          if (order == null || Position.MarketPosition == MarketPosition.Flat)
          return 0;
          
          try
          {
          tickValue = Position.MarketPosition == MarketPosition.Long ? (int)((order.LimitPrice - Position.AveragePrice) / TickSize)
          : Position.MarketPosition == MarketPosition.Short ? (int)((Position.AveragePrice - order.LimitPrice) / TickSize)
          : 0;
          }
          catch (Exception ex)
          {
          string errorMsg = string.Format("[" + this.GetType().Name + "][" + MethodBase.GetCurrentMethod().Name + "]-[ERROR]-{0}", ex.ToString());
          Debug.Print(errorMsg);
          Print(errorMsg);
          Log(errorMsg, LogLevel.Error);
          }
          return (int)RoundToTickSize(tickValue);
          }
          error code:
          PHP Code:
          09.03.2021 17:08:54:000 > [SidiSystemBase][GetTickValuePosAvgToTP]-[ERROR]-System.IndexOutOfRangeException: Der Index war außerhalb des Arraybereichs.
          bei NinjaTrader.NinjaScript.NinjaScriptBase.get_TickSi ze()
          bei NinjaTrader.NinjaScript.Strategies.SidiSystemBase. GetTickValuePosAvgToTP(Order order) 
          
          do you have any other ideas what it could be?
          sidlercom80
          NinjaTrader Ecosystem Vendor - Sidi Trading

          Comment


            #6
            Hello sidlercom80,

            Thank you for your reply.

            Where are you calling this method in your code? Is it being called from an instrument thread/data processing method, and if not does the issue go away using TriggerCustomEvent?



            Also, can you try Instruments[0].MasterInstrument.TickSize instead of just TickSize to have a definite index for the Instrument?

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

            Comment


              #7
              Hi _Kate
              Where are you calling this method in your code?
              from "OnOrderUpdate" should I therefore use TriggerCustomEvent?

              Also, can you try Instruments[0].MasterInstrument.TickSize instead of just TickSize to have a definite index for the Instrument?
              I will definitely try it out
              sidlercom80
              NinjaTrader Ecosystem Vendor - Sidi Trading

              Comment


                #8
                Hello sidlercom80,

                Thanks for your reply.

                OnOrderUpdate generally runs on a non-instrument/ui thread, so I'd give it a try there and see if that resolves it, yes.

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

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Yesterday, 05:17 AM
                0 responses
                71 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                143 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                76 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                47 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                51 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X