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

How to gather last 4 values of Swing indicator

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

    How to gather last 4 values of Swing indicator

    Hi,

    I want to work with lat four swing indicator values. How do i obtain them?

    I created this series but its not right, when i print they dont come up as last 3 values that swing indicator laid on the chart...
    Thank you

    private Series<double> myDoubleSeries;
    in dataloaded
    myDoubleSeries = new Series<double>(this);
    and in barupdate
    myDoubleSeries[0] = High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))];
    myDoubleSeries[1] = High[Math.Max(0, Swing(5).SwingHighBar(0, 2, 10))];
    myDoubleSeries[2] = High[Math.Max(0, Swing(5).SwingHighBar(0, 3, 10))];

    Print("TmyDoubleSeries[0] " + myDoubleSeries[0]);
    Print("TmyDoubleSeries[1] " + myDoubleSeries[1]);
    Print("TmyDoubleSeries[2] " + myDoubleSeries[2]);​

    #2
    Hello tkaboris,

    The SwingHighBar method returns the bars ago for the instance you select, to have 4 swing values would require 4 lines of code where you increment the instance in SwingHighBar. You would just need to use double variables for that task. Here is an example of getting the price of 1 instance of the swing.

    int swing1BarsAgo = Swing(5).SwingHighBar(0, 1, 10);
    double firstSwingInstance = High[Math.Max(0,swing1BarsAgo)];

    You could repeat this type of code a number of times if you wanted multiple variables for multiple swings. Keep in mind that using Math.Max will make it return non swing values if a swing is not found. You are just getting the High of 0 bars ago if the swing is not found. If you want to always have swing values you would have to check if the returned BarsAgo greater than 0 is being used to know if that specific swing instance was found instead of using the default of 0 bars ago like this sample code.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you
      You mean like this?

      int swing1BarsAgo = Swing(5).SwingHighBar(0, 1, 10);
      double firstSwingInstance = High[Math.Max(0,swing1BarsAgo)];

      int swing2BarsAgo = Swing(5).SwingHighBar(0, 2, 10);
      double secondSwingInstance = High[Math.Max(0,swing2BarsAgo)];

      Print("firstSwingInstance"+firstSwingInstance);
      Print("secondSwingInstance"+secondSwingInstance);​

      Comment


        #4
        Hello tkaboris,

        Yes that would get either the swing values or the current bar high depending if swings are found in that lookback period.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you can you please help with the syntax for checking the bars ago >0 my solution not working
          if (SwingHighBar.barsAgo > 0){
          int swing1BarsAgo = Swing(5).SwingHighBar(0, 1, 10);
          double firstSwingInstance = High[Math.Max(0,swing1BarsAgo)];


          int swing2BarsAgo = Swing(5).SwingHighBar(0, 2, 10);
          double secondSwingInstance = High[Math.Max(0,swing2BarsAgo)];
          }​

          Comment


            #6
            Hello tkaboris,

            You would need to make a condition using the bars ago variable in that sample, that is named swing1BarsAgo.

            The SwingHighBar method returns a bars ago, that is saved to a variable in the sample you are working with.
            JesseNinjaTrader Customer Service

            Comment


              #7
              I am sorry its not working for me. There is no barsAgo method in swing1barsAgo

              Click image for larger version

Name:	image.png
Views:	76
Size:	82.3 KB
ID:	1262386

              Comment


                #8
                Hello tkaboris,

                Why are you putting a period after the variable name? The variable is already the bars ago as an int. You would compare that variable against the number of bars ago that you wanted.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  I changed to this but still its not picking the second instance correctly. Second one should print 15066 value.
                  Here is my code. What am i doing wrong?

                  int swing1BarsAgo = Swing(5).SwingHighBar(0, 1, 10);
                  if(swing1BarsAgo > 0){
                  firstSwingInstance = High[Math.Max(0,swing1BarsAgo)];
                  }
                  int swing2BarsAgo = Swing(5).SwingHighBar(0, 2, 10);
                  if(swing2BarsAgo > 0){
                  secondSwingInstance = High[Math.Max(0,swing2BarsAgo)];
                  }
                  Print("firstSwingInstance"+firstSwingInstance);
                  Print("secondSwingInstance"+secondSwingInstance);​

                  Click image for larger version

Name:	image.png
Views:	76
Size:	185.6 KB
ID:	1262405

                  Comment


                    #10
                    Hello tkaboris,

                    You are only looking back 10 bars, try increasing the lookback period to a period that includes the full length of the swings you wanted.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      ok thank you i think its what i needed now.

                      Comment


                        #12
                        When defining Low, do i use Math.Max or Math.Min?

                        int swing1BarsAgoL = Swing(5).SwingLowBar(0, 1, 100);
                        if(swing1BarsAgoL > 0){
                        firstLowSwingInstance = Low[Math.Max(0,swing1BarsAgoL)];
                        }​
                        Last edited by tkaboris; 07-31-2023, 12:02 PM.

                        Comment


                          #13
                          Hello tkaboris,

                          The purpose of the Math.max is to prevent -1 from being used as the bars ago. A bars ago needs to be at least 0. The swing methods return -1 if no swing is found which would generate an error. Math.Max returns 0 if the choice is between -1 and 0.
                          JesseNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Zeezee, Today, 12:45 PM
                          1 response
                          4 views
                          0 likes
                          Last Post NinjaTrader_ChristopherJ  
                          Started by tomasak, Today, 12:54 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post tomasak
                          by tomasak
                           
                          Started by swjake, Today, 12:04 PM
                          2 responses
                          9 views
                          0 likes
                          Last Post swjake
                          by swjake
                           
                          Started by Richozzy38, Yesterday, 01:06 PM
                          5 responses
                          24 views
                          0 likes
                          Last Post Richozzy38  
                          Started by tradingnasdaqprueba, 05-07-2024, 03:42 AM
                          13 responses
                          52 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X