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

Problem with Swing Prints

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

    Problem with Swing Prints

    Hi Guys... I'm just trying to print the swing values to ensure that I'm referencing the correct swings in my trading logic. Here's what I've done:

    if (1==1)
    {
    SHCurrent = Swing(swingStrength).SwingHighBar(barsago,1,lookba ck);
    SLCurrent = Swing(swingStrength).SwingHighBar(barsago,1,lookba ck);
    SH1 = Swing(swingStrength).SwingHighBar(barsago,2,lookba ck);
    SL1 = Swing(swingStrength).SwingHighBar(barsago,3,lookba ck);
    Print("SH0 = " + SHCurrent + "SL0 = " + SLCurrent + "SH1 = " + SH1 + "SL1 = " + SL1 );

    }


    But all the returns is this:

    SH0 = -1SL0 = -1SH1 = -1SL1 = -1

    Doesen't seem right :-)

    Tks,

    Brian

    #2
    Hi Brian,

    -1 means it found no instance of swing high. Try increasing your lookback so that it looks further historically.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Oh... I see that in the documentation now.

      So I changed that... but now the double values are off:

      Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


      SH0 = 331SL0 = 331SH1 = -1SL1 = -1
      SH0 = 76SL0 = 76SH1 = 332SL1 = 332
      SH0 = 77SL0 = 77SH1 = 333SL1 = 333
      SH0 = 78SL0 = 78SH1 = 334SL1 = 334

      Comment


        #4
        I'm not sure what you mean by double values. It returns a bars ago value that represents the number of bars since the swing high, not the prices of a particular swing high.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Hi Ryan... thanks for the help, but I'm still not getting where I want to go. All I want to do is get the values of the last two swing highs and lows. This is the code I have so far:

          if (1==1)
          {
          //Swing(swingStrength).SwingHighBar(barsago,1,lookba ck);
          //Swing(swingStrength).SwingHighBar(barsago,1,lookba ck);
          SH1BarsAgo = Swing(swingStrength).SwingHighBar(barsago,2,lookba ck);
          SL1BarsAgo = Swing(swingStrength).SwingHighBar(barsago,2,lookba ck);

          SHCurrent = Swing(SwingStrength).SwingHigh[0];
          SLCurrent = Swing(SwingStrength).SwingLow[0];
          SH1 = Swing(SwingStrength).SwingHigh[SH1BarsAgo];
          SL1 = Swing(SwingStrength).SwingLow[SL1BarsAgo];

          Print("SH0 = " + SHCurrent + "SL0 = " + SLCurrent + "SH1 = " + SH1 + "SL1 = " + SL1 );

          ... and this is what it produces:

          SH0 = 98.26SL0 = 0SH1 = 98.26SL1 = 0
          **NT** Error on calling 'OnBarUpdate' method for strategy 'TickSwingFader/c7d05927ceed44c4a362eadbe2e0bb8d': barsAgo needed to be between 0 and 255 but was 332

          Comment


            #6
            heyligerb,

            It appears you have some set of code that is looking back too far into the past. You could try to set maximum bars to look back to infinite, but this is not memory optimal.

            Code:
            protected override void Initialize() 
            {
                // Store all series values instead of only the last 256 values
                MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
            }
            Your only other option is to find out why your code is looking back that far into the past and making some check to ensure it doesn't do so. Without seeing more of your code, we cannot pinpoint the issue right away.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Sure... here is all the code. I'm running this on a 1 tick timeframe:

              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              /// <summary>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class TickSwingFader : Strategy
              {
              #region Variables
              // Wizard generated variables
              private int swingStrength = 75; // Default setting for SwingStrength
              private int target = 2; // Default setting for Target
              private int barsago = 100;
              private int lookback = 5; // The numnber of bars to look back to check swing condition
              private double SHCurrent = 0;
              private double SLCurrent = 0;
              private double SH1 = 0; // Previous swing high
              private double SL1 = 0; // Previous swing low
              private int SH1BarsAgo = 0; // Number of bars away of the previous swing
              private int SL1BarsAgo = 0; // Number of bars away of the previous swing

              // User defined variables (add any user defined variables below)
              #endregion

              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
              SetProfitTarget("", CalculationMode.Ticks, Target);
              SetStopLoss("", CalculationMode.Price, Variable0, true);

              CalculateOnBarClose = false;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              if (1==1)
              {
              //Swing(swingStrength).SwingHighBar(barsago,1,lookba ck);
              //Swing(swingStrength).SwingHighBar(barsago,1,lookba ck);
              SH1BarsAgo = Swing(swingStrength).SwingHighBar(barsago,2,lookba ck);
              SL1BarsAgo = Swing(swingStrength).SwingHighBar(barsago,2,lookba ck);
              SHCurrent = Swing(SwingStrength).SwingHigh[0];
              SLCurrent = Swing(SwingStrength).SwingLow[0];
              SH1 = Swing(SwingStrength).SwingHigh[SH1BarsAgo];
              SL1 = Swing(SwingStrength).SwingLow[SL1BarsAgo];

              Print("SH0 = " + SHCurrent + "SL0 = " + SLCurrent + "SH1 = " + SH1 + "SL1 = " + SL1 );


              }


              }

              Comment


                #8
                You need to choose a starting point to check for these swing highs. If you want to get the two most recent swing highs, then the last bar on the chart is a suitable starting point.

                There are a couple recent threads here on zig zag (has similar methods to swing) high with examples on this:


                Here is the snippet where the two most recent zig zag highs are captured, and the time between them is printed.

                Code:
                	
                if (Count - 2 == CurrentBar) //Last bar on chart when COBC = true. 
                {
                	int zzHigh1 =  Math.Max(0, ZigZag(DeviationType.Points, 0.5, false).HighBar(0, 1, Bars.BarsSinceSession));
                	int zzHigh2 =  Math.Max(0, ZigZag(DeviationType.Points, 0.5, false).HighBar(0, 2, Bars.BarsSinceSession));
                									
                	TimeSpan myTimeSpan = Time[zzHigh1] - Time[zzHigh2];
                					
                	Print(myTimeSpan.ToString());
                }
                Last edited by NinjaTrader_RyanM1; 11-18-2011, 11:47 AM.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Your syntax here is looking for the Swing that occurred 100 bars ago. (barsago is 100, per your variable definitions)
                  Code:
                  SH1BarsAgo = Swing(swingStrength).SwingHighBar(barsago,2,lookba  ck);
                  If you are looking for Swings relative to the currentBar, your barsago parameter should be zero, not 100. There are other lines that will need correction too, along similar lines.

                  Comment


                    #10
                    Yea... thanks for that. I had it at 0, but changed it to 100 to see if it would help. Obviously it didn't accomplish what I was looking for. That said... I think I actually have it working now:

                    SH0 = 97.88SL0 = 97.83SH1 = 97.92SL1 = 97.86

                    I'm astonished.

                    Any ideas on how to have it print only when the swing levels update? Rather than on every bar update?

                    Comment


                      #11
                      Originally posted by heyligerb View Post
                      Yea... thanks for that. I had it at 0, but changed it to 100 to see if it would help. Obviously it didn't accomplish what I was looking for. That said... I think I actually have it working now:

                      SH0 = 97.88SL0 = 97.83SH1 = 97.92SL1 = 97.86

                      I'm astonished.

                      Any ideas on how to have it print only when the swing levels update? Rather than on every bar update?
                      You would have to write a filter.
                      1. Record the SwingValue as a store.
                      2. Take the Swing on the currentBar, and compare to the stored value.
                      3. If not the same, then Print() and update SwingValue.

                      Comment


                        #12
                        Okay... thanks. And I would use something like this to do that?

                        Comment


                          #13
                          I got it working guys... thanks for all the help. Here's the code for all to see:

                          // Get the last two current swings and update all variables
                          SH1BarsAgo = Swing(swingStrength).SwingHighBar(barsago,2,lookba ck);
                          SL1BarsAgo = Swing(swingStrength).SwingLowBar(barsago,2,lookbac k);
                          SH0 = Swing(SwingStrength).SwingHigh[0];
                          SL0 = Swing(SwingStrength).SwingLow[0];
                          SH1 = Swing(SwingStrength).SwingHigh[SH1BarsAgo];
                          SL1 = Swing(SwingStrength).SwingLow[SL1BarsAgo];

                          // If the current variable don't match the stored variables, update and print
                          if (SH0 != SH0Store || SH0 ==0 || SL0 != SL0Store || SL0 ==0 || SH1 !=SH1Store || SH1 ==0 || SL1 != SL1Store || SL1 == 0)
                          {

                          SH0Store = Swing(SwingStrength).SwingHigh[0];
                          SL0Store = Swing(SwingStrength).SwingLow[0];
                          SH1Store = Swing(SwingStrength).SwingHigh[SH1BarsAgo];
                          SL1Store = Swing(SwingStrength).SwingLow[SL1BarsAgo];
                          Print(Time [0] +" SH0 =" + SH0 + " SL0 =" + SL0 + " SH1 =" + SH1 + " SL1 =" + SL1 );
                          }

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by andrewtrades, Today, 04:57 PM
                          1 response
                          8 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by chbruno, Today, 04:10 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post chbruno
                          by chbruno
                           
                          Started by josh18955, 03-25-2023, 11:16 AM
                          6 responses
                          436 views
                          0 likes
                          Last Post Delerium  
                          Started by FAQtrader, Today, 03:35 PM
                          0 responses
                          7 views
                          0 likes
                          Last Post FAQtrader  
                          Started by rocketman7, Today, 09:41 AM
                          5 responses
                          19 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X