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

Same average different results?

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

    Same average different results?

    I've found a bit of a puzzling problem, hopefully someone can steer me in the right direction on how to fix this or why it happens.
    I'm adding a secondary dataseries to plot an SMA of 1440 minutes on a 5 minutes chart. I know that this is possible to do manually by loading a secondary chart in the background but I need to do it with an indicator for reasons that don't matter now.
    The problem is that if I then create a chart that is 1440 minutes and load the normal SMA indicator in that chart de average on the 5 minute chart that corresponds to the 1440 minute dataserias is diferent than the one on the 1440 minute chart.
    I'll simplify the code here as an example.

    Code:
    if (State == State.SetDefaults)
    {
        AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Dot, anchoDefecto), PlotStyle.Line, "SMA200_1440");
    }
    else if (State == State.Configure)
    {
        int daysLoaded = Bars.ToDate.Subtract(Bars.FromDate).Days;
    
        AddDataSeries(null, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1440 }, daysLoaded, null, null);            
    }
    
    protected override void OnBarUpdate()
    {
        if(CurrentBars[0] >= 0 && CurrentBars[1] >= 0){
            SMA200_1440[0] = SMA(Closes[1], 200)[0];
        }            
    }
    This indicator is loaded on a 5 minute chart.
    Then I load an SMA with period 200 on a 1440 minute chart.
    I'd expect both SMA's to show the same but they don't, there is allways a small diference. Any ideas why this happens?

    I should add that this happens in some markets more than others, as an example I'm using CL 9-19 (Crude Oil Futures)
    Last edited by Catalon; 07-17-2019, 10:09 AM.

    #2
    Hello Catalon,

    Thanks for your post.

    The logic is looking sound, however you may want to make sure that enough data is requested with the Primary Data Series so there is enough data to create "normalized" plots that are built off of as many data points as needed from your secondary series.

    I have attached some code and a demonstration that you can test on your end.

    Demo - https://drive.google.com/file/d/15nn...w?usp=drivesdk

    Code:
        public class SMA1440 : Indicator
        {
            private double value;
            private SMA SMA1;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "SMA1440";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = false;
                    AddPlot(Brushes.White, "SMA1440");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(Data.BarsPeriodType.Minute, 1440);
                }
                else if (State == State.DataLoaded)
                {
                    SMA1 = SMA(BarsArray[1], 14);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if(BarsInProgress == 0)
                    Value[0] = value;
                else
                    value = SMA1[0];    
            }
        }
    Please let me know if you are seeing anything different when setting up the same test or if you have any additional questions.

    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    59 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    21 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    10 views
    0 likes
    Last Post cre8able  
    Working...
    X