iteration above below in Dictionary

aekzof

Member
Joined
Sep 6, 2023
Messages
5
Programming Experience
Beginner
how i do this:
        double maxVolume = double.MinValue; // Initialize with the smallest possible value
            double Price = 0;
            double Volume = 0;
            
            double correspondingKey = 0.0;

            double AllVolumes = 0.0;
            double totalVolumes = 0.0;


            Dictionary<double, double> MyVolumeDictionary = new Dictionary<double, double>();


            // Add data to the dictionary
            MyVolumeDictionary.Add(9638.0, 3217.0);
            MyVolumeDictionary.Add(9637.0, 3215.0);
            MyVolumeDictionary.Add(9636.0, 3215.0);
            MyVolumeDictionary.Add(9635.0, 3215.0);
            MyVolumeDictionary.Add(9634.0, 3215.0);
            MyVolumeDictionary.Add(9633.0, 3215.0);
            MyVolumeDictionary.Add(9632.0, 3215.0);
            MyVolumeDictionary.Add(9631.0, 3215.0);
            MyVolumeDictionary.Add(9630.0, 3215.0);
            MyVolumeDictionary.Add(9629.0, 3215.0);
            MyVolumeDictionary.Add(9628.0, 3215.0);
            MyVolumeDictionary.Add(9627.0, 3215.0);
            MyVolumeDictionary.Add(9626.0, 3215.0);
            MyVolumeDictionary.Add(9625.0, 3215.0);
            MyVolumeDictionary.Add(9624.0, 3215.0);
            MyVolumeDictionary.Add(9623.0, 4311.0);
            MyVolumeDictionary.Add(9622.0, 5071.0);
            MyVolumeDictionary.Add(9621.0, 6056.0);
            MyVolumeDictionary.Add(9620.0, 9087.0);
            MyVolumeDictionary.Add(9619.0, 10819.0);
            MyVolumeDictionary.Add(9618.0, 14921.0);
            MyVolumeDictionary.Add(9617.0, 16496.0);
            MyVolumeDictionary.Add(9616.0, 17601.0);
            MyVolumeDictionary.Add(9615.0, 19626.0);
            MyVolumeDictionary.Add(9614.0, 21800.0);
            MyVolumeDictionary.Add(9613.0, 21973.0);
            MyVolumeDictionary.Add(9612.0, 22168.0);
            MyVolumeDictionary.Add(9611.0, 18791.0);
            MyVolumeDictionary.Add(9610.0, 15700.0);
            MyVolumeDictionary.Add(9609.0, 16900.0);
            MyVolumeDictionary.Add(9608.0, 13971.0);
            MyVolumeDictionary.Add(9607.0, 12367.0);
            MyVolumeDictionary.Add(9606.0, 8706.0);
            MyVolumeDictionary.Add(9605.0, 7134.0);
            MyVolumeDictionary.Add(9604.0, 6107.0);
            MyVolumeDictionary.Add(9603.0, 5341.0);
            MyVolumeDictionary.Add(9602.0, 3827.0);

            // Access dictionary data
            foreach (var kvp in MyVolumeDictionary)
            {
                Price = kvp.Key;
                Volume = kvp.Value;
               
AllVolumes  += Volume;

                if (kvp.Value > maxVolume)
                {
                    maxVolume = kvp.Value;
                    correspondingKey = kvp.Key;
                }

            }
                 Print("correspondingKey :" + correspondingKey + " maxVolume : " + maxVolume);

            

            Print("AllVolumes:" +AllVolumes);

Hello
From this script, I am searching for the price with the highest volume.

Results:

correspondingKey = 9612.0,
maxVolume= 22168.0,
AllVolumes= 327000. //total of volumes in this sample

What I wish to do next, using correspondingKey = 9612.0 as a reference,
is to find, as a priority, the first pair directly above correspondingKey, which is as follows:

1. Above: 9614.0 - 9613.0

I will add the volumes of this pair, resulting in a total of 43773.

Then, I will explicitly take the pair that is below correspondingKey, which is as follows:
2. Below:9611.0 - 9610.0

I will add the volumes of this pair, resulting in a total of 34491.
I repeat this operation, alternating between pairs, one above and one below, without repeating the price.
At each pair iteration, I add the volumes until the total volume exceeds, for example, 240000.
This total of 240000 must include the volume of correspondingKey = 9612.0.

To clarify, up to now, we have dealt with only 2 pairs:
  1. Above: 9614.0 - 9613.0
  2. Below: 9611.0 - 9610.0
Continuing with:
3. Above: 9616.0 - 9615.0
4. Below: 9609.0 - 9608.0
5. Above: 9618.0 - 9617.0
6. Below: 9607.0 - 9606.0
7. Above: 9619.0 - 9618.0

As soon as the total volume surpasses this threshold, for example, 240000, we stop and retrieve the minimum and maximum prices used to calculate this volume.
In this case, the values are:
Minimum: 9606.0
Maximum: 9620.0

I have spent hours trying to achieve this, but I have not succeeded yet, and I won't be able to solve this problem on my own.
Please help me find a solution.
 
Is there a solution to convert this code to C# 5.0?

Yes, but I did say I wasn't going to keep doing your work for you; I've given a solution and explained how it works, I'm happy to explain further, but you need to develop the solution to meet the revised goalposts that keep being moved
 
unfortunately, it needs to be compiled in a project that exclusively supports only C# 5.0 and .NET 4.8.
Is there a solution to convert this code to C# 5.0?

C# 5.0 goes with .NET Framework 4.5, 4.6 which both went of out of support in April 2022.

.NET Framework 4.8 uses C# 7.3.
 
Back
Top Bottom