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

Convert Price Series to Array

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

    Convert Price Series to Array

    Hi,

    I am trying to convert a price series to an array that I can pass as a parameter to a custom method.. I have tried the approach below but no luck.

    List<Series<double>> arrayOfDoubleSeries = new List<Series<double>>();
    arrayOfDoubleSeries.Add(new Series<double>(Median));

    double[] data = arrayOfDoubleSeries.ToArray();


    I get the errors:

    MyCustomIndicator.cs Argument 1: cannot convert from 'NinjaTrader.NinjaScript.ISeries<double>' to 'NinjaTrader.NinjaScript.NinjaScriptBase'
    MyCustomIndicator.cs Cannot implicitly convert type 'NinjaTrader.NinjaScript.Series<double>[]' to 'double[]'


    Is there a way to convert a price series to an array? I'd like to pass that array to a custom method.

    Thanks for any suggestions or help. It would be appreciated.​​​

    EDIT: Yes, I can use a loop and create the array from the series but I was looking for something more efficient.
    Last edited by Zeos6; 04-15-2023, 03:25 PM.

    #2
    Are you sure you really want to do that?

    Beg pardon, but a Series is not equivalent to
    an Array, as you obviously know by now.

    They may look the same, but they are very
    different data structures.

    [There is no automatic conversion, you'd probably
    have to write some 'for' loops and perform all the
    copying logic yourself -- but no one really does this,
    because it's extremely awkward & usually quite
    unnecessary, and very burdensome codewise.]

    You'd be much better off re-coding your custom
    method to work with a Series.

    What does this custom method do?
    Who wrote it?

    Just my 2˘.

    Last edited by bltdavid; 04-15-2023, 10:34 PM.

    Comment


      #3
      I may have misspoke.
      It doesn't look that hard or burdensome after all.

      Just create a List<double> variable as a private global
      member in your class,

      private List<double> CloseValues = new List<double>();
      private List<double> OpenValues = new List<double>();
      ...


      The idea is to pre-create each List<double> you may need.

      That is, you'll need one List<double> for each Series<double>
      that you want to turn into an Array later.

      So, choose a naming convention and stick to it. Here,
      I'm using the 'Values' suffix for the List<double> version
      of the corresponding Series<double> variable.

      -=o=-

      Inside each OnBarUpdate, add the element to the List<double>.

      CloseValues.Add(Close[0]);

      When you need an array for your custom method,

      CloseValues.ToArray();

      or

      CloseValues.GetRange(start, count).ToArray();

      Make sense?
      Last edited by bltdavid; 04-15-2023, 10:49 PM. Reason: more ideas ...

      Comment


        #4
        bltdavid is correct about this. But, you should probably rethink why you want to do this - you're making a complete duplicate of the price series which is wasteful of both memory and CPU. There is probably a better way to do what you are trying to do but if not, I suppose go for it and duplicate the entire thing.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Thanks for the replies. They are appreciated.

          I am well aware that the Series object is very different form an array object. I am also well aware of the memory and load implications of what I need to do, and I know how I am going to handle those issues. I am using a method to perform some complex calculations. The method was written by someone else. It uses an array of input data and it is not feasible for me to rewrite it. The list approach may be workable for my needs so thank you bitdavid.

          Comment


            #6
            Hello Zeos6,

            Thanks for your post.

            Using arrays would fall under C# education and goes beyond the support we would be able to provide you with in the Support department. You could consider doing some research on working with arrays in C# to find more information about this.

            That said, bltdavid is correct. there is no automatic conversion and this would likely take custom C# logic to accomplish. You may consider using the logic bltdavid shared as a possible solution.

            QuantKey_Bruce is also correct in stating that it seems you are making a duplicate of the PriceSeries which could cause a higher number of resources to be used compared to just using PriceSeries.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Thank you BrandonH.

              I am not sure how the perception arose that I do not understand C# and arrays. I am well aware of the differences between series and arrays objects and how to use them. I also have a very good understanding and working knowledge of C#. Moreover, I am well aware of the memory and resource impact of doing things this way. I was simply asking if there was any built-in conversion of the Series data to any other collection object. You have indicated that there is not. Thank you. I appreciate the information.

              Comment


                #8
                Originally posted by Zeos6 View Post
                I am not sure how the perception arose that I do not understand C# and arrays.


                My apologies, I think that was my fault.

                Bruce and I both thought the question sounded odd, as if
                the developer (aka, you) did not understand how different
                Series and Array classes really are ... I should have done
                a bit more research, you have 865 posts as I write this,
                so you are not new to NT by any means.

                Again, I meant no offense.

                I'm glad I was able to suggest something useful.

                Onward and upward!



                PS: Hey, if ya could, let us know a little more about that
                custom method -- anything you could share would be very
                interesting. Thanks!

                Comment


                  #9
                  No worries @bitdavid. No offense taken. I found it odd that people would think that after all this time I had little understanding of C# collection objects, arrays, and of NT. It's OK.
                  I am well aware of the resource impacts involved but I only need the full series once, so the resource impact is not as severe as one might think. Moreover, I only extract segments of data of various lengths as needed, and I can efficiently use vector methods, LINQ, etc. on these data segments in array form. Recoding the method for NT series is not worth the effort in this case.

                  Your suggestion of using the List<T>.GetRange(Int32, Int32).ToArray() approach is actually very effective in my case. I hadn't thought of it at the time so I really appreciate it. Thank you!

                  And since you asked, the method detects changes in price volatility in real time.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Pattontje, Yesterday, 02:10 PM
                  2 responses
                  14 views
                  0 likes
                  Last Post Pattontje  
                  Started by flybuzz, 04-21-2024, 04:07 PM
                  17 responses
                  229 views
                  0 likes
                  Last Post TradingLoss  
                  Started by agclub, 04-21-2024, 08:57 PM
                  3 responses
                  17 views
                  0 likes
                  Last Post TradingLoss  
                  Started by TradingLoss, 04-21-2024, 04:32 PM
                  4 responses
                  44 views
                  2 likes
                  Last Post TradingLoss  
                  Started by cre8able, 04-17-2024, 04:16 PM
                  6 responses
                  56 views
                  0 likes
                  Last Post cre8able  
                  Working...
                  X