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

Help with Multiple DataSeries / Daily simple moving average : Not rendering

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

    Help with Multiple DataSeries / Daily simple moving average : Not rendering

    I am writing a daily simple moving average; an indicator which intended to show the daily SMA on an intraday chart. I have tried multiple variations to get this to work and need help.

    In all options :
    [HTML
    ]In `OnStateChange()`
    if (State == State.Configure)
    {
    AddDataSeries(BarsPeriodType.Day, 1);
    }​
    [/HTML]​

    ## Option 1: Use an SMA internally to handle drawing :

    [HTML
    ]In `OnStateChange()`
    if (State == State.DataLoaded)
    {
    var sma = SMA(BarsArray[1], Period);
    }​
    [/HTML]

    Result : No error in console, nothing renders.​

    ## Option 2: Copy the `OnBarUpdate` code to the new indicator:


    HTML Code:
            void OnBarUpdate()
            {
                if (BarsInProgress != 1)
                    return;
    
                if (BarsArray[1].BarsType.IsRemoveLastBarSupported)
                {
                    if (CurrentBars[1] == 0)
                    {
    
                        Value[0] = Closes[1][0];
                    }
                    else
                    {
                        double last = Value[1] * Math.Min(CurrentBars[1], Period);
    
                        if (CurrentBars[1] >= Period)
                        {
                            Value[0] = (last + Closes[1][0] - Closes[1][Period]) / Math.Min(CurrentBars[1], Period);
                        }
                        else
                        {
                            Value[0] = ((last + Closes[1][0]) / (Math.Min(CurrentBars[1], Period) + 1));
                        }
                    }
                }
                else
                {
                    if (IsFirstTickOfBar)
                    {
                        priorSum = sum;
                    }
                    sum = priorSum + Closes[1][0] - (CurrentBars[1] >= Period ? Closes[1][Period] : 0);
                    Value[0] = sum / (CurrentBars[1] < Period ? CurrentBars[1] + 1 : Period);
                }
                Print(Value[0]);​
            }​
    Result : No error in console, nothing renders.​​
    4454
    4437
    4419.5
    4410.3125
    4399.9375
    4394.75​​
    I have verified my indicator settings look right.
    - Auto Scale,
    - Panel same as input
    - Visible
    - Plots are thick and red

    I have also tried adding

    ```
    Values[1][0] = Value[0];
    ```



    HTML Code:
    /// <summary>
        /// The SMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
        /// </summary>
        public class DailySMA : Indicator
        {
            private double priorSum;
            private double sum;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionSMA;
                    Name                        = "Daily SMA";
                    IsOverlay                    = true;
                    IsSuspendedWhileInactive    = true;
                    Period                        = 5;
                    AddPlot(Brushes.Red, "Daily SMA");
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Day, 1);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 1)
                    return;
    
                if (BarsArray[1].BarsType.IsRemoveLastBarSupported)
                {
                    if (CurrentBars[1] == 0)
                    {
    
                        Values[1][0] = Closes[1][0];
                    }
                    else
                    {
                        double last = Values[1][1] * Math.Min(CurrentBars[1], Period);
    
                        if (CurrentBars[1] >= Period)
                        {
                            Value[0] = (last + Closes[1][0] - Closes[1][Period]) / Math.Min(CurrentBars[1], Period);
                        }
                        else
                        {
                            Value[0] = ((last + Closes[1][0]) / (Math.Min(CurrentBars[1], Period) + 1));
                        }
                    }
                }
                else
                {
                    if (IsFirstTickOfBar)
                    {
                        priorSum = sum;
                    }
                    sum = priorSum + Closes[1][0] - (CurrentBars[1] >= Period ? Closes[1][Period] : 0);
                    Value[0] = sum / (CurrentBars[1] < Period ? CurrentBars[1] + 1 : Period);
                }
                Print(Value[0]);
                Values[1][0] = Value[0];
            }
    
            #region Properties
    
            [Range(1, int.MaxValue), NinjaScriptProperty]
            [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
            public int Period
            { get; set; }
            #endregion
        }​

    #2
    Hello nicthe,

    Thanks for your post.

    You are currently using Values[1][0] to assign values to the plot in your script but you only have a single plot in the script. Values[1][0] would refer to a second plot that is added in the script and you do not have a second plot.

    Values[0][0] should be used to assign a value to the plot in your script since you only have one plot, not two plots.

    See this help guide page for more information about AddPlot() and sample code: https://ninjatrader.com/support/help...t8/addplot.htm

    Further, you should make sure that you have enough bars processed for the data series you are accessing. A CurrentBars check should be added to the script to ensure you have enough bars for the data series being accessed.

    See this help guide page for more information about making sure you have enough bars: https://ninjatrader.com/support/help...nough_bars.htm

    Anytime you do not see the script behaving as expected, you should note the Log tab of the Control Center for any error messages that are occurring. And, debugging prints should be added to the script to see how the logic in your script is behaving.

    Something you could consider is creating a new custom NinjaScript indicator, creating class-level SMA variable, adding a plot to the script, adding a daily data series to the script, instantiating the SMA variable based on the added daily series, add a CurrentBars condition in your script to make sure you have enough bars, and assign the SMA variable to the plot.

    I have added a sample script to this post so you could view the concept of the steps mentioned above.​
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you @NinjaTrader_BrandonH

      That worked!

      To any one else reading along, I did need to need to make a change on line 56.

      ```
      sma = SMA(BarsArray[1], Period); // was hard coded to `5`.
      ```

      Comment


        #4
        I was very interested by this code as I want to code a DailySMA indicator to my intraday chart but I'm not sure the code DailySMAExample is working properly but I may be wrong, as I dont have much experience in coding Indicator. I'm better at Strategies but I usually understand pretty well the coding process.

        I'll explain my problem:

        After analysing the code and doing some tests, I found that the the DailySMA code works fine with 1-2-3 period but it doesnt seem to go further the DailySMA(3) (So, 4 and above).

        In the code, I tried to change (in the region properties) the range and replace this line [Range(1, int.MaxValue)] to [Range(1, 200)] but nothing changed. I try to look elsewhere but I havent found something relevant to my eyes and understanding, so, I'm verifying if I'm the only one having that problem. I did the modification proposed by nicthe, to get an adjustable period and it is working fine, as I said, for the 1-2-3 period.

        I'm planning to build this same indicator but with 4 periods adjustment (20-50-100-200) but I need to fix my problem prior to this. I should post the complete code here at the end so it could become available for those in need of this and I also suggest a new DailySMA Indicator that could be part of the basic indicator of Ninjatrader.

        But for now, can I get help for my problem pls.

        Thank you.

        Comment


          #5
          Hello Brocolico,

          Thanks for your notes.

          You would need to make sure that you have at least the same number of Days To Load in the Data Series that you are adding the indicator to as the Period property of the indicator for the plot to appear on the chart correctly.

          For example, if the Period is set to say 5, you would need to make sure that you have at least 5 days of data loaded on the chart so the indicator can calculate the 5 days of data.

          See the 'How to edit Data Series parameters' section of this help guide page for more information about editing a Data Series properties, such as the 'Days To Load' property: https://ninjatrader.com/support/help...price_data.htm
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            OK nice. This is my problem. I'm working with a 1 min timeframe and only have a little more than 3 days of data loaded. because I separated NT8 into 4 screens, I have all the important timeframe in front of me but the daily data is on the daily chart that is another opened window.

            On my Thinkorswim account (for trading stock), this indicator is working properly on my 1m timeframe chart but I suppose this is related to the internal configuration of the plateform. Here, on NT8, I cant load 200 days of 1 minute candlebars. I thought that the the AddDataSeries(Data.BarsPeriodType.Day, 1); instruction could have loaded up maybe 6 month or a year of daya but I understand that it depends of the period requested by the user.

            Ok so, I'll live without it for now, hoping that someone could come up with a good ide, some days.

            Thanks for the help. It didnt resolve my problem but you found what was my problem, at least.

            Thank you again!

            Comment


              #7
              I get back on this problem of daily SMAs available on intraday charts.

              As suggested, the problem could be resolved by downloading a large amount of data on the intraday chart so we can build the indicator properly. By doing so, unfortunately, we sacrifice the performance to get those daily SMAs.Only to get few days of 200 daily SMA, it required to load up to a year of intraday data, so, it's too large of data to me.

              If I may suggest, as I'm working with another platform that deal with this problem very well, would be this solution (that need implementation by NinjaTrader coders):

              When working with multiple charts, the platform would assign a different chart number to each opened charts (like it is done when working with multiple indicator panels in a single chart). A new indicator that could be called DailySMA could be created which could refers (coding reference, I mean) to the daily chart number and then, the indicator would only have only to report the Daily SMA value of the daily chart, without having to download an entire year or two of intraday data that would affects the performance of the system.

              I also suggest to create a checkbox on the "Prior day OHLC" that would ask if the user want the OHLC for the day session only (9h30 to 16h00), instead of the whole day. I think it would be very welcome for a lot of user, possibly.

              Just some simple improvements to an already nice existing platform.

              Thank you.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,788 views
              0 likes
              Last Post aligator  
              Started by Jimmyk, 01-26-2018, 05:19 AM
              6 responses
              837 views
              0 likes
              Last Post emuns
              by emuns
               
              Started by jxs_xrj, 01-12-2020, 09:49 AM
              6 responses
              3,292 views
              1 like
              Last Post jgualdronc  
              Started by Touch-Ups, Today, 10:36 AM
              0 responses
              12 views
              0 likes
              Last Post Touch-Ups  
              Started by geddyisodin, 04-25-2024, 05:20 AM
              11 responses
              62 views
              0 likes
              Last Post halgo_boulder  
              Working...
              X