Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing BarsArray from add on script

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

    Accessing BarsArray from add on script

    Hi all,

    I am currently trying to put my calculation codes into an add on script so that I may use it for my other indicators and strategies. My current method is currently as such:

    Code:
    public IList<double> GetMaximas(int ChartID, int Limit, int NumberOfValues)
    {
          HighMaximas.Clear();
    
          for(int i = 0; i < Limit; i++)
          {  
                SlidingWindow[0] = Highs[ChartID][i];
                SlidingWindow[1] = Highs[ChartID][i+1];
                SlidingWindow[2] = Highs[ChartID][i+2];
    
                 if(Counter == 0)
                 {
                       if((SlidingWindow[1] > SlidingWindow[0]) && (SlidingWindow[1] > SlidingWindow[2]) && (SlidingWindow[1] >= Highs[ChartID][0]))
                       {    
                             HighMaximas.Add(SlidingWindow[1]);
                             HighMaximaIndices.Add(i+1);
                             Counter ++;
                        }
    
                       else
                       {    
                             ;
                       }
                   }
    
                   else if((Counter > 0) && (Counter < NumberOfValues))
                   {
                         if((SlidingWindow[1] > SlidingWindow[0]) && (SlidingWindow[1] > SlidingWindow[2]) && (SlidingWindow[1] > HighMaximas[Counter-1]))
                         {                
                               HighMaximas.Add(SlidingWindow[1]);
                               HighMaximaIndices.Add(i+1);
                               Counter ++;
                         }
    
                         else
                         {
                               ;
                         }
                   }
    
                   else
                   {
                         break;
                   }
               }
    
          Counter = 0;
    
          return HighMaximas;
    }
    But when I try to compile it, I'm getting a CS0103 message saying "The name 'Highs' does not exist in the current context.". Is it possible to create a method that relies on accessing the bars array within an AddOn script?

    Thanks and Regards,
    Somebody
    Last edited by MrSomebody; 03-29-2020, 07:00 AM.

    #2
    If you pass the indicator or strategy as a variable you will be able to access the data series.

    Code:
            private NinjaScriptBase indicator;
    
            public CandleRejectionCalculator(
                  NinjaScriptBase indicator
            ) {
                this.indicator = indicator;
    Code:
            public void OnBarUpdate() {
                double open = indicator.Open[0];

    Comment


      #3
      Hi Mojo,

      Thanks for the guidance but let's say that I have a scenario where I wanted to use this method in indicator 1 and indicator 2 and since I've passed only indicator 1 in the AddOn file, wouldn't I only be able to access the BarsArray of indicator 1? If this were the case, wouldn't it lead to erroneous BarsArray accessing when I attempt to use the same method for indicator 2?

      Comment


        #4
        Hi MrSomebody,

        If I understand your question correctly
        E.g. with my example
        Code:
        new  CandleRejectionCalculator(this);
        will creating a separate instance of the class with the shared code in indicator 1 and 2.
        This means that they are totally independent of each other and the calculation won't interfere with each other.


        Comment


          #5
          Hi MrSomebody, thanks for your note.

          The BarsArray belongs to the Indicator instance, so you would need to pass in either the indicator instance or the BarsArray into the method to be able to access it.

          This example might be helpful. It uses property changed events to that NinjaScript objects that subscribe to the even can be notified of a global property change


          Please let me know if I can assist any further.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          603 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          349 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          104 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          560 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          560 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X