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:

    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);

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        599 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        344 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        558 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        557 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X