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

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)[B][0][/B]);
    }
    }
    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 BarzTrading, Today, 07:25 AM
          2 responses
          15 views
          1 like
          Last Post BarzTrading  
          Started by devatechnologies, 04-14-2024, 02:58 PM
          3 responses
          19 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by tkaboris, Today, 08:01 AM
          0 responses
          3 views
          0 likes
          Last Post tkaboris  
          Started by EB Worx, 04-04-2023, 02:34 AM
          7 responses
          162 views
          0 likes
          Last Post VFI26
          by VFI26
           
          Started by Mizzouman1, Today, 07:35 AM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X