Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get swings on a period

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

    Get swings on a period

    Hi,

    I need a way to use the "Swing" indicator to get the swings high and low between two bars.
    For example, when I'm on a bar X, I want to get the highs and lows that have been there from that bar, to another bar before it.

    Or in other words, get the high swings and low swings bettween a period.

    How can I use it?

    Thanks​

    #2
    Hello Powerbucker,

    Yes, the Swing().SwingHigh[int barsAgo] and Swing().SwingLow[int barsAgo] allows for specifying a bars ago value.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


    If you specify 5 bars ago, this will provide the swing high as it was 5 bars ago from the most recent swing previous to 5 bars ago.
    You can loop from 5 bars to 10 bars ago, and get the swing high values for each bar in that range.

    Keep in mind the Swing indicator does change it historic values. A bar might not have a swing value when the bar updates, but on a later bar a swing value might be set for the previously closed bar.



    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      ok thanks, is what I looking for

      Comment


        #4
        Hi,

        I'm trying to get the swing highs and lows of the Asian session on the London opening candle.

        This code works for me, but it returns wrong values ​​compared to the visual indicator.

        Code:
                        if (isLondonBar(Time[0]))
                        {
                            var swing = Swing(20);
                            Print ("HIGHS");
                            for (int i = 1; i<100; i++)
                            {
                                int BarsAgo = swing.SwingHighBar(0, i, Bars.BarsSinceNewTradingDay);
                                if(BarsAgo < 0) break;
                                DateTime d = Bars.GetTime(CurrentBar - BarsAgo);
                                double swingHigh = Bars.GetHigh(CurrentBar - BarsAgo);
                                //swing.SwingHigh[BarsAgo];    -> this throws an error
                            
                                Print($"Instance - {i} - SwingHigh: {swingHigh} - Time: {d.ToString()}");
                            }
                            Print ("LOWS");
                            for (int i = 1; i<100; i++)
                            {
                                int BarsAgo = swing.SwingLowBar(0, i, Bars.BarsSinceNewTradingDay);
                                if(BarsAgo < 0) break;
                                DateTime d = Bars.GetTime(CurrentBar - BarsAgo);
                                double swingLow = Bars.GetLow(CurrentBar - BarsAgo);
                                //Swing(20).SwingLow[BarsAgo];    -> this throws an error
                            
                                Print($"Instance - {i} - SwingLow: {swingLow} - Time: {d.ToString()}");
                            }
                            londonChecked = true;    
        ​


        I'm getting these results:

        Click image for larger version

Name:	image.png
Views:	51
Size:	12.9 KB
ID:	1335139

        Only the first five of each series are correct, then they no longer match.

        This is the chart with the visual Swing indicator

        Click image for larger version

Name:	image.png
Views:	47
Size:	20.4 KB
ID:	1335140
        The swing indicator is configured with 20 periods, both visual and by code

        Could you help me?

        Thanks​

        Comment


          #5
          If I try get the values like this

          Code:
          try
          {
          swingHigh = swing.SwingHigh[BarsAgo];
          }
          catch (Exception ex)
          {
          Print("Error al obtener el valor de SwingHigh: " + ex.Message);
          }
          When BarsAgo is greater than 255, it throws an exception​


          Comment


            #6
            Hello Powerbucker,

            The swing indicator changes its historical values. This means if you print the value on a bar before a swing is detected, it may have a different value on a later bar.

            Note, I provided a warning of this in post # 2.

            Below are links to forum threads that discuss.




            If you want the values exactly as they are on the chart, wait until the very last historical has processed before attempting to use any swing values.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I understand that the indicator recalculates the data, but I don't understand why the indicator draws one thing and when I use it by code it returns different information

              I am asking the indicator for past information that is already calculated, therefore the indicator should return the same as the visual indicator

              What reliable way is there to obtain the swing high and low of a historical period?

              Thanks​

              Comment


                #8
                Hello Powerbucker,

                I'm not seeing any code in post # 4 that would wait until after the swing is detected and the values on previous bars set.

                I see it loops over previous bars, but there is no logic I am seeing to run on the last bar or wait until after a swing.

                It looks like this code would run on every bar update.

                The reliable way would be to wait until State == State.Historical && CurrentBar == Count - 2.

                Or use logic to detect a new swing high bar / swing low bar, and when this changes, get the values of the swing after that bar.

                Also, you would want to double check your custom indicator and the Swing indicator are applied to exact same chart and using the same data and that the parameters are all the same.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi,

                  what I need is for every historical day, from the opening bar of London, to recover the swings of the Asian session.

                  Could you give me an example of how I can do it?​

                  Comment


                    #10
                    Hello Powerbucker,

                    I'm not able to write your custom logic for you but I can provide the tools you need.

                    To run logic on the last historical bar:
                    Code:
                    if (State == State.Historical && CurrentBar == Count - 2)
                    However, opening bar of london doesn't tell me what specific time you are looking for or what trading hours template you are using as I don't know what instrument you are referring to.
                    The NQ 03-25 future shown in your screenshot uses the CME US Index Futures ETH trading hours which is in Chicago using the Central Time Zone.

                    If you want the session open time of a trading hours template you can a sessionIterator.
                    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


                    If you have a specific time you want to translate to another time zone you can use TimeZoneInfo.ConvertTimeBySystemTimeZoneId(), TimeZoneInfo.ConvertTimeFromUtc(), TimeZoneInfo.ConvertTimeToUtc().
                    https://learn.microsoft.com/en-us/do...temtimezoneid? view=netframework-4.8.1
                    https://learn.microsoft.com/en-us/do...?view=netframe work-4.8.1
                    https://learn.microsoft.com/en-us/do...tc?view=netfra mework-4.8.1


                    If you have a specific time range (in the local computers time zone) you can use Bars.GetBar() to get a bar number by the date and time.
                    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.



                    For example, if I want to find all the swings in all the historical data on the last historical bar I would write:
                    Code:
                    private Swing mySwing;
                    In OnStateChange State.DataLoaded:
                    Code:
                    mySwing = Swing(5);
                    In OnBarUpdate():
                    Code:
                    if (State != State.Historical || CurrentBar != Count -2)
                    return;
                    
                    int swingAgo = 1;
                    
                    while (mySwing.SwingHighBar(0, swingAgo, CurrentBar) > 0)
                    {
                    Print(string.Format("{0} | swingAgo: {1}, Time[Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar))]: {2}]: {3}, High[{2}]: {4}",
                    Time[0], swingAgo, Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar)), Time[Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar))], High[Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar))]));
                    Draw.Dot(this, "myDot" + Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar)), true, Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar)), High[Math.Max(0, mySwing.SwingHighBar(0, swingAgo, CurrentBar))] + 2, Brushes.Blue);
                    swingAgo++;
                    }



                    If you want a condition that evaluates as true when the swing bar changes save the current swing bar to a variable and then compare that on new bars to the current swing bar.

                    private int mostRecentSwingBar;

                    if (mostRecentSwingBar != CurrentBar - swing.SwingHighBar(0, 1, CurrentBar))
                    {
                    Print(Time[0] + | The swing high bar has changed, was " + mostRecentSwingBar + ", is now: " + (CurrentBar - swing.SwingHighBar(0, 1, CurrentBar)));
                    mostRecentSwingBar = CurrentBar - swing.SwingHighBar(0, 1, CurrentBar)
                    }


                    If you would like someone to develop your logic for you, you can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello,

                      I have been researching and testing. As I mentioned before, requesting a Swing value greater than 255 results in an error.

                      Code:
                      swingHigh = swing.SwingHigh[BarsAgo];
                      Based on this, I tried retrieving only the swings from the last 250 bars, and in that case, it works correctly:

                      Code:
                      int swingHighBar = swing.SwingHighBar(0, instance, 250);
                      Summary: The indicator does not work correctly when requesting data beyond 250 bars. It only returns valid swings within the last 250 bars, while older ones are incorrect.

                      I believe this is a bug in the indicator and should be fixed.

                      For now, I have found a workaround by retrieving data in blocks of 250 bars.

                      Thanks for the support provided​

                      Comment


                        #12
                        Hello Powerbucker,

                        This would likely be due to the custom series defaulting to MaximumBarsLookBack.TwoHundredFiftySix.
                        Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.


                        Make a copy of the Swing indicator script.

                        Change lines 81 to 84 from:
                        swingHighSeries = new Series<double>(this);
                        swingHighSwings = new Series<double>(this);
                        swingLowSeries = new Series<double>(this);
                        swingLowSwings = new Series<double>(this);​

                        To:
                        swingHighSeries = new Series<double>(this, MaximumBarsLookBack.Infinite​);
                        swingHighSwings = new Series<double>(this, MaximumBarsLookBack.Infinite​);
                        swingLowSeries = new Series<double>(this, MaximumBarsLookBack.Infinite​);
                        swingLowSwings = new Series<double>(this, MaximumBarsLookBack.Infinite​);


                        I'll note your request for this to be modified in the installer.​
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          It Works !!

                          Thanks You very much

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by clarocque1, Today, 06:18 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post clarocque1  
                          Started by clarocque1, Today, 06:17 PM
                          0 responses
                          2 views
                          0 likes
                          Last Post clarocque1  
                          Started by clarocque1, 03-23-2025, 08:17 AM
                          4 responses
                          27 views
                          0 likes
                          Last Post clarocque1  
                          Started by Artorias, Today, 05:22 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post Artorias  
                          Started by several, Today, 07:07 AM
                          8 responses
                          26 views
                          0 likes
                          Last Post several
                          by several
                           
                          Working...
                          X