Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Market Replay: incorrect values of Swing indicator

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

    Market Replay: incorrect values of Swing indicator

    hi,
    using the Market Reply I'm getting errors in the calculation of Swing.

    On FirstTickOfBar, I print on Output Window the values of the Swing indicator (strength=3) for the last 3 bars [0], [1] and [2]

    As you can see in image below, I do not get the correct values of the indicator of Swing.

    for example, at 08:14 in Output Window
    Swing[0] = 11949.5 but the correct value is 11948.0
    Swing[1] = 11949.5 but the correct value is 11948.0
    Swing[2] = 11949.5 and this is the correct value!

    Why?




    #2
    Hello umbertosm,

    What you are getting are the calculated results of the swing at these points but not the plot of the swing.

    Each bar the swing will re-calculate swing levels, but the rest of the internal code is determining whether or not to update the plot value.

    If this is what you are looking for you would need to save the swing indicator as a new indicator that you can custom code.

    You would then go to the properties section of the new indicator and change the SwingHighPlot and SwingLowPlot and change these to Public DataSeries rather then the current Private.

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Cal View Post
      What you are getting are the calculated results of the swing at these points but not the plot of the swing.

      Each bar the swing will re-calculate swing levels, but the rest of the internal code is determining whether or not to update the plot value.

      This thing is very misleading for those who will write a strategy with this indicator Swing.
      I wanted to print in the Output Window the Swing values on the bars [0], [1] and [2] because I observed that my simple strategy NOT work at all.

      As you can see I used the strategy wizard.
      So the wizard is misleading, because it allows you to quickly write,

      if (Close [1] > Swing (StrenghtSwing) .SwingHigh [2] && Open [0] > Swing (StrenghtSwing) .SwingHigh [2])
      { EnterShort (DefaultQuantity, "SWShort"); }

      when instead NinjaTrader NOT work at all as allows writing.




      If I write code with the wizard, how do I know if what I write is correct?
      The wizard allows me to use indicators that do not do what is encoded!

      How many other indicators NOT behave as they should?
      With the Bollinger Bands it happens just the same?

      Moreover, even on the current bar [0] the value Market Replay gets for the Swing indicator is always wrong.
      On FirstTickOfBar, by definition, the Swing is always N/A, as properly displayed in the Data Box: only at the end of the bar you can know the value of Swing, but instead, mistakenly, Maket Replay prints Swing [0] = a value, always!

      I think there is a problem or in coding of Swing indicator or in the functioning of the Market Replay.


      Originally posted by NinjaTrader_Cal View Post
      If this is what you are looking for you would need to save the swing indicator as a new indicator that you can custom code.

      You would then go to the properties section of the new indicator and change the SwingHighPlot and SwingLowPlot and change these to Public DataSeries rather then the current Private.

      Let me know if I can be of further assistance.
      If it is a problem of code of Swing indicator, how many other indicators have to be modified to use them in a Strategy?

      These indicators work well for the MANUAL trading, but if you want to use in a TRADING SYSTEM they must be changed!
      Is it therefore necessary, time by time, to check if they work properly?


      regards,
      Umberto

      Comment


        #4
        Originally posted by umbertosm View Post
        This thing is very misleading for those who will write a strategy with this indicator Swing.
        I wanted to print in the Output Window the Swing values on the bars [0], [1] and [2] because I observed that my simple strategy NOT work at all.

        As you can see I used the strategy wizard.
        So the wizard is misleading, because it allows you to quickly write,

        if (Close [1] > Swing (StrenghtSwing) .SwingHigh [2] && Open [0] > Swing (StrenghtSwing) .SwingHigh [2])
        { EnterShort (DefaultQuantity, "SWShort"); }

        when instead NinjaTrader NOT work at all as allows writing.




        If I write code with the wizard, how do I know if what I write is correct?
        The wizard allows me to use indicators that do not do what is encoded!

        How many other indicators NOT behave as they should?
        With the Bollinger Bands it happens just the same?

        Moreover, even on the current bar [0] the value Market Replay gets for the Swing indicator is always wrong.
        On FirstTickOfBar, by definition, the Swing is always N/A, as properly displayed in the Data Box: only at the end of the bar you can know the value of Swing, but instead, mistakenly, Maket Replay prints Swing [0] = a value, always!

        I think there is a problem or in coding of Swing indicator or in the functioning of the Market Replay.




        If it is a problem of code of Swing indicator, how many other indicators have to be modified to use them in a Strategy?

        These indicators work well for the MANUAL trading, but if you want to use in a TRADING SYSTEM they must be changed!
        Is it therefore necessary, time by time, to check if they work properly?


        regards,
        Umberto
        The indicator is not wrong. It is returning what it is written to return and is logically consistent. Here is the documentation on the property that you are accessing.
        Syntax - Value
        High Value
        Swing(int strength).SwingHigh[int barsAgo]
        Swing(IDataSeries input, int strength).SwingHigh[int barsAgo]

        Low Value
        Swing(int strength).SwingLow[int barsAgo]
        Swing(IDataSeries input, int strength).SwingLow[int barsAgo]

        Return Value
        double; Accessing this method via an index value [int barsAgo] returns the indicator value of the referenced bar.
        * A return value of 0 (zero) will be returned if the CurrentBar is less than the "strength" value or a swing pivot has not yet been found.
        (emphasis mine)

        Inter alia, it "... returns the indicator value of the referenced bar." What is the indicator value? It is the value of the last Swing value. By definition, the Swing value will not be updated until there is a new Swing, at which time that new value will hold until another new Swing causes an update.

        Until the new Swing is validated (by the Strength criterion), the value will be the last valid value. When the new Swing is validated, the Plot is back-adjusted to the value at the most current Swing. In other words, this indicator does some possibly, unavoidable, repainting, because a Swing can only really be confirmed after it has occurred (i.e., with some amount of hindsight). This may require you to change your logic to avoid using values within the Swing development validation, until the Swing is confirmed.

        These are lines 119 - 127 from the indicator code.
        Code:
        					if (isSwingHigh)
        					{
        						[COLOR="blue"][B]for (int i=0; i<=strength; i++)
        							swingHighSeries.Set(i, lastSwingHighValue);[/B][/COLOR]
        					}
        					else 
        					{ 
        						swingHighSeries.Set(lastSwingHighValue);
        					}
        The for loop that I have emphasised in blue is resetting the values within the SwingStrength validation bars.

        Yes, before you say so: I agree with you that the description of the indicator could be considered somewhat misleading, as this nuance is not quite ever explained.
        Last edited by koganam; 03-02-2016, 11:18 PM. Reason: Removed extraneous text.

        Comment


          #5
          thanks koganam, you have been very clear!
          Congratulations for your in-depth knowledge of the code.

          The only solution to my strategy is to NOT use the indicator of Swing, but building the swing points high/low within the code of the trading system.

          Comment


            #6
            Originally posted by umbertosm View Post
            thanks koganam, you have been very clear!
            Congratulations for your in-depth knowledge of the code.

            The only solution to my strategy is to NOT use the indicator of Swing, but building the swing points high/low within the code of the trading system.
            Whereas that is certainly a viable approach, it is not really any different from just ensuring that you validate all your logic against Swing (StrenghtSwing) .SwingHigh [Math.Max(StrenghtSwing, 2)], or whatever other value you choose to be the lookBack offset. The idea is that you only start evaluating a Swing after, and as soon as, it is confirmed. (Note that I have used your exact syntax in the post: even though it looks like a possible misspelling? )

            Comment


              #7
              thanks koganam, but my idea is to completely abandon the Swing indicator and find swing points with another code.
              For example, in metaquote language (for Metatrader) I find the swing points of in this way ... and it works great!

              PHP Code:
                 int SWStrength=3;
                 
              bool SwingLowFound=false;
                 
              int ij;
                 for(
              j=SWStrengthj<Bars-SWStrengthj++)
                   {
                    
              //Print("............ j="+j);
                    
              SwingLowFound true;
                    for(
              i=1;i<=SWStrength;i++)
                      {
                       if (!(
              Low[j+i] >= Low[j]  &&  Low[j] <= Low[j-i]) )  
                         {
                          
              SwingLowFound false;
                          
              //Print( Low[j+i]+" >= "+Low[j]+" && "+Low[j]+" <= "+Low[j-i] );
                          //Print("i="+i+"   SwingLowFound="+SwingLowFound);
                         
              }
                       if (
              SwingLowFound==false) break; // out 'for i' --> go to next 'for j'
                      
              }
                    if (
              SwingLowFound==true) break;
                   }
                 if (
              SwingLowFound==true)   Print("SwingLow="+IntegerToString(j));
                 else                       Print(
              "SwingLow= N.A.");
                } 

              Comment


                #8
                Originally posted by umbertosm View Post
                thanks koganam, but my idea is to completely abandon the Swing indicator and find swing points with another code.
                For example, in metaquote language (for Metatrader) I find the swing points of in this way ... and it works great!

                PHP Code:
                   int SWStrength=3;
                   
                bool SwingLowFound=false;
                   
                int ij;
                   for(
                j=SWStrengthj<Bars-SWStrengthj++)
                     {
                      
                //Print("............ j="+j);
                      
                SwingLowFound true;
                      for(
                i=1;i<=SWStrength;i++)
                        {
                         if (!(
                Low[j+i] >= Low[j]  &&  Low[j] <= Low[j-i]) )  
                           {
                            
                SwingLowFound false;
                            
                //Print( Low[j+i]+" >= "+Low[j]+" && "+Low[j]+" <= "+Low[j-i] );
                            //Print("i="+i+"   SwingLowFound="+SwingLowFound);
                           
                }
                         if (
                SwingLowFound==false) break; // out 'for i' --> go to next 'for j'
                        
                }
                      if (
                SwingLowFound==true) break;
                     }
                   if (
                SwingLowFound==true)   Print("SwingLow="+IntegerToString(j));
                   else                       Print(
                "SwingLow= N.A.");
                  } 
                That is actually no different from the Swing code, and may even be more resource intensive, as you are running a nested loop on every tick. The only real issue in this case is the limited repainting/reset of the code within the Swing validation bars.

                Certainly, putting the code directly in the class has the benefit of the code being self-contained.
                Last edited by koganam; 03-26-2015, 10:18 AM.

                Comment


                  #9
                  Originally posted by koganam View Post
                  That is actually no different from the Swing code, and may even be more resource intensive, as you are running a nested loop on every tick.
                  I'd run my code only once to start bar,
                  but the key difference is that
                  - with my code the value of swing on the bar [1] or [2] is always correct
                  - instead, the calculation of Swing indicator on the bar [1] or [2] is often incorrect, as I checked

                  Comment


                    #10
                    Originally posted by umbertosm View Post
                    I'd run my code only once to start bar,
                    but the key difference is that
                    - with my code the value of swing on the bar [1] or [2] is always correct
                    - instead, the calculation of Swing indicator on the bar [1] or [2] is often incorrect, as I checked
                    Analyze your code again, and you will see that on every bar you run the code, you are evaluating the bar SWStrength ago, just the same as always evaluating Swing(SWStrength).SwingHigh [SWStrength].

                    The same response that Harry gives here: http://www.ninjatrader.com/support/f...89&postcount=2

                    But no matter: the only thing that counts is that you have found a formulation that works for you.

                    All the best.

                    Comment


                      #11
                      Originally posted by koganam View Post
                      Analyze your code again, and you will see that on every bar you run the code, you are evaluating the bar SWStrength ago, just the same as always evaluating Swing(SWStrength).SwingHigh [SWStrength].

                      But no matter: the only thing that counts is that you have found a formulation that works for you.
                      Sorry koganam, but I disagree
                      I attach my strategy code swing_my.cs
                      where I do the comparison between
                      - the value of swing low with my code
                      - and the value calculated by Swing indicator with
                      Swing(SWStrength).SwingLow[1]

                      My code correctly calculates the values of swing on bar[1]
                      instead Swing indicator is correct on the chart,
                      but the values obtained from the code
                      Swing(SWStrength).SwingLow[1]
                      sometimes miscalculate the value!

                      You can check it yourself with swing_my.cs and the Market Replay.

                      As you can see in image below, at the first tick of the bar 10:34, as the bar 10:33 did a minimum lower than the last Swing,
                      in Data Box at 10:33 Swing Low = N/A:
                      - my code correctly calculates N/A,
                      - while the Swing indicator does a miscalculation!




                      PHP Code:
                              protected override void Initialize()
                              {
                                      
                      // il codice della strategy viene elaborato: 'true' soltanto ad ogni chiusura di barra, o 'false' ad ogni nuovo tick
                                  
                      CalculateOnBarClose false;
                              }

                              
                      /// <summary>
                              /// Called on each bar update event (incoming tick)
                              /// </summary>
                              
                      protected override void OnBarUpdate()
                              {
                                  if (
                      FirstTickOfBar)
                                  {
                                      
                      int i_SwingLow iBar_SwingLow(SWStrength);
                                      
                      string str_my_SwingLow_1;
                                      if         (
                      i_SwingLow!=-1)    str_my_SwingLow_1 Low[i_SwingLow].ToString("0.0");
                                      else                         
                      str_my_SwingLow_1 "N/A";

                                      
                      double indicator_SwingLow_1 Swing(SWStrength).SwingLow[1];    // Swing(int strength).SwingLow[int barsAgo]
                                      
                                      
                      Print(Time[0].ToString("dd/MM/yyyy") + "," Time[0].ToString("HH:mm:ss"));
                                      Print(
                      "     Open[0]=" Open[0] + ", Close[1]=" Close[1] + ", my_SwingLow_1=" str_my_SwingLow_1 ", indicator_SwingLow_1=" indicator_SwingLow_1.ToString("0.0"));
                                  }
                              }        

                          
                              
                      // ---------------------------------------------------------+
                              /// iBar_SwingLow(int SWStrength) finds bar's index of the most recent swing low with 'SWStrength' bars
                              /// 
                              
                      private int iBar_SwingLow(int SWStrength)
                              {
                                  
                      int numSearchBars 100;
                                  
                      bool SwingLowFound=false
                                  
                      int ij
                                  for(
                      j=SWStrength+1j<numSearchBars-SWStrengthj++) 
                                  { 
                                      
                      //Print("............ j="+j); 
                                      
                      SwingLowFound true
                                      for(
                      i=1;i<=SWStrength;i++) 
                                      { 
                                          if (!(
                      Low[j+i] >= Low[j]  &&  Low[j] <= Low[j-i]) )   
                                          { 
                                              
                      SwingLowFound false
                                              
                      //Print( Low[j+i]+" >= "+Low[j]+" && "+Low[j]+" <= "+Low[j-i] ); 
                                              //Print("i="+i+"   SwingLowFound="+SwingLowFound); 
                                          

                                          if (
                      SwingLowFound==false) break; // out 'for i' --> go to next 'for j' 
                                      

                                      if (
                      SwingLowFound==true) break; 
                                  } 
                                  
                      // if a swing low is found, it should be verified that recent bars up to the bar [1] does not have a minimum less than swing low
                                  
                      if (SwingLowFound==true)
                                  {
                                      Print(
                      "last iBar_SwingLow="+j+" --> SwingLow="+Low[j].ToString("0.0"));
                                      for (
                      int k=j-1k>k--)
                                      {
                                          if (
                      Low[k]<Low[j])
                                          {
                                              
                      SwingLowFound=false;
                                              return(-
                      1);
                                          }
                                      }
                                      return(
                      j);
                                  }    
                                  else
                                  {
                                      Print(
                      "last iBar_SwingLow= N.A."); 
                                      return(-
                      1);
                                  }
                              } 
                      Attached Files

                      Comment


                        #12
                        Originally posted by umbertosm View Post
                        Sorry koganam, but I disagree
                        I attach my strategy code swing_my.cs
                        where I do the comparison between
                        - the value of swing low with my code
                        - and the value calculated by Swing indicator with
                        Swing(SWStrength).SwingLow[1]

                        My code correctly calculates the values of swing on bar[1]
                        instead Swing indicator is correct on the chart,
                        but the values obtained from the code
                        Swing(SWStrength).SwingLow[1]
                        sometimes miscalculate the value!

                        You can check it yourself with swing_my.cs and the Market Replay.

                        As you can see in image below, at the first tick of the bar 10:34, as the bar 10:33 did a minimum lower than the last Swing,
                        in Data Box at 10:33 Swing Low = N/A:
                        - my code correctly calculates N/A,
                        - while the Swing indicator does a miscalculation!




                        PHP Code:
                                protected override void Initialize()
                                {
                                        
                        // il codice della strategy viene elaborato: 'true' soltanto ad ogni chiusura di barra, o 'false' ad ogni nuovo tick
                                    
                        CalculateOnBarClose false;
                                }

                                
                        /// <summary>
                                /// Called on each bar update event (incoming tick)
                                /// </summary>
                                
                        protected override void OnBarUpdate()
                                {
                                    if (
                        FirstTickOfBar)
                                    {
                                        
                        int i_SwingLow iBar_SwingLow(SWStrength);
                                        
                        string str_my_SwingLow_1;
                                        if         (
                        i_SwingLow!=-1)    str_my_SwingLow_1 Low[i_SwingLow].ToString("0.0");
                                        else                         
                        str_my_SwingLow_1 "N/A";

                                        
                        double indicator_SwingLow_1 Swing(SWStrength).SwingLow[1];    // Swing(int strength).SwingLow[int barsAgo]
                                        
                                        
                        Print(Time[0].ToString("dd/MM/yyyy") + "," Time[0].ToString("HH:mm:ss"));
                                        Print(
                        "     Open[0]=" Open[0] + ", Close[1]=" Close[1] + ", my_SwingLow_1=" str_my_SwingLow_1 ", indicator_SwingLow_1=" indicator_SwingLow_1.ToString("0.0"));
                                    }
                                }        

                            
                                
                        // ---------------------------------------------------------+
                                /// iBar_SwingLow(int SWStrength) finds bar's index of the most recent swing low with 'SWStrength' bars
                                /// 
                                
                        private int iBar_SwingLow(int SWStrength)
                                {
                                    
                        int numSearchBars 100;
                                    
                        bool SwingLowFound=false
                                    
                        int ij
                                    for(
                        j=SWStrength+1j<numSearchBars-SWStrengthj++) 
                                    { 
                                        
                        //Print("............ j="+j); 
                                        
                        SwingLowFound true
                                        for(
                        i=1;i<=SWStrength;i++) 
                                        { 
                                            if (!(
                        Low[j+i] >= Low[j]  &&  Low[j] <= Low[j-i]) )   
                                            { 
                                                
                        SwingLowFound false
                                                
                        //Print( Low[j+i]+" >= "+Low[j]+" && "+Low[j]+" <= "+Low[j-i] ); 
                                                //Print("i="+i+"   SwingLowFound="+SwingLowFound); 
                                            

                                            if (
                        SwingLowFound==false) break; // out 'for i' --> go to next 'for j' 
                                        

                                        if (
                        SwingLowFound==true) break; 
                                    } 
                                    
                        // if a swing low is found, it should be verified that recent bars up to the bar [1] does not have a minimum less than swing low
                                    
                        if (SwingLowFound==true)
                                    {
                                        Print(
                        "last iBar_SwingLow="+j+" --> SwingLow="+Low[j].ToString("0.0"));
                                        for (
                        int k=j-1k>k--)
                                        {
                                            if (
                        Low[k]<Low[j])
                                            {
                                                
                        SwingLowFound=false;
                                                return(-
                        1);
                                            }
                                        }
                                        return(
                        j);
                                    }    
                                    else
                                    {
                                        Print(
                        "last iBar_SwingLow= N.A."); 
                                        return(-
                        1);
                                    }
                                } 
                        1. I see that you completely ignored my last post and the need to calculate with an offset of SWStrength if values are to be correctly invariant in the Swing Indicator.
                        2. That aside, you need to compare like to like. Swing() calculates with COBC = true, so necessarily, on FirstTickOfBar must return "N/A" as the bar has not yet closed. Your indicator is using COBC = false. That means that the execution context is different between the two of them.
                        3. Regardless, as I have said, use whatever makes the most sense to you. We all code according to our lights.
                        Last edited by koganam; 03-02-2016, 11:08 PM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by GussJ, 03-04-2020, 03:11 PM
                        11 responses
                        3,221 views
                        0 likes
                        Last Post xiinteractive  
                        Started by andrewtrades, Today, 04:57 PM
                        1 response
                        10 views
                        0 likes
                        Last Post NinjaTrader_Manfred  
                        Started by chbruno, Today, 04:10 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post chbruno
                        by chbruno
                         
                        Started by josh18955, 03-25-2023, 11:16 AM
                        6 responses
                        438 views
                        0 likes
                        Last Post Delerium  
                        Started by FAQtrader, Today, 03:35 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post FAQtrader  
                        Working...
                        X