Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding custom calc values to each price level of a bar

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

    Adding custom calc values to each price level of a bar

    Hi,

    i would like to add custom calculation to each price level inside a given bar.
    For example, i use a 4 range NQ bar, with the high of 19875 and the low of 19874. I want to add custom calculated values for this bar at price level 19875, 19874.75, 19874.5, 19874.25, 19874.
    I convert the price value to integer, so i can have an integer index :

    Code:
    pr = Convert.ToInt32(GetLastAsk(0)*100);

    My idea was to create a 2 dimensional array like

    Code:
    calc[0][pr] = x;
    where 0 would be the current candle index, and pr the price level for the current candle. I wanted to do it this way so i can easily check the price value of any past candle.

    My problem is in defining the array. If i define it as a Series<double> it would search for secondary data series and i don't use secondary data series.

    Code:
    private Series<double> calc
    
    
    
    if ( State == State.DataLoaded)
                {
                     calc = new Series<double>(this, MaximumBarsLookBack.Infinite);
                   
                }​
    
    ;
    How can i define my array?

    Thanks for your help


    #2
    Hi,

    i would like to add custom calculation to each price level inside a given bar.
    For example, i use a 4 range NQ bar, with the high of 19875 and the low of 19874. I want to add custom calculated values for this bar at price level 19875, 19874.75, 19874.5, 19874.25, 19874.
    I convert the price value to integer, so i can have an integer index :

    Code:
    pr = Convert.ToInt32(GetLastAsk(0)*100);

    My idea was to create a 2 dimensional array like

    Code:
    calc[0][pr] = x;
    where 0 would be the current candle index, and pr the price level for the current candle. I wanted to do it this way so i can easily check the price value of any past candle.

    My problem is in defining the array. If i define it as a Series<double> it would search for secondary data series and i don't use secondary data series.

    Code:
    private Series<double> calc
    
    
    
    if ( State == State.DataLoaded)
                {
                     calc = new Series<double>(this, MaximumBarsLookBack.Infinite);
                   
                }​
    
    ;
    How can i define my array?

    Thanks for your help

    Comment


      #3
      Hello gyilaoliver,

      You could use an array with a series, Series<T> is a generic meaning any type can be used not just double.

      calc = new Series<YourArrayTypeHere>(this, MaximumBarsLookBack.Infinite);

      Then for each OnBarUpdate event you would have to assign the array to the series:

      calc[0] = new YourArrayTypeHere;

      Then you can use it like you suggested:

      calc[0][index] = a value;
      JesseNinjaTrader Customer Service

      Comment


        #4
        Hi Jesse,

        thanks for you reply.
        I made this code, however it gives me error code CS0828 : No best type found for implicitly-typed array.

        I saw somewhere in the forum that i have to initialize the array like this :

        calc[0] = new []{0,1,2,3, ...};

        but given that i want to add values to price levels, those numbers are way over 10000...



        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class MyCustomIndicator : Indicator
        {
        private Series<int[]> calc;
        private int pr;
        
        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = @"Enter the description for your new custom Indicator here.";
        Name = "MyCustomIndicator";
        Calculate = Calculate.OnEachTick;
        IsOverlay = false;
        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;
        }
        else if (State == State.Configure)
        {
        }
        else if ( State == State.DataLoaded)
        {
        calc = new Series<int[]>(this, MaximumBarsLookBack.Infinite);
        
        }
        }
        protected override void OnBarUpdate()
        {
        if (CurrentBar < 4)
        return;
        
        if ( IsFirstTickOfBar )
        calc[0] = new []{};
        
        pr = (Convert.ToInt32(GetCurrentAsk(0)))*100;
        
        calc[0][pr] = 3;
        
        }
        }
        }
        ​​
        Attached Files
        Last edited by gyilaoliver; 03-20-2025, 09:50 AM.

        Comment


          #5
          Hello gyilaoliver,

          You likely will want to just use a dictionary for this type of use so you don't have to have an array with the size spanning to the index vvalues you wanted.

          Code:
          private Series<Dictionary<int,double>> calc;
          ​
          calc = new Series<Dictionary<int, double>>(this, MaximumBarsLookBack.Infinite);
          ​
          
           protected override void OnBarUpdate()
           {
               calc[0] = new Dictionary<int, double>();
               int key = 1000;
               if (calc[0].ContainsKey(key))
               {
                   calc[0][key] = Close[0];
               }
               else
               {
                   calc[0].Add(key, Close[0]);
               }
           }
          That will let you use any int as a key and get a value from the dictionary based on the key.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Thank you Jesse.
            Didi as you advised.
            Here is the code :

            Code:
            protected override void OnBarUpdate()
            {
            if (CurrentBar < 4)
            return;
            
            calc[0] = new Dictionary<int, double>();
            
            pr = Convert.ToInt32(GetCurrentAsk(0) * 100);
            
            if (calc[0].ContainsKey(pr))
                    {
                         calc[0][pr] = Close[0];
                         Print("Added ok");
                    }
               else
                   {
                          calc[0].Add(pr, Close[0]);
                          Print("Added new");
                    }
            
            //  next I want to check a value from the previous bar
            
            if (IsFirstTickOfBar)
              {
                   int prx = Convert.ToInt32(High[1]-3*TickSize)*100;
                   Print("Prx found");
                   double dd = calc[1][prx];
                   Print("Last bar val 3 ticks below high = "+dd);
            
               }
            }

            The code works perfectly, until I want to check a value of the previous bar. The code runs until it prints "Prx found", then i get error
            "Error on calling "OnBarUpdate" method on bar 4 : Object reference not set to an instance of the object

            I also tried the following :

            Code:
            if ( IsFirstTickOfBar ) calc[0] = new Dictionary<int, double>();
            and his :

            Code:
            if (IsFirstTickOfBar && CurrentBar > 6)
            {
            int prx = Convert.ToInt32(High[1]-3*TickSize)*100;
            Print("Prx found");
            
            if (calc[0].ContainsKey(prx))   double dd = calc[1][prx];
                           else calc[1].Add(prx, Close[0]);
            
            Print("Last bar val 3 ticks below high = "+dd);
            
            }

            Also tried to work with calc[CurrentBar] instead of calc[0], but it seems that no matter what i do, the calc dictioary doesn''t exist anymore on the previous bar.

            What am I doing wrong? sorry for bothering with so many stupid questions, but i really want to learn to work properly.

            Thanks
            Attached Files
            Last edited by gyilaoliver; 03-21-2025, 08:08 AM.

            Comment


              #7
              I am not analyzing your code but after a quick look...
              Try getting rid of this:
              if (CurrentBar < 4)
              return;​

              and just use your:
              if (IsFirstTickOfBar && CurrentBar > 6)
              eDanny
              NinjaTrader Ecosystem Vendor - Integrity Traders

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by rajeshks1988, 01-13-2021, 12:00 PM
              10 responses
              1,254 views
              0 likes
              Last Post konganda  
              Started by M_ichel, 04-22-2025, 02:21 PM
              7 responses
              57 views
              1 like
              Last Post brucerobinson  
              Started by michelz, 02-18-2025, 08:30 AM
              33 responses
              1,054 views
              0 likes
              Last Post MiCe1999  
              Started by volIQ, 04-23-2025, 05:43 PM
              4 responses
              38 views
              0 likes
              Last Post volIQ
              by volIQ
               
              Started by algospoke, 04-21-2025, 06:44 PM
              2 responses
              48 views
              0 likes
              Last Post MiCe1999  
              Working...
              X