• 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

SMA question

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

    SMA question

    Hi. I'm having a problem which suggests I don't know how SMA is calculated. I have following lines of code:

    Print("BetaSMASeries[0]= "+BetaSMASeries[0]);
    Print("BetaSMASeries[1]= "+BetaSMASeries[1]);
    Print("BetaSMASeries[2]]= "+BetaSMASeries[2]);
    beta = SMA(BetaSMASeries,1);

    So here's the problem I'm having. The Print statements all show valid values for the BetaSMASeries. However, when I try to calculate the SMA(BetaSMASeries), it gives me the NaN, even if it's just the 1 period SMA which I assume would just be equal to BetaSMASeries[1] (or maybe BetaSMASeries[0]) which the Print statements have shown me to be populated. Any quick suggestions what might be going on here or is it too complicated without seeing the rest of the code?

    #2
    Hello stewarco,

    Thanks for your post.

    I do notice that you are assigning the SMA, and not the SMA's most recent value. The SMA is "Not a Number" when the most recent value would be.

    When I test the following code, I see the behavior I would expect:
    Code:
    public class MyCustomIndicator1 : Indicator
    {
    private Series<double> mySeries;
    protected override void OnStateChange()
    {
    	if (State == State.SetDefaults)
    	{
    		Name										= "MyCustomIndicator1";
    	}
    	else if (State == State.DataLoaded)
    	{
    		mySeries = new Series<double>(this, MaximumBarsLookBack.Infinite);
    	}
    }
    
    protected override void OnBarUpdate()
    {
    	mySeries[0] = Close[0];
    	
    	if(CurrentBar < 1) return;
    	
    	Print(SMA(mySeries, 1)[0]);
    }
    }
    Some further examples for using system indicators can be found publicly in our help guide. I'll provide a link below.

    Valid Input for System Indicator methods - https://ninjatrader.com/support/help..._indicator.htm

    I may also suggest using the Strategy Builder to create logic using these items and to use View Code to see the resulting NinjaScript syntax.

    Strategy Builder 301 - https://www.youtube.com/watch?v=HCyt90GAs9k

    Please let us know if we can be of further help.
    JimNinjaTrader Customer Service

    Comment


      #3
      still having problems

      Thanks for feedback. I reviewed the materials and made a few changes. Here's what the snippet looks like now:

      Print("BetaSMASeries[0]= "+BetaSMASeries[0]);
      Print("BetaSMASeries[1]= "+BetaSMASeries[1]);
      Print("BetaSMASeries[2]]= "+BetaSMASeries[2]);
      Print(SMA(BetaSMASeries,1)[0]);

      And here's what the output looks like:

      BetaSMASeries[0]= -0.0276422268647064
      BetaSMASeries[1]= -0.0127809424974953
      BetaSMASeries[2]]= -0.0149870144078708
      NaN

      So I think I have the right syntax for the SMA, and I know that my series has values for at least the current bar and the prior two, but the SMA is still giving me NaN. Is it possible that earlier values of the BetaSMASeries would impact the SMA calculation, even if we're asking for an SMA that theoretically only needs the prior value (which we know has a legitimate value)?

      Comment


        #4
        Hello stewarco,

        If we look at the SMA's source code, we can see that it uses a "priorSum" variable in its calculation for bars that are not "IsRemoveLastBarSupported" (Renko Bars specifically.) So the indicator will use some previous value in its calculation.

        To test further, you could set up a script that only uses CurrentBar 0, 1 and 2 so then you only give the indicator those values for the calculation without caching any previous values.

        For example:
        Code:
        protected override void OnBarUpdate()
        {
        	if(CurrentBar == 2)
        	{
        		BetaSMASeries[0] = -0.0276422268647064;
        		Print("BetaSMASeries[0]= "+BetaSMASeries[0]);
        		Print("BetaSMASeries[1]= "+BetaSMASeries[1]);
        		Print("BetaSMASeries[2]]= "+BetaSMASeries[2]);
        		Print(SMA(BetaSMASeries,1)[0]);
        	}
        	else if (CurrentBar == 1)
        	{
        		BetaSMASeries[0] = -0.0127809424974953;	
        	}
        	else if (CurrentBar == 0)
        	{
        		BetaSMASeries[0] = -0.0149870144078708;
        	}
        }
        Based off of this, we should expect there to be only the valid values that should not effect the indicators calculation. This would confirm if the caching is effecting the scripts behavior.

        After taking this test and you find this to be the case, could you provide a barebones export of the code (excluding all other code that is not relevant to this case) and some steps I could take to observe the behavior on my end? There are some ways that we can calculate the indicator without the caching behavior, but I would need to see the bigger picture for how your Series object is being used throughout your script before giving further input.

        Exporting - https://ninjatrader.com/support/help...tAsSourceFiles

        I look forward to being of further assistance.
        JimNinjaTrader Customer Service

        Comment


          #5
          hey thanks been away for a while just saw this will review and revert

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Udimuz, Today, 06:24 AM
          1 response
          6 views
          0 likes
          Last Post NinjaTrader_ChrisL  
          Started by derivtrader, Today, 07:19 AM
          0 responses
          5 views
          0 likes
          Last Post derivtrader  
          Started by MarthaClines, Yesterday, 06:20 PM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_Jim  
          Started by danitralla, Today, 01:23 AM
          4 responses
          44 views
          0 likes
          Last Post NinjaTrader_DrewO  
          Started by Longhornmark, Today, 06:52 AM
          0 responses
          3 views
          0 likes
          Last Post Longhornmark  
          Working...
          X