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

C# collection-type for building/storing dynamic arrays?

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

    C# collection-type for building/storing dynamic arrays?

    C# collection-type for building/storing dynamic arrays?

    At the beginning of each market session "hour", I'd like to "go back" through that corresponding "hour" of daily history chart data and collect data to store temporarily for analysis:
    - how many EMA cross-above/cross-belows, price at crossover point, ticks above/below that price during each crossover, ending price when EMAs cross back over and maybe time in minutes of each crossover......I have about 13 elsements (fields) I'd like to collect for EACH crossover in the current hour. [I could make them all doubles if need be].

    I'm relatively new to C# but have many decades of other programming languages. So, should/could I use a generic collection of some sort? Perhaps a "List Collection"?

    The number of crossovers in a given hour is limited but I'd keep a record for each crossover and for each history day (30-days max probably) FOR THE "HOUR" being analyzed.

    Once I've collected the data, I'd need to iterate through it to create weighted averages of some of the elements, etc. The results could be stored in variables and would be used to supplement entry/exit decision making.

    Sorry if this is too complex,or if I've explained it poorly. But anyone have ideas on what feature(s) of C# i could use to (temporarily) store/sort/filter/summarize/analyze the data?

    Thanks! -Bill-

    #2
    Hi Bill,

    I work in C# in my day to day. May I suggest an IEnumerable? Will not have to worry about preserving array order as it is simply a collection that can be accessed via linq query. Ref: https://docs.microsoft.com/en-us/dot...ew=netcore-3.1

    So maybe you cast it as IEnumerable<HistoryHour> where HistoryHour would be your object containing your 13 important elements.

    I am sure you are already thinking about this but C# is not the best language to try and save results in memory for 30 days at a time; are you planning on caching your results anywhere for easier and quicker access?

    Best of luck.

    Edit: IDictionary will also work although I do not use this for these purposes.
    Last edited by Sleemo; 05-22-2020, 05:42 PM.

    Comment


      #3
      Thanks Sleemo, Once I have the "scoop" for my new "hour" of trading, I'll transfer the pertinent info into variables which I'll use to influence trade decisions during that hour. The structure I use could then be released / destroyed / reset to nothing (terminology?). Then near the beginning of the NEXT trading hour, I'd do it again for the upcoming hour.

      Yes I did business/accounting -related programming mainly, where fast access to data-files containing records and fields was a given....adjusting to the limitations of NT8 & C# requires a major tweak to my mindset....I'll bone up on IEnumerables and IDictionary (saw a tutorial on IDictionary just today...somewhere :-) ). I appreciate the pointers. -Bill-

      Comment


        #4
        Originally posted by NoLoEffeCo View Post
        C# collection-type for building/storing dynamic arrays?

        At the beginning of each market session "hour", I'd like to "go back" through that corresponding "hour" of daily history chart data and collect data to store temporarily for analysis:
        - how many EMA cross-above/cross-belows, price at crossover point, ticks above/below that price during each crossover, ending price when EMAs cross back over and maybe time in minutes of each crossover......I have about 13 elsements (fields) I'd like to collect for EACH crossover in the current hour. [I could make them all doubles if need be].

        I'm relatively new to C# but have many decades of other programming languages. So, should/could I use a generic collection of some sort? Perhaps a "List Collection"?

        The number of crossovers in a given hour is limited but I'd keep a record for each crossover and for each history day (30-days max probably) FOR THE "HOUR" being analyzed.

        Once I've collected the data, I'd need to iterate through it to create weighted averages of some of the elements, etc. The results could be stored in variables and would be used to supplement entry/exit decision making.

        Sorry if this is too complex,or if I've explained it poorly. But anyone have ideas on what feature(s) of C# i could use to (temporarily) store/sort/filter/summarize/analyze the data?

        Thanks! -Bill-
        My suggestion is setup a class to collect your data points.
        Code:
        // this class represents a single occurrence of a crossover
        public class CrossOver
        {
            public int Direction = 0; // CrossAbove=1 CrossBelow=-1
            public int CurrentBar = 0;
            public double Price = 0;
            public DateTime Time = DateTime.MinValue;
            public CrossOver(int direction, int currentBar)
            {
                Direction = direction;
                CurrentBar = currentBar;
            }
        }
        Then create a list to maintain these occurrences,
        Code:
        private List<CrossOver> CrossOvers = new List<CrossOver>();
        
        // previous occurence, regardless of when it occurred
        private CrossOver PreviousCrossOver = null;
        Each time a new cross over occurs, create a new CrossOver instance and
        record the information, then add it to your CrossOvers list,
        Code:
        if (CrossAbove(MA1, MA2, 1))
            CrossOvers.Add(new CrossOver(1, CurrentBar));
        else if (CrossBelow(MA1, MA2, 1))
            CrossOvers.Add(new CrossOver(-1, CurrentBar));
        Inside your OnBarUpdate, at the recognition of a new hour, you want to perform
        your analysis of all cross overs collected in your list, then clear the list,
        Code:
        if (Time[0].Hour != Time[1].Hour)
        {
            foreach (CrossOver co in CrossOvers)
            {
                int BarsAgo = CurrentBar - co.CurrentBar;
                co.Price = Close[BarsAgo];
                co.Date = Date[BarsAgo];
                if (PreviousCrossOver != null)
                {
                    double PriceDelta = Math.Abs(co.Price - PreviousCrossOver.Price);
                    TimeSpan TimeDelta = co.Date - PreviousCrossOver.Date;
                    int TotalBars = co.CurrentBar - PreviousCrossOver.CurrentBar;
                    .... print these items, save to database, etc ....
                }
               PreviousCrossOver = co;
            }
            CrossOvers.Clear();
        }
        None of this is tested code, I just rattled it off.
        But hopefully it helps to give you a good head start.

        Further analysis on 30 days of stored data (assuming you did that
        "save to database" part) is left as an exercise for the reader.

        Good luck!
        Last edited by bltdavid; 05-23-2020, 12:09 AM.

        Comment


          #5
          When this code runs in your OnBarUpdate, it will collect all the historical
          crossovers in your chart.

          Perhaps setup a new class HistoryHour, using the name suggested by Sleemo,
          that would actually retain analytical results of most recent hour.

          Like perhaps,

          1. Longest duration of all crossovers
          2. Shortest duration of all crossovers
          3. Average duration of all crossovers

          Calculate these three items for Time, Ticks (aka price) and
          maybe for count of bars.

          Calculate for all crossovers, then calculate for just CrossAbove,
          and again for just CrossBelow.

          Now, what you actually do with these gathered statistics, well, I suppose
          that's the secret sauce ...

          Comment


            #6
            More (untested) code to help you,

            Code:
            public struct DataTuple<T>
            {
                public T Longest;
                public T Shortest;
                public T Average;
                public void Reset()
                {
                    Longest = default(T);
                    Shortest = default(T);
                    Average = default(T);
                }
            }
            
            public class DataHistory
            {
                public DataTuple<DateTime> Dates;
                public DataTuple<double> Ticks;
                public DataTuple<int> Bars;
            }
            
            public class DataAnalysis
            {
                public DataHistory CrossOvers = new DataHistory();
                public DataHistory CrossAboves = new DataHistory();
                public DataHistory CrossBelows = new DataHistory();
            }
            
            private DataAnalysis Hourly = new DataAnalysis();
            Then access like,

            Code:
              Hourly.CrossOvers.Dates.Longest
              Hourly.CrossBelows.Ticks.Average
            Sounds kinda fun ... Enjoy!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cre8able, Today, 01:16 PM
            2 responses
            9 views
            0 likes
            Last Post cre8able  
            Started by chbruno, 04-24-2024, 04:10 PM
            3 responses
            48 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by samish18, Today, 01:01 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by WHICKED, Today, 12:56 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by WHICKED, Today, 12:45 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X