Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting Swing to a variable?

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

    Setting Swing to a variable?

    Hi,

    How is it possible to set a variable equal to the last swing high (price and # of bars back)?

    I've tried:

    Opening = Bars.BarsSinceSession;
    HI_PERIOD = Swing(4).SwingHighBar(0, 1, Opening);

    I've tried using a dataseries:

    Opening = Bars.BarsSinceSession;
    HI_PERIOD.Set(Swing(4).SwingHighBar(0, 1, Opening));

    When I have variables then I can use them to reference other commands, such as Stochastics and Momentum...

    double StochHI_K = Stochastics(14, 3, 3).K[HI_PERIOD];
    double HI_MOM = Momentum(14)[HI_PERIOD];

    #2
    Hi,

    I've made some progress, however it seems (according to Print statements) these variables seem to be returning exactly the same value...can somebody point out the error here?

    HI_PERIOD = Math.Abs(Math.Max(0, Swing(7).SwingHighBar(0, 1, Opening))); //most recent high bar, back from CurrentBar
    HI_PERIOD2 = Math.Abs(Math.Max(0, Swing(7).SwingHighBar(0, 2, Opening))); //2nd most recent high bar, back from CurrentBar

    Comment


      #3
      To debug you want to remove as much excess variables as possible. Just run print statements on Swing(7).SwingHighBar(0, 1, CurrentBar) and Swing(7).SwingHighBar(0, 2, CurrentBar). Manually look at your chart to try and match up things. I usually print Time[0] to help mediate the pairing of prints with the chart too.

      Code:
      Print(Time[0] + " " + Swing(7).SwingHighBar(0, 1, CurrentBar) + " " + Swing(7).SwingHighBar(0, 2, CurrentBar));
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        So is the syntax for what I'm trying to do correct? In other words this syntax is correct to determine the last 2 "highbars" (most recent and second most recent)...I'll do the print statements I just wanted to be sure I had the code correct.

        Comment


          #5
          Yes it should work, but you really don't need the Math.Abs after the Math.Max. If you have to be at least 0 already you are already positive and Math.Abs is redundant.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Thank you.

            Comment


              #7
              Hmm, well this is odd--this seems this works fine:

              HI_PERIOD = Math.Abs(Math.Max(0, Swing(7).SwingHighBar(0, 1, Opening))); //most recent high bar, back from CurrentBar
              LAST_HI = High[HI_PERIOD];

              but this is returning an incorrect value (from Print statements), I'm at a loss as to why:

              LO_PERIOD = Math.Abs(Math.Min(0, Swing(7).SwingLowBar(0, 1, Opening))); //most recent low bar, back from CurrentBar
              LAST_LO = Low[LO_PERIOD];


              The "LO_PERIOD" DOES calculate back to the correct bar #, however the value for "LAST_LO" is returning something strange...is there an obvious code error here??

              Comment


                #8
                Are you sure you are just not viewing the wrong bar in your manual comparison? If you tell it to print the value of Low[100] it will, without fail, return you the low of 100 bars ago.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I don't see that the Math.Abs, Math.Min and Math.Max functions are buying you anything but added complexity and trouble.

                  The value of Math.Min(0, ...) will never exceed zero. All valid bar numbers are zero or greater.

                  Thus you will only get a single valid bar number as LO_PERIOD and that will be zero (the most recent bar.)

                  You may want a check the return value from "Swing(x).Swing...Bar(...)" to make sure it isn't "-1" which indicates "not found", but I don't think Math.Min/Math.Max is going to give you the desired results in any/all cases.

                  And, as Josh pointed out, the Math.Abs is redundant. I believe it's always best to remove redundant or unneeded code, so one doesn't inadvertantly confuse oneself with it later.

                  Good luck.

                  Comment


                    #10
                    Thanks for your responses. I have a related but different question. Is it possible to use the same (or similar) logic (or perhaps completely different logic) to determine the last time the Stochastics value was above/below a certain level (80/20 for example)??

                    Comment


                      #11
                      You sure can. Check out MRO. http://www.ninjatrader-support.com/H...urenceMRO.html
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks, that command seems to work nicely. I'm using the following:

                        Opening = Bars.BarsSinceSession;
                        int HI_CONSTRAINT = MRO(delegate {return Stochastics(3, 14, 7).K[0] > 80;}, 1, Opening);
                        int LO_CONSTRAINT = MRO(delegate {return Stochastics(3, 14, 7).K[0] < 20;}, 1, Opening);
                        if((HI_CONSTRAINT < 0) || (LO_CONSTRAINT < 0)){return;} //Constrain to Stochastic above AND below 80/20

                        Is there a smooth way to compare and/or test for a "swing" at the bars that the MRO command returns? For example if (for the "HI_CONSTRAINT") a value is returned of "16" bars ago what might be a command to use that could be used to check if there is a "swing" at that period 16 bars ago?

                        Comment


                          #13
                          I would say just take the MRO's return and pop it into the lookback period for finding the swing. If the swing returns the same number then you have a winner on that bar. You may or may not need to also play with the barsAgo variable. Maybe have that set to one bar before what the MRO returns and then have lookback be one bar.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Thanks, I've tried your suggestion both ways but am getting some odd results, I've tried:

                            HI_PERIOD = Math.Max(0, Swing(7).SwingHighBar(HI_CONSTRAINT - 1, 1, 1));
                            LO_PERIOD = Math.Max(0, Swing(7).SwingLowBar(LO_CONSTRAINT - 1, 1, 1));

                            And:

                            HI_PERIOD = Math.Max(0, Swing(7).SwingHighBar(0, 1, HI_CONSTRAINT));
                            LO_PERIOD = Math.Max(0, Swing(7).SwingLowBar(0, 1, LO_CONSTRAINT));



                            It is possible I mis-understood your last posting...

                            Comment


                              #15
                              The problem is that HI_CONSTRAINT can return as 0 or even -1. This becomes a problem when you simply do HI_CONSTRAINT - 1 to it because the barsAgo property needs to be at least a 0. You will need a check to prevent this from being below zero.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              569 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              330 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              548 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              548 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X