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

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.
          Chris L.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Gerik, Today, 09:40 AM
          2 responses
          7 views
          0 likes
          Last Post Gerik
          by Gerik
           
          Started by RookieTrader, Today, 09:37 AM
          2 responses
          11 views
          0 likes
          Last Post RookieTrader  
          Started by alifarahani, Today, 09:40 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by KennyK, 05-29-2017, 02:02 AM
          3 responses
          1,285 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by AttiM, 02-14-2024, 05:20 PM
          11 responses
          186 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X