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

How to add Moving Average as Dataseries?

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

    How to add Moving Average as Dataseries?

    I a building multi-dropdown chooseable (enum) MA indicators. code looks like

    Code:
    enum MaTypes{
    	DEMA,
    	EMA,
    	HMA 
    	.....
    }
    
    private DataSeries ma_value;
    
    Initialize () {
    	ma_value =  
    		( maType == MovAvgType.DEMA	? DEMA	(Close,ma_Length) :
    		( maType == MovAvgType.EMA	? EMA	(Close,ma_Length) :
    		( maType == MovAvgType.HMA	? HMA	(Close,ma_Length) :
    		 ....  
    		 
    			
    	Add(ma_value);
    	ma_value.Plots[0].Pen.Color = ma_1_Color;
    }
    
    OnBarUpdate(){
    ......
    	double xyz= ma_value[0];
    ......
    }
    however, something here seems incorrect, because as i know, ma_value should be set not to DataSeries, but the exact type i.e. SMA ma_value, EMA ma_value or etc..

    however, setting up all those private variables individually kills me, because in strategy i am making multiple dropdowns for 4 choosable MA-s, and I want the code to be compact like above...

    I have found this topic - http://ninjatrader.com/support/forum...ead.php?t=3420, but as i say, i dont want that way:

    Code:
    private DEMA dema_val;
    private EMA ema_val;
    .............
    
    Initialize () {
    ...
    	if( maType == MovAvgType.DEMA	)
    		dema_val	= DEMA(Close,ma_Length);
    	}
    	else if( maType == MovAvgType.DEMA	)
    		ema_val		= EMA(Close,ma_Length);
    	}
    ...
    }
    
    
    
    OnBarUpdate(){
    ......
    	if( maType == MovAvgType.DEMA	)
    		double xyz = dema_val[0];
    	}
    	else if( maType == MovAvgType.DEMA	)
    		double xyz = ema_val[0];
    	}
    ......
    }
    this will be very annoying... I wish i could achieve like i've written in the top .. What C# thing i have to do , i.e. https://stackoverflow.com/questions/...ord-in-the-clr ?
    Last edited by ttodua; 09-10-2017, 03:37 PM.

    #2
    Hello selnomeria,

    An example of this can be found in the reference samples.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by selnomeria View Post
      I a building multi-dropdown chooseable (enum) MA indicators. code looks like

      Code:
      enum MaTypes{
          DEMA,
          EMA,
          HMA 
          .....
      }
      
      private DataSeries ma_value;
      
      Initialize () {
          ma_value =  
              ( maType == MovAvgType.DEMA    ? DEMA    (Close,ma_Length) :
              ( maType == MovAvgType.EMA    ? EMA    (Close,ma_Length) :
              ( maType == MovAvgType.HMA    ? HMA    (Close,ma_Length) :
               ....  
               
                  
          Add(ma_value);
          ma_value.Plots[0].Pen.Color = ma_1_Color;
      }
      
      OnBarUpdate(){
      ......
          double xyz= ma_value[0];
      ......
      }
      however, something here seems incorrect, because as i know, ma_value should be set not to DataSeries, but the exact type i.e. SMA ma_value, EMA ma_value or etc..

      however, setting up all those private variables individually kills me, because in strategy i am making multiple dropdowns for 4 choosable MA-s, and I want the code to be compact like above...

      I have found this topic - http://ninjatrader.com/support/forum...ead.php?t=3420, but as i say, i dont want that way:

      Code:
      private DEMA dema_val;
      private EMA ema_val;
      .............
      
      Initialize () {
      ...
          if( maType == MovAvgType.DEMA    )
              dema_val    = DEMA(Close,ma_Length);
          }
          else if( maType == MovAvgType.DEMA    )
              ema_val        = EMA(Close,ma_Length);
          }
      ...
      }
      
      
      
      OnBarUpdate(){
      ......
          if( maType == MovAvgType.DEMA    )
              double xyz = dema_val[0];
          }
          else if( maType == MovAvgType.DEMA    )
              double xyz = ema_val[0];
          }
      ......
      }
      this will be very annoying... I wish i could achieve like i've written in the top .. What C# thing i have to do , i.e. https://stackoverflow.com/questions/...ord-in-the-clr ?
      This might help: http://ninjatrader.com/support/forum...74&postcount=1

      Comment


        #4
        so, SMA type can hold other as reference, without throwing type mismatch errors, right? Wow, that's what i wanted!


        Do you know what I am interested in?
        How support team, in many cases, is unable to offer solution, while users like konagam, know solutions...

        Excellent! and the Enums are declared nicely too!
        Last edited by ttodua; 09-11-2017, 02:06 PM.

        Comment


          #5
          Originally posted by selnomeria View Post
          so, SMA type can hold other as reference, without throwing type mismatch errors, right? Wow, that's what i wanted!
          Well, more like I am using the parent class, Indicator, to hold the derived types.

          Do you know what I am interested in?
          How support team, in many cases, is unable to offer solution, while users like konagam, know solutions...
          The critique is only somewhat valid, because we also have to remember that NT Support are employees, and have to act within the support parameters that the company sets. We, on the other hand are geeks, who can go outside those boundaries, to help other geeks.

          Excellent! and the Enums are declared nicely too!
          Well, I really hate pollution of the Global namespace. Many years ago, when I was a Product Engineer for a huuuuuge computer company, I routinely rejected any code that polluted the Global namespace, unless it could really be justified, in which case the declaration was moved into the Global namespace definition file, not a class file.
          Last edited by koganam; 09-11-2017, 05:07 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Barry Milan, Today, 10:35 PM
          0 responses
          1 view
          0 likes
          Last Post Barry Milan  
          Started by DJ888, Yesterday, 06:09 PM
          2 responses
          9 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          40 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Today, 08:51 AM
          2 responses
          16 views
          0 likes
          Last Post bill2023  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          167 responses
          2,260 views
          0 likes
          Last Post jeronymite  
          Working...
          X