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

Calculate distance between swings

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

    Calculate distance between swings

    Hello

    I have just created a simple little indicator to mark the swing highs & lows on the chart. I am now adding bits to the code in order to automatically calculate the distance between the two but I am running into a slight issue.

    The swings are properly calculating and plotting. It is also calculating the distance between the swings - and the calculations are, generally correct.

    I have attached a screenshot of the AUDUSD. On the chart you will find green dots on the low of bars representing a swing low and yellow dots on the highs of other bars representing swing highs.

    Within the box, you will also notice two consecutive green dots followed by 2 consecutive yellow dots.

    As the code currently stands (which will be posted in a moment), it is calculating the distance between the swing high and both of the following swing lows (giving 2 calculations). The same is true with the following two consecutive swing highs - it is then calculating both of them with the previous swing low.

    What I would like to see is that it calculates the swing high to the lowest swing low and vice versa. In other words, if there are two consecutive swing lows (without a swing high between them) then I would like it to effectively ignore the first swing low and only calculate the distance between the first swing high and the lowest swing low.

    Attached is also the Output window showing the calculations so that you can see how it is calculating.

    Here is my current code:
    Code:
    	if ((High[0] < High[2]
    				&& High[1] < High[2]
    				&& High[2] > High[3]
    				&& High[2] > High[4])
    				|| (High[0] < High[2]
    				&& High[1] == High[2]
    				&& High[2] > High[3]
    				&& High[2] > High[4]))
    			{
    				swingHigh = High[2];
    	            SwingHigh.Set(2,High[2]);
    				Print(Time[2] + " Range = " + (Math.Round((swingHigh - swingLow)*10000, 1)));
    
    			}
    			
    			if ((Low[0] > Low[2]
    				&& Low[1] > Low[2]
    				&& Low[2] < Low[3]
    				&& Low[2] < Low[4])
    				|| (Low[0] > Low[2]
    				&& Low[1] == Low[2]
    				&& Low[2] < Low[3]
    				&& Low[2] < Low[4]))
    			{
    				swingLow = Low[2];
    	            SwingLow.Set(2,Low[2]);
    				Print(Time[2] + " Range(s) = " + (Math.Round((Math.Abs(swingLow - swingHigh)*10000),1)));
    			}
    Naturally, as the code currently stands, it will not be able to completely ignore the first swing low in a series of consecutive swing lows. That, theoretically is fine, but I would need that it can overwrite that calculation if/when it finds a lower swing consecutive swing low.

    I hope that this makes sense what I am attempting to communicate.

    Do you have any ideas on how I could accomplish this? Thank you very much for your help!!
    Attached Files

    #2
    Jg123,

    How are setting the swingLow and swingHigh data series? I.e what calculations are you using?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal

      To be honest, I am not quite sure what you are asking with the Data Series.

      To answer your questions to the best of my ability, this is all of the code with the exception of creating the variables. In which case they are

      private double swingHigh;
      private double swingLow;

      My answer probably has something to do with setting the Data Series, but I am not quite sure what that is. The only Data Series that I can think of in NT is dealing with the time frame of a chart, etc. Is this what you are referring to in some way?

      Thank you for your help

      Comment


        #4
        Jg123,

        SwingHigh.Set(2,High[2])

        This is what I referring to. I found them in your code when I went through a second pass. What are you using these for?

        You stated that the it is giving two calculations for the lows but what you want is the Lowest.
        You could pass through a Math.Min() method to get the lower of the two values you can pass through.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Hi Cal

          I have been working to figure this out as you mentioned using the Math.Min()

          One of the things that I did was change this to an oscillator in order to see very clearly the way that this is calculating. Attached is the chart and the code.

          On the chart you will see that there are several "swingHigh" points in a row. I would like the highest "swingHigh" to be calculated as swingHigh - swingLow.

          Could you give me an example of how this would work using a Math.Min or Math.Max?

          I really appreciate your help. Thank you!
          Attached Files

          Comment


            #6
            Hello jg123,

            Thank you for your response.

            I have edited the SwingOsc and attached it to this response.

            Please let me know if you have any questions.
            Attached Files

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by arvidvanstaey, Today, 02:19 PM
            4 responses
            11 views
            0 likes
            Last Post arvidvanstaey  
            Started by samish18, 04-17-2024, 08:57 AM
            16 responses
            61 views
            0 likes
            Last Post samish18  
            Started by jordanq2, Today, 03:10 PM
            2 responses
            9 views
            0 likes
            Last Post jordanq2  
            Started by traderqz, Today, 12:06 AM
            10 responses
            18 views
            0 likes
            Last Post traderqz  
            Started by algospoke, 04-17-2024, 06:40 PM
            5 responses
            48 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X