Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using the Swing Indicator

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

    Using the Swing Indicator

    Hello

    I'm using the standard Ninja Swing indicator and I'd like to add a very simple functionality to it:
    Basically, whenever the Swing Line is rather long (let's say, 10+ candles), I want the indicator to draw an arrow next to the Swing Line, in order to highlight its importance (I'm using a Swing Line with a value of 5).

    I've played around with the DrawArrowUp/Down function but it only lets me draw an arrow above/below a candle, not next to another element on the chart like the Swing line. How should I go about this?

    Thanks in advance for your suggestions.
    Last edited by laocoon; 07-26-2012, 01:57 AM.

    #2
    Hello laocoon,
    A very simple code will be like

    Code:
    protected override void OnBarUpdate()
    {
    	if (CurrentBar < 10) return;
    	
    	if (this.Swing(5)[0] == Swing(5)[10])
    	{
    		this.DrawArrowUp("swing" + CurrentBar, false, 0, Swing(5)[0], Color.Blue);
    	}
    }
    The above code draws an arrow if the current swing value equals the value of the swing 10 bars back.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Hello Joydeep

      Thanks a lot for your reply and the code snippet.
      I'd like to use the code for both the Upper Swing Line and the Lower Swing Line but when I apply the following modification

      Old:
      this.DrawArrowUp("swing" + CurrentBar, false, 0, Swing(5)[0], Color.Blue);

      New:
      this.DrawArrowUp("swingUpper" + CurrentBar, false, 0, SwingHigh(5)[0], Color.Blue);
      this.DrawArrowDown("swingLower" + CurrentBar, false, 0, SwingLow(5)[0], Color.Blue);

      it doesn't work (I get an error message although I'm using SwingHigh & SwingLow in another context and they work perfectly.

      Also, I'd like the arrow to appear only ONCE per Swing Line and not on every candle.

      Thanks again.

      Comment


        #4
        Hello laocoon,
        NinjaTrader natively does not comes with any SwingHigh or SwingLow indicator. To access the Swing high value you can use the following code for example
        Code:
        Swing(5).SwingHigh[0];

        Please try this modified code, which will draw only one arrow,

        In variable region
        Code:
        bool onlyOnce = true;
        InOnBarUpdate

        Code:
        if (CurrentBar < 10) return;
        
        if (Swing(5).SwingHigh[0] != Swing(5).SwingHigh[10])
        {
        	onlyOnce = true;
        }
        
        if (onlyOnce && this.Swing(5).SwingHigh[0] == Swing(5).SwingHigh[10])
        {
        	this.DrawArrowUp("swing" + CurrentBar, false, 0, Swing(5).SwingHigh[0], Color.Blue);
        	onlyOnce = false;
        }
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Looks good so far, thanks a lot for helping out Joydeep!

          Comment


            #6
            Hello laocoon,
            Thanks for your note.
            Please let me know if I can assist you any further.
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Hello Joydeep

              I have a quick follow-up question regarding the Swing Indicator: how can I address an "older" Swing line, ie not the most recent one but the one before? (See attached chart for clarification)

              The Swing Line I'd like to address in my code is highlighted by a blue arrow.

              Thanks a lot.

              Regards
              Attached Files

              Comment


                #8
                Hello laocoon,
                You can use the MRO method to do it. A sample code will be like:
                Code:
                if (CurrentBar < 100) return;
                int m = MRO(delegate {return Swing(5)[0] != Swing(5)[1]; }, 1, 50);
                JoydeepNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Joydeep, I'll look into it.

                  Regards

                  Comment


                    #10
                    Sorry to bother you again with this, Joydeep, but I have two issues with the solution you suggested:

                    1. In the online support section it is written (about MRO) that "This method will NOT work on multi-instrument or multi-time frame strategies." Unfortunately my strategy IS multi-time frame.

                    2. I understand this snippet: int m = MRO(delegate {return Swing(5)[0] != Swing(5)[1]; }, 1, 50);
                    but the problem I have with it is the following: the most recent Swing Line is obviously easy to address with "Swing(5)[0]" because it is drawn above/below the most recent candle.

                    But now it gets a little more tricky because in order to address the *second* most recent Swing Line (the one highlighted by a blue arrow in the attached chart in one of my previous posts) I need to know how many candles back it was drawn and this value will almost never be the same. In the example on the chart it is "Swing(5)[6]", but the next one might be at "Swing(5)[9]" etc.
                    To sum it all up: all I'm looking to achieve is to address the *second* most recent Swing Line in order to be able to compare it to the most recent one.

                    Thanks again.

                    Regards

                    Comment


                      #11
                      Hello laocoon,
                      Unfortunately the MRO function works on single series scripts only as you rightly pointed out.

                      Unfortunately there are no native functions to do it. You have to further custom code to get the 2nd most swing value.
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by laocoon View Post
                        Sorry to bother you again with this, Joydeep, but I have two issues with the solution you suggested:

                        1. In the online support section it is written (about MRO) that "This method will NOT work on multi-instrument or multi-time frame strategies." Unfortunately my strategy IS multi-time frame.

                        2. I understand this snippet: int m = MRO(delegate {return Swing(5)[0] != Swing(5)[1]; }, 1, 50);
                        but the problem I have with it is the following: the most recent Swing Line is obviously easy to address with "Swing(5)[0]" because it is drawn above/below the most recent candle.

                        But now it gets a little more tricky because in order to address the *second* most recent Swing Line (the one highlighted by a blue arrow in the attached chart in one of my previous posts) I need to know how many candles back it was drawn and this value will almost never be the same. In the example on the chart it is "Swing(5)[6]", but the next one might be at "Swing(5)[9]" etc.
                        To sum it all up: all I'm looking to achieve is to address the *second* most recent Swing Line in order to be able to compare it to the most recent one.

                        Thanks again.

                        Regards
                        So why not just determine the actual Swing bar? Once you know the bar number, you can reference it any way that you want.

                        From the NT Help:

                        Syntax - Bars Ago
                        High Bar
                        Swing(int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod)
                        Swing(IDataSeries input, int strength).SwingHighBar(int barsAgo, int instance, int lookBackPeriod)

                        Low Bar
                        Swing(int strength).SwingLowBar(int barsAgo, int instance, int lookBackPeriod)
                        Swing(IDataSeries input, int strength).SwingLowBar(int barsAgo, int instance, int lookBackPeriod)

                        Return Value
                        An int value representing the number of bars ago. Returns a value of -1 if a swing point is not found within the look back period.

                        Comment


                          #13
                          Thanks for your reply Joydeep. I was hoping that there would be an easy solution to my problem but apparently that's not the case. Will have to look further.

                          Regards

                          Comment


                            #14
                            Hello laocoon,
                            My apologies, kognams solution should get you going. A sample code will be like

                            Code:
                            if (CurrentBar < 20) return;
                            int i = Swing(5).SwingHighBar(0, 2, 20);
                            if (i > 0)
                            {
                              //do something
                            }
                            @koganam - thanks for the pointer.
                            JoydeepNinjaTrader Customer Service

                            Comment


                              #15
                              Thank you very much Koganam & Joydeep for your help, much appreciated!
                              I'm currently working on implementing your suggested solution and will let you know how it goes.

                              Thanks again & have a nice weekend.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              606 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by KenneGaray, Today, 03:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post KenneGaray  
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              470 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Started by aa731, Today, 02:54 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post aa731
                              by aa731
                               
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Christopher_R  
                              Working...
                              X