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 kaywai, 09-01-2023, 08:44 PM
          5 responses
          601 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          6 responses
          22 views
          0 likes
          Last Post xiinteractive  
          Started by Pattontje, Yesterday, 02:10 PM
          2 responses
          18 views
          0 likes
          Last Post Pattontje  
          Started by flybuzz, 04-21-2024, 04:07 PM
          17 responses
          230 views
          0 likes
          Last Post TradingLoss  
          Started by agclub, 04-21-2024, 08:57 PM
          3 responses
          17 views
          0 likes
          Last Post TradingLoss  
          Working...
          X