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

How to find the 1st, 2nd, and 3rd highest volume for the day in real time

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

    How to find the 1st, 2nd, and 3rd highest volume for the day in real time

    I want to create and indicator that will tell me in real time the 3 highest volumes during the trading day. i.e.- the highest, second highest and third highest.

    Finding the highest wouldn't be so bad, but if there is a subsequent volume bar that is turning out to be the second or third highest, I would like to know that too. So my thought is that it's not just a simple dataseries but an array that gets sorted.

    And if it runs in real time, I'm concerned about cpu load.

    Can someone give me guidance on which method would be the best to pursue?

    thanks,

    taddypole...

    #2
    Creating code to do this would be simple.

    You are on the right path.

    You could use 3 variables in this case without complicating things in arrays, etc.

    If CalculateBarOnClose=TRUE - cpu load would not be of concern.
    real time... maybe - but probably not, unless you are looking at 100 stocks or something.

    I'm not sure where you want output or where you are viewing these 3 volumes of 1st/2nd/3rd ranking...





    Originally posted by Taddypole View Post
    I want to create and indicator that will tell me in real time the 3 highest volumes during the trading day. i.e.- the highest, second highest and third highest.

    Finding the highest wouldn't be so bad, but if there is a subsequent volume bar that is turning out to be the second or third highest, I would like to know that too. So my thought is that it's not just a simple dataseries but an array that gets sorted.

    And if it runs in real time, I'm concerned about cpu load.

    Can someone give me guidance on which method would be the best to pursue?

    thanks,

    taddypole...

    Comment


      #3
      I've had indicators to do this but it only finds the highest volume during the day. Later in the day, there might be a high volume bar but it gets "hidden" by the earlier volume bar.

      So my solution would be a horizontal line on the chart that indicates the 3 highest volume bars.

      I only do futures and have multiple timeframes up... maybe 4 or 5 charts so hopefully the cpu load would not be that much, but i do worry about the sorting aspect.

      Comment


        #4
        Originally posted by Taddypole View Post
        I've had indicators to do this but it only finds the highest volume during the day. Later in the day, there might be a high volume bar but it gets "hidden" by the earlier volume bar.

        So my solution would be a horizontal line on the chart that indicates the 3 highest volume bars.

        I only do futures and have multiple timeframes up... maybe 4 or 5 charts so hopefully the cpu load would not be that much, but i do worry about the sorting aspect.

        The internet has a lot of answers:


        I have to put 3 values in correct order and print them out to the console. A solution is to put them into an array and then sort them, but I remember (from school times) that there was faster to



        You would just need to control the last bar until it closes... as I am guessing you would want to know if it was the "highest" currently, or 2nd/3rd...

        You wouldn't want the current bar to override all other previous bars until it closes (but can still distinguish where it is in relation)...

        Comment


          #5
          yes..

          so in the reference post there is:

          if (a > b)
          swap(a,b)
          if (b > c)
          swap(b,c)
          if (a > b)
          swap(a,b)
          print a,b,c


          I think this is the insertion method. But not sure as I am only a self trained programmer. Would I use an array? I've heard of simple Arrays, Dictionarys, Lists, SortedDictionary's. Which one would I use and what would be the basic structure.

          Thanks,
          taddypole....

          Comment


            #6
            Originally posted by Taddypole View Post
            yes..

            so in the reference post there is:

            if (a > b)
            swap(a,b)
            if (b > c)
            swap(b,c)
            if (a > b)
            swap(a,b)
            print a,b,c


            I think this is the insertion method. But not sure as I am only a self trained programmer. Would I use an array? I've heard of simple Arrays, Dictionarys, Lists, SortedDictionary's. Which one would I use and what would be the basic structure.

            Thanks,
            taddypole....
            Easist to use will be an ArrayList.

            Code:
            [LIST=1][*]Insert the current value[*]Sort[*]Remove the smallest value.[/LIST]

            Comment


              #7
              Thanks koganam,

              Great idea for keeping the list small.

              Can you give me a hint on how to set this array up?

              protected override void Initialize()

              int[] highestVol = new int[4]; // highestVol array

              protected override void OnBarUpdate()

              Value.Set(Volume[0]);
              highestVol .Add(Value)

              Sorry for the feeble attempt but this is all very new to me.

              Comment


                #8
                Hello Taddypole,

                You may want to view the following link for an Array Tutorial.
                JCNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by Taddypole View Post
                  Thanks koganam,

                  Great idea for keeping the list small.

                  Can you give me a hint on how to set this array up?

                  protected override void Initialize()

                  int[] highestVol = new int[4]; // highestVol array

                  protected override void OnBarUpdate()

                  Value.Set(Volume[0]);
                  highestVol .Add(Value)

                  Sorry for the feeble attempt but this is all very new to me.
                  If you want to use an Array, rather than an ArrayList, it might be simpler if you just assigned the current value to the last index, then sorted, knowing that the first 3 indices then contain the values of interest.

                  You want to declare your Array first, as a Class variable, in the Variables section, not in the Initialize() method.
                  Code:
                  long[] highestVol = new long[4]{0, 0, 0, 0}; // highestVol array
                  Then in OBU:
                  Code:
                  [FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]
                  if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] (Volume[[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]] > highestVol[[/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]])[/COLOR]
                  {
                  highestVol[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]3[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]] = ([/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]long[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New])Volume[[/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]];
                  Array.Sort(highestVol);
                  Array.Reverse(highestVol);
                  [/FONT][FONT=Courier New][COLOR=#008000][FONT=Courier New][COLOR=#008000]//Let us be sure this is doing what we want. Print to output window
                  [/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]for[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] ( [/COLOR][/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000] i = highestVol.GetLowerBound([/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]); i <= highestVol.GetUpperBound([/COLOR][/FONT][FONT=Courier New][COLOR=#800080][FONT=Courier New][COLOR=#800080]0[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New][COLOR=#000000]); i++ )[/COLOR]
                  Print( String.Format([/FONT][FONT=Courier New][COLOR=#800000][FONT=Courier New][COLOR=#800000]"\t[{0}]:\t{1}"[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New], i, highestVol.GetValue( i )));
                  Print([/FONT][FONT=Courier New][COLOR=#0000ff][FONT=Courier New][COLOR=#0000ff]null[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New]);
                  }
                  [/FONT]

                  Comment


                    #10
                    Thanks koganam,

                    And sorry for the delayed response. The house is undergoing remodeling and have been down for most of the week.

                    What I've come to realize are two additional requirements:

                    1. I need to record the bar number the volume occurred on.
                    2. I need to handle duplicate volumes.


                    So I've been searching and have found some code that i think will do the job. It uses a simple list and collects both volume and bar number. As each new volume bar is added, it searches for a duplicate. If none is found, it adds the new one. Even this code needs to be modified as I want to delete the previous duplicate before adding the new duplicate.

                    Again, this code is just conceptual as this is way out of my league.


                    Here is the conceptual code:


                    Using declarations
                    Using System.Collections.Generic;

                    #region Variables
                    Private List<int> posVolumeList = new List<int>();
                    Private int volume;
                    Private int VolumeBarNumber;
                    #endregion

                    Initialize()

                    posVolumeList = new List<int>();

                    OnBarUpdate

                    posVolumeList.Add(208 , 1594); // add volume bar value and VolumeBarNumber

                    var ordered = posVolumeList.OrderBy(posVolumeList.Volume)
                    List<Item> uniqueItems = new List<Item>();
                    Foreach (Item result in ordered)
                    {
                    If(!unique.Any(posVolumeList.Volume.Equals(result. Volume)))
                    {
                    Unique.Add(result);
                    }


                    }

                    note: I haven't even tried to complie this code as it needs a lot of work. Any help? Thanks... taddypole...

                    Comment


                      #11
                      So, to carry this discussion further, I have some specific questions below:


                      If I want to create a list to store my positive volume values, do i declare them in the Variables section as below?

                      #region Variables
                      Private List<int> posVolumeList = new List<int>();

                      If I want to store the volume and the bar number of the volume bar as integers, would I declare them as below?

                      Private int volume;
                      Private int VolumeBarNumber;

                      #endregion

                      Do I need to initialize my posVolumeList as below?

                      Initialize()

                      posVolumeList = new List<int>(); //

                      Comment


                        #12
                        Originally posted by Taddypole View Post
                        So, to carry this discussion further, I have some specific questions below:


                        If I want to create a list to store my positive volume values, do i declare them in the Variables section as below?

                        #region Variables
                        Private List<int> posVolumeList = new List<int>();

                        If I want to store the volume and the bar number of the volume bar as integers, would I declare them as below?

                        Private int volume;
                        Private int VolumeBarNumber;

                        #endregion

                        Do I need to initialize my posVolumeList as below?

                        Initialize()

                        posVolumeList = new List<int>(); //
                        You are looking for a SortedList. The example on the page is pretty comprehensive, and demonstrates all of the methods that you will need to use.

                        ref: http://msdn.microsoft.com/en-us/library/ms132319.aspx
                        Last edited by koganam; 05-22-2013, 10:40 AM.

                        Comment


                          #13
                          I looked at SortedList but the problem I was having is the volume I'm recording is the "key" and is what I want to sort on. Occasionally the latest volume bar will be a duplicate of a previously recorded volume and keys can't be duplicates. So it seems I first need to check to see if the latest volume bar being added is a duplicate, if it is, delete the old one and add the new one into the sorted list.

                          So i'm trying to figure out what code i need to handle the duplicate issue.

                          edit: I'm assuming that sortedList sorts on the "Key" and that Keys can't have duplicates. And since the value i'm interested in sorting is the volume; volume needs to be the key.
                          Last edited by Taddypole; 05-22-2013, 11:47 AM.

                          Comment


                            #14
                            Originally posted by Taddypole View Post
                            I looked at SortedList but the problem I was having is the volume I'm recording is the "key" and is what I want to sort on. Occasionally the latest volume bar will be a duplicate of a previously recorded volume and keys can't be duplicates. So it seems I first need to check to see if the latest volume bar being added is a duplicate, if it is, delete the old one and add the new one into the sorted list.

                            So i'm trying to figure out what code i need to handle the duplicate issue.

                            edit: I'm assuming that sortedList sorts on the "Key" and that Keys can't have duplicates. And since the value i'm interested in sorting is the volume; volume needs to be the key.
                            Code:
                             
                                    // ContainsKey can be used to test keys before inserting  
                                    // them. 
                                    if (!highestVolume.ContainsKey(Volume[0]))
                                    {
                                        highestVolume.Add(Volume[0], CurrentBar);
                                    }
                            On the other hand, if you want to insert the most current value, then you have to test, remove and add.
                            Code:
                                    if (highestVolume.ContainsKey(Volume[0]))
                                     {
                                         highestVolume.Remove(Volume[0]);
                                     }
                                        highestVolume.Add(Volume[0], CurrentBar);
                            As the list is sorted by default, I would probably compare the Volume[0] to the current value in index position 2, and only insert or replace the key/value pair if the indexed value is exceeded.

                            Comment


                              #15
                              Thanks koganam, I really appreciate your help!

                              Can you help me with the declaration and Initialization sections for a SortedList. I've been trying but get compile errors.

                              Variables:
                              private int VolumeValue;
                              private int VolumeBar;

                              private SortedList<VolumeValue,VolumeBar> MySlVolume = new SortedList<VolumeValue,VolumeBar>();

                              Initialize: ???

                              I was reading this thread



                              and someone was having problems with initialization the SortedList. So not which way to go.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,404 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              95 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              160 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X