Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've tried to use the code in this post, and it works fine, but when converting from a minute timeframe to an hourly timeframe from 8am-8pm it does not include the last hour 7-8pm, it only includes it on the very last bar, not the dates prior to that. Each hourly bar before that only goes from 8AM to 6PM. I should see 8AM to 7PM for each date.

Converter to convert OHLC data to a larger timeframe[^]

The code from the Author post:
C#
// bars to convert should be order by date time
static public Collection<Bar> to_larger_timeframe(IEnumerable<Bar> bars_to_convert, TimeSpan time)
{
    var bars_converted = new Collection<Bar>();
    long current_tick_interval = -1;
    DateTime boundary_adjusted_time = default(DateTime);
    double current_bar_open = default(double);
    double current_bar_high = default(double);
    double current_bar_low = default(double);
    double current_bar_close = default(double);

    if (bars_to_convert.Count() == 0)
        return bars_converted;

    foreach (var bar in bars_to_convert)
    {
        var this_tick_interval = bar.Time.Ticks / time.Ticks;
        if (this_tick_interval != current_tick_interval)
        {
            if (current_tick_interval != -1)
            {

                bars_converted.Add(new Bar( Time: boundary_adjusted_time, 
                    Open: current_bar_open, 
                    High: current_bar_high, 
                    Low: current_bar_low, 
                    Close: current_bar_close));
            }
            current_tick_interval = this_tick_interval;
            boundary_adjusted_time = new DateTime(current_tick_interval * time.Ticks);
            current_bar_open = bar.Open;
            current_bar_high = bar.High;
            current_bar_low = bar.Low;
            current_bar_close = bar.Close;
        }
        else
        {
            current_bar_high = bar.High > current_bar_high ? bar.High : current_bar_high;
            current_bar_low = bar.Low < current_bar_low ? bar.Low : current_bar_low;
            current_bar_close = bar.Close;
        }
    }
    // Add the final bar
    bars_converted.Add(new Bar(boundary_adjusted_time, current_bar_open, current_bar_high, current_bar_low, current_bar_close));
    return bars_converted;
} 


here is the output:

4/20/2016 10:00:00 AM 15.17 15.94 15.16 15.57
4/20/2016 11:00:00 AM 15.56 15.7 14.93 15.21
4/20/2016 12:00:00 PM 15.22 15.25 15 15.11
4/20/2016 1:00:00 PM 15.12 15.28 15.02 15.21
4/20/2016 2:00:00 PM 15.22 15.22 14.9 15.09
4/20/2016 3:00:00 PM 15.09 15.88 15.01 15.75
4/20/2016 4:00:00 PM 15.75 15.99 15.74 15.85
4/20/2016 5:00:00 PM 15.85 15.9 15.83 15.89
4/20/2016 6:00:00 PM 15.9 15.9 15.85 15.89
4/21/2016 8:00:00 AM 16.12 16.13 15.78 16
4/21/2016 9:00:00 AM 16 16.41 15.83 16.36
4/21/2016 10:00:00 AM 16.36 16.59 15.85 16.03
4/21/2016 11:00:00 AM 16.02 16.5 15.94 16.49
4/21/2016 12:00:00 PM 16.49 16.73 16.22 16.31
4/21/2016 1:00:00 PM 16.32 16.65 16.23 16.42
4/21/2016 2:00:00 PM 16.41 16.84 16.28 16.43
4/21/2016 3:00:00 PM 16.43 16.68 16.3 16.35
4/21/2016 4:00:00 PM 16.32 16.81 16.25 16.59
4/21/2016 5:00:00 PM 16.59 16.75 16.56 16.75
4/21/2016 6:00:00 PM 16.73 16.79 16.72 16.76
4/22/2016 8:00:00 AM 16.28 16.49 16.16 16.41
4/22/2016 9:00:00 AM 16.43 16.59 15.95 15.97
4/22/2016 10:00:00 AM 15.97 16.78 15.88 16.63
4/22/2016 11:00:00 AM 16.63 16.72 16.04 16.05
4/22/2016 12:00:00 PM 16.05 16.56 16.02 16.44
4/22/2016 1:00:00 PM 16.44 16.45 15.9 16.02
4/22/2016 2:00:00 PM 16.02 16.06 15.57 15.62
4/22/2016 3:00:00 PM 15.61 15.8 15.39 15.48
4/22/2016 4:00:00 PM 15.51 15.56 15.36 15.46
4/22/2016 5:00:00 PM 15.48 15.5 15.44 15.46
4/22/2016 6:00:00 PM 15.45 15.5 15.45 15.47
4/25/2016 8:00:00 AM 15.78 15.99 15.71 15.88
4/25/2016 9:00:00 AM 15.87 16.2 15.75 16.1
4/25/2016 10:00:00 AM 16.1 16.42 15.94 16.29
4/25/2016 11:00:00 AM 16.3 16.6 16.12 16.6
4/25/2016 12:00:00 PM 16.6 16.6 16.05 16.12
4/25/2016 1:00:00 PM 16.12 16.23 16 16.04
4/25/2016 2:00:00 PM 16.04 16.38 15.9 16.19
4/25/2016 4:00:00 PM 15.87 16.46 15.54 15.59
4/25/2016 5:00:00 PM 15.6 15.61 15.57 15.58
4/25/2016 6:00:00 PM 15.58 15.6 15.54 15.59
4/26/2016 8:00:00 AM 15.38 15.51 15.33 15.46
4/26/2016 9:00:00 AM 15.47 15.72 15.26 15.27
4/26/2016 10:00:00 AM 15.27 15.66 15.22 15.63
4/26/2016 11:00:00 AM 15.63 15.69 15.15 15.24
4/26/2016 12:00:00 PM 15.24 15.33 15.04 15.25
4/26/2016 1:00:00 PM 15.25 15.35 15.14 15.22
4/26/2016 2:00:00 PM 15.21 15.3 15 15.23
4/26/2016 3:00:00 PM 15.22 15.43 15.14 15.21
4/26/2016 4:00:00 PM 15.21 15.34 14.87 15.34
4/26/2016 5:00:00 PM 15.35 15.45 15.31 15.43
4/26/2016 6:00:00 PM 15.44 15.45 15.39 15.4
4/26/2016 7:00:00 PM 15.4 15.42 15.35 15.4


What I have tried:

I can see that I am adding data correctly to bars_to_convert, but in bars_converted it is leaving out 7PM from each date except the last. I've been trying to fix for hours and I'm stumped. I can see the 7pm-7:59pm in the class ConvertOpenHighLowCloseData, by getting bar.Time, but its not adding it to bars_converted. If anyone could please help me I would appreciate
Posted
Updated 26-Apr-16 17:17pm
v4
Comments
Sergey Alexandrovich Kryukov 26-Apr-16 22:46pm    
Will you better talk to the author of that post?
—SA
scotty356 26-Apr-16 22:58pm    
I've used codeproject for years but have never posted anything. How do I do contact them. It looks like its from 2012 so not sure if the author is still around. Hoping someone that knows their stuff might notice the issue right away.
Sergey Alexandrovich Kryukov 26-Apr-16 23:13pm    
If you comment on any post, or add a message in "Discussions" section, the author of that post gets a notification. As I can see, you already done that.

If the author doesn't support the code, you should better address to us with your ultimate goals and problems, not related to a particular article.

—SA
scotty356 26-Apr-16 23:59pm    
I'm basically trying to group together stock prices by hour instead of every minute. lets say I have 30 days worth of data, for each day the data that I have is for every minute between 8am and 7:59pm. I have datetime,double open, double high, double low, double close in a collection. Instead of having 720 items for each day, I want to cut that down to 12, one for each hour in another collection. Each of those 12 from that day should contain the Open price from the first minute open in that hour, the close price from the last minute close in that hour, the highest high found throughout that hour, the lowest low from that hour. The code in the post works fine except that last 12th hour never shows up until day 30. Any suggestions or a way to do it differently that you can think of? Thanks so much
Richard MacCutchan 27-Apr-16 3:06am    
Make sure the close time is one minute past 7, or fix the code that does the compares. Unfortunately you did not indicate where that test is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900