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

EMA of EMA?

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

    EMA of EMA?

    I would like to calculate the True Strength Index exactly as it was developed, which is

    TSI = (EMA(EMA(close-close[1], longLength), shortLength)) / (EMA(EMA(AbsValue(close-close[1]), longLength), shortLength)) * 100;

    Signal = ExpAverage(TSI, signalLength);

    The calculation in NT uses an EMA formula and the double smoothing isn't as accurate as it is in other platforms (thinkorswim) where you can do an EMA of an EMA directly. The result is choppier and less reliable than the one I coded in thinkscript. NT is a much better trading platform so I would love to have the exact same indicator for use in a strategy. Is there a comparable way to do this in NT?

    #2
    Hi ExAmex, this is possible in NinjaScript as well, for example consider this snippet :

    Value.Set(EMA(EMA(Period), Period)[0]);
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Originally posted by ExAmex View Post
      TSI = (EMA(EMA(close-close[1], longLength), shortLength)) / (EMA(EMA(AbsValue(close-close[1]), longLength), shortLength)) * 100;
      Code:
      TSI.Set((EMA(EMA(Close - Close[1], LongLength), ShortLength)) / (EMA(EMA(Abs(Close-Close[1]),LongLength),ShortLenght)) * 100);
      Originally posted by ExAmex View Post
      Signal = ExpAverage(TSI, signalLength);
      Code:
      Signal.Set(EMA(TSI[0], SignalLength);
      Note:
      TSI and Signal are dataseries type and have to be set in:
      Code:
      protected override void initialize()
      {
           Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "TSI"));
           Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Signal"));
           .....
      NinjaScript is Case sensitive. You have to use LongLength, ShortLength and SignalLenth for Input and not longLength, shortLength and signalLength.

      So this should work probably. Only, If it is important to see more than 256Bars, you have to set this befor everyting else:
      Code:
      protected override void initialize()
      {
           [B]MaximumBarsLookBack = MaximumBarsLookBack.Infinite;[/B]
           Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "TSI"));
           Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Signal"));
           .....
      Jens

      Comment


        #4
        Still won't compile

        Thanks for the reply. I used your code snippets in a freshly-generated script (only changing abs to math.abs and substituting my variable names) and I still can't get it to compile. Attached is a screen shot of the indicator code and the compiler errors. Any suggestions would be greatly appreciated. Love this platform, not so crazy about this scripting language.
        Attached Files
        Last edited by ExAmex; 06-30-2011, 12:32 PM. Reason: New attachment, more detail

        Comment


          #5
          Originally posted by ExAmex View Post
          Thanks for the reply. I used your code snippets in a freshly-generated script (only changing abs to math.abs and substituting my variable names) and I still can't get it to compile. Attached is a screen shot of the indicator code and the compiler errors. Any suggestions would be greatly appreciated. Love this platform, not so crazy about this scripting language.
          Maybe if you posted the entire script, we might all be able to take a crack at it?

          Comment


            #6
            The attachment in my second post should show the entire script (excluding the stuff NinjaTrader generated on its own). I would like to use this formula for the TSI because I find it whipsaws less than the code included in NT. Thanks all.
            Last edited by ExAmex; 06-30-2011, 02:05 PM. Reason: More info

            Comment


              #7
              Originally posted by ExAmex View Post
              The attachment in my second post should show the entire script (excluding the stuff NinjaTrader generated on its own). I would like to use this formula for the TSI because I find it whipsaws less than the code included in NT. Thanks all.
              It is a picture. I really am too lazy to type in all that stuff. Can you not attach the .cs file?

              Comment


                #8
                Script attached

                Sorry. .cs file attached below.
                Attached Files

                Comment


                  #9
                  Originally posted by ExAmex View Post
                  Sorry. .cs file attached below.
                  You had multiple errors in calling indicators, where you were trying to call the EMA with invalid parameters. Indicators use DataSeries as inputs, not doubles.

                  To help us all learn, instead of just writing it correctly, I have used comments to liberally show what I did, and what was wrong. I do not mean to offend: I just find that pointing out actual errors helps MY learning process better; I know that YMMV.

                  Very nice indicator for catching trends, btw!
                  Attached Files

                  Comment


                    #10
                    Many thanks.

                    Thanks for all your work. The comments have taught me much more about NinjaScript than just the code would ever have. Thanks for helping me put this invaluable indicator into my NT toolbox.

                    Comment


                      #11
                      Ok, well, I had made some littel errors. But if you apply this TSI, you may got errors: "Error on setting indicator plot for indicator 'TSIhistogramm'. Value outside of valid range."

                      For example ES 1 min. Reason: If Close[0] - Close[1] = 0 for the first Bar of the Chart, a divide by zero error occurs in the calculation of the TSI.

                      solution:
                      You have to check, if the difference of Close[0] and Close[1] is not zero:

                      Code:
                      TSI.Set((EMA(EMA(_ds1DayMomoAbs, LongLen), ShortLen)[0]) != 0 ? (EMA(EMA(_ds1DayMomo, LongLen), ShortLen)[0]) / (EMA(EMA(_ds1DayMomoAbs, LongLen), ShortLen)[0]) *100 : 0);
                      An additional Error I found was:
                      using longLen instead of LongLen. This is important! If you use longLen, your calculation only use the default values regardless what you select for input! So Change longLen to LongLen, shortLen to ShortLen and signalLen to SignalLen in your calculations. (Only in the protected override voide OnBarUpdate() section!)

                      Jens
                      Last edited by xenayoo; 07-14-2011, 10:46 AM. Reason: correcting an error in my code...

                      Comment


                        #12
                        Originally posted by xenayoo View Post
                        Ok, well, I had made some littel errors. But if you apply this TSI, you may got errors: "Error on setting indicator plot for indicator 'TSIhistogramm'. Value outside of valid range."

                        For example ES 1 min. Reason: If Close[0] - Close[1] = 0 for the first Bar of the Chart, a divide by zero error occurs in the calculation of the TSI.

                        solution:
                        You have to check, if the difference of Close[0] and Close[1] is not zero:

                        Code:
                        TSI.Set((EMA(EMA(_ds1DayMomoAbs, LongLen), ShortLen)[0]) != 0 ? (EMA(EMA(_ds1DayMomo, LongLen), ShortLen)[0]) / (EMA(EMA(_ds1DayMomoAbs, LongLen), ShortLen)[0]) *100 : 0);
                        An additional Error I found was:
                        using longLen instead of LongLen. This is important! If you use longLen, your calculation only use the default values regardless what you select for input! So Change longLen to LongLen, shortLen to ShortLen and signalLen to SignalLen in your calculations. (Only in the protected override voide OnBarUpdate() section!)

                        Jens
                        As far as the division by zero error, you are quite right, but that is because I really just made a quick and dirty rewrite to illustrate the errors, without putting in any real error checking code of my own in there. Mea culpa.

                        About your inputs situation, I guess I will have to take another look, but I kind of doubt that you are correct. The private representation of the variable gets and sets its value from the public representation, so they have the same value; the difference is in their scope. Have you done a "Print()" to demonstrate that the values are different. I admit that I have not yet done so. Maybe I might tackle it after the market closes. It will be instructive for all of us. (The funny thing is that when I write from scratch, I always use the public variable in my calculations anyway).

                        Comment


                          #13
                          I just checked the validity of your statement about the inputs. It would seem that it is not correct. The picture below shows the indicator, without your modification, loaded twice, using different inputs.

                          It clearly shows that the graphs are different, and reflect the different inputs: the faster period inputs are more responsive than the slower ones.
                          Attached Files

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by rocketman7, Today, 02:12 AM
                          5 responses
                          23 views
                          0 likes
                          Last Post rocketman7  
                          Started by trilliantrader, 04-18-2024, 08:16 AM
                          7 responses
                          28 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Started by samish18, 04-17-2024, 08:57 AM
                          17 responses
                          66 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Started by briansaul, Today, 05:31 AM
                          1 response
                          15 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by PaulMohn, Today, 03:49 AM
                          1 response
                          12 views
                          0 likes
                          Last Post NinjaTrader_BrandonH  
                          Working...
                          X