• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Lists or Arrays to Create my own SMA

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

    Using Lists or Arrays to Create my own SMA

    Okay, so this is probably a really dumb question that belongs in a c# forum, but I'm hoping I can get an answer here.

    I've been lazily assigning variables to past values of NT indicators as a way of easily calculating 3 value moving averages to whatever set of double or int numbers I'm working with. This method is also used for custom values that are combinations of indicator values to capture signals from multiple indicators.

    My question is: can I bypass this process of declaring/assigning values in my custom, non-managed strategy and use arrays and/or lists to store previous values and calculate 3,5 or even 10 value averages of assigned values?

    I know I'll probably need to post some code, but I'm away from my Dev laptop and figured
    I'd start the question via mobile phone to see if anyone had a quick answer.

    Thanks in advance for any replies!

    #2
    Hello Spiderbird,

    Thank you for your post.

    Do you need to store these values on the bar as an indicator would? If so you can use DataSeries and IntSeries synced to the bars object.

    For information please visit the following links:
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      Here's the related code...

      Hi Patrick,

      That's what I'm not sure about. I'm working with double values primarily. For example, here's how they're declared and how they're defined/calculated:

      Code:
      public class ExampleCode : Strategy     
      {
      
      double
      ema_5,
      ema_5_Prev,
      ema_5_Prev3,
      ema_5_Prev4,
      ema_5_Prev5;
      
      protected override void OnBarUpdate()
      {
      
      (...)
      
      //  All EMA settings for different time periods are set here
      	ema_5 = Math.Round(EMA(5)[1],2);
      	ema_5_Prev = Math.Round(EMA(5)[2],2);
      	ema_5_Prev3 = Math.Round(EMA(5)[3],2);
      	ema_5_Prev4 = Math.Round(EMA(5)[4],2);
      	ema_5_Prev5 = Math.Round(EMA(5)[5],2);
      
      //  Calculate the difference between the five ema figures
      ema_BarTrend = Math.Round((ema_5 + ema_5_Prev + ema_5_Prev3 + ema_5_Prev4 + ema_5_Prev5) / 5,2);	
      
      (...)
      
      }
      }
      I have about 9-11 other instances where this occurs. I define variables for previous values of an indicator and create my own calculations for use in charts/trading.

      How would DataSeries or IntSeries work in this scenario to generate more elegant/efficient code?

      Comment


        #4
        Hello Spiderbird,

        Thank you for your response.

        Below is an example:
        Code:
                #region Variables
                private DataSeries emaSeries;
        		private double ema_BarTrend = 0;
                #endregion
        		
                protected override void Initialize()
                {
        			emaSeries = new DataSeries(this);
                }
        		
                protected override void OnBarUpdate()
                {	
        			if(CurrentBar <= 5)
        				return;
        			
        			emaSeries[0] = Math.Round(EMA(5)[0],2);
        			
        			ema_BarTrend = Math.Round((emaSeries[0] + emaSeries[1] + emaSeries[2] + emaSeries[3] + emaSeries[4])/5, 2);
        Patrick H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by akuntysh, Today, 02:43 AM
        0 responses
        4 views
        0 likes
        Last Post akuntysh  
        Started by thgamey, Today, 02:05 AM
        0 responses
        6 views
        0 likes
        Last Post thgamey
        by thgamey
         
        Started by tpdk2012, Today, 12:51 AM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_ChrisSch  
        Started by simonerg, Today, 12:13 AM
        0 responses
        7 views
        0 likes
        Last Post simonerg  
        Started by rickyblah12, 07-20-2019, 01:01 PM
        2 responses
        24 views
        0 likes
        Last Post NinjaTrader_ChrisSch  
        Working...
        X