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

while loop problem

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

    while loop problem

    I think this loop is running infinitely. The purpose of this while loop is to assign the highest closing price while the stochastic is above 80 to a double so I can use it another method. This should be a simple process but this is my first time using a loop and I'm obviously doing it wrong.

    here is the code:

    double stochfast = StochasticsFast(StochD, StochK).D[0];
    double highestcloseabove80 = 0;

    while(stochfast > 80)
    {
    highestcloseabove80 = Close[0];
    if(Close[0] > highestcloseabove80)
    {
    highestcloseabove80 = Close[0];
    }

    }


    When I run this on a list of instruments in the analyzer my cpu gets maxed out and I have a very fast computer so I'm assuming this loop is running infinitely. Shouldn't this terminate on it's own when the while condition evaluates as false (stochfast < 80)? I need somebody that knows what they're doing to tell me how to achieve the desired result. Thanks in advance.
    Last edited by gordongekko; 01-26-2018, 11:14 AM.

    #2
    Hello,

    Thank you for the post.

    You are likely correct, while loops are not generally suggested when using NinjaScript specifically as this holds the logic execution at this point until you break out of the while loop.

    Very likely the solution would be to change the while to an if statement. When using the platform you do not want to hold OnBarUpdate from processing as new data comes in, this would prevent the script from working in Realtime.

    an if statement would essentially work the same way here assuming we have the same expectation.

    when OBU is called, it will execute this code one time for this bar (or each tick depending on the settings). This would be repeated over and over for each new bar, so assuming that this condition should occur out over a few bars time the condition should remain true for a period of time.

    The following page contains some information on how bars are built and processed which can be helpful in understanding the general flow a script should follow. https://ninjatrader.com/support/help...nstruments.htm

    I would also suggest using Prints as they are very key to understanding how your logic is working. You could add a Print into OnBarUpdate to verify if your stuck in the loop.
    Code:
    Print(CurrentBar + " enter while loop");
    while(stochfast > 80)
    {
    
    }
    Print(CurrentBar + " exit while loop");


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

    Comment


      #3
      Would this work by adding a break statement?

      while(stochfast > 80)
      {
      highestcloseabove80 = Close[0];
      if(Close[0] > highestcloseabove80)
      {
      highestcloseabove80 = Close[0];
      }
      if(stochfast < 80)
      break;
      }

      Comment


        #4
        Hello,

        Possibly you would need to test it.

        I still wouldn't advise using a while loop as that is prone to locking in case you didn't account for all situations that can occur to break the loop. If you test this and it works for the use, then I could say that would work. in reality, you can really use whatever logic you want so long as it achieves the intended goal.

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by trilliantrader, 04-18-2024, 08:16 AM
        4 responses
        18 views
        0 likes
        Last Post trilliantrader  
        Started by mgco4you, Today, 09:46 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by wzgy0920, Today, 09:53 PM
        0 responses
        10 views
        0 likes
        Last Post wzgy0920  
        Started by Rapine Heihei, Today, 08:19 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by Rapine Heihei, Today, 08:25 PM
        0 responses
        10 views
        0 likes
        Last Post Rapine Heihei  
        Working...
        X