• 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

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 Newtrader101, Today, 03:54 PM
    1 response
    15 views
    0 likes
    Last Post NinjaTrader_ShawnB  
    Started by williamzz, Today, 03:28 PM
    0 responses
    7 views
    0 likes
    Last Post williamzz  
    Started by Robert87, Today, 11:07 AM
    0 responses
    14 views
    0 likes
    Last Post Robert87  
    Started by phta123, Today, 10:24 AM
    0 responses
    17 views
    0 likes
    Last Post phta123
    by phta123
     
    Started by sidviciousX, Today, 06:47 AM
    0 responses
    16 views
    0 likes
    Last Post sidviciousX  
    Working...
    X