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

Mode from a Series double

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

    #46
    Or one CurrentBar Check with BarsBack * 2 ?

    with
    PHP Code:
        protected override void OnBarUpdate()
        {  

          if(
    CurrentBar BarsBack 2)
          {
            return;
          } 
    in
    PHP Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;

    using System.Text.RegularExpressions;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
      public class 
    Testa1 Indicator
      
    {

        private 
    Series<doublevarO;
        private 
    Series<doublevarH;
        private 
    Series<doublevarL;
        private 
    Series<doublevarC;


        private 
    Series<doubleRng0;
        private 
    Series<doubleRng1;

        private 
    Series<doubleRng0Rng1;

        private List<
    doublelistOfSeriesValues;
        private List<
    doublelistOfSeriesValuesBarsBack;



        protected 
    override void OnStateChange()
        {
          if (
    State == State.SetDefaults)
          {
            
    Description                   = @"Enter the description for your new custom Indicator here.";
            
    Name                     "Testa1";
            
    Calculate                   Calculate.OnBarClose;
            
    IsOverlay                   true;
            
    DisplayInDataBox               true;
            
    DrawOnPricePanel               true;
            
    DrawHorizontalGridLines             true;
            
    DrawVerticalGridLines             true;
            
    PaintPriceMarkers               true;
            
    ScaleJustification               NinjaTrader.Gui.Chart.ScaleJustification.Right;
            
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
            //See Help Guide for additional information.
            
    IsSuspendedWhileInactive           true;
            
    BarsBack                 20;
            
    Rng0Index               0;
            
    Rng1Index             0;

          }
          else if (
    State == State.Configure)
          {
          }
          else if (
    State == State.DataLoaded)
          {
            
    varO = new Series<double>(thisMaximumBarsLookBack.Infinite);
            
    varH = new Series<double>(thisMaximumBarsLookBack.Infinite);
            
    varL = new Series<double>(thisMaximumBarsLookBack.Infinite);
            
    varC = new Series<double>(thisMaximumBarsLookBack.Infinite);

            
    Rng0Rng1 = new Series<double>(thisMaximumBarsLookBack.Infinite);

            
    Rng0 = new Series<double>(thisMaximumBarsLookBack.Infinite);
            
    Rng1 = new Series<double>(thisMaximumBarsLookBack.Infinite);

            
    listOfSeriesValues = new List<double>();
            
    listOfSeriesValuesBarsBack = new List<double>();
          }
        }




        protected 
    override void OnBarUpdate()
        {  

          if(
    CurrentBar BarsBack 2)
          {
            return;
          }



      
    varO[0] = Open[0];
          
    varH[0] = High[0];
          
    varL[0] = Low[0];
          
    varC[0] = Close[0];    


          
    // the logic for Countif() etc.    


          
    Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];


          
    listOfSeriesValues.Add(Rng0Rng1[0]);
    //       listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack);

    //       Print(listOfSeriesValues.Count() - BarsBack);



          
    Print("CurrentBar : " CurrentBar);
          Print(
    string.Format("{0} | listOfSeriesValuesBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count() ): {4} "Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (listOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() ));

          
    listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);


        }

        
    #region Properties
        
    [Range(1int.MaxValue), NinjaScriptProperty]
        [
    Display(ResourceType typeof(Custom.Resource), Name "BarsBack"GroupName "Number of Bars for Stats"Order 0)]
        public 
    int BarsBack
        
    getset; }




        [
    Range(0int.MaxValue), NinjaScriptProperty]
        [
    Display(ResourceType typeof(Custom.Resource), Name "Rng0Index"GroupName "Range Bottom Index"Order 3)]
        public 
    int Rng0Index
        
    getset; }



        [
    Range(0int.MaxValue), NinjaScriptProperty]
        [
    Display(ResourceType typeof(Custom.Resource), Name "Rng1Index"GroupName "Range Bottom Index"Order 4)]
        public 
    int Rng1Index
        
    getset; }
        
    #endregion
      
    }
    }

    #region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
      public 
    partial class Indicator NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      
    {
        private 
    Testa1[] cacheTesta1;
        public 
    Testa1 Testa1(int barsBackint rng0Indexint rng1Index)
        {
          return 
    Testa1(InputbarsBackrng0Indexrng1Index);
        }

        public 
    Testa1 Testa1(ISeries<doubleinputint barsBackint rng0Indexint rng1Index)
        {
          if (
    cacheTesta1 != null)
            for (
    int idx 0idx cacheTesta1.Lengthidx++)
              if (
    cacheTesta1[idx] != null && cacheTesta1[idx].BarsBack == barsBack && cacheTesta1[idx].Rng0Index == rng0Index && cacheTesta1[idx].Rng1Index == rng1Index && cacheTesta1[idx].EqualsInput(input))
                return 
    cacheTesta1[idx];
          return 
    CacheIndicator<Testa1>(new Testa1(){ BarsBack barsBackRng0Index rng0IndexRng1Index rng1Index }, inputref cacheTesta1);
        }
      }
    }

    namespace 
    NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
      public 
    partial class MarketAnalyzerColumn MarketAnalyzerColumnBase
      
    {
        public 
    Indicators.Testa1 Testa1(int barsBackint rng0Indexint rng1Index)
        {
          return 
    indicator.Testa1(InputbarsBackrng0Indexrng1Index);
        }

        public 
    Indicators.Testa1 Testa1(ISeries<doubleinput int barsBackint rng0Indexint rng1Index)
        {
          return 
    indicator.Testa1(inputbarsBackrng0Indexrng1Index);
        }
      }
    }

    namespace 
    NinjaTrader.NinjaScript.Strategies
    {
      public 
    partial class Strategy NinjaTrader.Gui.NinjaScript.StrategyRenderBase
      
    {
        public 
    Indicators.Testa1 Testa1(int barsBackint rng0Indexint rng1Index)
        {
          return 
    indicator.Testa1(InputbarsBackrng0Indexrng1Index);
        }

        public 
    Indicators.Testa1 Testa1(ISeries<doubleinput int barsBackint rng0Indexint rng1Index)
        {
          return 
    indicator.Testa1(inputbarsBackrng0Indexrng1Index);
        }
      }
    }

    #endregion 

    Comment


      #47
      Hello PaulMohn,

      Yes. If you are wanting the list element added on every bar, it would have to added be before bar 20.

      I'm not sure how you are using Rng0Index.. I imagine if you are doing this assignment to Rng0Rng1 before adding the value to the list, then CurrentBar would need to be greater than Rng0Index.

      The CurrentBar check stops code below from evaluating if CurrentBar is not at that number.

      When do you want to start assigning values to the list?

      When do you want to check that list and ensure it has 20 element in it?
      Last edited by NinjaTrader_ChelseaB; 03-09-2022, 01:34 PM.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #48
        I just test with
        PHP Code:
            protected override void OnBarUpdate()
            {  

              if(
        CurrentBar BarsBack 2)
              {
                return;
              } 
        I get
        CurrentBar : 41
        04/03/2022 00:42:00 | listOfSeriesValuesBarsBack.Count(): 0 - BarsBack: 20 = -20 | listOfSeriesValues.Count() ): 1
        Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 41: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

        And I tested with the 2 CurrentBar Checks
        PHP Code:
            protected override void OnBarUpdate()
            {  
              if (
        CurrentBar 1)
                return;

              
        Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];


              
        listOfSeriesValues.Add(Rng0Rng1[0]);
        //       listOfSeriesValuesBarsBack = listOfSeriesVa lues.GetRange(0, BarsBack);

        //       Print(listOfSeriesValues.Count() - BarsBack);


              
        if(CurrentBar BarsBack)
              {
                return;
              }

              Print(
        "CurrentBar : " CurrentBar);
              Print(
        string.Format("{0} | listOfSeriesValue sBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count()  ): {4} "Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (l istOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() ));

              
        listOfSeriesValuesBarsBack listOfSeriesVal ues.GetRange(0BarsBack);


            } 
        And I get
        Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

        For now I've kept the Rng0Index and Rng1Index values at 0 zero.

        Comment


          #49
          Hello PaulMohn,

          Are the prints not appearing?

          Can you print Rng0Rng1[Rng0Index] before listOfSeriesValues.Add(Rng0Rng1[0]);?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #50
            Yes. With what CurrentBar check?

            Comment


              #51
              With

              PHP Code:

              protected override void OnBarUpdate()
              {
                 if (
              CurrentBar BarsBack)
                    return; 
              and
              PHP Code:
              Print(" Rng0Rng1[Rng0Index] : " +Rng0Rng1[Rng0Index]);

              listOfSeriesValues.Add(Rng0Rng1[0]);
              // listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack);

              // Print(listOfSeriesValues.Count() - BarsBack);

              // if(CurrentBar <= BarsBack)
              // {
              // return;
              // }

              Print("CurrentBar : " CurrentBar);
              Print(
              string.Format("{0} | listOfSeriesValuesBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count() ): {4} "Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (listOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() ));

              listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack); 
              I get
              Rng0Rng1[Rng0Index] : 0.0499999999999972
              CurrentBar : 20
              04/03/2022 00:21:00 | listOfSeriesValuesBarsBack.Count(): 0 - BarsBack: 20 = -20 | listOfSeriesValues.Count() ): 1
              Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 20: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

              with

              PHP Code:

              protected override void OnBarUpdate()
              {
                 if (
              CurrentBar BarsBack 2)
                    return; 
              and
              PHP Code:
              Print(" Rng0Rng1[Rng0Index] : " +Rng0Rng1[Rng0Index]);

              listOfSeriesValues.Add(Rng0Rng1[0]);
              // listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack);

              // Print(listOfSeriesValues.Count() - BarsBack);

              // if(CurrentBar <= BarsBack)
              // {
              // return;
              // }

              Print("CurrentBar : " CurrentBar);
              Print(
              string.Format("{0} | listOfSeriesValuesBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count() ): {4} "Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (listOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() ));

              listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack); 
              I get

              Rng0Rng1[Rng0Index] : 0
              CurrentBar : 40
              04/03/2022 00:41:00 | listOfSeriesValuesBarsBack.Count(): 0 - BarsBack: 20 = -20 | listOfSeriesValues.Count() ): 1
              Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 40: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
              Last edited by PaulMohn; 03-09-2022, 02:18 PM.

              Comment


                #52
                Hello PaulMohn,

                On what bar do you want the values to start being added to the list?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #53
                  From the [BarsAgo] indexing perspective, whatever the BarsBack value is.

                  For example, if BarsBack = 20,

                  Then I expect the listOfSeriesValuesBarsBack to return the most recent 20 bars doubles values (not the 20 doubles from the CurrentBar indexing perspective i.e. the earliest values).

                  From post #34

                  I think this is the correct GetRange() formulation
                  PHP Code:
                            protected override void OnBarUpdate()
                            {

                                 
                  Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];

                                 
                  listOfSeriesValues.Add(Rng0Rng1[0]);
                                 
                  listOfSeriesValuesBarsBack listOf SeriesValues.GetRange(listOfSeriesValuesBarsBack.Count() - BarsBacklistOfSeriesValuesBarsBack.Count());

                                 if (
                  listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0)
                                 {
                                    for (
                  int element 0element <  listOfSeriesValuesBarsBack.Count(); element++)
                                    {
                                       Print(
                  listOfSeriesValuesBarsBack[element]);
                                    }
                                 } 

                  I also tried this
                  PHP Code:
                            protected override void OnBarUpdate()
                            {

                                 
                  Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];

                                 
                  listOfSeriesValues.Add(Rng0Rng1[0]);
                                 
                  listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);

                                 if (
                  listOfSeriesValuesBarsBack != null && listOfSeriesValuesBarsBack.Count() > 0)
                                 {
                                    for (
                  int element 0element <  listOfSeriesValuesBarsBack.Count(); element++)
                                    {
                                       Print(
                  listOfSeriesValuesBarsBack[element]);
                                    }
                                 } 
                  Last edited by PaulMohn; 03-09-2022, 02:49 PM.

                  Comment


                    #54
                    Hello PaulMohn,

                    Without getting complex think about where the list.Add() call is being called.

                    If BarsBack is 20, how many bars before BarsBack do you want to start saving values to the to the list?

                    You want 20 bars in the list correct?

                    You said "the earliest values".

                    What is CurrentBar equal to on the earliest values?

                    Did you try what I suggested in post #43?

                    Are you still stuck?
                    Last edited by NinjaTrader_ChelseaB; 03-09-2022, 03:14 PM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #55
                      You want 20 bars in the list correct?
                      I would need the mode over the BarsBack value.
                      The BarsBack is a Users Input Variable the user can modify. So no fixed value.
                      But yes we could start with 20 as BarsBack value.

                      If BarsBack is 20, how many bars before BarsBack do you want to start saving values to the to the list?
                      I don't know how many bars before BarsBack (I thought BarsBack was enough / no Bars before or above 20 (BarsBack) was needed).

                      For example,
                      If I set BarsBack = 5,
                      I mean for the
                      PHP Code:
                      listOfSeriesValues.Add(Rng0Rng1[0]); 
                      to put in the list only

                      Rng0Rng1[0] (= double value = 1.00) (the most recent/currently processing bar, let say x)
                      Rng0Rng1[1] (= double value = 2.00) (x - 1)
                      Rng0Rng1[2] (= double value = 5.00) (x - 2)
                      Rng0Rng1[3] (= double value = 5.00) (x - 3)
                      Rng0Rng1[4] (= double value = 6.00) (x - 4)

                      And for this example
                      I'd latter need the mode as 5.00 (the most frequent double value in the list)
                      Last edited by PaulMohn; 03-09-2022, 03:18 PM.

                      Comment


                        #56
                        Hello PaulMohn,

                        If you want the list to have 20 elements, meaning 20 bars, in it by the time CurrentBar is 20, then you would need to start saving the values when CurrentBar is 0.

                        BarsBack is how many elements you want in the list correct?

                        You are running the code when CurrentBar is 20 correct?

                        Then the list needs elements added, 20 bars (BarsBack) before CurrentBar. If CurrentBar is 20, 20 bars before this is 0.

                        CurrenBar: 20 - BarsBack: 20 = 0

                        However, waiting until CurrentBar is 1 can often prevent errors on a blank chart when there is no data or other odd scenarios.

                        Please test the code provided in post #43 and provide the output.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #57
                          I tested the new code from post #43 with the two CurrentBar Checks
                          PHP Code:
                          if (CurrentBar 1)
                          return;

                          Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];

                          listOfSeriesValues.Add(Rng0Rng1[0]);

                          if (
                          CurrentBar 21)
                          return;

                          Print(
                          string.Format("{0} | CurrentBar: {1}, listOfSeriesValues.Count(): {2}"Time[0], CurrentBarlistOfSeriesValues.Count())); 
                          as
                          PHP Code:
                              protected override void OnBarUpdate()
                              {  
                                if (
                          CurrentBar 1)
                                  return;

                                
                          Rng0Rng1[Rng0Index] = Rng0[Rng0Index] - Rng1[Rng1Index];


                                
                          listOfSeriesValues.Add(Rng0Rng1[0]);
                          //       listOfSeriesValuesBarsBack = listOfSeriesValues.GetRange(0, BarsBack);

                          //       Print(listOfSeriesValues.Count() - BarsBack);


                                
                          if(CurrentBar 21)
                                {
                                  return;
                                }

                                Print(
                          "CurrentBar : " CurrentBar);
                                Print(
                          string.Format("{0} | listOfSeriesValue  sBarsBack.Count(): {1} - BarsBack: {2} = {3} | listOfSeriesValues.Count()   ): {4} "Time[0], listOfSeriesValuesBarsBack.Count(), BarsBack, (l  istOfSeriesValuesBarsBack.Count() - BarsBack), listOfSeriesValues.Count() ));

                                
                          listOfSeriesValuesBarsBack listOfSeriesValues.GetRange(0BarsBack);


                              } 
                          I get
                          Indicator 'Testa': Error on calling 'OnBarUpdate' method on bar 1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                          Comment


                            #58
                            Hello PaulMohn,

                            I am not able to reproduce.

                            Attached is an exported test script with the sample code I provided you.

                            Also attached is the output I've received.


                            In your script, there is no output at all from prints? There is just the error message and no output?
                            Attached Files
                            Last edited by NinjaTrader_ChelseaB; 03-09-2022, 04:16 PM.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #59
                              Thanks for your script and test.
                              I imported your script successfully and it does print the output on my end too.

                              A difference from mine is the BarsBack in yours is declared at Class level scope without Properties variable, while mine is declared at State.SetDefaults level scope (similar as in post #46) with Properties variable.
                              I tested with modification same as mine and it also printed successfully.

                              Then I thought it must be then an issue with the remaining code.
                              I've commented out large portions of the code and I see it's printing ok on my indicator now.
                              I haven't yet isolated the exact snippet nor lines or line but I'll check them one by one next.
                              The strange thing is that it wasn't throwing any error with the yet to be identified snippet prior to those new prints being added (the same code works fine without them).

                              Many thanks for the direction and very helpful debug walkthrough.
                              I'll be back with results asa. Thanks again!

                              Comment


                                #60
                                I identified one for the error throwing snippets

                                With BarsBack it throws the error
                                PHP Code:
                                double sum 0;
                                for(
                                int i=0i<BarsBacki++)
                                {
                                   
                                sum += Rng0Rng1[i];
                                }

                                double var3 = (sum BarsBack);

                                double var03 Instrument.MasterInstrument.RoundToTickSize(var3 TickSize); 

                                but with CurrentBar it doesn't
                                PHP Code:
                                double sum 0;
                                for(
                                int i=0i<CurrentBari++)
                                {
                                   
                                sum += Rng0Rng1[i];
                                }

                                double var3 = (sum CurrentBar);

                                double var03 Instrument.MasterInstrument.RoundToTickSize(var3 TickSize); 

                                Why does Barsback throws the error? Thanks! (I'll be back asa tomorrow)
                                Last edited by PaulMohn; 03-10-2022, 08:33 AM.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by manueldecastro, Yesterday, 10:26 AM
                                4 responses
                                18 views
                                0 likes
                                Last Post manueldecastro  
                                Started by TraderIqbal, 10-20-2023, 10:12 AM
                                2 responses
                                119 views
                                0 likes
                                Last Post Funnywo
                                by Funnywo
                                 
                                Started by Creamers, 04-27-2024, 05:32 AM
                                12 responses
                                72 views
                                0 likes
                                Last Post Creamers  
                                Started by fredericlebre, Today, 01:19 AM
                                0 responses
                                8 views
                                0 likes
                                Last Post fredericlebre  
                                Started by TraderJA, Today, 12:06 AM
                                0 responses
                                11 views
                                0 likes
                                Last Post TraderJA  
                                Working...
                                X