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

Condition Definition

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

    Condition Definition

    Hello NT,

    I want to add some code to a custom indicator that will continue to color bars the same color after the first condition (Condition1) occurs until a new condition (Condition2) occurs. I’m trying to figure out how I need to set up Condition1 and Condition2 since it needs to replicate the condition of the previous bar and apply the same color. Do I need to create a method for these or is it something else?

    Input variables declared:
    Entry1_sl
    Entry1_slx

    Condition1 = Close[0] < Close[Entry1_sl] and Close[0] > Close [Entry1_slx]
    Condition2 = Close[0] > Close[Entry1_sl] and Close[0] < Close [Entry1_slx]

    If Condition1 = False and Condition2 = False then
    Condition1 = Condition1[1]
    If Condition1 = False and Condition2 = False then
    Condition2 = Condition2[1]

    Thanks,
    Ben

    #2
    Hello Ben,

    Thanks for your post.

    I would suggest using a bool as director of the color and use the conditions to set the state of the bool. The bool will maintain its state (and subsequently the brush color) until changed by the conditions.

    for example

    if (your conditions1)
    {
    myBool = true;
    }
    if (your conditions2)
    {
    myBool = false;
    }

    BarBrush = myBool ? Brushes.Blue : Brushes.Orange; // set the bar color based on the bool, true = blue, false = orange.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      I think your logic is pretty close already.

      I'd say use a Brush initialized to the previous bar's color,
      Code:
      Brush NewBackBrush = BackBrushes[1];
      Then go through your logic. If both your conditions are false
      then your code never overrides NewBackBrush.

      After your logic completes, add a final line of code that
      does the real work,
      Code:
      BackBrushes[0] = NewBackBrush;
      and you're done.

      See that?

      Just bracket your existing code with those two lines.

      Restructure your logic so it's purpose is to assign a
      new value to NewBackBrush, making sure the logic
      effectively does nothing if conditions are never met.
      Last edited by bltdavid; 08-04-2021, 08:32 AM.

      Comment


        #4
        Thanks Paul and David. Appreciate you guys looking at this. I tried both suggestions but I'm such noob I couldn't get the bool variable declared or get the BackBrushes to work.

        This is where I starting from that I am able to get to work. I'm trying to color the bars in between these two codes. So if I get blue bars, it will continue to color the bars blue until I get orange bars and vice versa.

        Code:
         if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])
        {if (showpaintbars)
        BarBrushes[0] = Brushes.Blue;
        }
        Code:
         else if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
        {
        if (showpaintbars)
        BarBrushes[0] = Brushes.Orange;
        I think I need both your ideas but I'm not sure if I should test if the following Condition1 and Condition2 are false before or after the codes above. I think I need a boolean statement for the following Conditions 1 and Condition2 being false that initiates the Brush NewBackBrush = BackBrushes[1]; and BackBrushes[0] = NewBackBrush; idea.

        Code:
        // Condition1 = Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx];
        // Condition2 = Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx];
        Any suggestions you may have appreciated.

        Thanks,
        Ben

        Comment


          #5
          Hello harr5754,

          Thanks for your reply.

          Just to clarify, BackBrush will color the background behind the bar. If you want to color the bar you would need to use BarBrush or BarBrushes[].
          References:
          https://ninjatrader.com/support/help.../?barbrush.htm
          https://ninjatrader.com/support/help...barbrushes.htm
          https://ninjatrader.com/support/help...?backbrush.htm
          https://ninjatrader.com/support/help...ackbrushes.htm

          To create a bool variable, I would suggest creating it above the line that shows: protected override void OnStateChange()

          It would look like this:

          private bool myBool;

          protected override void OnStateChange()


          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_PaulH View Post
            Just to clarify, BackBrush will color the background behind the bar. If you want to color the bar you would need to use BarBrush or BarBrushes[].
            You're quite right, my bad.

            I did not read OP's first post carefully enough,
            and so I answered it from the wrong mindset.

            Comment


              #7
              Hi Paul,

              My code below is getting all kinds of error messages but I'm throwing it out there so I can explain what I'm trying to do. Here goes:

              If bool1 is true, the bar is painted blue by the original code. If bool1 is false and bool2 is false, it keeps painting the bar blue by making BarBrushes[0] = BarBrushes[1] until bool2 is true. If bool2 is true, the bar is painted orange by the original code further down in the script. If bool2 is false and bool1 is false, it keeps painting the bar orange by making BarBrushes[0] = BarBrushes[1] until bool1 is true.

              Code:
              bool1 = (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx]);
              bool2 = (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx]);
              
              if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])
              {if (showpaintbars)
              BarBrushes[0] = Brushes.Blue;
              }
              if (bool1 = false && bool2 = false)
              BarBrushes[0] = BarBrushes[1]
              
              else if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
              {
              if (showpaintbars)
              BarBrushes[0] = Brushes.Orange;
              }
              if (bool1 = false && bool2 = false)
              BarBrushes[0] = BarBrushes[1]
              I hope I was able to explain that.

              Thanks,
              Ben

              Comment


                #8
                Originally posted by harr5754 View Post
                If bool1 is true, the bar is painted blue by the original code. If bool1 is false and bool2 is false, it keeps painting the bar blue by making BarBrushes[0] = BarBrushes[1] until bool2 is true. If bool2 is true, the bar is painted orange by the original code further down in the script. If bool2 is false and bool1 is false, it keeps painting the bar orange by making BarBrushes[0] = BarBrushes[1] until bool1 is true.
                Try this,
                Code:
                bool1 = (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx]);
                bool2 = (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx]);
                
                if (bool1)
                    BarBrushes[0] = Brushes.Blue;
                else if (bool2)
                    BarBrushes[0] = Brushes.Orange;
                else
                    BarBrushes[0] = BarBrushes[1];

                Comment


                  #9
                  Or this,
                  Code:
                  bool1 = (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx]);
                  bool2 = (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx]);
                  
                  Brush NewBrush;
                  
                  if (bool1)
                      NewBrush = Brushes.Blue;
                  else if (bool2)
                      NewBrush = Brushes.Orange;
                  else
                      NewBrush = BarBrushes[1];
                  
                  if (showpaintbars)
                      BarBrushes[0] = NewBrush;

                  Comment


                    #10
                    Here is your original code, cleaned up a bit,

                    Code:
                    bool1 = (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx]);
                    bool2 = (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx]);
                    
                    if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])
                    {
                        if (showpaintbars)
                            BarBrushes[0] = Brushes.Blue;
                    }
                    else if (Close[0] < Close[entry1_sl] && Close[0] > Close[entry1_slx])
                    {
                        if (showpaintbars)
                            BarBrushes[0] = Brushes.Orange;
                    }
                    else
                    {
                        if (showpaintbars)
                            BarBrushes[0] = BarBrushes[1];  // <-- missing semi-colon
                    }
                    Note that you used,

                    if (Close[0] > Close[entry1_sl] && Close[0] < Close[entry1_slx])

                    when you could have simplified that as,

                    if (bool1)

                    Do you see why?

                    Hint:
                    Your bool1 and bool2 variables contain the results of the expressions
                    you're testing in the if statements, so just use bool1 and bool2 directly.




                    Comment


                      #11
                      Hello harr5754,

                      Thanks for your reply.

                      As forum member bltdavid has advised, reduce down to the two bools you have created.

                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        It's working. Thanks, David!

                        Appreciate both your help.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by johnMoss, Today, 04:06 PM
                        0 responses
                        2 views
                        0 likes
                        Last Post johnMoss  
                        Started by agraham, Today, 01:05 PM
                        0 responses
                        25 views
                        0 likes
                        Last Post agraham
                        by agraham
                         
                        Started by jagswe, Today, 01:34 PM
                        0 responses
                        13 views
                        0 likes
                        Last Post jagswe
                        by jagswe
                         
                        Started by PaulMohn, Yesterday, 05:25 AM
                        9 responses
                        34 views
                        0 likes
                        Last Post NinjaTrader_PaulH  
                        Started by srbdoc, Today, 12:20 PM
                        1 response
                        8 views
                        0 likes
                        Last Post NinjaTrader_ChristopherJ  
                        Working...
                        X