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

Creating Horizontal Ray at Fractal Points

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

    Creating Horizontal Ray at Fractal Points

    Hello

    I am trying to create a very basic indicator that will be continually developed into something more and more complex over time.(NT8)

    To start out, though, I am just trying to get a horizontal ray to be formed at the high of a certain candle when the criteria is met.

    When the high of a candle is higher than the previous 4 candles and higher than the following 2 candles, it should place a horizontal ray at the high of that candle.

    The logic seems very easy - but I am getting a very strange result.

    Here is my current code as found in OnBarUpdate()

    Code:
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < 6)
    			return;
    			
    			if (High[0] < High[2]
    				&& High[1] < High[2]
    				&& High[3] < High[2]
    				&& High[4] < High[2]
    				&& High[5] < High[2]
    				&& High[6] < High[2]);
    			{
    
    				Draw.Ray(this,"test",2,High[2],0,High[2],Brushes.Blue);
    				Print(string.Format("{0};{1};{2};{3};{4};{5};{6};{7}", Time[0],High[0],High[1],High[2],High[3],High[4],High[5],High[6]));
    			}
    		}
    As you can see in the screenshot titled "Capture", there is one blue ray that has been created on the far right side of the chart.

    The primary problem is that there is only one candle to the left that has a lower high than the one with the ray. According to what I believe I have written, it should not be placed there because there need to be 4 candles to the left that have a lower high.

    Here is a copy of the Output Window:

    Code:
    04-Aug-16 11:00:00 PM;1.75658;1.76176;1.76005;1.75028;1.76;1.76577;1.76778
    05-Aug-16 11:00:00 PM;1.72168;1.75658;1.76176;1.76005;1.75028;1.76;1.76577
    08-Aug-16 11:00:00 PM;1.72238;1.72168;1.75658;1.76176;1.76005;1.75028;1.76
    09-Aug-16 11:00:00 PM;1.70495;1.72238;1.72168;1.75658;1.76176;1.76005;1.75028
    10-Aug-16 11:00:00 PM;1.7011;1.70495;1.72238;1.72168;1.75658;1.76176;1.76005
    You can see that NinjaTrader also recognizes that High[4] - High[6] are higher than High[2].

    So how could this have created the Ray?

    Can you point me in the right direction to be able to fix this bug?

    Thank you!
    Attached Files

    #2
    Hello jg123, and thank you for your question.

    While I agree that is a highly unusual thing to occur, and I am at a loss as to why you are seeing the behavior you are seeing, I can at least let you know what my debugging approach would be.

    At a first brush, I would turn my condition into a series of units which could be tested separately. That is,

    Code:
    [FONT=Courier New]
                if (true
                    && (High[0] < High[2])
                    && (High[1] < High[2])
                    && (High[3] < High[2])
                    && (High[4] < High[2])
                    && (High[5] < High[2])
                    && (High[6] < High[2])
                    ) ...[/FONT]
    You will notice that this should be equivalent to your logic, but also that each line is something I can break away and test by itself easily now. Try running it to make sure.

    Assuming you got the same results, I would then test each of those separately.

    Code:
    [FONT=Courier New]
                Print(High[0] < High[2] ? "TEST 1 PASS" : "TEST 1 FAIL")
                Print(High[1] < High[2][/FONT][FONT=Courier New][FONT=Courier New] ? "TEST 2 PASS" : "TEST 2 FAIL"[/FONT]);
                Print(High[3] < High[2][/FONT][FONT=Courier New][FONT=Courier New] ? "TEST 3 PASS" : "TEST 3 FAIL"[/FONT]);
                [/FONT][FONT=Courier New][FONT=Courier New]Print[/FONT](High[4] < High[2][/FONT][FONT=Courier New][FONT=Courier New] ? "TEST 4 PASS" : "TEST 4 FAIL"[/FONT]);
                [/FONT][FONT=Courier New][FONT=Courier New]Print[/FONT](High[5] < High[2][/FONT][FONT=Courier New][FONT=Courier New] ? "TEST 5 PASS" : "TEST 5 FAIL"[/FONT]);
                [/FONT][FONT=Courier New][FONT=Courier New]Print[/FONT](High[6] < High[2][/FONT][FONT=Courier New][FONT=Courier New] ? "TEST 6 PASS" : "TEST 6 FAIL"[/FONT]);
                Print(string.Format("{0};{1};{2};{3};{4};{5};{6};{7}", Time[0], High[0], High[1], High[2], High[3], High[4], High[5], High[6]));
    [/FONT]
    The last line I borrowed from you, and makes a nice divider between your tests.

    I would then run this in Market Replay until I found a period of time where all 6 tests passed. I would then place your original code back in, and try seeing what the output was during this period.

    If you have done all that (the above approach is known as "unit testing"), and you still have not found out what is occurring, sometimes the best approach is to refactor and simplify. In our case, another piece of code which should be easier to test and modify which does the same thing as your code is

    Code:
    [FONT=Courier New]int barsAgoToCheck = 2, maxBarsAgoToCheck = 6;
    bool success = true;
    for (int i = 0; i < maxBarsAgoToCheck; i++)
    {
      if (i == barsAgoToCheck)
      {
        continue;
      }
      if (High[barsAgoToCheck] <= High[i])
      {
        success = false;
      }
    }
    if (success)
    {
      Draw.Ray(this,"test",2,High[2],0,High[2],Brushes.Blue);
      Print(string.Format("{0};{1};{2};{3};{4};{5};{6};{7}", Time[0], High[0], High[1], High[2], High[3], High[4], High[5], High[6]));
    }[/FONT]
    Best of luck, we are here to help if anything else comes up.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Okay, I have worked through your email. Thanks for it!

      I am at such a loss for what is going on though.

      I changed the code and did the unit testing as you suggested. There were a number of times that all 6 tests passed and, when looking at the data, they passed in the correct spots.

      As expected, the results of the testing code gave me the same results as my original code.

      I then went through and changed the code to your final suggestion, which, again, gave me the same exact results.

      This time I have attached a screenshot of the GBPAUD Daily. You can see that the 4th bar to the left of the candle with the blue line has a high that is higher than High[2]. Therefore, this one should not be receiving the Ray.

      very confused. haha.

      ps, I have also attached a picture of the current code so you can make sure I didn't make any mistakes during the copying...
      Attached Files
      Last edited by jg123; 08-12-2016, 05:57 AM.

      Comment


        #4
        Originally posted by jg123 View Post
        Okay, I have worked through your email. Thanks for it!

        I am at such a loss for what is going on though.

        I changed the code and did the unit testing as you suggested. There were a number of times that all 6 tests passed and, when looking at the data, they passed in the correct spots.

        As expected, the results of the testing code gave me the same results as my original code.

        I then went through and changed the code to your final suggestion, which, again, gave me the same exact results.

        This time I have attached a screenshot of the GBPAUD Daily. You can see that the 4th bar to the left of the candle with the blue line has a high that is higher than High[2]. Therefore, this one should not be receiving the Ray.

        very confused. haha.

        ps, I have also attached a picture of the current code so you can make sure I didn't make any mistakes during the copying...
        What is your Calculate setting?

        Your original code should be fine and less resource intensive than this code, if you take off the semi-colon that you used to terminate your if filter, which thus turned it into a null statement.

        Comment


          #5
          Koganam correctly identified what is causing this. On line 11 of your original code, there is a stray semicolon. It is often the case that when your logic is correct, those are the hardest bugs to track down.

          Please let us know if there are any other ways we can help.
          Jessica P.NinjaTrader Customer Service

          Comment


            #6
            Great spotting Kogonam!

            Thanks for both of your help. And thanks for the help on better debugging practices.

            I will continue working on finishing this first phase of this and will let you know as and when I have more questions.

            Thanks again!

            Comment


              #7
              Why is this only doing the most recent of these levels. Is it because all of the tag strings are the same?

              If I want to initially show every occurrence that meets the conditional criteria, do I need to have a unique tag with each one?

              I want to be able to reference previous more than just the most recent occurrence and delete the Ray if/when the price revisits the high/low of the support or resistance candle.

              I have attached a picture of the Yen futures daily chart that shows ultimately what I would like to create as this first phase of the indicator.

              There is a horizontal Ray on every occurrence of the high/low as specified in the code previously. For the candles that meet the criteria but do not have a Ray, the Ray would have been deleted because the price of the Ray was revisited.

              Therefore I think that I need to be able to reference Ray as time goes on.

              So my current idea is to make the "tag" because a string version of the High/Low price. But I am not too sure how to then reference it later....something like (written in a mix of code & standard english):

              if (the price[0] >= a previous drawn Ray
              && the price from that candle to the current candle has been lower than the price at which the Ray was drawn)
              {delete Ray}

              Can you point me in the right direction about how to create this?

              Thanx!
              Attached Files

              Comment


                #8
                Originally posted by jg123 View Post
                Why is this only doing the most recent of these levels. Is it because all of the tag strings are the same?

                If I want to initially show every occurrence that meets the conditional criteria, do I need to have a unique tag with each one?

                I want to be able to reference previous more than just the most recent occurrence and delete the Ray if/when the price revisits the high/low of the support or resistance candle.

                I have attached a picture of the Yen futures daily chart that shows ultimately what I would like to create as this first phase of the indicator.

                There is a horizontal Ray on every occurrence of the high/low as specified in the code previously. For the candles that meet the criteria but do not have a Ray, the Ray would have been deleted because the price of the Ray was revisited.

                Therefore I think that I need to be able to reference Ray as time goes on.

                So my current idea is to make the "tag" because a string version of the High/Low price. But I am not too sure how to then reference it later....something like (written in a mix of code & standard english):

                if (the price[0] >= a previous drawn Ray
                && the price from that candle to the current candle has been lower than the price at which the Ray was drawn)
                {delete Ray}

                Can you point me in the right direction about how to create this?

                Thanx!
                Pretty much that exact question asked and answered here, with annotated code. Instead of replacing the Ray with a Line, you would simply delete the Ray, though if you ask me, replacing it with a line might be more useful because you can then see past Rays that got broken.

                ref: http://ninjatrader.com/support/forum...78&postcount=6

                Comment


                  #9
                  Thank you!

                  I will try this out and get back to you.

                  I noticed the date of the post seemed to be in 2013. So just want to check in to see if there is anything I should be paying attention to when putting it in NT8?

                  Once again, thank you very much for this.

                  Comment


                    #10
                    Originally posted by jg123 View Post
                    Thank you!

                    I will try this out and get back to you.

                    I noticed the date of the post seemed to be in 2013. So just want to check in to see if there is anything I should be paying attention to when putting it in NT8?

                    Once again, thank you very much for this.
                    That was written for NT7. If you want to use that in NT8, you will have to modify the drawing commands to use the correct NT8 syntax. Beyond that, it is just C#.

                    Comment


                      #11
                      I am systematically converting your code to my code now and have run into an issue.

                      I have identified the problem, but I do not know the solution to this one.

                      Here is the code that is causing the problem:

                      Code:
                              protected override void OnBarUpdate()
                              {
                      			if(CurrentBar < 2) 
                      				return;
                      			
                      			bool Resistance = false;
                      			Resistance = 
                      //					High[0] < High[2] 
                      //					&& High[1] < High[2] 
                      					High[3] < High[2] 
                      //					&& High[4] < High[2] 
                      //					&& High[5] < High[2] 
                      //					&& High[6] < High[2]
                      					;
                      
                      			if(Resistance)
                      			{
                      				RayCount++;
                      				DrawRay("ray" + RayCount.ToString(), false, 2, High[2],0,High[2],Color.Lime, DashStyle.Solid,2);
                      			}
                      For:
                      High[3]
                      High[4]
                      High[5]
                      High[6]

                      It doesn't return ANY rays. (The chart is completely void of any Rays). I have tested each of them individually and none of them return any rays

                      But, for
                      High[0]
                      High[1]

                      It returns rays as it should. (There are hundreds of Rays). I have also tested both of these individually and both properly return Rays.

                      Any insight as to what might be going wrong here?

                      Comment


                        #12
                        Code:
                        if(CurrentBar [COLOR="Red"][B]< 2[/B][/COLOR])
                        You must make sure that your CurrentBar check is such that it takes care of how many bars back you want to look.

                        Comment


                          #13
                          This was exactly the problem

                          This leads me now to my next question.

                          I need to create this for the "support" side and I also want to make it so that I can see the Rays for several timeframes on the same chart. (240 minute, daily, weekly, monthly)

                          I don't have any experience with turning something like this into its own method in order to cut down on the amount of code that needs to be written. Can you point me in the right direction to be able to do that?

                          Comment


                            #14
                            Hello again jg123,

                            Since I am not quite sure which part you'd like to turn into a method, I have decided to give you some example code which you can adapt to suit your needs. I am going to take the place where you set Resistance and turn it into a metthod.

                            Code:
                            [FONT=Courier New]        private bool [B]getResistance[/B]()
                                    {
                                        return true
                            //                    && High[0] < High[2] 
                            //                    && High[1] < High[2] 
                                                && High[3] < High[2] 
                            //                    && High[4] < High[2] 
                            //                    && High[5] < High[2] 
                            //                    && High[6] < High[2]
                                                ;
                                    }
                                    protected override void OnBarUpdate()
                                    {
                                        if(CurrentBar < 2) 
                                            return;
                                        
                                        bool Resistance = [B]getResistance[/B]();
                            // ...[/FONT]
                            I would like to mention, getResistance is position independent, meaning you can write it physically in your code above or below your OnBarUpdate method.
                            Jessica P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by AttiM, 02-14-2024, 05:20 PM
                            12 responses
                            213 views
                            0 likes
                            Last Post DrakeiJosh  
                            Started by cre8able, 02-11-2023, 05:43 PM
                            3 responses
                            237 views
                            0 likes
                            Last Post rhubear
                            by rhubear
                             
                            Started by frslvr, 04-11-2024, 07:26 AM
                            8 responses
                            116 views
                            1 like
                            Last Post NinjaTrader_BrandonH  
                            Started by stafe, 04-15-2024, 08:34 PM
                            10 responses
                            47 views
                            0 likes
                            Last Post stafe
                            by stafe
                             
                            Started by rocketman7, Today, 09:41 AM
                            3 responses
                            11 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Working...
                            X