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

Determining Pivots based on oscillation around SMA

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

    Determining Pivots based on oscillation around SMA

    All,

    This one's doing my head in. I am attempting to determine "pivot points" or "turning points" based on price action's oscillation around a SMA.

    I do not believe the "Pivots" formula is capable of doing this. I've attempted to determine it with the SwingHighBar and SwingLowBar formulas which gets it correct sometimes but not consistently. The Swing points are arbitrarily set at 1/3 the SMA period.

    The attached picture illustrates the points I'm trying to determine (circled green) and the incorrect Swing points (circled red).

    Any ideas on how to approach this would be greatly appreciated.

    Thanks
    Shannon
    Attached Files

    #2
    swing high/swing low

    you can not determine a swing high or low before you inspect a certain number of bars after the suspected swing.

    a swing high with a strength of 5 is established when the highs of the 5 bars prior of the suspected swing are lower and the high of the 5 bars after the suspected swing are lower.

    basically you have to count lower highs before the suspected swing (the high of the series) and lower high after the suspected swing. invert for a swing low.

    if you want a strength of 5 count 5 bars before and 5 bars after. hope this helps

    Comment


      #3
      Ivatan,

      Thank you for your response. I am aware how the SwingHighBar and SwingLowBar functions operate.

      As noted in my original post, while I've tried a makeshift method of determining the "pivot points" or "turning points" based on price action's oscillation around a SMA using arbitrarily set SwingHighBar and SwingLowBar functions, it has been unsuccessful.

      I believe the solution to this problem may require several new dataseries in the code. For example, in determining the "High turning points", perhaps something along the lines of if price > SMA then datapoint = High of the bar. This would result in all datapoints where the price < SMA being set to zero or possibly null. Effectively this isolates all price oscillations above the SMA. (A second dataseries would be created as the first step in establishing the "Low turning points").

      This is where my logic fails, I do not know how to go about determining the "High turning points" in the new dataseries without using the SwingHighBar function. Introducing the SwingHighBar function at this point would be no different to using it from the start, which I've already determined is not appropriate.

      Another issue is the "combining" of these two dataseries, containing the "high turning points" and the "low turning points" into the one dataseries.

      Again, throwing this out to the floor, any suggestions on the logic of how to go about this, or the code in which to do it, would be greatly appreciated.

      Thanks
      Shannon

      Comment


        #4
        Originally posted by Shansen View Post
        Ivatan,

        Thank you for your response. I am aware how the SwingHighBar and SwingLowBar functions operate.

        As noted in my original post, while I've tried a makeshift method of determining the "pivot points" or "turning points" based on price action's oscillation around a SMA using arbitrarily set SwingHighBar and SwingLowBar functions, it has been unsuccessful.

        I believe the solution to this problem may require several new dataseries in the code. For example, in determining the "High turning points", perhaps something along the lines of if price > SMA then datapoint = High of the bar. This would result in all datapoints where the price < SMA being set to zero or possibly null. Effectively this isolates all price oscillations above the SMA. (A second dataseries would be created as the first step in establishing the "Low turning points").

        This is where my logic fails, I do not know how to go about determining the "High turning points" in the new dataseries without using the SwingHighBar function. Introducing the SwingHighBar function at this point would be no different to using it from the start, which I've already determined is not appropriate.

        Another issue is the "combining" of these two dataseries, containing the "high turning points" and the "low turning points" into the one dataseries.

        Again, throwing this out to the floor, any suggestions on the logic of how to go about this, or the code in which to do it, would be greatly appreciated.

        Thanks
        Shannon

        Sorry if I misunderstood something, but can't you just use simple MAX or MIN methods?
        When price crosses SMA, you mark that bar and check for MAX since that bar. Opposite for price below SMA...

        Comment


          #5
          Roonius,

          Thank you for the input. It would appear I overlooked the simple solution. Thank you.

          In coding this I require the ability to reference the last two "high turning points" and last two "low turning points". I pretty sure this would require a new dataseries?

          That said, I've run into troubles trying to "mark the bar" where price crosses above the SMA. The CrossAbove function does not have a barsago parameter, so I can not use it to mark a historical bar. I tried the code:

          for(int i = 0; i < 100; i++)
          {
          if (Low[i] > SMA(Close, 24)[i] && Low[i+1] < SMA(Close,24)[i+1])
          PrevCrossAboveBar = i;
          }

          However this results in a CS0200 error.

          As always, any suggestions are welcome.
          Shannon

          Comment


            #6
            Is your prevCrossAboveBar a DataSeries? If yes, you would net .Set to assign the values...

            Otherwise I would look in the MRO / LRO methods to accomplish what you are looking for.



            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by Shansen View Post
              Roonius,

              Thank you for the input. It would appear I overlooked the simple solution. Thank you.

              In coding this I require the ability to reference the last two "high turning points" and last two "low turning points". I pretty sure this would require a new dataseries?

              That said, I've run into troubles trying to "mark the bar" where price crosses above the SMA. The CrossAbove function does not have a barsago parameter, so I can not use it to mark a historical bar. I tried the code:

              for(int i = 0; i < 100; i++)
              {
              if (Low[i] > SMA(Close, 24)[i] && Low[i+1] < SMA(Close,24)[i+1])
              PrevCrossAboveBar = i;
              }

              However this results in a CS0200 error.

              As always, any suggestions are welcome.
              Shannon

              Also make sure you have condition
              if (CurrentBar < 1) return;

              Comment


                #8
                Bertrand & Roonius,

                Again thanks for your input.

                I know this should be simple, I just can't see where I'm going wrong.

                Earlier in the thread, Roonius correctly pointed out the solution to the "high turning points" problem would be to simply "mark" the bar where price crossed above the SMA and check for the MAX since that bar. It quickly became clear that the MAX would be from the crossabove bar to the subsequent crossbelow bar (and vice-versa for the low turning points).

                I'm falling down in the "marking" of these bars. Bertrand, as the parameters for the MAX formula would be -> MAX(High, (CrossAboveBarsAgo - CrossBelowBarsAgo))[CrossBelowBarsAgo], I require the "marked" bars to be integers that measure BarsAgo (not a dataseries). To boot, this element will eventually form part of a multi-time frame strategy, thus ruling out the use of the LRO or MRO formulas.

                The below formula appears to working... of sorts. Though, somehow, it appears to be counting "i" from the left-hand side of the chartseries. It goes back to the first bar, yes, the first bar of the future I am analysing, not the first bar at the edge of the chart window but the first bar of futures contract. I was of the understanding that, for example, Low[i] where i = 0 would return the low of latest bar the current bar... not the first bar.

                if (CurrentBar < 1) return;
                for(int i = 0; i < 100; i++)
                {
                if (Low[i] > SMA(Close, 24)[i] && Low[i+1] < SMA(Close,24)[i+1])
                PrevCrossAboveBar = i;
                }

                Any further direction would be greatly appreciated.
                Shannon

                Comment


                  #9
                  Shannon,

                  Sorry I did not fully understand what you are trying to do. There is no concept of "future" bars. A [0] index means the most current bar. This reference is constantly rolling forward. If a new bar comes in [0] references that new bar.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh,

                    I understand a [0] index refers to the most current bar and is currently rolling forward. However, I've missed where this is a problem with the script I've attempted to write. Please elaborate.

                    Thanks
                    Shannon

                    Comment


                      #11
                      All,

                      This is a two-pronged post, two questions:

                      1) Along the lines of Roonius's suggestion in the below thread, the below script aims to identify the points where price crosses above and below the SMA to evaluate the high "turning point", where PrevCrossAboveBar and PrevCrossBelowBar are integer variables. My question is this, why would this script not return anything for the most recent 200 bars or so, given the CurrentBar is somewhere in the area of 2500?

                      protected override void OnBarUpdate()
                      {
                      if (CurrentBar < 300) return;
                      for(int i = 0; i < 200; i++)
                      {
                      if (Low[i] > SMA(Close, 72)[i] && Low[i+1] < SMA(Close,72)[i+1])
                      {
                      PrevCrossAboveBar = i+1;
                      DrawText("CA"+CurrentBar, "CA", PrevCrossAboveBar, High[PrevCrossAboveBar], Color.GreenYellow );
                      }

                      if (High[i] < SMA(Close, 72)[i] && High[i+1] > SMA(Close,72)[i+1])
                      {
                      PrevCrossBelowBar = i+1;
                      DrawText("CB"+CurrentBar, "CB", PrevCrossBelowBar, Low[PrevCrossBelowBar], Color.DarkTurquoise );
                      }
                      }
                      }


                      2) The concept around this script hinges on the identifying the MAX bar between price crossing the SMA, using the formula MAX (High, PrevCrossAboveBar - PrevCrossBelowBar) [PrevCrossBelowBar]. However, this formula returns a double being the highest high. How would I go about returning the bar?


                      As always, thanks
                      Shannon

                      Comment


                        #12
                        I think this is due to you looking 200 bars back to evaluate if bar x was a CrossBelow / CrossAbove bar, since you can do this in realtime only with the hindsight benefit.

                        You can use Highest / LowestBar for your second request -



                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          I think this is due to you looking 200 bars back to evaluate if bar x was a CrossBelow / CrossAbove bar, since you can do this in realtime only with the hindsight benefit.

                          You can use Highest / LowestBar for your second request -



                          http://www.ninjatrader-support.com/H...LowestBar.html
                          Betrand,

                          Thank you for the quick response.

                          On the first point, I should have noted that price did crossabove / crossbelow the SMA several times during the most recent 200 or so bars on the chart I was viewing. While the condition was "true" no text was drawn on the chart. I was of the understanding the DrawText("CA"+CurrentBar....) formula would draw text each time the condition as the tag is unique each time. Any ideas?

                          On the second question, the Help Guide defines the Highest / LowestBar formula syntax as HighestBar(IDataSeries series, int lookBackPeriod). With no [int barsAgo] element how can this formula be applied?

                          Again thank you
                          Shannon

                          Comment


                            #14
                            Shannon,

                            You will need to debug if the condition was truly true. Please use Print() to evaluate the condition.

                            HighestBar/LowestBar are functions that return a bar number. You can use it like this Close[HighestBar(...)] to get the close price of the highest bar.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Code:
                               
                                 if (CrossAbove(Close, SMA(sMAPeriod),1)) upbar = CurrentBar;
                                 if (CrossBelow(Close, SMA(sMAPeriod),1)) downbar = CurrentBar;
                                 if (upbar > downbar)
                                 {
                                  int x = HighestBar(High, CurrentBar - upbar);
                                  DrawDot(upbar.ToString(), true, x, High[x] + TickSize, Color.Red);
                                 }
                                 if (downbar > upbar)
                                 {
                                  int x = LowestBar(Low, CurrentBar - downbar);
                                  DrawDot(downbar.ToString(), true, x, Low[x] - TickSize, Color.Green);
                                 }
                              You can try something like this.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by algospoke, 04-17-2024, 06:40 PM
                              6 responses
                              49 views
                              0 likes
                              Last Post algospoke  
                              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
                              21 views
                              0 likes
                              Last Post traderqz  
                              Working...
                              X