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

Ninja Swing Indicator Question

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

    Ninja Swing Indicator Question

    Hello

    I've come to like the standard Ninja Swing indicator, but I feel it would be much more useful to me if I could integrate it into a custom strategy. What I'd like to do is to call it from a strategy and compare the VALUES of the last 2 "Swing High" / "Swing Low" Lines it has drawn on the chart.

    Example:
    Swing High 1 (most recent Swing High) = 120
    Swing High 2 (2nd most recent Swing High) = 100

    By extracting the values from the indicator and integrating them into a strategy I can then define an Up/Downtrend. In the example above it would obviously be an uptrend (120-100) = +20 => Trend is Up

    I'd very much appreciate it if I could get some help in achieving that.
    What I'd like to know specifically is how to:
    -Call the Swing indicator from a strategy
    -Save the last 2 Swing High/Low values in order to be able to compare them

    Thanks

    #2
    Hello laocoon,

    Swing is a repainting indicator. The swing values are not known until future bars develop. I suggest you use print statements to see how this indicator works in real time. It may not work the way you expect in an automated strategy.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hello RyanM

      Thanks for your reply. I'm aware of the fact that it is a repainting indicator.
      What I meant is that once the Swing Line has been drawn, its value is obviously a known variable that won't change anymore. This is the value I'd like to extract to use it in my strategy.

      Thanks.

      Comment


        #4
        You can edit the Swing code and see how it works. Click Tools > Edit NinjaScript > Indicator > Open Swing.

        You can work with any of the values generated by the indicator. If the value isn't a plot, see this reference sample for how to expose from another indicator:
        Exposing indicator values that are not plots
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          You can get this value in the following method.

          Code:
          double value = Swing(strength).SwingLow[0];
          I suggest you draw dots on the chart with this value so you can see how it relates to the actual swing indicator.
          mrlogik
          NinjaTrader Ecosystem Vendor - Purelogik Trading

          Comment


            #6
            Thanks for the tip, MrLogik.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks Mrlogik. I'm currently experimenting with the code.

              @ All: I have a more general question regarding the Swing indicator:
              Its description in the Help Guide
              (http://www.ninjatrader-support.com/H...ide.html?Swing)
              mentions the following example:

              // Prints the high price of the most recent swing high
              Print("The high of the swing bar is " + High[Math.Max(0, Swing(5).SwingHighBar(0, 1, 10))]);

              I added the equivalent code snippet for swing lows:

              Print("The low of the swing bar is " + Low[Math.Min(0, Swing(5).SwingLowBar(0, 1, 10))]);

              I included this code snippet in my strategy and it prints the values in the Output Window, but they don't match the Horizontal Lines drawn by the Swing indicator.
              What's more, I don't understand why the lookBackPeriod variable is needed here since the whole point of the Swing indicator is to draw horizontal Swing High & Low lines defined by the Strength parameter and irrespective of the number of bars ago where they happened.

              All I'm trying to do here is to access the VALUES of the most recent and second most recent Swing High & Low points to be able to use them in my strategy.
              This should be really straightforward but somehow I find it difficult to accomplish.

              Thanks for enlightening me.

              Comment


                #8
                laocoon,

                I would have to dissect this a little more, but the code I gave you yesterday should accomplish this for you. That is how I reference Swing values. The only exception is that I keep a DataSeries of swing values, updating it as such.

                Code:
                private DataSeries sLow;
                
                
                OnBarUpdate()
                {
                
                sLow.Set(sLow[1]);
                if(Swing(5).SwingLow[0] != Swing(5).SwingLow[1] && Swing(5).SwingLow[0] != 0)
                    sLow.Set(Swing(5).SwingLow[0]);
                }
                Same for High. This could obviously be done in a cleaner more efficient manner.
                mrlogik
                NinjaTrader Ecosystem Vendor - Purelogik Trading

                Comment


                  #9
                  Thanks a lot for your reply MrLogik.
                  Let me go one step back: I used the code you gave me yesterday (no DataSeries yet) and created the following snippet in my strategy:

                  double SwingHigh;
                  double SwingLow;
                  bool SwingShort;
                  bool SwingLong;

                  OnBarUpdate()
                  {

                  if(Swing(3).SwingLow[0] < Swing(3).SwingLow[1])
                  {
                  SwingShort = true;
                  }
                  else
                  {
                  SwingShort = false;
                  }

                  if(Swing(3).SwingHigh[0] > Swing(3).SwingHigh[1])
                  {
                  SwingLong = true;
                  }
                  else
                  {
                  SwingLong = false;
                  }
                  }

                  By comparing the most recent Swing High value (SwingHigh[0]) with the second most recent one (SwingHigh[1]), I can find out if there's a higher high and thus an uptrend or not. To me this logic seems correct and the code compiles OK but for some reason it doesn't work. Did I make a mistake somewhere?

                  Thanks

                  Comment


                    #10
                    MrLogik: I did some tests over the weekend and now understand why the DataSeries is an essential element of the whole concept, but I'm still having some problems with it. Here's what I've got so far, but there must be a mistake somewhere because when I apply this strategy to a chart it disables itself.
                    Could I ask you to have a quick look at it?

                    Thanks a lot.

                    double SwingHigh;
                    double SwingLow;
                    bool SwingShort;
                    bool SwingLong;
                    private DataSeries sLow;
                    private DataSeries sHigh;


                    OnBarUpdate()
                    {

                    sLow.Set(sLow[1]);
                    if(Swing(3).SwingLow[0] != Swing(3).SwingLow[1] && Swing(3).SwingLow[0] != 0)
                    sLow.Set(Swing(3).SwingLow[0]);

                    sHigh.Set(sHigh[1]);
                    if(Swing(3).SwingHigh[0] != Swing(3).SwingHigh[1] && Swing(3).SwingHigh[0] != 0)
                    sHigh.Set(Swing(3).SwingHigh[0]);



                    if(Swing(3).SwingLow[0] < Swing(3).SwingLow[1])
                    {
                    SwingShort = true;
                    }
                    else
                    {
                    SwingShort = false;
                    }

                    if(Swing(3).SwingHigh[0] > Swing(3).SwingHigh[1])
                    {
                    SwingLong = true;
                    }
                    else
                    {
                    SwingLong = false;
                    }

                    if (SwingLong == true && other conditions here )
                    {
                    Do this etc
                    }

                    Comment


                      #11
                      laocoon, is it throwing any error to the log tab as it gets into disabled state?

                      Thanks
                      BertrandNinjaTrader Customer Service

                      Comment


                        #12
                        Bertrand,

                        The code compiles fine and there's no error message in the log.
                        That's what got me thinking.

                        Thanks

                        Comment


                          #13
                          Laocoon,

                          Try removing and reapplying the strategy to your chart. You'll also have to verify that you are currently connected.
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            RyanM,

                            Thanks for your suggestions, but I did all that and it still doesn't work.

                            Thanks

                            Comment


                              #15
                              laocoon, would you mind forwarding us the strategy to support at ninjatrader dot com to give it a test run? Have you checked the days trace / log files? Is there any suspicious entry around the time it stops working? Would the traces orders output shed any light into the situation?

                              Thanks
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Irukandji, Yesterday, 02:53 AM
                              2 responses
                              17 views
                              0 likes
                              Last Post Irukandji  
                              Started by adeelshahzad, Today, 03:54 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post adeelshahzad  
                              Started by CortexZenUSA, Today, 12:53 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post CortexZenUSA  
                              Started by CortexZenUSA, Today, 12:46 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post CortexZenUSA  
                              Started by usazencortex, Today, 12:43 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post usazencortex  
                              Working...
                              X