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

Trouble with Brackets, Parentheses, and Conditional "If" Statements

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

    Trouble with Brackets, Parentheses, and Conditional "If" Statements

    I continue writing a strategy that will adjust Connors RSI values, depending on whether market is strong or weak. Yes, those are general characterizations, which I have defined specifically in my code. That's not germane to my issue below.

    The idea of the following two Condition Sets is to adjust Connors RSI thresholds for identifying entry or reversal prices. In the first Condition Set, my intention is to increment the entry SignalThreshold variable by a constant double I've defined as SlopeFactor.

    In the second Condition Set, my intention is to increment the newly calculated revised value of SignalThreshold by SlopeFactor a second time, if the Daily Range of the trading session is at least TargetBDistance (another double variable).

    Thus, my intention is for SignalThreshold to increment higher by SlopeFactor when market has not attained something I've defined as OppositeThreshold. But if market has not reached OppositeThreshold when Daily Range is at least TargetBDistance wide, I want NT to increment SignalThreshold again. Speaking generically, for this second case, the result should be SignalThreshold + SlopeFactor + SlopeFactor.

    I believe either my brackets are misplaced, or maybe I've not written my condition correctly. The Output Window only shows those adjustments NT has made in the second case, as if it has ignored the first case. (Note in my coded Print statements, I've labeled those changes (1) and (2), so I can know which Condition Set has changed SignalThreshold and which has not.)

    As always, I will appreciate any suggestions provided in this forum.

    Thanks.

    ==========

    Extracted Condition Sets from my strategy:

    // If CRSI has NOT FALLEN BELOW OppositeThreshold during LookbackPeriod, market is not weak and sell signals could be premature:

    if (OppositeThreshold < MIN(anaConnorsRSI(240, 3, 2).ConnorsRSI, LookbackPeriod)[0])

    {
    SignalThreshold = Math.Min(100.0, Math.Max(0.0, SignalThreshold + SlopeFactor));
    OppositeThreshold = Math.Min(100.0, Math.Max(0.0, OppositeThreshold + SlopeFactor));
    Counter = 0;
    Print("Timestamp for FPSell Threshold Change (1): " + Time[0]);
    Print("Counter = " + Counter);
    Print("initialThreshold = " + initialThreshold);
    Print("signalThreshold = " + signalThreshold);
    Print("oppositeThreshold = " + oppositeThreshold);
    }

    // If current range has expanded beyond TargetBDistance while market is not weak, sell signals could be premature:

    if (TargetBDistance <= CurrentDayOHL().CurrentHigh[0] - CurrentDayOHL().CurrentLow[0]
    && OppositeThreshold < MIN(anaConnorsRSI(240, 3, 2).ConnorsRSI, LookbackPeriod)[0])

    {
    SignalThreshold = Math.Min(100.0, Math.Max(0.0, SignalThreshold + SlopeFactor));
    OppositeThreshold = Math.Min(100.0, Math.Max(0.0, OppositeThreshold + SlopeFactor));
    Counter = 0;
    Print("Timestamp for FPSell Threshold Change (2): " + Time[0]);
    Print("Counter = " + Counter);
    Print("initialThreshold = " + initialThreshold);
    Print("signalThreshold = " + signalThreshold);
    Print("oppositeThreshold = " + oppositeThreshold);
    }

    #2
    Hello Longhornmark,

    Thank you for your post.

    I notice that you refer to several variables differently at different times...for example, you're using both SignalThreshold and signalThreshold. Is this intentional? Please note that variables are case sensitive so SignalThreshold is not the same as signalThreshold.

    I would suggest trying to put some prints outside of these statements so you can understand whether or not the variables in the conditions would evaluate to True - at this point, you know you're getting prints from one and not the other. If you add some prints outside of these sets you should be able to look and tell which sets should be triggered on a given bar.

    Another thing to try would be setting this logic up in the Strategy Builder so you can then unlock that code and make sure your syntax is correct. You can even add prints using the Strategy Builder directly.

    This forum post goes into great detail on how to use prints to help figure out where issues may stem from — this should get you going further in the correct direction.



    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Kate, I've noticed when I simply put a Print statement outside the bracketed statements, the strategy yields a different result. That is, it prints arrows on bars for which it hadn't printed arrows before the outside Print statement. I don't understand how a Print statement can change the calculation to yield a different a result. Could enclosing a Print statement with { } versus not enclosing it with curly brackets cause this behavior?
      Last edited by Longhornmark; 06-01-2019, 12:33 PM.

      Comment


        #4
        When my custom strategy tests an if-condition and determines that it is false, and if in that case I then want it to leapfrog over all remaining condition sets without testing them while waiting for the next OnBarUpdate(), how do I tell it to skip all remaining condition sets and wait until the current bar closes before beginning its tests again?

        (Note: I asked this question over the weekend in a general C# forum, but the expert who responded didn't know what I meant by OnBarUpdate(). So, I guess this is a NinjaTrader question, rather than a C# question.)

        Specifically, the following are my tests. If they all are true, I want the strategy to continue to the next statement. But if any is false, I want it to stop further testing and wait for OnBarUpdate().

        But as presently written, it seems to me whether those tests are true or false, the strategy will continue to the next statement. So, when false, how do I prevent it from advancing to the next statement?

        ==========

        // If any of the most recent 4 candles including candle[0] exceeds TargetADistance in length, prevent a sell signal at candle[0] unless its low would be more than TargetADistance above PeriodLow:

        if ((Math.Abs(TargetADistance) < Math.Abs(High[0] - Low[0])
        || Math.Abs(TargetADistance) < Math.Abs(High[1] - Low[1])
        || Math.Abs(TargetADistance) < Math.Abs(High[2] - Low[2])
        || Math.Abs(TargetADistance) < Math.Abs(High[3] - Low[3]))

        && PeriodHHandLL(108).PeriodLow[0] < Low[0] + TargetADistance
        && PeriodHHandLL(108).PeriodLow[1] < Low[1] + TargetADistance
        && PeriodHHandLL(108).PeriodLow[2] < Low[2] + TargetADistance
        && PeriodHHandLL(108).PeriodLow[3] < Low[3] + TargetADistance

        || (Math.Abs(High[0] - Low[0]) < Math.Abs(TargetADistance)
        && Math.Abs(High[1] - Low[1]) < Math.Abs(TargetADistance)
        && Math.Abs(High[2] - Low[2]) < Math.Abs(TargetADistance)
        && Math.Abs(High[3] - Low[3]) < Math.Abs(TargetADistance)

        // 70 & 71.428571428571 failed here:

        && Math.Abs(anaConnorsRSI(240, 3, 2).ConnorsRSI[1] - anaConnorsRSI(240, 3, 2).ConnorsRSI[0]) < 73.0))

        The strategy continues onward from here....
        Last edited by Longhornmark; 06-03-2019, 09:59 AM. Reason: To provide the specific situation in which I want the tests to terminate until OnBarUpdate() when any of those tests is false.

        Comment


          #5
          Hello Longhornmark,

          Thank you for your replies.

          If/else statements have a specific structure:

          if (condition a is true)
          {
          // do something here
          }
          else if (condition b is true)
          {
          // do something else here
          }
          else if (condition c is true)
          {
          // do something else here
          }

          You cannot place other statements in between the closing bracket and the next else if, or you will get compile errors. In an if/else statement, only the first condition that evaluates to true will have it's code executed. If any other condition in the set is also true, that will not be executed.

          Contrast that with using three separate if statements:

          if (condition a is true)
          {
          // do something here
          }

          if (condition b is true)
          {
          // do something else here
          }

          if (condition c is true)
          {
          // do something else here
          }

          The difference between the two structures is this:

          In this case, if condition a and condition c are both true, both sets of code will be executed, starting with the condition a block. However, if you wanted it to skip any remaining code in OnBarUpdate if our condition a is true, you could use a return statement at the end of the code block like so:

          if (condition a is true)
          {
          // do something here
          return; //this will skip all remaining code in OnBarUpdate if condition a is true, after it's completed running through any code above it in this if statement.
          }

          if (condition b is true)
          {
          // do something else here
          }

          if (condition c is true)
          {
          // do something else here
          }

          Here what would happen is that if condition a is true, the code within that if statement will execute. Once it hits the return statement, it will skip all remaining code in OnBarUpdate including conditions b and c. If condition a is false, it will then evaluate conditions b and c.

          Also, note that you can nest if statements to control the flow of OnBarUpdate, like below:

          if (condition a is true)
          {
          if (condition b is true)
          {
          //do something here
          }
          }

          As far as your print statements go, wrapping a print in curly braces will do nothing if there is no if statement above it. Putting a print within an action block succeeding an if statement would only allow the print to print if the if statement evaluates as true. You cannot place other statements in the middle of an if/else statement (between the closing bracket and the next else if), or you will get compile errors - this includes Print statements. This could account for the behavior you're seeing with changes in results.

          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Hi Kate - You have provided me with a lot of valuable information in your post. I also have found a site, which has provided me with general instruction and examples. Between that site and your post, I believe I'm on the right track now. Thank you!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, Yesterday, 06:40 PM
            2 responses
            18 views
            0 likes
            Last Post algospoke  
            Started by ghoul, Today, 06:02 PM
            3 responses
            14 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            44 views
            0 likes
            Last Post jeronymite  
            Started by Barry Milan, Yesterday, 10:35 PM
            7 responses
            20 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by AttiM, 02-14-2024, 05:20 PM
            10 responses
            180 views
            0 likes
            Last Post jeronymite  
            Working...
            X