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

Fresh eyes needed for rucursive method

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

    Fresh eyes needed for rucursive method

    My coding skills are pretty rusty. Can someone find the error in the recursive method HighIn().

    Code:
    private int firstRun=0;
    
    protected override void OnBarUpdate(){
       if(firstRun<15){
          firstRun++;
       return;
       }
    
       Print(HighIn(3));
    }
    
    private double HighIn(int bars){
       if(bars <= 2){
           return High[bars];
       }
       double value = HighIn(bars--);
       if(value>High[bars]){return value;}else{ return High[bars];}
    }

    #2
    It compiles fine, but crashes NT when I try to run it in the strategy analyzer.

    Comment


      #3
      Hi
      So first and foremost the -- in
      Code:
      bars--
      is not decrementing right away
      Example


      Code:
      int i = 3;
      Console.WriteLine(i); // output: 3
      Console.WriteLine(i--); // output: 3
      Console.WriteLine(i); // output: 2
      So you would need to use --bars

      I would introduce an extra variable though to keep the value of --base to have control and make debugging easier.

      Comment


        #4
        Hello Chippy,

        If the method crashes the platform that means you entered an infinite loop. You would need to use prints to identify what the problem is with the way you have called the method recursively. One problem may be that you are trying to decrement a passed in variable. I am not sure what your goal was with the method to comment on a possible solution however using a Print with the variables you are using should help to identify what the problem is before the crash.

        If possible I would suggest to avoid recursive functions and use a standard finite for loop.

        Please let me know if I can be of additional help.

        JesseNinjaTrader Customer Service

        Comment


          #5
          SuneSorgenfrei..... thank you, that did it. I've spent hours trying to find it. I originally downloaded VS 2022 which NT apparently can't launch so I couldn't debug. I'm currently DL'ing 2019. Stepping through the code would have been a big help.

          Comment


            #6
            Jesse, "print" doesn't help much if it crashes. It was indeed an infinite loop. Simply having the decrement after the variable instead of before was the problem. Credit and thanks to SuneSorgenfrei for finding it.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by gotturbo, Yesterday, 06:41 PM
            1 response
            25 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by fromagno, Today, 02:30 AM
            0 responses
            15 views
            0 likes
            Last Post fromagno  
            Started by FrederLksenn, Today, 02:18 AM
            0 responses
            13 views
            0 likes
            Last Post FrederLksenn  
            Started by alphaextractuk, Today, 02:05 AM
            0 responses
            10 views
            0 likes
            Last Post alphaextractuk  
            Started by alphaextractuk, Today, 02:03 AM
            0 responses
            1 view
            0 likes
            Last Post alphaextractuk  
            Working...
            X