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

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 GLFX005, Today, 03:23 AM
        0 responses
        1 view
        0 likes
        Last Post GLFX005
        by GLFX005
         
        Started by XXtrader, Yesterday, 11:30 PM
        2 responses
        11 views
        0 likes
        Last Post XXtrader  
        Started by Waxavi, Today, 02:10 AM
        0 responses
        6 views
        0 likes
        Last Post Waxavi
        by Waxavi
         
        Started by TradeForge, Today, 02:09 AM
        0 responses
        12 views
        0 likes
        Last Post TradeForge  
        Started by Waxavi, Today, 02:00 AM
        0 responses
        2 views
        0 likes
        Last Post Waxavi
        by Waxavi
         
        Working...
        X