Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using the Swing Indicator

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

    #16
    Hello Joydeep

    please look at the picture. I have value of first Swing High R0 with instance 1 Swing(2).SwingHighBar(0, 1, 100);
    How to get the value (or instance number) of Swing High which is the closest from R0 ago and which is higher than R0?
    It is R1 on picture.
    Swings A,B,C,D are not important for me because their values are lower than R0.

    Thanks for answer
    Lico
    Attached Files

    Comment


      #17
      Hello,

      Thank you for the question.

      I have prepared a example of how you might loop through the instances to find a match and then stop looping once the first match has been found.

      Code:
      protected override void OnBarUpdate()
              {
                  if (CurrentBar < Count - 1) return;
                  int barsAgo = 500;
                  double referenceValue = Close[Swing(2).SwingHighBar(0, 1, barsAgo)];
      
                  for (int i = 2; i < barsAgo; i++)
                  {
                      int instance = Swing(2).SwingHighBar(0, i, barsAgo);
                      if (instance == -1) return;
      
                      double price = Swing(2).SwingHigh[instance];
      
                      Print(referenceValue + "  " + price);
      
                      if (price > referenceValue)
                      {
                          Print("Reference Value: " + referenceValue +
                          " Price " + price + "  BarsAgo: " + instance);
                          break;
                      }
                  }
              }
      I will explain what this is doing.

      Starting off I have added if (CurrentBar < Count - 1) return; just so it only processes the most recent bar, this was for control during testing.

      Next you define a barsAgo, this will be used through the process.

      The Reference value would be the first instance in swingHighBar.

      Next we loop starting at 2 because there is no need to include the reference, this will either return a -1 if there was nothing found or a barsAgo value. I have enclosed the statement in Close[] so that I can directly get a double value of the price, you can use anything in place of Close now that you know the bar number to get the value from.

      Finally there is an if statement to check if the current price is Higher than the reference price and if so print or do logic and break the operation to stop the loop.

      Please let me know if I may be of additional assistance.
      JesseNinjaTrader Customer Service

      Comment


        #18
        Hello Jesse

        Thanks for your code, I tried it and it is super. I have one more question.
        How should I proceed if I needed next value R2, where R2 > R1?

        R0 < R1 < R2 < R3 etc.

        thanx
        Lico
        Attached Files

        Comment


          #19
          Hello,

          Thank you for the reply.

          For this I believe you would just need to change your reference value so something like this:
          Code:
          double referenceValue = Close[Swing(2).SwingHighBar(0, 2, barsAgo)];
          
          for (int i = 3; i < barsAgo; i++)
          {
          
          }
          I changed the instance to a 2 from 1 and the i= from a 2 to a 3.

          you could also modify this into a variable

          Code:
          int referenceInstanceValue = 2;
          double referenceValue = Close[Swing(2).SwingHighBar(0, referenceInstanceValue, barsAgo)];
          
          for (int i = referenceInstanceValue + 1; i < barsAgo; i++)
          {
          
          }
          If you need to keep finding if it is greater than the last reference you could do something like this making it into a method.

          Code:
          protected override void OnBarUpdate()
          {
                  if(CurrentBar < 100) return;
                  if(CheckLevel(100, 2) > CheckLevel(100, 1))
          	{
          		Print("true");
          	}
          }
          
          private double CheckLevel(int barsAgo, int referenceValueStart)
          {
          	if(barsAgo < Count - 1)
          	{
          		double referenceValue = Close[Swing(2).SwingHighBar(0, referenceValueStart, barsAgo)];
          
          		for (int i = referenceValueStart + 1; i < barsAgo; i++)
          		{
          			int instance = Swing(2).SwingHighBar(0, i, barsAgo);
          			if (instance != -1)
          			{
          				double price = Swing(2).SwingHigh[instance];
          						
          				if (price > referenceValue)
          				{
          					Print("Reference Value: " + referenceValue +
          					" Price " + price + "  BarsAgo: " + instance);
          					return price;
          				}
          			}
          		}
          	}
                  return 0;
          }

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

          Comment


            #20
            Jesse

            thanks for replay. But I think that we do not understand. I will try to explain it again and fully.

            I need to get 4 values (R0, R1, R2 and R3) at the same time.
            R0 = last swing high with instance 1 (that's ok)
            R1 = the closest swing high from R0 ago, which is higher than R0 (that's ok too)

            and from now I don´t know what to do next
            R2 = the closest swing high from R1 ago > R1
            R3 = the closest swing high from R2 ago > R2

            Look at the picture what I mean. Swings "x" are not important.

            I send also my code file I wrote.

            Thank you for your help and patience
            Lico
            Attached Files
            Last edited by licovata; 10-26-2014, 01:07 AM.

            Comment


              #21
              Originally posted by licovata View Post
              Jesse

              thanks for replay. But I think that we do not understand. I will try to explain it again and fully.

              I need to get 4 values (R0, R1, R2 and R3).
              R0 = last swing high with instance 1 (that's ok)
              R1 = the closest swing high from R0 ago, which is higher than R0 (that's ok too)

              and from now I don´t know what to do next
              R2 = the closest swing high from R1 ago > R1
              R3 = the closest swing high from R2 ago > R2

              Look at the picture what I mean. Swings "x" are not important.

              I send also my code file I wrote.

              Thank you for your help and patience
              Lico
              You would call the CheckLevel() method with increasing referenceValueStart values.

              Comment


                #22
                Is there any change in NT8 in this code? I try rewrite my indicator in NT8, but this code does not work :-(
                while in nt7 works without problems.
                thank you

                Originally posted by NinjaTrader_Jesse View Post
                Hello,

                Thank you for the question.

                I have prepared a example of how you might loop through the instances to find a match and then stop looping once the first match has been found.

                Code:
                protected override void OnBarUpdate()
                        {
                            if (CurrentBar < Count - 1) return;
                            int barsAgo = 500;
                            double referenceValue = Close[Swing(2).SwingHighBar(0, 1, barsAgo)];
                
                            for (int i = 2; i < barsAgo; i++)
                            {
                                int instance = Swing(2).SwingHighBar(0, i, barsAgo);
                                if (instance == -1) return;
                
                                double price = Swing(2).SwingHigh[instance];
                
                                Print(referenceValue + "  " + price);
                
                                if (price > referenceValue)
                                {
                                    Print("Reference Value: " + referenceValue +
                                    " Price " + price + "  BarsAgo: " + instance);
                                    break;
                                }
                            }
                        }
                I will explain what this is doing.

                Starting off I have added if (CurrentBar < Count - 1) return; just so it only processes the most recent bar, this was for control during testing.

                Next you define a barsAgo, this will be used through the process.

                The Reference value would be the first instance in swingHighBar.

                Next we loop starting at 2 because there is no need to include the reference, this will either return a -1 if there was nothing found or a barsAgo value. I have enclosed the statement in Close[] so that I can directly get a double value of the price, you can use anything in place of Close now that you know the bar number to get the value from.

                Finally there is an if statement to check if the current price is Higher than the reference price and if so print or do logic and break the operation to stop the loop.

                Please let me know if I may be of additional assistance.

                Comment


                  #23
                  Hello,

                  I do see this is still working, I am unsure on the script you were trying so I will provide a sample for NT8. please try using the attached export, I have copied this into an NT8 indicator named SampleSwingHighBar.

                  Please let me know if I may be of additional assistance.
                  Attached Files
                  JesseNinjaTrader Customer Service

                  Comment


                    #24
                    Hi
                    I tried to import your indicator into NT8 and works well. Then I tried to copy the code into my indicator, but it is not working.
                    Then I tried one more thing:
                    I wrote another row with FixedText and found the following:

                    if (CurrentBar < Count -1) return;
                    - FixedText and Draw.Ray are not displayed

                    if (CurrentBar == 0) return;
                    - FixedText is displayed, but Draw.Ray is not displayed

                    if (CurrentBar < BarsRequiredToPlot) return;
                    - FixedText is displayed, but Draw.Ray is not displayed

                    I don't know where is the problem :-(

                    My indicator is in the annex.
                    thanks


                    Originally posted by NinjaTrader_Jesse View Post
                    Hello,

                    I do see this is still working, I am unsure on the script you were trying so I will provide a sample for NT8. please try using the attached export, I have copied this into an NT8 indicator named SampleSwingHighBar.

                    Please let me know if I may be of additional assistance.
                    Attached Files

                    Comment


                      #25
                      Hello,

                      Code:
                       if (CurrentBar < Count - 1) return;
                      This check would be if you are using OnPriceChange, your indicator is on OnBarClose so instead you would need to subtract 2.

                      Code:
                       if (CurrentBar < Count - 2) return;
                      Please try using this instead if you will be using OnBarClose.

                      This check is to maintain that this is only executed while in realtime, or in this case on the current bars close and not historically.

                      Also you could wrap it in a historical check:
                      Code:
                      if (State == State.Realtime) { 
                      //code
                       }
                      I look forward to being of further assistance.
                      JesseNinjaTrader Customer Service

                      Comment


                        #26
                        Thank you
                        "Count -2" helped ;-)

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by martin70, 03-24-2023, 04:58 AM
                        15 responses
                        114 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by The_Sec, Today, 02:29 PM
                        1 response
                        6 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by jeronymite, 04-12-2024, 04:26 PM
                        2 responses
                        31 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by Mindset, 05-06-2023, 09:03 PM
                        10 responses
                        265 views
                        0 likes
                        Last Post NinjaTrader_BrandonH  
                        Started by michi08, 10-05-2018, 09:31 AM
                        5 responses
                        743 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Working...
                        X