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

Convert EMA to DataSeries

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

    Convert EMA to DataSeries

    I feel like this should be easier than it is.

    I want to have a single DataSeries variable, then set it to one of two MA types. Something like below:

    PHP Code:

    DataSeries myMA 
    = new DataSeries(this);
    // Or, I've tried:  var myMA = default(DataSeries);

    if (blah)
       
    myMA EMA(Close,21);
    else
       
    myMA SMA(Close,21); 
    I've tried a couple different approaches that didn't seem to work

    PHP Code:

    myMA 
    = new DataSeries(EMA(Close,21));  
    // myMA[0] always returns "0"

    or

    myMA = (DataSeries)EMA(Close,21);
    // ERROR: Can't convert EMA to DataSeries 
    How can I accomplish what I want without having a myEMA and mySMA variables?

    Any help is greatly appreciated!
    Daniel

    #2
    Hello neoikon,

    Thanks for writing in.

    It is not possible to copy a data series from an indicator in this way.

    You will need to use an if statement and reference the bar directly.

    (Or set the value to a variable)

    You can also set your dataseries.

    For example:

    if (//conditions)
    {
    myMA.Set(EMA(Close, 21)[0]);
    }
    else
    {
    myMA.Set(SMA(Close, 21)[0]);
    }


    Let me know if this does not resolve your inquiry.
    Last edited by NinjaTrader_ChelseaB; 08-27-2013, 12:58 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. That's the path that I am resorting to, unfortunately. My list is longer than just those "2 MAs", so it really makes the code more complex than I'd want, but oh well.

      Thanks!
      Daniel

      Comment


        #4
        Try

        Code:
        #region Global Enums
        
        public enum myMAType( SMA, EMA, TMA, WMA} // add as many averages as needed
        
        #endregion
        
        namespace NinjaTrader.Indicator
        {
              public class myIndicator : Indicator
              {  
                    # region Variables:
                           period = 14;
                           myMAType thisMAType = myMAType.SMA;
                           private IDataSeries average;
                    # endregion
        
                    protected override void Initialize()
                    {
                          // nothing special here
                    }
        
                    protected override void OnStartUp()
                    {
                           .....
                          switch(thisMAType)
                          {
                                case myMAType.SMA: 
                                       average = SMA(Input, period);
                                       break;
                                case myMAType.EMA: 
                                       average = EMA(Input, period);
                                       break;
                                // include one average for each value of enum                 
                           }
                      }
        
                     protected override void OnBarUpdate()
                     {
                           ......
                           Value.Set(average[0]);
                           ......
                     }
               }
        }
        You can also add another DataSeries and set it to the same value as average:

        Code:
         myMA.Set(average[0]);

        Comment


          #5
          Thanks Harry, that's along the route I am going.

          Comment


            #6
            Harry,

            Thanks for your input here. I wanted to confirm this does work.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Looks like I didn't look close enough at Harry's post! This is what I was originally needing:

              private IDataSeries average;

              Oh well, I've already gone down another path. Good to know in the future, though.

              Daniel

              Comment


                #8
                Originally posted by neoikon View Post
                Looks like I didn't look close enough at Harry's post! This is what I was originally needing:

                private IDataSeries average;

                Oh well, I've already gone down another path. Good to know in the future, though.

                Daniel
                The advantage of the approach is that you do the selection in OnStartUp(), which is only executed once, when the chart is set up. Therefore the execution of the code will be fast, as the IDataSeries "average" points to the moving average selected.

                I understand that it is good practice to put as much of the code as possible into OnStartUp(). This reduces the repetitive calculations in OnBarUpdate(), which is either executed with each bar or each incoming tick on real-time data. Also it avoids to put too much code into the Initialize() section, which is compiled, regardless whether an indicator is selected for a chart or not.

                What alternative approach did you follow?

                Comment


                  #9
                  Originally posted by Harry View Post
                  What alternative approach did you follow?
                  Ah, you make a good point about performance. Looks like I'll be restructuring this thing for the 3rd time today! ;]

                  I'm going with what you outlined before, setting the IDataSeries to the MA type in OnStartUp(), then setting the plot itself in the OnBarUpdate(), etc.

                  Daniel

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kevinenergy, Yesterday, 12:01 PM
                  6 responses
                  22 views
                  0 likes
                  Last Post kevinenergy  
                  Started by DawnTreader, 05-08-2024, 05:58 PM
                  15 responses
                  48 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by ZeroKuhl, Yesterday, 04:31 PM
                  7 responses
                  40 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by xepher101, Today, 12:19 PM
                  1 response
                  22 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by jpeep, 08-16-2020, 08:31 AM
                  16 responses
                  498 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X