Announcement

Collapse
No announcement yet.

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.


    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.

        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.

            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:	210
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.

                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:	214
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.

                    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.

                          Comment


                            #14
                            I have this which 2nd, 3rd, 4th and 5th swing instances are wrong, only 1st instance is correct, why?

                            1st prints High = 5388.75, and is correct 5388.75 (on the Data Box)​​
                            2nd prints High = 5387, but should be 5388.75 (on the Data Box) // they are duplicate with 1st in a list

                            3rd prints Low = 5387.75, but should be 5377.75 (as on the Data Box)
                            4th prints High = 5395, but should be 5387 (as on the Data Box)
                            5th prints High = 20116.25, but should be 5393 (as on the Data Box)

                            Code:
                            // Class variable
                            private int strength = 7;
                            
                            //OnBarUpdate
                                                double th5swingPrice = (th5 < 0) ? Low[Math.Max(0, Swing(strength).SwingLowBar(1,5,200) )] :
                                                                       (th5 > 0) ? High[Math.Max(0, Swing(strength).SwingHighBar(1,5,200) )] : double.NaN;
                                                double th4swingPrice = (th4 < 0) ? Low[Math.Max(0, Swing(strength).SwingLowBar(1,4,200) )] :
                                                                       (th4 > 0) ? High[Math.Max(0, Swing(strength).SwingHighBar(1,4,200) )] : double.NaN;
                                                double rd3swingPrice = (rd3 < 0) ? Low[Math.Max(0, Swing(strength).SwingLowBar(1,3,200) )] :
                                                                       (rd3 > 0) ? High[Math.Max(0, Swing(strength).SwingHighBar(1,3,200) )] : double.NaN;
                                                double nd2swingPrice = (nd2 < 0) ? Low[Math.Max(0, Swing(strength).SwingLowBar(1,2,200) )] :
                                                                       (nd2 > 0) ? High[Math.Max(0, Swing(strength).SwingHighBar(1,2,200) )] : double.NaN;
                                                double st1swingPrice = (st1 < 0) ? Low[Math.Max(0, Swing(strength).SwingLowBar(1,1,200) )] :
                                                                       (st1 > 0) ? High[Math.Max(0, Swing(strength).SwingHighBar(1,1,200) )] : double.NaN;
                                                
                                                Print("th5swingPrice: " + th5swingPrice );
                                                Print("th4swingPrice: " + th4swingPrice );
                                                Print("rd3swingPrice: " + rd3swingPrice );
                                                Print("nd2swingPrice: " + nd2swingPrice );
                                                Print("st1swingPrice: " + st1swingPrice );​
                            demo:


                            Fixed:

                            Code:
                            // At Class Scope 
                                  
                                        private int last1HighBar()
                                        {
                                            return Swing(strength).SwingHighBar(1,1,200);    
                                        }
                                        private int last1LowBar()
                                        {
                                            return Swing(strength).SwingLowBar(1,1,200);
                                        }​
                                    
                                        private int last2HighBar()
                                        {
                                            return Swing(strength).SwingHighBar(1,2,200);    
                                        }
                                        private int last2LowBar()
                                        {
                                            return Swing(strength).SwingLowBar(1,2,200);
                                        }
                                    
                                        private int last3HighBar()
                                        {
                                            return Swing(strength).SwingHighBar(1,3,200);    
                                        }
                                        private int last3LowBar()
                                        {
                                            return Swing(strength).SwingLowBar(1,3,200);
                                        }
                                    
                                        private int last4HighBar()
                                        {
                                            return Swing(strength).SwingHighBar(1,4,200);    
                                        }
                                        private int last4LowBar()
                                        {
                                            return Swing(strength).SwingLowBar(1,4,200);
                                        }
                                    
                                        private int last5HighBar()
                                        {
                                            return Swing(strength).SwingHighBar(1,5,200);    
                                        }
                                        private int last5LowBar()
                                        {
                                            return Swing(strength).SwingLowBar(1,5,200);
                                        }​
                            
                            
                            //In OnBarUpdate
                            
                                                Print("Swing(strength).SwingLowBar(1,5,200): " + Swing(strength).SwingLowBar(1,5,200) );
                                                Print("Swing(strength).SwingHighBar(1,5,200): " + Swing(strength).SwingHighBar(1,5,200) );
                                                Print("Swing(strength).SwingLowBar(1,4,200): " + Swing(strength).SwingLowBar(1,4,200) );
                                                Print("Swing(strength).SwingHighBar(1,4,200): " + Swing(strength).SwingHighBar(1,4,200) );
                                                Print("Swing(strength).SwingLowBar(1,3,200): " + Swing(strength).SwingLowBar(1,3,200) );
                                                Print(" Swing(strength).SwingHighBar(1,3,200): " +  Swing(strength).SwingHighBar(1,3,200) );
                                                Print("Swing(strength).SwingLowBar(1,2,200): " + Swing(strength).SwingLowBar(1,2,200) );
                                                Print("Swing(strength).SwingHighBar(1,2,200): " + Swing(strength).SwingHighBar(1,2,200) );
                                                Print("Swing(strength).SwingLowBar(1,1,200): " + Swing(strength).SwingLowBar(1,1,200) );
                                                Print("Swing(strength).SwingHighBar(1,1,200): " + Swing(strength).SwingHighBar(1,1,200) );
                                                
                                                double th5swingHighPrice =  High[Math.Max(0, Swing(strength).SwingHighBar(1,5,200) )];
                                                double th5swingLowPrice =   Low[Math.Max(0, Swing(strength).SwingLowBar(1,5,200) )];
                                                
                                                double th4swingHighPrice =  High[Math.Max(0, Swing(strength).SwingHighBar(1,4,200) )];
                                                double th4swingLowPrice =   Low[Math.Max(0, Swing(strength).SwingLowBar(1,4,200) )];
                            
                                                double rd3swingHighPrice =  High[Math.Max(0, Swing(strength).SwingHighBar(1,3,200) )];
                                                double rd3swingLowPrice =   Low[Math.Max(0, Swing(strength).SwingLowBar(1,3,200) )];
                            
                                                double nd2swingHighPrice =  High[Math.Max(0, Swing(strength).SwingHighBar(1,2,200) )];
                                                double nd2swingLowPrice =   Low[Math.Max(0, Swing(strength).SwingLowBar(1,2,200) )];
                            
                                                double st1swingHighPrice =  High[Math.Max(0, Swing(strength).SwingHighBar(1,1,200) )];
                                                double st1swingLowPrice =   Low[Math.Max(0, Swing(strength).SwingLowBar(1,1,200) )];
                                                
                                                
                                                double th5swingPrice = ( last3HighBar() < last3LowBar() ) ? rd3swingHighPrice :
                                                                       ( last3LowBar()  < last3HighBar() ) ? rd3swingLowPrice : double.NaN;
                                                
                                                double th4swingPrice = ( last2HighBar() > last2LowBar() ) ? nd2swingHighPrice :
                                                                       ( last2LowBar()  > last2HighBar() ) ? nd2swingLowPrice : double.NaN;
                                                
                                                double rd3swingPrice = ( last2HighBar() < last2LowBar() ) ? nd2swingHighPrice :
                                                                       ( last2LowBar()  < last2HighBar() ) ? nd2swingLowPrice : double.NaN;
                                                
                                                double nd2swingPrice = ( lastHighBar() > lastLowBar() ) ? st1swingHighPrice :
                                                                       ( lastLowBar()  > lastHighBar() ) ? st1swingLowPrice : double.NaN;
                                                
                                                double st1swingPrice = ( last1HighBar() < last1LowBar() ) ? st1swingHighPrice :
                                                                       ( last1LowBar()  < last1HighBar() ) ? st1swingLowPrice : double.NaN;
                                                
                                                
                                                Print("th5swingPrice: " + th5swingPrice );
                                                Print("th4swingPrice: " + th4swingPrice );
                                                Print("rd3swingPrice: " + rd3swingPrice );
                                                Print("nd2swingPrice: " + nd2swingPrice );
                                                Print("st1swingPrice: " + st1swingPrice );​
                            Last edited by PaulMohn; 09-25-2024, 07:59 AM.

                            Comment


                              #15
                              Hello PaulMohn,

                              That may be the lookback period being used, from the code I don't see anything specifically wrong. You may need to debug the situation and experiment to see what changes the result.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Yesterday, 05:17 AM
                              0 responses
                              63 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              139 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              75 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              50 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X