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

Using Swing indicator

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

    Using Swing indicator

    Hi All..

    I'm using the swing indicator in my custom indicator, to calculate high low points for a Fib Extension calculation. I use say high bar [3] and low bar [2] and it calculates a zone for me.

    It works well, until I realised using the swing indicator it's possible to get multiple high points in a row, or multiple lows, so doesn't always go high - low - high etc. sometimes low - low - high etc

    Is it possible to code that if there is multiple highs or lows in a row that it uses the highest one and ignores the lower one??

    #2
    Originally posted by wells0414 View Post
    Hi All..

    I'm using the swing indicator in my custom indicator, to calculate high low points for a Fib Extension calculation. I use say high bar [3] and low bar [2] and it calculates a zone for me.

    It works well, until I realised using the swing indicator it's possible to get multiple high points in a row, or multiple lows, so doesn't always go high - low - high etc. sometimes low - low - high etc

    Is it possible to code that if there is multiple highs or lows in a row that it uses the highest one and ignores the lower one??
    Would this be what you are looking for? See pictures, which should be self explanatory. However, for completeness sake:
    1. Chart showing the "Strict Sequence" enforced.
    2. PropertyGrid of the indicator, explaining what the "Strict Sequence" means.
    3. Chart showing "Strict Sequence", and also showing (in a different color) which Swings were invalidated by the "Strict Sequence" requirement, just in case, you decide that you want to see them.
    4. Chart showing all swings without regard to sequence. This is the same as the shipping NT indicator, visually.

    However, the Osi_Swing uses a much more efficient algorithm that is much less CPU and memory intensive.

    Send me a PM, and I will let you have a 14-day Trial version. While still in beta, this is almost certainly feature complete and what we shall release.
    Attached Files
    Last edited by koganam; 12-19-2014, 11:06 PM.

    Comment


      #3
      Hello wells0414,

      You can try using a loop through these and then use Math.Max() to get the higher value between your multiple highs that you are getting.
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Thanks for the offer koganam but really want to figure this out..

        Cal .. Would you be able to help me figure it out.

        Loops are new to me, done some reading on it but unsure how to make it work in this situation.. Maths. Max I've used and kinda get but loops make me loopy

        Where do I begin?

        Comment


          #5
          Wells,

          Essentially, the for loop will allow you to determine how many time we want to process the information held in the { }

          Example -
          Code:
          double range = 0;
          
          for(int i = 0; i < 10; i++)
          {
              range += High[i] - Low[i];
          }
          
          double avgRange = range / 10;
          The above sample will take the range which is 0 at each new bar and will then take the last 10 bars range and combine it. Once the loop is done we take the avgRange which is our range divided by 10, the number of bars we added.

          Let me know if this helps in starting
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            Hi Cal

            Thanks for your reply

            I'm kinda understanding it now but the bit I can't get right is the

            For ( consecutive swing highs )

            How do u get it to find consecutive swing highs ??

            Comment


              #7
              Hi wells0414,

              I'm happy to try and help you come up with a loop that will find what you are looking for, however, I am not quite sure of the rule.

              Currently the Swing has a Swing High and Swing Low plot. Are you trying to decide which plot to call? If so, what rule decides for the Swing High or Swing Low?

              Are you looking for just the high or low of the bar?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi chelsea

                Thanks for your reply..

                What I'm trying to do is.. Create a loop for when.

                The swing indicator can produce 2 or more swing high plots in a row, as it can also plot consecutive swing low plots in a row.. It's doesn't always go high - low -high - low.

                So if you get a situation when say it goes low - high - high - low

                I want it to take the two highs in a row and find the highest of the two and ignore the other.

                Comment


                  #9
                  Hi wells0414,

                  What is the actual value you are using? (e.g. Swing(9).SwingHigh[0])

                  Are you using two different plots or one plot?

                  If the swing low has 3 plots in a row and the swing high also has 3 plots in a row, what action do you want to take?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    I'm using the to different plots..

                    Swing high bar and low bar

                    I'm using the high and low plots to work out some fib calculations

                    So for example I would be referencing high [3 plots ago] with the low plot [2 plots ago] etc.

                    If there's two highs in a row it messes this up.. Because it's gets out of its high -low - high sequence

                    So to answer your question I'm using the two seperate plots, but I assume there would be 2 seperate loops for them.

                    Comment


                      #11
                      Hello wells0414,

                      Thank you for your response.

                      Can you provide a screenshot of what you are trying to avoid on your chart to help illustrate this matter?

                      Comment


                        #12
                        Ok so as you can see in the pic, the swing indicator has plotted two lows in a row before another high...I don't wont this to happen... I want the higher of the 2 lows removed..

                        Hope this helps me explain myself better
                        Attached Files

                        Comment


                          #13
                          Hello wells0414,

                          Thank you for your patience.

                          Please try the following code in a new indicator and advise if this is performing as you intended. Altering the Swing indicator's code would take further programming and is not necessary if we can just call it's values in another indicator to perform the desired behavior.

                          Code:
                                  #region Variables
                                  private int switchSwing = 0;
                          		private int highBarNum = 0;
                          		private int lowBarNum = 0;
                                  #endregion
                          
                                  /// <summary>
                                  /// This method is used to configure the indicator and is called once before any bar data is loaded.
                                  /// </summary>
                                  protected override void Initialize()
                                  {
                          			Overlay = true;
                          			AutoScale = false;
                          			DrawOnPricePanel = true;
                                  }
                          
                                  /// <summary>
                                  /// Called on each bar update event (incoming tick)
                                  /// </summary>
                                  protected override void OnBarUpdate()
                                  {
                          			if(CurrentBar == 0)
                          			{
                          				highBarNum = CurrentBar;
                          				lowBarNum = CurrentBar;
                          			}
                          			
                          			if(Swing(5).SwingHigh[0] > Swing(5).SwingHigh[6]
                          				&& Swing(5).SwingLow[0] == Swing(5).SwingLow[6])
                          			{
                          				highBarNum = CurrentBar;
                          			}
                          			if(Swing(5).SwingLow[0] < Swing(5).SwingLow[6]
                          				&& Swing(5).SwingHigh[0] == Swing(5).SwingHigh[6])
                          			{
                          				lowBarNum = CurrentBar;
                          			}
                          			
                          				DrawLine("l"+lowBarNum, CurrentBar - lowBarNum, Swing(5).SwingLow[CurrentBar - lowBarNum], 0, Swing(5).SwingLow[CurrentBar - lowBarNum], Color.Red);
                          				DrawLine("h"+highBarNum, CurrentBar - highBarNum, Swing(5).SwingHigh[CurrentBar - highBarNum], 0, Swing(5).SwingHigh[CurrentBar - highBarNum], Color.Green);
                          		}

                          Comment


                            #14
                            The code seems to work well on different time frames with no errors except in the one minute time frame.
                            Error on calling 'OnBarUpdate': barsAgo needed to be between 0 and 255 but was 256.

                            Upon adding:
                            MaximumBarsLookBack = MaximumBarsLook.Back.Infinite;
                            the following error appears:
                            You are accessing an index with a value that is invalid since it is out of range. I.E. accessing a series [barsAgo] with a value of 5 where there are only 4 bars on the chart.

                            How can these issues be solved for it to work also in that time frame? Can MaximumBarsLookBack.Infinte be avoided? What about the one minute time frame that is different from the others?
                            Thanks

                            Comment


                              #15
                              Hello,

                              The maximum bars look back error is due to the secondary data series.

                              If you are loading more than 256 bars of historical data, it will be necessary to set MaximumBarsLookBack = MaximumBarsLook.Back.Infinite.


                              Regarding the error about being out of index, this may be due to calling the swing indicator 5 or 6 bars back on the first bar.

                              I recommend you add the following to the beginning of OnBarUpdate():

                              if (CurrentBar < 6)
                              return;

                              This will cause the script to wait until there are at least 6 bars before trying to look back 6 bars.
                              Last edited by NinjaTrader_ChelseaB; 04-27-2015, 02:10 PM.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,611 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              19 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              22 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X