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

Complicated Conditional Testing in NT

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

    Complicated Conditional Testing in NT

    I'm certain that this question has been asked before, but I am finding it difficult creating search terms that bring up relevant results.

    Let's say that there are four predefined price levels: A, B, C, D. If price is above any two of these levels, then I want to branch off in one direction of code, but if price is above only one of these levels, a different code should be executed. It is obviously possible to hard code for each possible set of outcomes where price would be above A and B, above A and C, above A and D, above B and C, above B and D, etc. but this could be very lengthy if a fifth or sixth predefined price level was also tested for.

    I am sure that there is a way to do this in a more programmatically elegant way, but I am not sure what to search for. Is this some sort of conditional testing, conditional branching, or something else?

    Could someone point me in the right direction and tell me what type of programmatic structure this is? Also, if anyone knows of an online reference that would show a few generic examples, that would be very useful.

    #2
    Hi h1000, unfortunately I am not aware of any such coding concepts. Hopefully some of the community members will chime in here.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by h1000 View Post
      I'm certain that this question has been asked before, but I am finding it difficult creating search terms that bring up relevant results.

      Let's say that there are four predefined price levels: A, B, C, D. If price is above any two of these levels, then I want to branch off in one direction of code, but if price is above only one of these levels, a different code should be executed. It is obviously possible to hard code for each possible set of outcomes where price would be above A and B, above A and C, above A and D, above B and C, above B and D, etc. but this could be very lengthy if a fifth or sixth predefined price level was also tested for.

      I am sure that there is a way to do this in a more programmatically elegant way, but I am not sure what to search for. Is this some sort of conditional testing, conditional branching, or something else?

      Could someone point me in the right direction and tell me what type of programmatic structure this is? Also, if anyone knows of an online reference that would show a few generic examples, that would be very useful.
      Simplest algorithm will be to sort the values, and determine whether your test value is above the value of the index that you want.

      So say you have 7 values, and you want to act if your test value is greater than 2 of them. Here is how it would work.
      1. Copy the 7 values sequentially into an ArrayList
      2. Sort the ArrayList
      3. Compare the test value to ArrayList[1], and act accordingly.
      Last edited by koganam; 10-17-2011, 10:54 AM. Reason: Corrected coding syntax brackets

      Comment


        #4
        Koganam,

        Thanks for responding to my post regarding ArrayLists. I did some further searching on the NT Forum for ArrayLists and you seem to be the foremost authority on them (at least on the NT Forum!).

        I don't have much programming experience and after searching Google for some examples of sorting and comparing ArrayLists, I am afraid I am out of my depth. I am not sure if I should be using IComparer or Icomparable, or if this would even be supported by NT.

        I have made some progress as far as creating a new array and adding items to it using the following code (I also had to add using System.Collections in the Using declarations area):
        #region Variables
        ///create new array
        private ArrayList myDataPoints = new ArrayList();
        ///define some variables for ease of assigning values to list items in the array
        double Open1;
        double High1;
        double Low1;
        double Close1;
        double Close0;
        Then in the bar update section, I have:
        Open1 = Open[1];
        High1 = High[1];
        Low1 = Low[1];
        Close1 = Close[1];
        Close0 = Close[0];

        ///Add items to ArrayList
        myDataPoints.Add(Open1);
        myDataPoints.Add(High1);
        myDataPoints.Add(Low1);
        myDataPoints.Add(Close1);
        What I want to do is compare Close[0] to the values added to the ArrayList (i.e., the previous day's high, low, open, and close) and take a specific action if the current day's close, Close[0], is above any two of them.

        At the rate I am going, it will take me days to understand how to sort and compare the ArrayList. As you have so much more experience in this field, and if you can spare the time, would you be able to help me out and make some code suggestions.

        I don't expect you to take on a load of work for me, but if this is easy to do for someone with experience, I would be really grateful as it would save me a great deal of time and frustration.

        Comment


          #5
          So then at this time, you have a populated ArrayList. All that remains is to use it for comparison purposes.
          Code:
          myDataPoints.Sort(); // will sort the list in situ
          // ArrayLists are zero indexed, so the second value is at index 1
          if (Close[0] > myDataPoints[1])
          {
          //Do stuff
          myDataPoints.Clear(); // ready for next run
          }

          Comment


            #6
            Koganam,

            Thanks for the code!

            I have added it to the indicator, but am getting the following error with reference to the line

            if (Close[0]>myDataPoints[1])

            Operator '>' cannot be applied to operands of type 'double' and 'object'

            The error code is CS0019: Strings cannot be compared with relational operators (<, >, <=, >=, ==, !=) to other object types. Strings can only be compared to other strings and only through the use of equality operators (==, !=).

            Is there a way around this problem?

            Comment


              #7
              Originally posted by h1000 View Post
              Koganam,

              Thanks for the code!

              I have added it to the indicator, but am getting the following error with reference to the line

              if (Close[0]>myDataPoints[1])

              Operator '>' cannot be applied to operands of type 'double' and 'object'

              The error code is CS0019: Strings cannot be compared with relational operators (<, >, <=, >=, ==, !=) to other object types. Strings can only be compared to other strings and only through the use of equality operators (==, !=).

              Is there a way around this problem?
              Oops, my error. You must cast the value to a double.

              if (Close[0]>(double)myDataPoints[1]) ...

              Comment


                #8
                Koganam,

                The casting correction corrected the error.

                Thanks very much for taking the time to assist with these questions, I would have been working on this for days without your help.

                Comment


                  #9
                  Originally posted by h1000 View Post
                  Koganam,

                  The casting correction corrected the error.

                  Thanks very much for taking the time to assist with these questions, I would have been working on this for days without your help.
                  Don't mention it. I am glad that I could help. Now just go out and make tons of money, please.

                  Comment


                    #10
                    Koganam,

                    In exploring the indicator a bit further, I decided to make a slight adaption which is generating some unusual results, which leads me to believe that there might be an issue with the Sort process.

                    As you mentioned earlier, the ArrayList has a zero index. The code below should compare Close[0] to the four values added to the ArrayList (the previous day's high, low, open, and close) and color the bar of days that close above the previous day's high, low, open, and close
                    Open1 = Open[1];
                    High1 = High[1];
                    Low1 = Low[1];
                    Close1 = Close[1];

                    myDataPoints.Add(Open1);
                    myDataPoints.Add(High1);
                    myDataPoints.Add(Low1);
                    myDataPoints.Add(Close1);

                    myDataPoints.Sort();
                    if (Close[0] > (double)myDataPoints[3])
                    {
                    BarColor = Color.Yellow;
                    myDataPoints.Clear();
                    }
                    Strangely, this is not happening. The attached screenshot shows three days (marked with red arrows) that do not close above the previous day's HLOC, and yet the bar is colored.

                    Do you have an idea as to what is generating this behavior? I suspect it must be something in the Sort procedure. Is there a way to stipulate how the sort is sorted?

                    I have included the full code from #regionVariables through the protectedoverridevoidOnBarUpdate() section below:
                    #region Variables
                    private ArrayList myDataPoints = new ArrayList();
                    double Open1;
                    double High1;
                    double Low1;
                    double Close1;
                    #endregion


                    protectedoverridevoid Initialize()
                    {
                    CalculateOnBarClose = false;
                    Overlay = true;
                    PriceTypeSupported = false;
                    }

                    protectedoverridevoid OnBarUpdate()
                    {
                    if(CurrentBar < 5)
                    return;

                    Open1 = Open[1];
                    High1 = High[1];
                    Low1 = Low[1];
                    Close1 = Close[1];

                    myDataPoints.Add(Open1);
                    myDataPoints.Add(High1);
                    myDataPoints.Add(Low1);
                    myDataPoints.Add(Close1);

                    myDataPoints.Sort();
                    if (Close[0] > (double)myDataPoints[3])
                    {
                    BarColor = Color.Yellow;
                    myDataPoints.Clear();
                    }

                    }
                    Attached Files

                    Comment


                      #11
                      Again my fault. I did not quite understand your requirement, and so we are clearing the myDataPoints[] only when the condition is met. We should be clearing it unconditionally. So move the .Clear() statement.

                      Code:
                      protectedoverridevoid OnBarUpdate()
                      {
                      if(CurrentBar < 5)
                      return;
                       
                      Open1 = Open[1];
                      High1 = High[1];
                      Low1 = Low[1];
                      Close1 = Close[1];
                       
                      myDataPoints.Clear();
                      myDataPoints.Add(Open1);
                      myDataPoints.Add(High1);
                      myDataPoints.Add(Low1);
                      myDataPoints.Add(Close1); 
                       
                      myDataPoints.Sort();
                      if (Close[0] > (double)myDataPoints[3])
                      {
                      BarColor = Color.Yellow;
                      }
                       
                      }
                      That having been said, for what you just described, this is an extremely inefficient method to do what you say. The highest value is IDENTICALLY the High[1], so there is no need for all the fancy sorting etc.,

                      Comment


                        #12
                        Koganam,

                        That solved the problem! The indicator is now acting as I had hoped it would.

                        I do recognize that this is a very inefficient method for marking days that close above the previous day's action.

                        The discrepancies came to light with the previous coding (mark days where the close was above 2 of the previous four datapoints) but I couldn't be certain if some other element was effecting the outcome. So I coded it to mark days where the close was above all 4 previous datapoints, and it was clear that something was truly amiss.

                        You have been a life saver in this whole matter and I can't thank you enough for the time and effort you have put into this.

                        Thanks very much.

                        Comment


                          #13
                          Originally posted by h1000 View Post
                          Koganam,

                          That solved the problem! The indicator is now acting as I had hoped it would.

                          I do recognize that this is a very inefficient method for marking days that close above the previous day's action.

                          The discrepancies came to light with the previous coding (mark days where the close was above 2 of the previous four datapoints) but I couldn't be certain if some other element was effecting the outcome. So I coded it to mark days where the close was above all 4 previous datapoints, and it was clear that something was truly amiss.

                          You have been a life saver in this whole matter and I can't thank you enough for the time and effort you have put into this.

                          Thanks very much.
                          Now that we have the skeleton sorted out, I should point out that if you are using COBC = false, this becomes an inefficient and resource hungry indicator, as it is going through ArrayList calculations on every tick. What if your instrument has a million ticks on the bar?

                          You want to be more efficient by letting the code run only once, as the values NEVER change on a given bar. All the DataSeries are one bar ago, so cannot change.

                          Try this:

                          Create another class variable, called testValue, and assign it only on FirstTickOfBar. Test Close[0] against this value, so that you do not need to process, or even read an ArrayList on every tick.

                          Code:
                                  #region Variables
                                  private ArrayList myDataPoints = new ArrayList();
                                  double Open1;
                                  double High1;
                                  double Low1;
                                  double Close1;
                                  private double testValue = 0;
                                  #endregion
                          
                                  protected override void Initialize()
                                  {
                                  CalculateOnBarClose = false;
                                  Overlay = true;
                                  PriceTypeSupported = false;        
                                  }
                          
                                  protected override void OnBarUpdate()
                                  {
                                  if(CurrentBar < 5)
                                   return;
                                  
                                  if (FirstTickOfBar)
                                  {
                                  Open1 = Open[1];
                                  High1 = High[1];
                                  Low1 = Low[1];
                                  Close1 = Close[1];
                                  
                                  myDataPoints.Clear();
                                  myDataPoints.Add(Open1);
                                  myDataPoints.Add(High1);
                                  myDataPoints.Add(Low1);
                                  myDataPoints.Add(Close1);
                                  
                                  myDataPoints.Sort();
                                      
                                  this.testValue = (double)myDataPoints[3];
                                  }
                                  
                                  if (Close[0] > this.testValue)
                                  {
                                  BarColor = Color.Yellow;
                                  }
                                  }
                          The natural question arises then, why did I not show all this from the beginning? Well, first I set out to answer your question as posed. By not introducing any other factors, it was easier for us to focus on the core of the issue; getting your output to look as you expected.

                          Efficient coding is a different issue.

                          Comment


                            #14
                            Koganam,

                            You are completely right--efficiency is another matter. Once the framework is in place, then efficiency has to be the next goal.

                            Efficiency has been a nagging issue on some of the other things I have put together in NT. I am certain that I will have to recode some other projects for efficient operation with real time data, and I think that the code you recommend here could come in handy not only for this project but for other projects as well!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Aviram Y, Today, 05:29 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post Aviram Y  
                            Started by quantismo, 04-17-2024, 05:13 PM
                            3 responses
                            25 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by ScottWalsh, 04-16-2024, 04:29 PM
                            7 responses
                            34 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by cls71, Today, 04:45 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post cls71
                            by cls71
                             
                            Started by mjairg, 07-20-2023, 11:57 PM
                            3 responses
                            217 views
                            1 like
                            Last Post PaulMohn  
                            Working...
                            X