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

GetValueAt multi-plot indicator and List<T> confusion

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

    GetValueAt multi-plot indicator and List<T> confusion

    Hello support staff and community,

    I have a custom overlay indicator which in total contains 15 equidistant plots on the chart.

    In my indicator logic I have an array which holds the CurrentBar value when each plot was passed through by price action. Like so...

    Code:
    int[] barHits = { Yminus7Hit, Yminus6Hit, Yminus5Hit, Yminus4Hit, Yminus3Hit, Yminus2Hit, Yminus1Hit, YHit, Yplus1Hit, Yplus2Hit, Yplus3Hit, Yplus4Hit, Yplus5Hit, Yplus6Hit, Yplus7Hit };
    Array.Sort(barHits);
    Array.Reverse(barHits);
    using this logic, barHits[0] always gives me the CurrentBar value when a plot was most recently hit. Each Yplus/minusxHit is simply
    Code:
    if (High[0] >= plot && Low[0] <= plot
    What I am having trouble with is determining not only the CurrentBar value when a plot was hit, but also the VALUE of the plot that was hit, i.e. which plot was hit and when?

    I am trying to create a List<T> such that the elements represent the currentbar value of a plot being hit, and which plot this was. But I am having trouble because when I say...

    Code:
    if (High[0] >= plot && Low[0] <= plot)
                   {  hitCache.Add(plot, currentbar);  }
    i get the correct value, but when I print the list using a foreach or for loop, it seems to only ever hold one element? and that one element is updated every time a new plot is hit, rather than adding to a historical collection of plot hits like I want it to.

    I hope my goal is clear here, and adequately explained. The final product would essentially be the same information that setting an MRO method for each individual plot would give me, only I would be holding each instance in the same list ( I do plan to add a List.Remove function when the list gets to a certain size, beyond which the instance is no longer useful).

    Thanks in advance, love you all.

    #2
    Hello lunardiplomacy,

    Thank you for your note.

    How are you initializing the hitCache List<T>?

    Also, it looks like what you're wanting to do is insert an object that contains both values into the list.

    Here's a publicly available link to Microsoft's documentation that has an example of doing this:

    Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.


    Specifically I'd look at where it adds Parts to a list in that example.

    Please let us know if we may be of further assistance to you.

    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate,

      Thanks for your response. Yes, I am pretty certain at this point having done some research that my best move forward is to create a struct or class with the params I need, and then to create either a list or an array of that struct/class. However I seem to be having a lot of trouble implementing a custom class into Ninjascript.

      Could you please provide the exact location and syntax required for this to work? It would need to be a nested class within the indicator class, right? and then would I initialize the array/list in OnBarUpdate? is that the equivalent to Main() in the example you provided?

      Code:
      public class myclass
      {
          public int x;
          public double y;
      }
      is what I have been trying, putting the code directly below the indicator class, and then in OnBarUpdate I have been trying different variations of initializing an array or list of the class, using my variables Yplus/minusxHit(int) and plot(double). None of it compiles!

      Comment


        #4
        Hello lunardiplomacy,

        Thank you for your reply.

        I've actually taken that example and created an indicator out of it so you can see within NinjaTrader how it would work. You'd just need to define your class outside of the Indicator class, and yes, the part that is in Main() in the example would be placed in OnBarUpdate in an indicator.

        Please let us know if we may be of further assistance to you.
        Attached Files
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Ah, very nice, Kate. Thank you.

          Going through this script should be very educational once I have tinkered around and understood what exactly is going on. In the meantime, I tried to implement this class into my own script using my own variables and I got a compilation error CS1922 'Cannot initialize type 'customclasspath' with a collection initializer because it does not implement 'System.Collections.IEnumerable''

          That is the same error I kept getting earlier when trying to implement a custom class to my indicator. Do you know what is causing this?

          Comment


            #6
            Hello lunardiplomacy,

            Thank you for your reply.

            Ah, in this case it wants you to create it as IEnumerable<yourClass> instead of IEquatable<yourClass> so that you can use foreach syntax. There's another example of that here:



            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Kate,

              This is definitely what I have been looking for. Thank you very much for your help.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ZeroKuhl, Today, 04:31 PM
              0 responses
              13 views
              0 likes
              Last Post ZeroKuhl  
              Started by Vulgoth_t_Destroyer, 05-09-2022, 04:45 PM
              54 responses
              5,337 views
              0 likes
              Last Post Gaterz
              by Gaterz
               
              Started by ETFVoyageur, 05-07-2024, 07:05 PM
              11 responses
              76 views
              0 likes
              Last Post -=Edge=-  
              Started by _Zero_, 04-10-2020, 03:21 PM
              145 responses
              7,900 views
              6 likes
              Last Post johng2
              by johng2
               
              Started by TheTradingMantis, 01-19-2023, 02:05 AM
              43 responses
              923 views
              0 likes
              Last Post jmagaia
              by jmagaia
               
              Working...
              X